CF1183B.Equalize Prices

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 > 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.

输入格式

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.

输出格式

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.

输入输出样例

  • 输入#1

    4
    5 1
    1 1 2 3 1
    4 2
    6 4 8 5
    2 2
    1 6
    3 5
    5 2 5
    

    输出#1

    2
    6
    -1
    7
    

说明/提示

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.

首页