Advertisement
mickypinata

PROG-T1108: Ore2

Aug 11th, 2020
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. using lli = long long;
  7. using pli = pair<lli, int>;
  8.  
  9. int main(){
  10.  
  11.     int n, lim;
  12.     scanf("%d", &n);
  13.     scanf("%d", &lim);
  14.  
  15.     vector<lli> qs(n + 1, 0);
  16.     for(int i = 1; i <= n; ++i){
  17.         scanf("%lld", &qs[i]);
  18.         qs[i] += qs[i - 1];
  19.     }
  20.  
  21.     vector<lli> dp(n + 1, 0);
  22.     deque<pli> q;
  23.     q.push_back(make_pair(0, 0));
  24.     for(int i = 1; i <= n; ++i){
  25.         while(!q.empty() && q.back().first < dp[i - 1] - qs[i]){
  26.             q.pop_back();
  27.         }
  28.         q.push_back(make_pair(dp[i - 1] - qs[i], i));
  29.         while(!q.empty() && qs[i] - qs[q.front().second] > lim){
  30.             q.pop_front();
  31.         }
  32.         dp[i] = qs[i] + q.front().first;
  33.     }
  34.  
  35.     cout << dp[n];
  36.  
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement