Advertisement
maxan

Untitled

Mar 17th, 2020
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef long double ld;
  6.  
  7. const ll COST = 1e9;
  8.  
  9. struct Exp {
  10.     int l, r, c;
  11.     Exp() {l = r = c = 0;}
  12. };
  13.  
  14. ll dp[(int)2e6 + 1];
  15. deque<ll> deq[107];
  16.  
  17. signed main() {
  18. #ifdef LOCAL
  19.     freopen("input.txt", "r", stdin);
  20.     freopen("output.txt", "w", stdout);
  21. #endif
  22.     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  23.  
  24.     Exp exp[107];
  25.     ll n, a;
  26.     cin >> n >> a;
  27.  
  28.     for (int i = 0; i < n; ++i)
  29.         cin >> exp[i].l >> exp[i].r >> exp[i].c;
  30.  
  31.     for (int i = a; i >= 0; --i) {
  32.         dp[i] = i * COST;
  33.  
  34.         for (int j = 0; j < n; ++j) {
  35.             if (i + exp[j].l <= a) {
  36.                 while (!deq[j].empty() && deq[j].back() > dp[i + exp[j].l])
  37.                     deq[j].pop_back();
  38.                 deq[j].push_back(dp[i + exp[j].l]);
  39.             }
  40.  
  41.             if ((i + exp[j].r + 1) <= a) {
  42.                 if (!deq[j].empty() && deq[j].front() == dp[i + exp[j].r + 1])
  43.                     deq[j].pop_front();
  44.             }
  45.         }
  46.  
  47.         for (int j = 0; j < n; ++j) {
  48.             if (!deq[j].empty() && (i + exp[j].r) <= a)
  49.                 dp[i] = max(dp[i], deq[j].front() - exp[j].c);
  50.         }
  51.     }
  52.  
  53.     cout << dp[0];
  54.    
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement