{"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 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?\n\n## Input\n\nThe 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.\n\nThe second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1 ≤ _a__i_ ≤ 109) — the initial prices.\n\n## Output\n\nPrint the only line containing the minimum number of seconds needed for prices to become equal, of «_\\-1_» if it is impossible.\n\n[samples]\n\n## Note\n\nConsider the first example.\n\nSuppose 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.\n\nThere could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3.\n\nIn 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.\n\nIn 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.","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 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?\n\nThe 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.\n\nThe second line contains $n$ integers $a_1, a_2, ..., a_n$ ($1 ≤ a_i ≤ 10^9$) — the initial prices.\n\nPrint the only line containing the minimum number of seconds needed for prices to become equal, or «-1» if it is impossible.\n\nConsider the first example. \n\nSuppose 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.\n\nThere could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is $3$.\n\nIn 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.\n\nIn 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## Input\n\nThe 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.\n\n## Output\n\nPrint the only line containing the minimum number of seconds needed for prices to become equal, or «-1» if it is impossible.\n\n[samples]\n\n## Note\n\nConsider 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.","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 of prices, where $ a_i \\in \\mathbb{Z}^+ $.  \n\n**Constraints**  \n1. $ 1 \\leq n \\leq 10^5 $  \n2. $ 1 \\leq k \\leq 10^9 $  \n3. $ 1 \\leq a_i \\leq 10^9 $ for all $ i \\in \\{1, \\dots, n\\} $  \n\n**Objective**  \nDetermine 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 $.  \n\n**Key Insight**  \nEach price $ a_i $ can only be decreased by multiples of $ k $, so after $ t_i $ decrements, it becomes $ a_i - k \\cdot t_i $.  \nFor all prices to become equal, there must exist a target value $ T $ such that:  \n$$\na_i - k \\cdot t_i = T \\quad \\forall i\n\\quad \\Rightarrow \\quad\na_i \\equiv T \\pmod{k}\n\\quad \\Rightarrow \\quad\na_i \\equiv a_j \\pmod{k} \\quad \\forall i,j\n$$  \nThus, a necessary condition for possibility is:  \n$$\na_i \\equiv a_j \\pmod{k} \\quad \\text{for all } i,j\n$$  \nIf this fails, output $ -1 $.  \n\nIf possible, let $ r = a_i \\bmod k $ (common residue). Define $ b_i = \\frac{a_i - r}{k} \\in \\mathbb{Z}_{\\geq 0} $.  \nThen $ T = r + k \\cdot m $ for some $ m \\in \\mathbb{Z} $, and $ t_i = b_i - m $.  \nWe require $ t_i \\geq 0 \\Rightarrow m \\leq b_i $ for all $ i $, so maximum feasible $ m $ is $ m^* = \\min_i b_i $.  \nTotal time:  \n$$\nt = \\sum_{i=1}^n t_i = \\sum_{i=1}^n (b_i - m^*) = \\left( \\sum_{i=1}^n b_i \\right) - n \\cdot m^*\n$$  \n\n**Final Formulation**  \nIf $ \\exists i,j : a_i \\not\\equiv a_j \\pmod{k} $, output $ -1 $.  \nElse:  \nLet $ r = a_1 \\bmod k $ (any $ a_i \\bmod k $),  \nLet $ b_i = \\frac{a_i - r}{k} $,  \nLet $ m^* = \\min_i b_i $,  \nOutput:  \n$$\n\\sum_{i=1}^n b_i - n \\cdot m^*\n$$","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF793A","tags":["implementation","math"],"sample_group":[["3 3\n12 9 15","3"],["2 2\n10 9","\\-1"],["4 1\n1 1000000000 1000000000 1000000000","2999999997"]],"created_at":"2026-03-03 11:00:39"}}