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.

22 lines
928 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
#define int long long
#define endl "\n"
int a[N], s[N];
int n, k; // n个城市传送半径为k
int res;
signed main() {
cin >> n >> k; // n个城市传送半径为k
for (int i = 1; i < n; i++) { // 以起点为线段命名的编号,所以1~n共n个城市,其实是n-1条线段
cin >> a[i]; // 走这条路要耗费时间a[i]
s[i] += s[i - 1] + a[i]; // 前缀和
}
/*
1、找出[1,n-1]的原始数组中长度为k的子区间加法和最大的区间段
2、注意如果是枚举原始数组,那么就是 [1,n-k]是有效区间
3、现在遍历的是前缀和数组为了表示a[1],需要使用的是s[1]-s[0],所以i从0开始
*/
for (int i = 0; i + k < n; i++) res = max(res, s[i + k] - s[i]);
cout << s[n - 1] - res << endl;
}