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 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] = INF;
- }
- dp[0] = 0; // za sum ednakva na 0 koristime 0 paricki
- for(int left_sum = 0; left_sum <= x; ++left_sum) {
- for(int i = 0; i < n; ++i) {
- if(left_sum + a[i] <= x) {
- dp[left_sum + a[i]] = min(dp[left_sum + a[i]], dp[left_sum] + 1);
- }
- }
- }
- if(dp[x] == INF) {
- dp[x] = -1;
- }
- cout << dp[x] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment