fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using ll = long long;
  4.  
  5. #define endl '\n'
  6. #define fi first
  7. #define sec second
  8. #define pb push_back
  9. #define sz(v) (int)(v).size()
  10. #define all(v) (v).begin(), (v).end()
  11.  
  12. using namespace std;
  13.  
  14. const int maxn = (int)1e5;
  15.  
  16. int a[maxn + 5];
  17. ll dp[maxn + 5];
  18. deque<int> dq;
  19. int n, k;
  20.  
  21. int main(){
  22. // YEU CAM TU :>
  23. ios_base::sync_with_stdio(0);
  24. cin.tie(0);
  25. cin >> n >> k;
  26. for(int i = 1; i <= n; i++){
  27. cin >> a[i];
  28. }
  29. dq.push_back(0);
  30. for(int i = 1; i <= n; i++){
  31. while(!dq.empty() && dq.front() < i - k) dq.pop_front();
  32. dp[i] = dp[dq.front()] + a[i];
  33. while(!dq.empty() && dp[dq.back()] >= dp[i]) dq.pop_back();
  34. dq.push_back(i);
  35. }
  36. ll ans = 0, total = accumulate(a + 1, a + n + 1, 0LL);
  37. for(int i = n - k + 1; i <= n; i++){
  38. ans = max(ans, total - dp[i]);
  39. }
  40. cout << ans;
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty