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.
Input
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.
Output
For each test case, print one integer — the maximum number of soldiers that can participate in the parade.
Example
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
Note
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.
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[300005];
LL n, m;
LL solve(LL l, LL r) {
if (l > r) return 0;
LL mid = (l + r) / 2;
LL b = 0, s = 0, t;
for (LL i = 0; i < n; i++) {
t = a[i] + b;
s += t / mid;
if (t > mid) //尽量使用上一高度的人
b = t % mid;
else
b = a[i] % mid;
if (s >= m) {
LL ans = solve(mid + 1, r);
if (ans) return ans;
return mid;
}
}
return solve(l, mid - 1);
}
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
memset(a, 0, sizeof(a));
cin >> n >> m;
LL sum = 0;
for (LL i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum >= m)
cout << m * solve(1, sum) << '\n';
else
cout << "0\n";
}
}