A. Oleg and shares

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

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 9rubles, 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.

很简单的题

代码如下:

//
//  main.cpp
//  A. Oleg and shares
//
//  Created by 徐智豪 on 2017/4/24.
//  Copyright © 2017年 徐智豪. All rights reserved.
//

#include <iostream>
#include <algorithm>
using namespace std;
int num[100005]={0},k,n;
int main(int argc, const char * argv[]) {
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        cin>>num[i];
    sort(num+1,num+n+1);
    long long count=0;
    int temp;
    for(int i=2;i<=n;i++)
    {
        temp=(num[i]-num[1])%k;
        if(temp!=0)
        {
            cout<<-1<<endl;
            return 0;
        }
        else
            count+=(num[i]-num[1])/k;
    }
    cout<<count<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值