提交时间:2023-08-14 15:09:32
运行 ID: 98326
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MX = 2e5 + 5; int n, k, len, lg[MX]; ll x; ll w[MX], c[MX], mul[18], st1[18][MX], st2[18][MX]; void ST() { for(int i=1; i<=17; i++) for(int x=1; x<=n-mul[i]+1; x++) st1[i][x] = min(st1[i-1][x], st1[i-1][x+mul[i-1]]), st2[i][x] = min(st2[i-1][x], st2[i-1][x+mul[i-1]]); } ll mx1(int l, int r) { int k = lg[r-l+1]; return min(st1[k][l], st1[k][r-mul[k]+1]); } ll mx2(int l, int r) { int k = lg[r-l+1]; return min(st2[k][l], st2[k][r-mul[k]+1]); } int main() { cin>>n>>k>>x; if(x<0) x=-x, k = n-k; for(int i=1; i<=n; i++) { scanf("%lld", &w[i]); c[i] = w[i]+c[i-1]; } mul[0] = 1; for(int i=1; i<=17; i++) mul[i] = mul[i-1]*2; lg[1] = 0; for(int i=2; i<=n; i++) lg[i] = lg[i>>1]+1; for(int i=1; i<=n; i++) st1[0][i] = c[i] + i*x, st2[0][i] = c[i]-i*x; ST(); ll ans = 0; int x1, x2; for(int i=1; i<=n; i++) { x1 = mx1(max(i-k, 0), i); ans = max(ans, st1[0][i]-x1); x2 = mx2(0, max(i-k, 0)); ans = max(ans, st1[0][i]-st1[0][i-k]+st2[0][i-k]-x2); } cout<<ans<<endl; return 0; }