fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. using namespace std;
  4. using namespace __gnu_pbds;
  5. #define REP(i,n) for(int i=0;i<(n);++i)
  6. #define FOR(i,a,b) for(int i=(a);i<=(b);++i)
  7. #define FORD(i,a,b) for(int i=(a);i>=(b);--i)
  8. #define PB push_back
  9. typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
  10. typedef long long ll;
  11. typedef pair<int,int> pii;
  12. typedef pair<ll,ll> pll;
  13.  
  14. // const ll INF = 0x3f3f3f3f3f3f3f3fLL;
  15. const int INF = 0x3f3f3f3f;
  16. const int mxN = 1e5+1;
  17. ll t, n, k, z, a[mxN], s[mxN];
  18.  
  19. int main() {
  20. ios::sync_with_stdio(false);
  21. cin>>t;
  22. s[0]=0;
  23. while(t--){
  24. cin>>n>>k>>z;
  25. REP(i, n){
  26. cin>>a[i+1];
  27. s[i+1]=s[i]+a[i+1];
  28. }
  29. ll mx=s[k+1];
  30. FOR(i, 2, k){
  31. ll cur=s[i], r=k-i+1; //r=moves remaining, took i-1 moves to reach i
  32.  
  33. int c=min(z, r/2); //maximum number of left moves we can make
  34. FOR(j, 0, c){
  35. cur+=(a[i]+a[i-1])*j;
  36. r-=2*j;
  37. mx=max(mx, cur+(s[i+r]-s[i]));
  38. if(z-j){
  39. mx=max(mx, cur+(s[i+r-1]-s[i])+a[i+r-2]);
  40. }
  41. r+=2*j;
  42. cur-=(a[i]+a[i-1])*j;
  43. }
  44. if(r-2*c && z-c){ //we might be able to make one last left move
  45. mx=max(mx, cur+a[i-1]);
  46. }
  47. }
  48. cout<<mx<<'\n';
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0s 4420KB
stdin
2
55 37 1
9 17 6 14 8 4 4 16 1 6 10 13 6 19 10 5 11 13 16 19 10 2 9 11 1 15 13 17 7 1 9 4 11 16 17 11 3 7 15 8 2 10 6 20 3 16 8 4 12 1 9 3 8 17 18
18 11 4
11 19 18 19 19 5 14 15 17 4 10 9 8 17 9 2 15 10
stdout
396
219