CF1183B.Equalize Prices

传统题 时间 2000 ms 内存 256 MiB 4 尝试 1 已通过 1 标签

Equalize Prices

There are nn products in the shop. The price of the ii-th product is aia_i. 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 ii in such a way that the difference between the old price of this product aia_i and the new price bib_i is at most kk. In other words, the condition aibik|a_i - b_i| \le k should be satisfied (x|x| is the absolute value of xx).

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 bib_i of each product ii should be positive (i.e. bi>0b_i \gt 0 should be satisfied for all ii from 11 to nn).

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

Note that the chosen price BB should be integer.

You should answer qq independent queries.

Input

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

The first line of the query contains two integers nn and kk (1n100,1k1081 \le n \le 100, 1 \le k \le 10^8) — the number of products and the value kk. The second line of the query contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1081 \le a_i \le 10^8), where aia_i is the price of the ii-th product.

Output

Print qq integers, where the ii-th integer is the answer BB on the ii-th query.

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

Note

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

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

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

In the fourth example query all values BB between 11 and 77 are valid. But the maximum is 77, so it's the answer.

Samples

4
5 1
1 1 2 3 1
4 2
6 4 8 5
2 2
1 6
3 5
5 2 5
2
6
-1
7

在线编程 IDE

建议全屏模式获得最佳体验