CF793A.Oleg and shares

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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?

银行客户奥列格每天都会查看股票价格。他关注 nn 种股票的价格。今天他观察到:每一秒恰好有一种股票价格下降 kk 卢布(注意:每一秒恰好有一种价格发生变化,但在不同的秒内,发生变化的股票可以不同)。股票价格可以变为负数。奥列格觉得这一过程很有意思,于是向金融分析师伊戈尔提问:所有 nn 种股票价格全部变得相等所需的最短时间是多少?或者根本不可能实现? 伊戈尔目前很忙,因此请你来帮助奥列格。你能回答这个问题吗?

输入格式

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 _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ 109) — the initial prices.

第一行包含两个整数 nn 和 kk(1 ≤ n ≤ 1051 \leq n \leq 10^5,1 ≤ k ≤ 1091 \leq k \leq 10^9)—— 分别表示股票价格的数量,以及每秒钟某价格下降的卢布数。

第二行包含 nn 个整数 a1, a2, ..., ana_1,\,a_2,\,...,\,a_n(1 ≤ ai ≤ 1091 \leq a_i \leq 10^9)—— 初始价格。

输出格式

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

输出唯一一行,包含价格变为相等所需的最少秒数;若不可能,则输出 "-1"。

输入输出样例

  • 输入#1

    3 3
    12 9 15

    输出#1

    3
  • 输入#2

    2 2
    10 9

    输出#2

    -1
  • 输入#3

    4 1
    1 1000000000 1000000000 1000000000

    输出#3

    2999999997

说明/提示

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.

考虑第一个例子。

假设在第一秒内,第三个价格下降并变为 12 卢布;接着在第二秒内,第一个价格下降并变为 9 卢布;在第三秒内,第三个价格再次下降并变为 9 卢布。此时所有价格均等于 9 卢布,耗时 3 秒。

可能存在其他情形,但该情形使所有价格变得相等所需时间最短。因此答案为 3。

在第二个例子中,我们注意到第一个价格与第二个价格的奇偶性不同,且在所述过程中该奇偶性始终保持不变。因此,价格永远无法变得相等。

在第三个例子中,可能发生如下情形:首先第二个价格下降,然后第三个价格下降,接着第四个价格下降。该循环重复 999999999 次;由于每秒仅允许一个价格下降,整个过程耗时 999999999 × 3 = 2999999997999999999 \times 3 = 2999999997 秒。可以注意到,这是可能的最短时间。

输入解题思路,AI测评打分。不知道怎么写?

首页