提交时间:2023-12-22 13:38:22
运行 ID: 117145
#include<bits/stdc++.h> using namespace std; #define rep(i,s,n)for(int i=(s);i<=(n);++i) #define all_(s,n)(s)+1,(s)+(n)+1 template<class T> void chmax(T&x,T y){ if(x<y)x=y; } typedef long long ll; ll n,k,x,a[200005],a_[200005],suma[200005],f[200005],f2[200005],len[200005],sta[200005]; int head=1,tail=1; int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n>>k>>x; if(x<=0)k=n-k,x=-x; rep(i,1,n){ cin>>a[i]; a_[i]=a[i]-x; a[i]+=x; suma[i]=suma[i-1]+a[i]; } ll ans=0; rep(i,1,n)f2[i]=max(f2[i-1]+a_[i],a_[i]); rep(i,1,n){ while(head!=tail&&suma[sta[tail-1]-1]>=suma[i-1]) tail--; sta[tail++]=i; while(head!=tail&&i-sta[head]+1>k) head++; ll mn = (head == tail ? 0x3f3f3f3f3f3f3f3fll : suma[sta[head] - 1]); int l = max(i - k, 0ll); f[i] = max(suma[i] - mn, suma[i] - suma[l] + f2[l]); chmax(ans, f[i]); } cout<<ans; return 0; }