There areNLittle kids sitting in a circle, each of them are carrying some java beans in their hand. Their teacher want to selectMKids who seated inMConsecutive
Seats and collect java beans from them.
The teacher knows the number of Java Beans each kids has, now she wants to know the maximum number of Java Beans she can get fromMConsecutively seated kids. Can you help her?
Input
There are multiple test cases. The first line of input is an integerTIndicating the number of test cases.
For each test case, the first line contains two integersN(1 ≤N≤ 200) andM(1 ≤M≤N).
HereNAndMAre defined in above description. The second line of each test case containsNIntegersCi(1
≤Ci≤ 1000) indicating number of Java BeansIthKid have.
Output
For each test case, output the corresponding maximum Java Beans the teacher can collect.
Sample Input
25 27 3 1 3 96 613 28 12 10 20 75
Sample output
16158
It is very easy to give n, k, and calculate the maximum sum of N numbers with the length of K, and there is no negative number. Note that these numbers constitute a ring.
Train of Thought: The data is small, and the water passes through
#include <stdio.h>#include <string.h>int main(){ int t,n,k,i,j,a[405],sum,MAX; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); for(i=1; i<=n; i++) { scanf("%d",&a[i]); a[n+i] = a[i]; } MAX = 0; for(i=1; i<=n; i++) { sum = 0; for(j = i; j<i+k; j++) { sum+=a[j]; } if(sum>MAX) MAX = sum; } printf("%d\n",MAX); } return 0;}