Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <algorithm>
- using namespace std;
- int main()
- {
- int n, sum;
- cin >> n >> sum;
- int *arr = new int[n];
- for (int i = 0; i < n; i++)
- cin >> arr[i];
- int *result = new int [sum+1];
- result[0] = 0;
- for(int s=1 ; s <= sum; ++s)
- {
- result[s] = INT16_MAX;
- for (int i = 0; i < n; ++i)
- if (s - arr[i] >= 0)
- result[s] = min(result[s], result[s - arr[i]] + 1);
- }
- if (result[sum] != INT16_MAX)
- cout << result[sum];
- else cout << -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement