fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. #define ld long double
  6.  
  7. void solve() {
  8. int n,y;
  9. cin >> n >> y;
  10.  
  11. vector<int>b(n);
  12. for(int i=0;i<n;i++)cin>> b[i];
  13.  
  14. int sum=accumulate(b.begin(),b.end(),0LL);
  15. vector<vector<int>>dp(n+1,vector<int>(y+1));
  16.  
  17. for (int i=0; i<=n; i++) dp[i][0] = 1;// 1 way-> not taking anything to make sum=0
  18.  
  19. for(int i=1;i<=n;i++){
  20. for(int j=1;j<=y;j++){
  21. dp[i][j]=(j-b[i-1]>=0 ? dp[i-1][j-b[i-1]]:0)+dp[i-1][j];
  22. }
  23. }
  24. cout<<dp[n][y]<<endl;
  25. }
  26.  
  27. int main() {
  28. ios::sync_with_stdio(false);
  29. cin.tie(nullptr);
  30. solve();
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5280KB
stdin
7 5
3 2 1 4 3 5 -2
stdout
4