There are n products in the shop. The price of the ii-th product is aiai. 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 aiai and the new price bibi is at most kk. In other words, the condition |ai−bi|≤k|ai−bi|≤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 bibiof each product ii should be positive (i.e. bi>0bi>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 |ai−B|≤k|ai−B|≤k should be satisfied (where aiai 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 (1≤q≤1001≤q≤100) — the number of queries. Each query is presented by two lines.

The first line of the query contains two integers nn and kk (1≤n≤100,1≤k≤1081≤n≤100,1≤k≤108) — the number of products and the value kk. The second line of the query contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1081≤ai≤108), where aiai 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 |ai−B|≤k|ai−B|≤k should be satisfied (where aiai 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.

Example
input

Copy
4
5 1
1 1 2 3 1
4 2
6 4 8 5
2 2
1 6
3 5
5 2 5
output

Copy
2
6
-1
7
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: |1−B|≤2|1−B|≤2, |6−B|≤2|6−B|≤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.

Analysis:

It is very intuitive that the maximum price we can obtain is min+kmin+k where minmin is the minimum value in the array. For this price we should check that we can change prices of all products to it. It can be done very easily: we can just check if each segment [ai−k;ai+k][ai−k;ai+k] covers the point min+kmin+k. But this is not necessary because if we can change the price of the maximum to this value (min+kmin+k) then we can change each price in the segment [min;max][min;max] to this value. So we just need to check that min+k≥max−kmin+k≥max−k and if it is then print min+kmin+k otherwise print -1.

codes:

 #include<bits/stdc++.h>
using namespace std;
int main(){
#ifdef _DEBUG
freopen("input.txt","r",stdin);
#endif int q;
cin>>q;
for(int i=0;i<q;i++){
int n,k;
cin>>n>>k;
vector<int> a(n);
for(int j=0;j<n;j++){
cin>>a[j];
}
int mn= *min_element(a.begin(),a.end());
int mx= *max_element(a.begin(),a.end());
if(mx-mn>2*k) cout<<-1<<endl;
else cout<<mn+k<<endl;
}
return 0;
}

向量数组(vector<int> a)自带有求取最大值(    *max_element(a.begin(),a.end())和最小值(*min_element(a.begin(),a.end()))的函数哦!!!

Eqaulize Prices的更多相关文章

  1. hdu 4163 Stock Prices 水

    #include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #de ...

  2. BZOJ 4145: [AMPPZ2014]The Prices( 状压dp + 01背包 )

    我自己只能想出O( n*3^m )的做法....肯定会T O( nm*2^m )做法: dp( x, s ) 表示考虑了前 x 个商店, 已买的东西的集合为s. 考虑转移 : 先假设我们到第x个商店去 ...

  3. Kaggle竞赛 —— 房价预测 (House Prices)

    完整代码见kaggle kernel 或 Github 比赛页面:https://www.kaggle.com/c/house-prices-advanced-regression-technique ...

  4. Kaggle:House Prices: Advanced Regression Techniques 数据预处理

    本博客是博主在学习了两篇关于 "House Prices: Advanced Regression Techniques" 的教程 (House Prices EDA 和 Comp ...

  5. 【BZOJ】【4145】【AMPPZ2014】The Prices

    状压DP/01背包 Orz Gromah 容易发现m的范围很小……只有16,那么就可以状压,用一个二进制数来表示买了的物品的集合. 一种简单直接的想法是:令$f[i][j]$表示前$i$个商店买了状态 ...

  6. hdu 4163 Stock Prices 花式排序

    Stock Prices Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. 【BZOJ4145】[AMPPZ2014]The Prices 状压DP

    [BZOJ4145][AMPPZ2014]The Prices Description 你要购买m种物品各一件,一共有n家商店,你到第i家商店的路费为d[i],在第i家商店购买第j种物品的费用为c[i ...

  8. CF1234A Equalize Prices

    洛谷 CF1234A Equalize Prices Again 洛谷传送门 题目描述 You are both a shop keeper and a shop assistant at a sma ...

  9. Codeforces Round #590 (Div. 3) A. Equalize Prices Again

    链接: https://codeforces.com/contest/1234/problem/A 题意: You are both a shop keeper and a shop assistan ...

随机推荐

  1. python中的__dict__和dir()的区别

    Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案. __dict__与dir()的区别: dir()是一个函数,返回的是list: __di ...

  2. 树莓派查看ip地址(命令ifconfig)和退出ping

    1.1树莓派查看ip地址用如下命令: ifconfig -a 结果如下图所示: 注意:树莓派查看ip地址是用命令ifconfig,而Windows的cmd命令查看ip地址是ipconfig.

  3. Android_下方弹出菜单的实现

    这一功能要用到动画相关知识 实现点击按钮弹出下方输入框,这里点击可弹出一个输入界面,其中包括一个小型计算器. 点击date可弹出datedialog设置date. 1.编写弹出框的布局文件 <? ...

  4. [CF1303B] National Project - 数学

    Solution \(2a>n\),一次性结束,直接输出 \(n\) \(a \geq b\),那么一直修即可,直接输出 \(n\) 否则,\(a\) 占弱势,我们考虑用 \(a\) 修一半需要 ...

  5. laravel打印查询sql

    方法一(此方法支持 select 语句,insert,delect,update不支持) : $sql = DB::table('my_table')->select()->tosql() ...

  6. Android开发长按菜单上下文菜单

    安卓开发中长按弹出菜单的创建方法: 1.首先给View注册上下文菜单registerForContextMenu(); 2.添加上下文菜单内容onCreateContextMenu(): ---可以通 ...

  7. laravel中redis数据库的简单使用

    1.简介 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s . 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Set ...

  8. css权重及计算

    一.一般而言:!important--->行间样式--->id--->class | 属性--->标签选择器--->通配符 二.权重值 !important        ...

  9. JavaScript-事件类型

    DOM3事件类型: 1.UI事件:当用户与页面上的元素交互时触发 a.DOMActivate:元素已经被用户操作激活. b.load:(1)页面完全加载:window触发. (2)所有框架加载完毕:框 ...

  10. 统计redis大key信息(前topN)

    相关包下载链接 https://github.com/sripathikrishnan/redis-rdb-tools/releaseshttps://pypi.org/project/python- ...