单调队列优化dp
#include <iostream>
#include <cstdio>
using namespace std;
int dp[1000005], n, m, q[1000005], l, r, w[1000005];
//dp[i] = dp[j] + w[i] | i-m<=j<=i-1
int main(){
cin>>n>>m;
for(int i=1; i<=n; i++) scanf("%d", &w[i]);
for(int i=1; i<=n; i++){
while(l<=r && q[l]<i-m) l++;
dp[i] = dp[q[l]] + w[i];
while(l<=r && dp[q[r]]>dp[i]) r--;
q[++r] = i;
}
int minn=0x3f3f3f3f;
for(int i=n-m+1; i<=n; i++)
minn = min(minn, dp[i]);
cout<<minn<<endl;
return 0;
}