fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n,m,F[100],a[100];
  7. cin>>n>>m;
  8. for (int i=1;i<=n;i++) cin>>a[i];
  9. for (int i=0;i<=m;i++) F[i]=0;
  10. for (int i=1;i<=n;i++){
  11. for (int j=m;j>=1;j--){
  12. // F[i][j]=F[i-1][j];
  13. if (j-a[i]>0) F[j]=max(F[j],a[i]+F[j-a[i]]);
  14. cout<<F[j]<<" ";
  15. }
  16. cout<<endl;
  17. }
  18. cout<<F[m];
  19. return 0;
  20. }
Success #stdin #stdout 0s 15232KB
stdin
5 7
1 2 2 0 0 
stdout
1 1 1 1 1 1 0 
3 3 3 3 2 1 0 
5 5 4 3 2 1 0 
5 5 4 3 2 1 0 
5 5 4 3 2 1 0 
5