Advertisement
Farjana_akter

Untitled

Jul 28th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long int dp[2000][100],value[2000],weight[2000],capacity[100];
  5.  
  6. void knapsack(int n,int cap)
  7. {
  8. int i,j;
  9. for(i=1;i<=n;i++)
  10. {
  11. for(j=1;j<=cap;j++)
  12. {
  13. if(j<weight[i])
  14. {
  15. dp[i][j]=dp[i-1][j];
  16. }
  17. else
  18. {
  19. dp[i][j]=max(dp[i-1][j-weight[i]]+value[i],dp[i-1][j]);
  20. }
  21. }
  22. }
  23. }
  24.  
  25.  
  26. int main()
  27. {
  28. freopen("in.txt","r",stdin);
  29. freopen("out.txt","w",stdout);
  30. int t,q,i,j,k,object,ans,capacity[200],max;
  31. cin>>t;
  32. while(t--)
  33. {
  34. ans=0,max=0;
  35. cin>>object;
  36. memset(value,0,sizeof(value));
  37. memset(weight,0,sizeof(weight));
  38. memset(capacity,0,sizeof(capacity));
  39. memset(dp,0,sizeof(dp));
  40. for(i=1;i<=object;i++)
  41. {
  42. cin>>value[i]>>weight[i];
  43. }
  44. cin>>q;
  45. for(i=1;i<=q;i++)
  46. {
  47. cin>>capacity[i];
  48. if(capacity[i]>max)
  49. max=capacity[i];
  50. }
  51. knapsack(object,max);
  52. for(i=1;i<=q;i++)
  53. {
  54. ans+=dp[object][capacity[i]];
  55. }
  56. cout<<ans<<endl;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement