You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
const int N = 200010;
|
|
|
|
|
const int INF = 0x3f3f3f3f;
|
|
|
|
|
int f[N];
|
|
|
|
|
int w[N], n, m;
|
|
|
|
|
int q[N], hh, tt;
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
cin >> n >> m;
|
|
|
|
|
for (int i = 1; i <= n; i++) cin >> w[i];
|
|
|
|
|
hh = 0, tt = 0, q[0] = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
|
// 滑动窗口在i左侧,不包括i,使用前序信息可以更新f[i],滑动窗口长度最长为m
|
|
|
|
|
while (hh <= tt && i - q[hh] > m) hh++;
|
|
|
|
|
|
|
|
|
|
// 因为i不在滑动窗口中,需要用滑动窗口的内容更新f[i],在while上方更新
|
|
|
|
|
f[i] = f[q[hh]] + w[i];
|
|
|
|
|
|
|
|
|
|
// i入队列
|
|
|
|
|
while (hh <= tt && f[q[tt]] >= f[i]) tt--;
|
|
|
|
|
q[++tt] = i;
|
|
|
|
|
}
|
|
|
|
|
// 答案可能存在于 n-1,n-2,...n-m+1中,枚举一下求最小值即可
|
|
|
|
|
int res = INF;
|
|
|
|
|
for (int i = n + 1 - m; i <= n; i++) res = min(res, f[i]);
|
|
|
|
|
printf("%d\n", res);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|