欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1183B.Equalize Prices
Equalize Prices
There are products in the shop. The price of the -th product is . 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 in such a way that the difference between the old price of this product and the new price is at most . In other words, the condition should be satisfied ( is the absolute value of ).
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 of each product should be positive (i.e. should be satisfied for all from to ).
Your task is to find out the maximum possible equal price of all productts with the restriction that for all products the condiion should be satisfied (where is the old price of the product and is the same new price of all products) or report that it is impossible to find such price .
Note that the chosen price should be integer.
You should answer independent queries.
Input
The first line of the input contains one integer () — the number of queries. Each query is presented by two lines.
The first line of the query contains two integers and () — the number of products and the value . The second line of the query contains integers (), where is the price of the -th product.
Output
Print integers, where the -th integer is the answer on the -th query.
If it is impossible to equalize prices of all given products with restriction that for all products the condition should be satisfied (where is the old price of the product and 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 . It is easy to see that the difference between each old price and each new price is no more than .
In the second example query you can choose the price and then all the differences between old and new price will be no more than .
In the third example query you cannot choose any suitable price . For any value at least one condition out of two will be violated: , .
In the fourth example query all values between and are valid. But the maximum is , 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
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |