单调队列优化dp板子
表示不大想写详细做法,自己看代码吧qwq (懒)
注意细节,不然就会跟我一样WA4次
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register
#define db double
#define eps (1e-5)
using namespace std;
const int N=100000+10;
il LL rd()
{
re LL x=0,w=1;re char ch;
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
LL s[N],f[N],n,k,ans;
LL q[N][2],hd=1,tl;
int main()
{
n=rd(),k=rd();
for(int i=1;i<=n;i++) s[i]=s[i-1]+rd();
q[++tl][0]=0,q[tl][1]=0,q[++tl][0]=-s[1],q[tl][1]=1;
for(int i=1;i<=n;i++)
{
while(hd<=tl&&q[hd][1]<i-k) ++hd;
f[i]=s[i]+q[hd][0];
ans=max(ans,f[i]);
LL x=ans-s[i+1];
while(hd<=tl&&x>=q[tl][0]) --tl;
q[++tl][0]=x,q[tl][1]=i+1;
}
printf("%lld\n",ans);
return 0;
}