English · Original
Chinese · Translation
Formal · Original
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 _a_1, _a_2, ..., _a__n_ (1 ≤ _a__i_ ≤ 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.
[samples]
## 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.
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?
The first line contains two integers $n$ and $k$ ($1 ≤ n ≤ 10^5, 1 ≤ k ≤ 10^9$) — 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 ≤ 10^9$) — the initial prices.
Print the only line containing the minimum number of seconds needed for prices to become equal, or «-1» if it is impossible.
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.
## Input
The first line contains two integers $n$ and $k$ ($1 ≤ n ≤ 10^5, 1 ≤ k ≤ 10^9$) — 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 ≤ 10^9$) — the initial prices.
## Output
Print the only line containing the minimum number of seconds needed for prices to become equal, or «-1» if it is impossible.
[samples]
## 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.
**Definitions**
Let $ n \in \mathbb{Z}^+ $ be the number of share prices.
Let $ k \in \mathbb{Z}^+ $ be the fixed decrement per second.
Let $ A = (a_1, a_2, \dots, a_n) $ be the initial sequence of prices, where $ a_i \in \mathbb{Z}^+ $.
**Constraints**
1. $ 1 \leq n \leq 10^5 $
2. $ 1 \leq k \leq 10^9 $
3. $ 1 \leq a_i \leq 10^9 $ for all $ i \in \{1, \dots, n\} $
**Objective**
Determine the minimum non-negative integer $ t $ such that after $ t $ seconds of decrementing exactly one price by $ k $ per second, all prices become equal. If impossible, return $ -1 $.
**Key Insight**
Each price $ a_i $ can only be decreased by multiples of $ k $, so after $ t_i $ decrements, it becomes $ a_i - k \cdot t_i $.
For all prices to become equal, there must exist a target value $ T $ such that:
$$
a_i - k \cdot t_i = T \quad \forall i
\quad \Rightarrow \quad
a_i \equiv T \pmod{k}
\quad \Rightarrow \quad
a_i \equiv a_j \pmod{k} \quad \forall i,j
$$
Thus, a necessary condition for possibility is:
$$
a_i \equiv a_j \pmod{k} \quad \text{for all } i,j
$$
If this fails, output $ -1 $.
If possible, let $ r = a_i \bmod k $ (common residue). Define $ b_i = \frac{a_i - r}{k} \in \mathbb{Z}_{\geq 0} $.
Then $ T = r + k \cdot m $ for some $ m \in \mathbb{Z} $, and $ t_i = b_i - m $.
We require $ t_i \geq 0 \Rightarrow m \leq b_i $ for all $ i $, so maximum feasible $ m $ is $ m^* = \min_i b_i $.
Total time:
$$
t = \sum_{i=1}^n t_i = \sum_{i=1}^n (b_i - m^*) = \left( \sum_{i=1}^n b_i \right) - n \cdot m^*
$$
**Final Formulation**
If $ \exists i,j : a_i \not\equiv a_j \pmod{k} $, output $ -1 $.
Else:
Let $ r = a_1 \bmod k $ (any $ a_i \bmod k $),
Let $ b_i = \frac{a_i - r}{k} $,
Let $ m^* = \min_i b_i $,
Output:
$$
\sum_{i=1}^n b_i - n \cdot m^*
$$
API Response (JSON)
{
"problem": {
"name": "A. Oleg and shares",
"description": {
"content": "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 ea",
"description_type": "Markdown"
},
"platform": "Codeforces",
"limit": {
"time_limit": 1000,
"memory_limit": 262144
},
"difficulty": "None",
"is_remote": true,
"is_sync": true,
"sync_url": null,
"sign": "CF793A"
},
"statements": [
{
"statement_type": "Markdown",
"content": "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 ea...",
"is_translate": false,
"language": "English"
},
{
"statement_type": "Markdown",
"content": "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 ea...",
"is_translate": true,
"language": "Chinese"
},
{
"statement_type": "Markdown",
"content": "**Definitions** \nLet $ n \\in \\mathbb{Z}^+ $ be the number of share prices. \nLet $ k \\in \\mathbb{Z}^+ $ be the fixed decrement per second. \nLet $ A = (a_1, a_2, \\dots, a_n) $ be the initial sequence...",
"is_translate": false,
"language": "Formal"
}
]
}