显示代码纯文本
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<deque>
#define LL unsigned long long
using namespace std;
deque<LL> q;
int n,k;
const int maxn=100500;
LL f[maxn],sum[maxn];
int a[maxn];
void Init();
//f[i]=f[j]+sum[i-1]-sum[j] ,f[i]表示不以i为结尾的最优解
//要把a[n+1]=0也算上,即输出 f[n+1]
int main(){
freopen("mowlawn.in","r",stdin);
freopen("mowlawn.out","w",stdout);
Init();
//system("pause");
return 0;
}
void Init(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
//if(i<=k+1)f[i]=sum[i-1];
}
sum[n+1]=sum[n];q.push_back(0);
for(int i=1;i<=n+1;i++){
while(!q.empty() && q.front()+k<i-1)q.pop_front();
if(!q.empty())f[i]=f[q.front()]+sum[i-1]-sum[q.front()];
if(i<=k+1)f[i]=sum[i-1];
while(!q.empty() && f[i]-sum[i]>=f[q.back()]-sum[q.back()])q.pop_back();//printf("%d\n",i);
q.push_back(i);
}
printf("%llu\n",f[n+1]);
}