CF BUDDY
← Problems·

1250J · The Parade

1800 · binary search, greedy

Problem: The Berland Army is preparing for a large military parade. It is already decided that the soldiers participating in it will be divided into kk rows, and all rows will contain the same number of soldiers.

Of course, not every arrangement of soldiers into kk rows is suitable. Heights of all soldiers in the same row should not differ by more than 11. The height of each soldier is an integer between 11 and nn.

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 kk 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 22 or more.

Calculate the maximum number of soldiers who can participate in the parade.

Input Format: The first line contains one integer tt (1t100001 \le t \le 10000) — the number of test cases. Then the test cases follow.

Each test case begins with a line containing two integers nn and kk (1n300001 \le n \le 30000, 1k10121 \le k \le 10^{12}) — 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 nn integers c1c_1, c2c_2, ..., cnc_n (0ci10120 \le c_i \le 10^{12}), where cic_i is the number of soldiers having height ii in the Berland Army.

It is guaranteed that the sum of nn over all test cases does not exceed 3000030000.

Output Format: For each test case, print one integer — the maximum number of soldiers that can participate in the parade.

Note: Explanations for the example test cases:

  1. the heights of soldiers in the rows can be: [3,3,3,3][3, 3, 3, 3], [1,2,1,1][1, 2, 1, 1], [1,1,1,1][1, 1, 1, 1], [3,3,3,3][3, 3, 3, 3] (each list represents a row);
  2. all soldiers can march in the same row;
  3. 3333 soldiers with height 11 in each of 33 rows;
  4. all soldiers can march in the same row;
  5. all soldiers with height 22 and 33 can march in the same row.

Sample Cases

Case 1

Input

5
3 4
7 1 13
1 1
100
1 3
100
2 1
1000000000000 1000000000000
4 1
10 2 11 1

Output

16
100
99
2000000000000
13

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback