Advertisement
nguyentien281006

MonneySum

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