cyga

Untitled

Sep 27th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4.     ios_base::sync_with_stdio(0);
  5.     cin.tie(0);
  6.     cout.tie(0);
  7.  
  8.     int s, n;
  9.     cin >> s >> n;
  10.     vector<int> w(n);
  11.     for(int i=0; i<n; ++i) cin >> w[i];
  12.  
  13.     vector<bool> dp(s+1, false);
  14.     dp[0] = true;
  15.     for(int i=0; i<n; ++i) {
  16.         for(int j=s-w[i]; j>=0; --j) {
  17.             if(dp[j]) dp[j+w[i]] = dp[j];
  18.         }
  19.     }
  20.     int max_w;
  21.     for(max_w=s; max_w>=0 && !dp[max_w]; --max_w) {}
  22.     cout << max_w << "\n";
  23.  
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment