Advertisement
DInhjeklaee

SNAPSACK, Dinh

Jan 19th, 2020
118
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. struct d{
  4.     int w;
  5.     int v;
  6. };
  7. int main()
  8. {
  9.     int n,s;
  10.     cin>>s>>n;
  11.     int f[s+1][n+1]={0};
  12.     d a[n+1];
  13.     for (int i=0; i<=s; i++)
  14.         for (int j=0; j<=n; j++)
  15.         f[i][j]=0;
  16.     for(int i=1; i<=n; i++)
  17.         cin>>a[i].w>>a[i].v;
  18.     for (int i=1; i<=s; i++){
  19.     for (int j=1; j<=n; j++){
  20.         if (i-a[j].w>=0) f[i][j]=max(f[i-a[j].w][j-1]+a[j].v,f[i][j-1]);
  21.         else f[i][j]=f[i][j-1];
  22.     }
  23.     }
  24.     cout<<f[s][n];
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement