fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. const int N=1e5+1;
  5. ll a[N],n,k,dp[N],p[N];
  6. deque <int> d;
  7. int main() {
  8. freopen("RONGTHAN.INP","r",stdin);
  9. freopen("RONGTHAN.OUT","w",stdout);
  10. ios::sync_with_stdio(false);
  11. cin.tie(0), cout.tie(0);
  12. cin >> n >> k,k--;
  13. for(int i=1;i<=n;i++) cin >> a[i], p[i]=p[i-1]+a[i];
  14. d.push_back(0), dp[1]=a[1];
  15. for(int i=2;i<=n;i++) {
  16. while(!d.empty() && dp[d.back()]+p[i]-p[d.back()+1]<=a[i]+dp[i-2]) d.pop_back();
  17. d.push_back(i-2);
  18. if(d.front()==i-k-2) d.pop_front();
  19. dp[i]=max(dp[i-1],dp[d.front()]+p[i]-p[d.front()+1]);
  20. if(i<=k) dp[i]=p[i];
  21. }
  22. cout << *max_element(dp+1,dp+n+1);
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Standard output is empty