Link: Codeforces Round #570 (Div. 3)

B. Equalize Prices

Description:

There are n products in the shop. The price of the i-th product is ai. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly.

In fact, the owner of the shop can change the price of some product i in such a way that the difference between the old price of this product ai and the new price bi is at most k. In other words, the condition |ai−bi|≤k should be satisfied (|x| is the absolute value of x).

He can change the price for each product not more than once. Note that he can leave the old prices for some products. The new price bi of each product i should be positive (i.e. bi>0 should be satisfied for all i from 1 to n).

Your task is to find out the maximum possible equal price B of all productts with the restriction that for all products the condiion |ai−B|≤k should be satisfied (where ai is the old price of the product and B is the same new price of all products) or report that it is impossible to find such price B.

Note that the chosen price B should be integer.

You should answer q independent queries.

Input

The first line of the input contains one integer q (1≤q≤100) — the number of queries. Each query is presented by two lines.

The first line of the query contains two integers n and k (1≤n≤100,1≤k≤108) — the number of products and the value k. The second line of the query contains n integers a1,a2,…,an (1≤ai≤108), where ai is the price of the i-th product.

Output

Print q integers, where the i-th integer is the answer B on the i-th query.

If it is impossible to equalize prices of all given products with restriction that for all products the condition |ai−B|≤k should be satisfied (where ai is the old price of the product and B is the new equal price of all products), print -1. Otherwise print the maximum possible equal price of all products.

Example

input

4

5 1

1 1 2 3 1

4 2

6 4 8 5

2 2

1 6

3 5

5 2 5

output

2

6

-1

7

Note

In the first example query you can choose the price B=2. It is easy to see that the difference between each old price and each new price B=2 is no more than 1.

In the second example query you can choose the price B=6 and then all the differences between old and new price B=6 will be no more than 2.

In the third example query you cannot choose any suitable price B. For any value B at least one condition out of two will be violated: |1−B|≤2, |6−B|≤2.

In the fourth example query all values B between 1 and 7 are valid. But the maximum is 7, so it’s the answer.

Intentional analysis:

An easy problem.First we should judge the difference between the maximum and minimum values in the series and the size relationship of 2*k.If the difference is greater than 2*k,we should output “-1”,else output the sum of the minimum in the series and k.

## Click to see Chinese Intentional analysis

我们只需要判断数列中最大最小值的差跟2\*k的大小关系，如果差大于2\*k，就输出“-1”，否则就输出数列中的最小值与k的和。Code:

```
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
int a[maxn];
int main()
{
int q, n, k;
cin >> q;
while (q--)
{
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
if ((a[n - 1] - a[0]) > 2 * k)
{
puts("-1");
continue;
}
else
{
cout << a[0] + k << endl;
}
}
return 0;
}
```

C. Computer Game

Description:

Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.

During each turn Vova can choose what to do:

If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;

if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;

if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.

Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays is the maximum possible. It is possible that Vova cannot complete the game at all.

Your task is to find out the maximum possible number of turns Vova can just play or report that Vova cannot complete the game.

You have to answer q independent queries.

Input

The first line of the input contains one integer q (1≤q≤105) — the number of queries. Each query is presented by a single line.

The only line of the query contains four integers k,n,a and b (1≤k,n≤1e9,1≤b<a≤1e9) — the initial charge of Vova’s laptop battery, the number of turns in the game and values a and b, correspondingly.

Output

For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play otherwise.

Example

input

6

15 5 3 2

15 5 4 3

15 5 2 1

15 5 5 1

16 7 5 2

20 5 7 3

output

4

-1

5

2

0

1

Note

In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.

In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the notebook battery will be 0 after the last turn.

Intentional analysis:

When the laptop battery is greater than a(b)(equal is not allowed to reduce),it is going to reduce a(b).The number of reductions is greater than n to win.

We should find the maximum number of times “a” if Vova can finish the game.This requires a two-point answer.

## Click to see Chinese Intentional analysis

如果能获胜，就输出a能得到的最大值。说人话就是设减a的次数为x，求出可以满足`ax+b(n-x)>k`的a的值。然后二分就行了。Code:

```
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll k, n, a, b;
bool cal(ll m)
{
return (a * m + b * (n - m)) < k;
}
int main()
{
ll q;
cin >> q;
while (q--)
{
cin >> k >> n >> a >> b;
ll l = 0, r = n;
while (l <= r)
{
ll m = (l + r) / 2;
if (cal(m))
l = m + 1;
else
r = m - 1;
}
if (l > 0)
cout << min(l - 1, n) << endl;
else
cout << "-1" << endl;
}
return 0;
}
```

Thanks for my master @boctorio

D. Candy Box (easy version)

Description:

This problem is actually a subproblem of problem G from the same contest.

There are n candies in a candy box. The type of the i-th candy is ai (1≤ai≤n).

You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 1 and two candies of type 2 is bad).

It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.

Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.

You have to answer q independent queries.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input

The first line of the input contains one integer q (1≤q≤2⋅105) — the number of queries. Each query is represented by two lines.

The first line of each query contains one integer n (1≤n≤2⋅105) — the number of candies.

The second line of each query contains n integers a1,a2,…,an (1≤ai≤n), where ai is the type of the i-th candy in the box.

It is guaranteed that the sum of n over all queries does not exceed 2⋅105.

Output

For each query print one integer — the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.

Example

input

3

8

1 4 8 4 5 6 3 8

16

2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1

9

2 2 4 4 4 7 7 7 7

output

3

10

9

Note

In the first query, you can prepare a gift with two candies of type 8 and one candy of type 5, totalling to 3 candies.

Note that this is not the only possible solution — taking two candies of type 4 and one candy of type 6 is also valid.

Intentional analysis:

Count the number of occurrences of each number and sort it out. Add from the biggest beginning, pay attention to if you can’t continue to add the same, you should add one smaller than him.

## Click to see Chinese Intentional analysis

统计出每个数字出现的次数，排一下序。从最大的开始往下加，注意如果出现相同的不能继续加，应该加上一个比它小一的（比如说4，4加出来应该是4+3，如果是4，2，加起来应该是4+2）。难点就是如何实现。Code:

```
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int num[maxn];
int n, u, ans;
int main()
{
int q;
cin >> q;
while (q--)
{
cin >> n;
for (int i = 1; i <= n; i++)
num[i] = 0;
ans = 0;
for (int i = 1; i <= n; i++)
{
cin >> u;
num[u]++;
}
sort(num + 1, num + 1 + n);
int nw = n;
for (int i = n; i >= 1; i--)
{
if (nw > num[i])
{
nw = num[i];
}
ans += nw;
nw--;
if (nw <= 0)
break;
}
cout << ans << endl;
}
}
```