题解:
单调队列+dp
f[i]=max(f[j-1]+sum[i]-sum[j])
然后维护f[j-1]-sum[j]单调性
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=200005; int n,m,a[N],p[N]; ll sum[N],q[N],f[N]; int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++)scanf("%d",&a[i]); for (int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i]; f[1]=a[1];p[2]=1;q[2]=-a[1]; int l=1,r=2; for (int i=2;i<=n;i++) { if (p[l]+m<i)l++; f[i]=sum[i]+q[l]; while (l<=r&&q[r]<=f[i-1]-sum[i])r--; p[++r]=i;q[r]=f[i-1]-sum[i]; } printf("%lld",f[n]); }