(ans=sumlimits_{i=1}^n E[i] - mincost)
因此我们求mincost即可
设f[i]表示前i个牛,第i头牛一定不在,满足条件的前提下的mincost
转移时一个区间最小值形式,下标单调递增,单调队列维护一下即可
答案就减去f[n+1]好了,这样保证1~n都是满足条件的了
#include<iostream>
#include<cstdio>
using namespace std;
inline bool isd(char c){return c>='0'&&c<='9';}
inline int rd(){
int ret=0,f=1;char c;
while(c=getchar(),!isd(c))f=c=='-'?-1:1;
while(isd(c))ret=ret*10+c-'0',c=getchar();
return ret*f;
}
const int MAXN = 100005;
typedef long long ll;
int n,m;
int a[MAXN];
ll sum,f[MAXN];
int q[MAXN],h,t;
int main(){
n=rd();m=rd();
for(int i=1;i<=n;i++)a[i]=rd(),sum+=a[i];
m++;
for(int i=1;i<=n+1;i++){
while(h<=t&&q[h]<i-m)h++;
f[i]=f[q[h]]+1ll*a[i];
while(h<=t&&f[q[t]]>=f[i])t--;
q[++t]=i;
}
cout<<sum-f[n+1];
}