CF1250J.The Parade
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The Berland Army is preparing for a large military parade. It is already decided that the soldiers participating in it will be divided into k rows, and all rows will contain the same number of soldiers.
Of course, not every arrangement of soldiers into k rows is suitable. Heights of all soldiers in the same row should not differ by more than 1. The height of each soldier is an integer between 1 and n.
For each possible height, you know the number of soldiers having this height. To conduct a parade, you have to choose the soldiers participating in it, and then arrange all of the chosen soldiers into k rows so that both of the following conditions are met:
- each row has the same number of soldiers,
- no row contains a pair of soldiers such that their heights differ by 2 or more.
Calculate the maximum number of soldiers who can participate in the parade.
伯兰德军队正在为一场大型军事阅兵式做准备。目前已确定,参加阅兵式的士兵将被分为 k 行,且每行士兵人数相同。
当然,并非所有将士兵划分为 k 行的方式都合适。同一行中所有士兵的身高之差不能超过 1。每位士兵的身高是一个介于 1 到 n 之间的整数。
对于每个可能的身高值,你已知具有该身高的士兵人数。为举行阅兵式,你需要先选出参与阅兵的士兵,再将所有选中的士兵安排成 k 行,使得同时满足以下两个条件:
- 每行士兵人数相同;
- 任意一行中均不存在两名士兵,其身高之差为 2 或更大。
请计算最多可有多少名士兵参加阅兵式。
输入格式
The first line contains one integer t (1≤t≤10000) — the number of test cases. Then the test cases follow.
Each test case begins with a line containing two integers n and k (1≤n≤30000, 1≤k≤1012) — the number of different heights of soldiers and the number of rows of soldiers in the parade, respectively.
The second (and final) line of each test case contains n integers c1, c2, ..., cn (0≤ci≤1012), where ci is the number of soldiers having height i in the Berland Army.
It is guaranteed that the sum of n over all test cases does not exceed 30000.
第一行包含一个整数 t(1≤t≤10000),表示测试用例的数量。随后是各测试用例。
每个测试用例以一行开始,该行包含两个整数 n 和 k(1≤n≤30000,1≤k≤1012),分别表示士兵的不同身高种类数以及阅兵队伍的行数。
每个测试用例的第二行(也是最后一行)包含 n 个整数 c1, c2, ..., cn(0≤ci≤1012),其中 ci 表示贝兰德军队中身高为 i 的士兵人数。
保证所有测试用例的 n 之和不超过 30000。
输出格式
For each test case, print one integer — the maximum number of soldiers that can participate in the parade.
对于每个测试用例,输出一个整数——即最多可以参加阅兵的士兵人数。
输入输出样例
输入#1
5 3 4 7 1 13 1 1 100 1 3 100 2 1 1000000000000 1000000000000 4 1 10 2 11 1
输出#1
16 100 99 2000000000000 13
说明/提示
Explanations for the example test cases:
- the heights of soldiers in the rows can be: [3,3,3,3], [1,2,1,1], [1,1,1,1], [3,3,3,3] (each list represents a row);
- all soldiers can march in the same row;
- 33 soldiers with height 1 in each of 3 rows;
- all soldiers can march in the same row;
- all soldiers with height 2 and 3 can march in the same row.
示例测试用例的解释:
- 各行士兵的身高可以为:[3,3,3,3]、[1,2,1,1]、[1,1,1,1]、[3,3,3,3](每个列表代表一行);
- 所有士兵均可排在同一行中;
- 每行各有 33 名身高为 1 的士兵,共 3 行;
- 所有士兵均可排在同一行中;
- 所有身高为 2 和 3 的士兵均可排在同一行中。
输入解题思路,AI测评打分。不知道怎么写?