fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. void solve(){
  7. int n,k,s=0,mx=INT_MIN;
  8. cin>>n;
  9. vector<int>v(n);
  10. for(int i=0;i<n;i++)cin>>v[i];
  11. cin>>k;
  12. for(int i=0;i<k;i++){
  13. s+=v[i];
  14. }
  15. mx=max(mx,s);
  16. int a=k;
  17. for(int i=n-1;i>=n-a;i--){
  18. s+=v[i];
  19. s-=v[k-1];
  20. k--;
  21. mx=max(mx,s);
  22. }
  23. cout<<mx<<"\n";
  24. }
  25.  
  26. int main(){
  27. ios_base::sync_with_stdio(false);
  28. cin.tie(NULL);
  29. solve();
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5328KB
stdin
5
5 -2 3 1 2
3
stdout
8