提交时间:2023-08-14 15:27:02
运行 ID: 98339
#include<bits/stdc++.h> #define int long long using namespace std; const int N = 2e5 + 5; int T; int n, k, x; int a[N]; int b[N]; int c[N]; int s[N]; int S[N]; int h[N], l, r; int H[N], L, R; int ans; signed main(){ ios::sync_with_stdio(false); // cin >> T; // while(T --){ cin >> n >> k >> x; if(x < 0){ x = -x; k = n - k; } for(int i = 1; i <= n; ++ i){ cin >> a[i]; b[i] = a[i] - x; c[i] = a[i] + x; s[i] = s[i - 1] + b[i]; S[i] = S[i - 1] + c[i]; } /* for(int i = 1; i <= n; ++ i){ cout << c[i] << ' '; }*/ ans = 0; l = 1; r = 1; h[1] = 0; L = 1; R = 1; H[1] = 0; for(int i = 1; i <= n; ++ i){ while(l <= r && h[l] + k < i) l ++; while(l <= r && S[h[r]] > S[i]) r --; h[++ r] = i; ans = max(ans, S[i] - S[h[l]]); if(i > k){ while(L <= R && s[H[R]] > s[i - k]) R --; H[++ R] = i - k; } if(i >= k) ans = max(ans, s[i] - s[H[L]] + 2 * k * x); // cout << i << ": " << ans << '\n'; } cout << ans << '\n'; // } return 0; } /* b[i] = a[i] - x; s[i] = s[i - 1] + b[i] ans = s[r] - s[l - 1] + min(k, r - l + 1) * 2x ans = s[r] - s[l - 1] + r * 2x - (l - 1) * 2x = s[r] - s[l - 1] + k * 2x */