Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int maxn = 1e6 + 5;
- const int MOD = 1e9 + 7;
- const int INF = 2e9 + 10;
- int n, x;
- int a[maxn];
- int dp[maxn];
- int rec(int sum) {
- if(dp[sum] != -1) {
- return dp[sum];
- }
- if(sum == 0) {
- return 0;
- }
- int ret = INF;
- for(int i = 0; i < n; ++i) {
- if(sum - a[i] >= 0) {
- ret = min(ret, rec(sum - a[i]) + 1);
- }
- }
- return dp[sum] = ret;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin >> n >> x;
- for(int i = 0; i < n; ++i) {
- cin >> a[i];
- }
- for(int i = 0; i < maxn; ++i) {
- dp[i] = -1;
- }
- if(rec(x) >= INF) {
- cout << -1 << endl;
- }
- else cout << rec(x) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment