Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n,mw,w,i,j,k;
  6. cin>>n;
  7. int wi[n+1],vi[n+1];
  8. for(i=1;i<=n;i++)
  9. {
  10. cin>>wi[i]>>vi[i];
  11. }
  12. cin>>mw;
  13. int c[n+1][mw+1];
  14.  
  15. for(i=0;i<=n;i++)
  16. {
  17. c[i][0]=0;
  18. }
  19.  
  20. for(i=0;i<=mw;i++)
  21. {
  22. c[0][i]=0;
  23. }
  24.  
  25. for(i=1;i<=n;i++)
  26. {
  27. for(w=1;w<=mw;w++)
  28. {
  29. if(wi[i]>w)
  30. {
  31. c[i][w]=c[i-1][w];
  32. }
  33. else
  34. {
  35. c[i][w]=max(c[i-1][w],(c[i-1][w-wi[i]]+vi[i]));
  36. }
  37. }
  38. }
  39.  
  40.  
  41. cout<<"total="<<c[n][mw]<<endl;
  42. return 0;
  43.  
  44. }
  45.  
  46.  
  47.  
  48. /*
  49. 4
  50. 2 3
  51. 3 4
  52. 4 5
  53. 5 6
  54.  
  55.  
  56. 5
  57. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement