Saleh127

UVA 10130

May 2nd, 2021
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. int main()
  6. {
  7. ios_base::sync_with_stdio(0);
  8. cin.tie(0);
  9. cout.tie(0);
  10.  
  11. test
  12. {
  13.  
  14. ll n,m,i,j,k,l=0;
  15.  
  16. ll v[1005],w[1005];
  17.  
  18. cin>>n;
  19.  
  20. for(i=0; i<n; i++)
  21. {
  22. cin>>v[i]>>w[i];
  23. }
  24.  
  25. ll dp[50];
  26.  
  27. memset(dp,0,sizeof dp);
  28.  
  29. for(i=0; i<n; i++)
  30. {
  31. for(j=30; j>=0; j--)
  32. {
  33. if(w[i]<=j && dp[j]<(dp[j-w[i]]+v[i]))
  34. {
  35. dp[j]=dp[j-w[i]]+v[i];
  36. }
  37. }
  38. }
  39.  
  40. cin>>m;
  41.  
  42. while(m--)
  43. {
  44. cin>>k;
  45.  
  46. l+=dp[k];
  47. }
  48.  
  49. cout<<l<<endl;
  50. }
  51.  
  52.  
  53. return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment