fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ul unsigned long long
  6.  
  7. ll mod = 1e9+7;
  8. const int N = 1e6+5;
  9. int MOD = 998244353;
  10. int bit[200000];
  11. int n, k;
  12. ll pref[N];
  13. ll ma[N];
  14. int a[N];
  15. ll kq = 0;
  16. int main() {
  17. //freopen("CSBN.INP", "r", stdin);
  18. //freopen("CSBN.OUT", "w", stdout);
  19. ios_base::sync_with_stdio(0);
  20. cin.tie(0);
  21. cout.tie(0);
  22.  
  23.  
  24. cin >> n >> k;
  25. pref[0]=0;
  26. for(int i = 1 ; i <= n ; i++){
  27. cin >> a[i];
  28. pref[i] = pref[i - 1] + a[i];
  29.  
  30. }
  31.  
  32. for(int i = k;i <= n;i+=1){
  33. ma[i] = max(ma[i-1],pref[i] - pref[i-k]);
  34. }
  35. for (int i = n ; i >= k; i--){
  36. kq = max(kq, (pref[i] - pref[i-k]) + ma[i - k]);
  37. }
  38. cout << kq << "\n";
  39.  
  40. }
  41.  
Success #stdin #stdout 0s 5720KB
stdin
9 3 
2 6 1 5 3 8 1 9 1
stdout
30