There are N little kids sitting in a circle, each of them are carrying some java beans in their hand. Their teacher want to select M kids who seated in M consecutive 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 from M consecutively seated kids. Can you help her?

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases.

For each test case, the first line contains two integers N (1 ≤ N ≤ 200) and M (1 ≤ M ≤ N). Here N and M are defined in above description. The second line of each test case contains N integers Ci (1 ≤ Ci ≤ 1000) indicating number of java beans the ith kid have.

Output

For each test case, output the corresponding maximum java beans the teacher can collect.

Sample Input

2
5 2
7 3 1 3 9
6 6
13 28 12 10 20 75

Sample Output

16
158

Author: FAN, Yuzhe
Contest: The 10th Zhejiang Provincial Collegiate Programming Contest

题目大意:有N个人坐成一圈,每个人有Ci个糖果,老师想找M个连续坐的同学中获得最多的糖果,问最多几个?

解题思路:最大连续和问题,这里连续数字个数为M,采用b[i]维护前i个糖果总和,那么求从i+1开始M个的总和就是:b[i+M]-b[i],枚举从i=0到i=N-1求最大值即可。

 #include<iostream>
#include<algorithm>
using namespace std;
int main(){
int T;
cin>>T;
int a[],b[];
while(T--){
int N,M;
cin>>N>>M;
for(int i=;i<;i++)b[i]=;
for(int i=;i<N;i++){
cin>>a[i];
if(i==)b[i]=a[i];
else b[i]=b[i-]+a[i];
}//边输入边维护一个前i个数之和b[i]
for(int i=N;i<*N;i++){
b[i]=b[i-]+a[i%N];
}//继续维护b[i]使之满足一个环遍历的要求
int max=-;
for(int i=;i<N;i++){
int sum=b[i+M]-b[i];
if(sum>max)max=sum;
}//取得长为M的最大连续和
cout<<max<<'\n';
}return ; }

[ACM_水题] ZOJ 3714 [Java Beans 环中连续m个数最大值]的更多相关文章

  1. zoj 3714 Java Beans

    /*很简单的一题,求连续的m位,求总和最多的值,循环找一下,就出来了*/ #include<stdio.h> ]; int main(int argc, char* argv[]) { i ...

  2. [ACM_水题] ZOJ 3706 [Break Standard Weight 砝码拆分,可称质量种类,暴力]

    The balance was the first mass measuring instrument invented. In its traditional form, it consists o ...

  3. [ACM_水题] ZOJ 3712 [Hard to Play 300 100 50 最大最小]

    MightyHorse is playing a music game called osu!. After playing for several months, MightyHorse disco ...

  4. ACM_水题你要信了(修改版)

    水题你要信了 Time Limit: 2000/1000ms (Java/Others) Problem Description: 某发最近又认识了很多妹(han)子,可是妹(han)子一多不免有时会 ...

  5. 水题 ZOJ 3875 Lunch Time

    题目传送门 /* 水题:找排序找中间的价格,若有两个,选价格大的: 写的是有点搓:) */ #include <cstdio> #include <iostream> #inc ...

  6. 水题 ZOJ 3876 May Day Holiday

    题目传送门 /* 水题:已知1928年1月1日是星期日,若是闰年加1,总天数对7取余判断就好了: */ #include <cstdio> #include <iostream> ...

  7. 水题 ZOJ 3880 Demacia of the Ancients

    题目传送门 /* 水题:) */ #include <cstdio> #include <iostream> #include <algorithm> #inclu ...

  8. ACM_水题你信吗

    水题你信吗 Time Limit: 2000/1000ms (Java/Others) Problem Description: 某发最近又认识了很多妹(han)子,可是妹(han)子一多不免有时会忘 ...

  9. 水题 ZOJ 3869 Ace of Aces

    题目传送门 水题,找出出现次数最多的数字,若多个输出Nobody //#include <bits/stdc++.h> //using namespace std; #include &l ...

随机推荐

  1. mysql——查询练习

    Sutdent表的定义 字段名 字段描述 数据类型 主键 外键 非空 唯一 自增 Id 学号 INT(10) 是 否 是 是 是 Name 姓名 VARCHAR(20) 否 否 是 否 否 Sex 性 ...

  2. javascript-test1

    var AAA = function(name, age) { this.name = name ; this.age = age; } AAA.prototype.getName = functio ...

  3. RMQ(范围最值问题)算法学习

    RMQ算法适合求解对一个数组多次查询给定范围内的最值. 预处理操作: 令d[i,j]表示从i开始,长度为2^j的一段元素的最值,可以用递推公式写出d[i,j] = min{ d[i][j-1], d[ ...

  4. 禅道Linux一键安装版

    禅道Linux一键安装版 Linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅道.从7.3版本开始,linux一键安装包分为32位和64位两个包,大家 ...

  5. html+css-----补

    其实html没什么好补充的了,主要是使用css如何构造出各种想要的效果 1.加减框 <!DOCTYPE html> <html lang="en"> < ...

  6. ios 用户相册

    ## 获得自定义的所有相簿 // 获得所有的自定义相簿 PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCol ...

  7. 【kd-tree】bzoj4154 [Ipsc2015]Generating Synergy

    区间修改的kd-tree,打标记,下传. 每次询问的时候,从询问点向上找到根,然后依次下传下来,再回答询问. #include<cstdio> #include<algorithm& ...

  8. Restful风格API接口开发springMVC篇

    Restful风格的API是一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机 ...

  9. 混合硬盘的U310重装系统

    最早接触U310 是在半年前,那时候同学在用的就是U310,需要升级Windows 8 依照以往的经验,弄了一张Windows 8的启动U盘,当在选择磁盘的时候居然发现没有可选项!这个有点出乎意料…… ...

  10. [转]Oracle数据泵的使用

    使用数据泵导出数据 1.连接Oracle数据库SQL> conn / as sysdba已连接. 2.创建一个操作目录SQL> create directory dump_dir as ' ...