josiftepe

Untitled

Nov 28th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 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.  
  12. int main() {
  13.     int N, W;
  14.     cin >> N;
  15.     cin >> W;
  16.     vector<ll> v(101);
  17.     vector<ll> w(101);
  18.     for (int i = 0; i < N; i++) {
  19.         cin >> w[i];
  20.         cin >> v[i];
  21.     }
  22.     for(int i = 0; i < N; ++i) {
  23.         for(int j = 0; j <= W; ++j) {
  24.             dp[i][j] = -inf;
  25.         }
  26.     }
  27.     // dp[i][j] - maximum value till i with remaining j weight
  28.     ll ret = -inf;
  29.     for(int at = 0; at <= N; ++at) {
  30.         for(int remaining_weight = 0; remaining_weight <= W; ++remaining_weight) {
  31.             if(at == 0) {
  32.                 dp[at][remaining_weight] = 0;
  33.                 continue;
  34.             }
  35.             if(remaining_weight == 0) {
  36.                 dp[at][remaining_weight] = 0;
  37.                 continue;
  38.             }
  39.             if(w[at - 1] <= remaining_weight) {
  40.                 dp[at][remaining_weight] = max(dp[at][remaining_weight], dp[at - 1][remaining_weight - w[at - 1]] + v[at - 1]);
  41.             }
  42.             dp[at][remaining_weight] = max(dp[at][remaining_weight], dp[at - 1][remaining_weight]);
  43.             ret = max(ret, dp[at][remaining_weight]);
  44.         }
  45.     }
  46.     cout << ret << endl;
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment