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_财务统计各产品品类各城市上周收入毛利表_20161202

    注:财务需要统计各产品品类各城市上周毛利情况 下面这样的表是沟通后展现的形式.数据展现形式我认为的大都是行列转 列转行的转置 从财务角度这样展现形式比较适合. 由于黄色部分为汇总项目,因此我拆分成9个 ...

  2. HTML <!DOCTYPE> Declaration

    <!DOCTYPE html><html><head><title>Title of the document</title></he ...

  3. ORACLE rowid切分大表

    通过如下sql获取rowid切分范围 ) || dbms_rowid.rowid_create(, DOI, lo_fno, lo_block, ) ) || ) || dbms_rowid.rowi ...

  4. Springmvc4 com/fasterxml/jackson/core/JsonProcessingException

    非常感谢: 谭卓博客 springmvc4 json springmvc4 集成hibernate4 出现 NoClassDefFoundError:com/fasterxml/jackson/cor ...

  5. mongodb:短网址项目

    短网址项目概述 1.短网址项目,是将给定的长网址,转换成短网址. 如 新浪 http://t.cn/zQd5NPw ,其中zQd5NPw就是短网址 前段页面如下

  6. 如何通过maven ,将本地jar 安装到仓库中去。

    场景: 现在很多公司,都有 maven 的私服 ,在maven项目中,基本上有两个仓库 ,一个是maven的公共仓库,一个是私服仓库: 有的时候,我们download 别人的代码的时候,pom文件中报 ...

  7. CSS透明属性详解

    .transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0.5; opacity: 0.5; ...

  8. linux 清理内存命令 查看内存命令

    查看内存: 我们可以用free命令查看内存信息: free -g total used free shared buffers cachedMem: 15   15    0     0       ...

  9. 编译jsoncpp库以及要注意的问题

    原创文章,转载请注明原作者与本文原始URL. 版本:jsoncpp-src-0.5.0.zip简介:jsoncpp是用cpp实现的json库,可以拼装,解析,生成json串.我们要把他编译成动态库.这 ...

  10. javascript中闭包的概念

    这个是每个前端工程师绕不开的一个问题,网上各种资料很多,整个春节,我仔细研读了红皮经典中关于这一块的注释,加深了对这一块的理解. 有好几个概念需要重申一下.以下都是我的理解: 1. 闭包是javasc ...