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 ;
}

C - Oleg and shares的更多相关文章

  1. 【codeforces 793A】Oleg and shares

    [题目链接]:http://codeforces.com/contest/793/problem/A [题意] 每次你可以对1..n中的任意一个数字进行减少k操作; 问你最后可不可能所有的数字都变成一 ...

  2. CF793A Oleg and shares 题解

    Content 有 \(n\) 支股票,第 \(i\) 支股票原价为 \(a_i\) 卢布.每秒钟可能会有任意一支股票的价格下降 \(k\) 卢布,以至于降到负数.求所有股票的价格均变得相同所要经过的 ...

  3. Tinkoff Challenge - Elimination Round 开始补题

    A. Oleg and shares time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. How to enable $Admin Shares in Windows 7

    Quote from: http://www.wintips.org/how-to-enable-admin-shares-windows-7/ As “Administrative shares” ...

  6. [rxjs] Shares a single subscription -- publish()

    If have an observable and you subscribe it twice, those tow subscritions have no connection. console ...

  7. How To mount/Browse Windows Shares【在linux{centos}上挂载、浏览window共享】

    How to mount remote Windows shares Contents Required packages Basic method Better Method Even-better ...

  8. Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008

    Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...

  9. Interview with Oleg

    Interview with Oleg time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. ASP.NET刷新页面的六种方法

    第一: private void Button1_Click( object sender, System.EventArgs e )   {      Response.Redirect( Requ ...

  2. 安卓多线程——AsyncTask

    在采集视频的同时需要对视频进行实时处理,因此要使用到多线程. AsyncTask是android提供的一个处理异步任务的框架,相当于Handler+Thread.相比而言,AsyncTask的优点是封 ...

  3. CorelDRAW关于使用鼠标的5个技巧分享

    CorelDRAW重度依赖者对快捷键和技巧性的操作爱不释手.本文我们介绍使用CorelDRAW关于鼠标操作的5个技巧,这是五种超快,非常有效,特别实用的技巧,如果每天的工作结合这些技巧的使用,效率会大 ...

  4. BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环

    Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...

  5. LINUX - getopts

    getopts optionString opt; optionString :所有参数组成的-参数串: opt:从optionString 每次取的参数值: 当optionString用[:]开头, ...

  6. 原来这才是Kafka的“真面目”

    作者介绍 郑杰文,腾讯云存储,高级后台工程师,2014 年毕业加入腾讯,先后从事增值业务开发.腾讯云存储开发.对业务性.技术平台型后台架构设计都有深入的探索实践.对架构的海量并发.高可用.可扩展性都有 ...

  7. vue-router 懒加载

    懒加载:也叫延迟加载,即在需要的时候进行加载,按需加载. 那vue 为什么需要懒加载呢? 使用 vue-cli构建的项目,在默认情况下,执行 npm run build  会将所有的 js代码打包为一 ...

  8. fzu 2087并查集的运用求最小生成树的等效边

    //对数组排序后,对于边相同并且边的两端不在一个集合内的一定是等效边或者必加边, //第一数数,第二合并集合 #include<stdio.h> #include<stdlib.h& ...

  9. 【ACM】hdu_1042_N!_201308071639

    N!Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  10. arcgis for javascript 添加featurelayer,设置地图最大最小等级

    转自原文arcgis for javascript 添加featurelayer,设置地图最大最小等级 var map; var livingCenter; var livingCenterUrl = ...