Oleg and shares CodeForces - 793A

Oleg and shares

CodeForces - 793A

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.

Example
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,k和n个数,每次只能对一个数减k,问最后这n个数变成相同的需要多少次,如果不可能输出-1

思路:首先说,这道题在大多数人中应该看作是一道水题,直接和最小的做个差,然后除以k就是这个数所需要的次数,最后求和

(但是但是我就做出不来,并不会做,而是跟没没敢去尝试,在集训队学练习了这么久,对自己反而越来越不自信,越是简单的题,越往难里想,绞尽脑汁套各种算法,结果算法用不上,简单的想法也直接忽略,其实在集训队练习多了会发现我们最害怕的并不是不会做,而是怕用简单的做法暴力之类的会超时,怕超时,结果就造成一个也不会做的局面,但是正确的方式应该是先从简单的做法开始尝试,不行再改,以后要端正心态)以上废话和此题无关,仅是自我反思可以跳过


code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define INF 1000000000
int n,k;
int a[100005];

int main(){
    int i,j;
    int mins = INF;
    cin >> n >> k;
    for(i = 0; i < n; i++){
        cin >> a[i];
        if(a[i] < mins)
            mins = a[i];//找出所有数字中的最小的来,目标就是向这个最小的变化
    }
    long long sum = 0;//记录总的变换次数即秒数,用long long会超int
    int tmp;
    int flag = 1;//判断能否变成一样大的
    for(i = 0; i < n; i++){
        if((a[i]-mins)%k){//如果当前数与最小数的差值不是k的倍数则永远不会减到相同的情况
            flag = 0;
            break;
        }
        sum += (a[i]-mins)/k;//如果可以,每次加上次数
    }
    if(flag)
        cout << sum << endl;
    else
        cout << "-1" << endl;
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值