Problem description

Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?

Input

The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.

Output

Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.

Examples

Input

3 3
12 9 15

Output

3

Input

2 2
10 9

Output

-1

Input

4 1
1 1000000000 1000000000 1000000000

Output

2999999997

Note

Consider the first example.

Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds.

There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3.

In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal.

In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.

解题思路:题目的意思就是输入一个n(表示有n个数)和一个公差k,其中n个数中最小值为minval,要求除最小值外,其他数按k值递减,如果刚好都递减到最小值(此时n个数都为minval),则输出递减的总次数,否则输出-1。做法:每个数先减去最小值,查看剩下的值是否为k的倍数,如果是累加其递减次数,否则就break,输出-1,水过。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n,k,s[],minval=INF;bool flag=false;long long tims=;//注意类型是long long,避免数据溢出
int main(){
cin>>n>>k;
for(int i=;i<n;++i){cin>>s[i];minval=min(minval,s[i]);}
for(int i=;i<n;++i){
s[i]-=minval;
if(s[i]%k){flag=true;break;}
else tims+=s[i]/k;
}
if(flag)cout<<"-1"<<endl;
else cout<<tims<<endl;
return ;
}

最新文章

  1. django用户认证
  2. texy
  3. Java 验证码、二维码
  4. [UCSD白板题] Minimum Dot Product
  5. 【Java每日一题】20161208
  6. pscp详解
  7. VLAN是什么
  8. C#面向对象的一些东西
  9. ASP.NET WEB API 2 框架揭秘 读书笔记(一)
  10. 提示constructor无法location的原因
  11. 双击jar包无法直接运行的问题
  12. PHP开发者常用的正则表达式及实例【长期更新收录】
  13. git添加秘钥提示Key is already in use
  14. 将Go的main包拆分为多个文件
  15. CSS盒子模型 box-sizing 用法
  16. loj #6.Guess Number
  17. curl的http上传文件代码
  18. CentOS入门
  19. Oracle数据库实例的启动及关闭
  20. java多线程系列17 ThreadLocal

热门文章

  1. 【C++】颜色的设置
  2. Restrictions.like(&quot;字段field&quot;,&quot;%表达式exp%&quot;);
  3. 【JavaScript游戏开发】JavaScript+HTML5封装的苏拉卡尔塔游戏(包含源码)
  4. 洛谷P1002 过河卒【dp】
  5. NOIP2009 T2 Hankson的趣味题
  6. [luogu4799 CEOI2015 Day2] 世界冰球锦标赛(折半搜索)
  7. 5.win上安装ES
  8. jQuery cxCalendar 日期选择器
  9. js中window.location的用法
  10. ip代理池学习