O - Watering Plants Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 533 点

問題文

高橋君は温室で N 株の植物を育てています。それぞれの植物には現在の水分量が設定されており、 i 番目の植物の水分量は T_i です。水分量が 0 以下になった植物は枯れてしまいます。

温室にはじょうろが 1 つだけあります。高橋君はこのじょうろで植物に水を与えることで、水分量を回復させることができます。具体的には、高橋君は毎秒の開始時に以下の操作を行います:

  1. まず、高橋君は N 株の植物の中から ちょうど 1 つ を選び、じょうろで水を与える。水を与えられた植物の水分量は P だけ増加する。
  2. その後、 すべての 植物の水分量が 1 減少する(水を与えられた植物も含む)。これは温室の高温による蒸発を表している。

高橋君は、すべての植物が枯れない状態(すべての植物の水分量が 1 以上である状態)を可能な限り長く維持したいと考えています。

高橋君が最適に行動したとき、すべての植物の水分量を 1 以上に保ち続けられる秒数が有限であればその最大値を、永遠に保ち続けられるならば -1 を出力してください。

ここで、秒数とは操作を行った回数を指します。つまり、 1 回も操作を行わずにいずれかの植物の水分量が 0 以下であれば答えは 0 です。操作を k 回行った直後にすべての植物の水分量が 1 以上であるような最大の k を求めてください(永遠に可能なら -1 )。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P \leq 10^4
  • 1 \leq T_i \leq 10^9
  • 入力はすべて整数である。

入力

N P
T_1 T_2 \ldots T_N
  • 1 行目には、植物の株数を表す N と、水やりによる水分量の増加量を表す P が、スペース区切りで与えられる。
  • 2 行目には、各植物の初期水分量を表す T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。

出力

すべての植物の水分量を 1 以上に保ち続けられる最大の操作回数を 1 行で出力せよ。永遠に保ち続けられる場合は -1 を出力せよ。


入力例 1

2 1
2 3

出力例 1

3

入力例 2

1 1
1

出力例 2

-1

入力例 3

6 3
4 8 2 10 5 7

出力例 3

7

入力例 4

12 5
15 3 22 8 19 6 11 30 4 17 25 9

出力例 4

17

入力例 5

1 10000
1000000000

出力例 5

-1

Score : 533 pts

Problem Statement

Takahashi is growing N plants in a greenhouse. Each plant has an associated current water level, and the water level of the i-th plant is T_i. A plant with a water level of 0 or less will wither.

There is only one watering can in the greenhouse. Takahashi can restore the water levels of the plants by watering them with this can. Specifically, at the beginning of each second, Takahashi performs the following operation:

  1. First, Takahashi chooses exactly one of the N plants and waters it with the watering can. The water level of the watered plant increases by P.
  2. After that, the water level of all plants decreases by 1 (including the plant that was just watered). This represents evaporation due to the high temperature in the greenhouse.

Takahashi wants to keep all plants from withering (i.e., keep the water level of all plants at 1 or more) for as long as possible.

Assuming Takahashi acts optimally, if the number of seconds he can keep the water level of all plants at 1 or more is finite, output the maximum such number of seconds. If he can keep them alive forever, output -1.

Here, the number of seconds refers to the number of operations performed. That is, if the water level of any plant is 0 or less before performing any operations, the answer is 0. Find the maximum k such that the water level of all plants is 1 or more immediately after performing the operation k times (or -1 if it can be kept alive forever).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P \leq 10^4
  • 1 \leq T_i \leq 10^9
  • All input values are integers.

Input

N P
T_1 T_2 \ldots T_N
  • The first line contains N, the number of plants, and P, the amount of water level increase per watering, separated by a space.
  • The second line contains the initial water levels of each plant, T_1, T_2, \ldots, T_N, separated by spaces.

Output

Print the maximum number of operations for which the water level of all plants can be kept at 1 or more. If they can be kept alive forever, print -1.


Sample Input 1

2 1
2 3

Sample Output 1

3

Sample Input 2

1 1
1

Sample Output 2

-1

Sample Input 3

6 3
4 8 2 10 5 7

Sample Output 3

7

Sample Input 4

12 5
15 3 22 8 19 6 11 30 4 17 25 9

Sample Output 4

17

Sample Input 5

1 10000
1000000000

Sample Output 5

-1