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. ios_base::sync_with_stdio(0);
  23. cin.tie(0);
  24. cin >> n >> k;
  25. for(int i = 1; i <= n; i++){
  26. cin >> a[i];
  27. }
  28. dq.push_back(0);
  29. for(int i = 1; i <= n; i++){
  30. while(!dq.empty() && dq.front() < i - k) dq.pop_front();
  31. dp[i] = dp[dq.front()] + a[i];
  32. while(!dq.empty() && dp[dq.back()] >= dp[i]) dq.pop_back();
  33. dq.push_back(i);
  34. }
  35. ll ans = 0, total = accumulate(a + 1, a + n + 1, 0LL);
  36. for(int i = n - k + 1; i <= n; i++){
  37. ans = max(ans, total - dp[i]);
  38. }
  39. cout << ans;
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty