Alex_tz307

Fundamente XI - Camion - 212

Sep 21st, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define pi pair < int , int >
  3. #define w first
  4. #define p second
  5.  
  6. using namespace std;
  7.  
  8. inline void max_self(int& a, int b) {
  9.     a = max(a, b);
  10. }
  11.  
  12. int main() {
  13.     ios_base::sync_with_stdio(false);
  14.     cin.tie(nullptr);
  15.     cout.tie(nullptr);
  16.     int N, gmax;
  17.     cin >> N >> gmax;
  18.     vector < pi > a(N + 1);
  19.     for(int i = 1; i <= N; ++i)
  20.         cin >> a[i].w >> a[i].p;
  21.     vector < int > dp(10001); // dp[x] = y <=> daca transporta x kilograme face maxim y profit
  22.     int mx = 0;
  23.     for(int i = 1; i <= N; ++i) { // iau pe rand fiecare obiect
  24.         for(int j = mx; j >= 0; --j) // pornesc de la maxim pentru a fi sigur ca nu folosesc un obiect de mai multe ori
  25.             if(dp[j + a[i].w] < dp[j] + a[i].p) { // pot transporta j + a[i].w si profitul nou ar putea fi dp[j] (profitul lui j) + a[i].p (profitul obiectului)
  26.                 dp[j + a[i].w] = dp[j] + a[i].p;
  27.                 max_self(mx, j + a[i].w);
  28.             }
  29.     }
  30.     int ans = 0;
  31.     for(int i = 0; i <= gmax; ++i)
  32.         max_self(ans, dp[i]);
  33.     cout << ans;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment