资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
输入n个数,围成一圈,求连续m(m<n)个数的和最大为多少?
输入格式
输入的第一行包含两个整数n, m。第二行,共n个整数。
输出格式
输出1行,包含一个整数,连续m个数之和的最大值。
样例输入
10 3
9 10 1 5 9 3 2 6 7 4
样例输出
23
数据规模和约定
0<m<n<1000, -32768<=输入的每个数<=32767。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
//ios::sync_with_stdio(false);
typedef long long LL;
using namespace std;
const int MAXN = 1e8;
int main() {
int n, m, a[1100];
LL max = -32767;
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for(int i = 0; i < n; i++) {
LL sum = 0;
for(int j = i, k = 0; k < m; j = (j + 1) % n, k++) {
sum += a[j];
}
if(sum > max) {
max = sum;
}
}
cout << max;
return 0;
}