josiftepe

Untitled

Nov 28th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6. typedef long long ll;
  7. const int maxn = 1e5 + 10;
  8. const int INF = 2e9 + 10;
  9. const ll inf = 1e18 + 10;
  10. ll dp[101][100001];
  11. int N, W;
  12. vector<ll> v(101);
  13. vector<ll> w(101);
  14. ll rec(int at, ll remaining_weight) {
  15.     if(at == N) {
  16.         return 0;
  17.     }
  18.     if(remaining_weight == 0) {
  19.         return 0;
  20.     }
  21.     if(dp[at][remaining_weight] != -1) {
  22.         return dp[at][remaining_weight];
  23.     }
  24.     ll ret = -inf;
  25.     ret = max(ret, rec(at + 1, remaining_weight));
  26.     if(remaining_weight - w[at] >= 0) {
  27.         ret = max(ret, rec(at + 1, remaining_weight - w[at]) + v[at]);
  28.     }
  29.     dp[at][remaining_weight] = ret;
  30.     return ret;
  31. }
  32. int main() {
  33.     cin >> N;
  34.      cin >> W;
  35.     for (int i = 0; i < N; i++) {
  36.         cin >> w[i];
  37.         cin >> v[i];
  38.     }
  39.     for(int i = 0; i < N; ++i) {
  40.         for(int j = 0; j <= W; ++j) {
  41.             dp[i][j] = -1;
  42.         }
  43.     }
  44.     cout << rec(0, W) << endl;
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment