Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int s, n;
- cin >> s >> n;
- vector<int> w(n);
- for(int i=0; i<n; ++i) cin >> w[i];
- vector<bool> dp(s+1, false);
- dp[0] = true;
- for(int i=0; i<n; ++i) {
- for(int j=s-w[i]; j>=0; --j) {
- if(dp[j]) dp[j+w[i]] = dp[j];
- }
- }
- int max_w;
- for(max_w=s; max_w>=0 && !dp[max_w]; --max_w) {}
- cout << max_w << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment