Advertisement
Josif_tepe

Untitled

Nov 15th, 2021
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include<ctime>
  3. #include<map>
  4. #include<vector>
  5. #include<queue>
  6. using namespace std;
  7. long long dp[100005];
  8. int main()
  9. {
  10.    int n;
  11.    int w;
  12.    cin>>w>>n;
  13.    vector<int>weight;
  14.    vector<int>value;
  15.    for(int i=0; i<n; i++){
  16.     int a, b;
  17.     cin>>a>>b;
  18.     value.push_back(a);
  19.     weight.push_back(b);
  20.    }
  21.  
  22.    for(int i=0; i<=w; i++){
  23.     dp[i]=-1e18;
  24.    }
  25.  
  26.    dp[w]=0;
  27.    for(int i=w; i>=0; i--){
  28.     for(int j=0; j<n; j++){
  29.         if(i - weight[j] >= 0){
  30.             dp[i-weight[j]]=max(dp[i-weight[j]], dp[i]+value[j]);
  31.         }
  32.         }
  33.    }
  34.    long long result=0;
  35.    for(int j=0; j<=w; j++){
  36.     result=max(result, dp[j]);
  37.    }
  38.    cout<<result;
  39.     return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement