A - りんごの重さ調整

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 233

問題文

高橋君は、果樹園で N 個のりんごを収穫しました。りんご i (1 \leq i \leq N) の重さは A_i グラムです。

収穫後、高橋君はすべてのりんごにコーティング剤を塗りました。コーティング剤を塗ることで各りんごの重さはちょうど R グラムずつ増えるため、コーティング後のりんご i の重さは A_i + R グラムになりました。

高橋君は、これらのりんごを贈答用の箱に詰めるため、すべてのりんごの重さを同じにしたいと考えています。そこで、各りんごを削って重さを減らすことにしました。各りんごからは 0 以上の任意の整数グラムだけ削ることができます。ただし、重さを増やすことはできず、削った後の重さが 0 グラム未満になることもできません。

高橋君の目標は、すべてのりんごの重さを同じ値 X グラム( X0 以上 \min(A_1+R, A_2+R, \ldots, A_N+R) 以下の整数)にすることです。このとき、すべてのりんごから削る量の合計、すなわち

\sum_{i=1}^{N} \bigl((A_i + R) - X\bigr)

を最小化したいと考えています。

この合計の最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq R \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

N R
A_1 A_2 \ldots A_N
  • 1 行目には、りんごの個数を表す整数 N と、コーティング剤によって各りんごが増加する重さを表す整数 R が、スペース区切りで与えられる。
  • 2 行目には、コーティング前の各りんごの重さを表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

すべてのりんごの重さを等しくするために削る量の合計の最小値を 1 行で出力してください。


入力例 1

3 5
10 12 15

出力例 1

7

入力例 2

5 100
50 30 80 30 60

出力例 2

100

入力例 3

8 1000000000
500 1200 800 950 600 1100 750 900

出力例 3

2800

Score : 233 pts

Problem Statement

Takahashi harvested N apples from an orchard. The weight of apple i (1 \leq i \leq N) is A_i grams.

After harvesting, Takahashi applied a coating agent to all the apples. Applying the coating agent increases each apple's weight by exactly R grams, so the weight of apple i after coating becomes A_i + R grams.

Takahashi wants to pack these apples into a gift box, so he wants all the apples to have the same weight. To achieve this, he decided to shave each apple to reduce its weight. Each apple can be shaved by any non-negative integer number of grams. However, the weight cannot be increased, and the weight after shaving cannot become less than 0 grams.

Takahashi's goal is to make all the apples have the same weight X grams (X is an integer satisfying 0 \leq X \leq \min(A_1+R, A_2+R, \ldots, A_N+R)). He wants to minimize the total amount shaved from all the apples, namely

\sum_{i=1}^{N} \bigl((A_i + R) - X\bigr)

Find the minimum value of this total.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq R \leq 10^9
  • 1 \leq A_i \leq 10^9
  • All input values are integers

Input

N R
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of apples and an integer R representing the weight increase from the coating agent for each apple, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the weight of each apple before coating, separated by spaces.

Output

Print the minimum total amount that must be shaved to make all the apples equal in weight, on a single line.


Sample Input 1

3 5
10 12 15

Sample Output 1

7

Sample Input 2

5 100
50 30 80 30 60

Sample Output 2

100

Sample Input 3

8 1000000000
500 1200 800 950 600 1100 750 900

Sample Output 3

2800
B - コストパフォーマンス最高のノートPC

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

高橋君は大学に入学するにあたり、新しいノートPCを購入しようとしています。

高橋君は N 個のノートPCの候補を見つけました。i 番目 (1 \leq i \leq N) のノートPCには、価格 P_i 円と性能スコア S_i が決まっています。性能スコアは値が大きいほど性能が高いことを表します。

高橋君の先輩である青木君は、以前ノートPCを購入した際に性能を気にせず安さだけで選んでしまい、性能が低くて後悔した経験があります。そんな青木君から、「価格に対して性能が高いノートPCを選ぶべきだ」とアドバイスを受けました。

そこで高橋君は、i 番目のノートPCの「コストパフォーマンス」を \frac{S_i}{P_i} と定義し、N 個のノートPCの中からコストパフォーマンスが最大となるものを 1 つ選ぶことにしました。

コストパフォーマンスが最大となるノートPCの番号を求めてください。コストパフォーマンスが最大となるノートPCが複数ある場合は、その中で最も番号が小さいものを出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq 10^6
  • 1 \leq S_i \leq 10^6
  • 入力はすべて整数

入力

N
P_1 S_1
P_2 S_2
\vdots
P_N S_N

1 行目には、ノートPCの候補の数 N が与えられます。続く N 行のうち i 行目 (1 \leq i \leq N) には、i 番目のノートPCの価格 P_i と性能スコア S_i がスペース区切りで与えられます。

出力

コストパフォーマンスが最大となるノートPCの番号(1 から N までの整数)を 1 行で出力してください。コストパフォーマンスが最大となるノートPCが複数ある場合は、その中で最も番号が小さいものを出力してください。


入力例 1

3
80000 400
100000 600
120000 480

出力例 1

2

入力例 2

5
50000 250
60000 360
80000 480
90000 450
70000 420

出力例 2

2

入力例 3

8
150000 600
200000 1000
180000 720
120000 600
250000 1000
100000 500
80000 320
160000 800

出力例 3

2

Score : 300 pts

Problem Statement

Takahashi is about to enter university and is looking to purchase a new laptop.

Takahashi has found N candidate laptops. The i-th (1 \leq i \leq N) laptop has a price of P_i yen and a performance score of S_i. A higher performance score indicates better performance.

Takahashi's senior, Aoki, once bought a laptop choosing solely based on low price without considering performance, and regretted it because the performance was poor. Aoki advised Takahashi: "You should choose a laptop that has high performance relative to its price."

Following this advice, Takahashi defines the "cost performance" of the i-th laptop as \frac{S_i}{P_i}, and decides to choose the one with the maximum cost performance among the N laptops.

Find the number of the laptop with the maximum cost performance. If there are multiple laptops with the maximum cost performance, output the one with the smallest number.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq 10^6
  • 1 \leq S_i \leq 10^6
  • All inputs are integers

Input

N
P_1 S_1
P_2 S_2
\vdots
P_N S_N

The first line gives the number of candidate laptops N. In the following N lines, the i-th line (1 \leq i \leq N) gives the price P_i and performance score S_i of the i-th laptop, separated by a space.

Output

Output the number (an integer from 1 to N) of the laptop with the maximum cost performance in a single line. If there are multiple laptops with the maximum cost performance, output the one with the smallest number.


Sample Input 1

3
80000 400
100000 600
120000 480

Sample Output 1

2

Sample Input 2

5
50000 250
60000 360
80000 480
90000 450
70000 420

Sample Output 2

2

Sample Input 3

8
150000 600
200000 1000
180000 720
120000 600
250000 1000
100000 500
80000 320
160000 800

Sample Output 3

2
C - 宝石集めの冒険

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 366

問題文

高橋君は NM 列のマス目で表されるダンジョンを探索しています。上から i 行目、左から j 列目のマスを (i, j) と表します。各マス (i, j)1 \leq i \leq N, 1 \leq j \leq M)には宝石が 1 つ置かれており、その価値は A_{i,j} です。

高橋君は現在、ダンジョンの左上のマス (1, 1) にいます。ゴールである右下のマス (N, M) まで移動したいと考えています。高橋君は 1 回の移動で、現在いるマスから右に隣接するマスまたは下に隣接するマスへ 1 マスだけ進むことができます。すなわち、マス (i, j) からはマス (i, j+1)j + 1 \leq M のとき)またはマス (i+1, j)i + 1 \leq N のとき)へ移動できます。

高橋君は、通過するすべてのマス(始点 (1, 1) および終点 (N, M) を含む)に置かれている宝石を回収します。

高橋君は、回収する宝石の価値の合計を最大化したいと考えています。(1, 1) から (N, M) まで移動するすべての経路のうち、通過するマスの宝石の価値の合計が最大となる値を求めて出力してください。

制約

  • 1 \leq N \leq 1000
  • 1 \leq M \leq 1000
  • 0 \leq A_{i,j} \leq 10^9
  • 入力はすべて整数

入力

N M
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}
  • 1 行目には、ダンジョンの行数を表す整数 N と列数を表す整数 M が、スペース区切りで与えられる。
  • 続く N 行のうち i 行目(1 \leq i \leq N)には、マス目の i 行目の各マスの宝石の価値 A_{i,1}, A_{i,2}, \ldots, A_{i,M} がスペース区切りで与えられる。

出力

高橋君が回収できる宝石の価値の合計の最大値を 1 行で出力してください。


入力例 1

3 3
1 2 3
4 5 6
7 8 9

出力例 1

29

入力例 2

4 5
3 1 4 1 5
9 2 6 5 3
5 8 9 7 9
3 2 3 8 4

出力例 2

54

入力例 3

6 8
100 200 50 300 10 400 150 80
250 30 500 20 600 70 90 200
80 700 100 800 50 300 400 100
150 60 900 200 100 1000 50 350
300 400 50 150 200 80 600 700
100 200 300 400 500 600 700 800

出力例 3

5610

Score : 366 pts

Problem Statement

Takahashi is exploring a dungeon represented as a grid with N rows and M columns. The cell at the i-th row from the top and the j-th column from the left is denoted as (i, j). Each cell (i, j) (1 \leq i \leq N, 1 \leq j \leq M) contains one gem with a value of A_{i,j}.

Takahashi is currently at the top-left cell (1, 1) of the dungeon. He wants to move to the bottom-right cell (N, M), which is the goal. In one move, Takahashi can advance exactly one cell to the right-adjacent cell or the down-adjacent cell from his current cell. That is, from cell (i, j), he can move to cell (i, j+1) (when j + 1 \leq M) or cell (i+1, j) (when i + 1 \leq N).

Takahashi collects the gems placed on all cells he passes through (including the starting cell (1, 1) and the ending cell (N, M)).

Takahashi wants to maximize the total value of the gems he collects. Among all paths from (1, 1) to (N, M), find and output the maximum possible total value of gems on the cells along the path.

Constraints

  • 1 \leq N \leq 1000
  • 1 \leq M \leq 1000
  • 0 \leq A_{i,j} \leq 10^9
  • All input values are integers.

Input

N M
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}
  • The first line contains an integer N representing the number of rows and an integer M representing the number of columns of the dungeon, separated by a space.
  • In the following N lines, the i-th line (1 \leq i \leq N) contains the gem values A_{i,1}, A_{i,2}, \ldots, A_{i,M} of each cell in the i-th row of the grid, separated by spaces.

Output

Output the maximum total value of gems that Takahashi can collect, on a single line.


Sample Input 1

3 3
1 2 3
4 5 6
7 8 9

Sample Output 1

29

Sample Input 2

4 5
3 1 4 1 5
9 2 6 5 3
5 8 9 7 9
3 2 3 8 4

Sample Output 2

54

Sample Input 3

6 8
100 200 50 300 10 400 150 80
250 30 500 20 600 70 90 200
80 700 100 800 50 300 400 100
150 60 900 200 100 1000 50 350
300 400 50 150 200 80 600 700
100 200 300 400 500 600 700 800

Sample Output 3

5610
D - スピーカーの設置

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君はイベント会場の音響担当です。会場には一直線の通路があり、通路沿いに N 個のブースが並んでいます。各ブースにアナウンスの音声を届ける必要があります。

通路上の位置は座標で表します。ブース i は座標 X_i の位置にあり、音声が聞こえたと判断されるために必要な音量の合計値(聴取閾値)は D_i です。

高橋君は通路上の好きな整数座標の位置 P(任意の整数)をちょうど 1選び、そこにスピーカーを設置します。スピーカーの設置位置は 1 箇所のみであり、途中で位置を変えることはできません。

スピーカーを 1 回鳴らすと、その出力音量は V です。位置 P に設置されたスピーカーからブース i に届く音量は、1 回あたり \max(V - |X_i - P|, 0) です。つまり、スピーカーとブースの距離が離れるほど音量が減衰し、距離が V 以上のブースには音は届きません。

スピーカーを同じ位置で K 回(K は正の整数)鳴らすと、各ブース i に届く音量の合計は K \times \max(V - |X_i - P|, 0) となります。ブース i に音声が届いたと判断されるためには、この合計が聴取閾値 D_i 以上である必要があります。

すべてのブースに音声を届けるためには、すべてのブース i1 \leq i \leq N)について同時に上の条件を満たす必要があります。このため、スピーカーの位置 P から距離 V 以上離れたブースが 1 つでも存在すると、そのブースには何回鳴らしても音量が届かず、条件を満たすことができません。

高橋君はスピーカーを鳴らす回数をできるだけ少なくしたいと考えています。スピーカーの設置位置 P を最適に選んだとき、すべてのブースに音声を届けるために必要なスピーカーを鳴らす最小回数 K を求めてください。

ただし、どの整数座標にスピーカーを設置しても、すべてのブースに音声を届けることが不可能な場合は -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq V \leq 10^9
  • 0 \leq X_i \leq 10^9
  • 1 \leq D_i \leq 10^{18}
  • X_i はすべて異なる
  • 入力はすべて整数である

入力

N V
X_1 D_1
X_2 D_2
:
X_N D_N
  • 1 行目には、ブースの個数を表す整数 N と、スピーカーの 1 回あたりの出力音量を表す整数 V が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各ブースの位置と聴取閾値が与えられる。
  • 1 + i 行目では、ブース i の座標 X_i と聴取閾値 D_i が整数としてスペース区切りで与えられる。

出力

すべてのブースに音声を届けるために必要なスピーカーを鳴らす最小回数を 1 行で出力せよ。すべてのブースに音声を届けることが不可能な場合は -1 を出力せよ。


入力例 1

3 5
0 4
3 6
7 2

出力例 1

2

入力例 2

2 3
0 10
6 10

出力例 2

-1

入力例 3

8 10
100 15
102 80
105 21
108 100
111 70
113 42
116 18
118 55

出力例 3

55

入力例 4

30 100
5000 1000000000000
5006 123456789012
5012 987654321098
5018 555555555555
5024 314159265358
5030 271828182845
5036 777777777777
5042 888888888888
5048 999999999999
5054 111111111111
5060 222222222222
5066 333333333333
5072 444444444444
5078 666666666666
5084 135791357913
5090 246802468024
5096 101010101010
5102 909090909090
5108 123123123123
5114 456456456456
5120 789789789789
5126 100000000000
5132 200000000000
5138 300000000000
5144 400000000000
5150 500000000000
5156 600000000000
5162 700000000000
5168 800000000000
5174 900000000000

出力例 4

75000000000

入力例 5

1 1
1000000000 1000000000000000000

出力例 5

1000000000000000000

Score : 400 pts

Problem Statement

Takahashi is in charge of audio for an event venue. The venue has a straight corridor, and N booths are lined up along the corridor. He needs to deliver announcement audio to each booth.

Positions along the corridor are represented by coordinates. Booth i is located at coordinate X_i, and the total sound volume required for the audio to be considered audible (hearing threshold) is D_i.

Takahashi will choose exactly one integer coordinate position P (any integer) on the corridor and place a speaker there. There is only one speaker placement location, and the position cannot be changed afterwards.

When the speaker is sounded once, its output volume is V. The volume that reaches booth i from a speaker placed at position P is \max(V - |X_i - P|, 0) per sounding. In other words, the volume attenuates as the distance between the speaker and the booth increases, and no sound reaches booths at a distance of V or more.

When the speaker is sounded K times (K is a positive integer) at the same position, the total volume reaching each booth i is K \times \max(V - |X_i - P|, 0). For booth i to be considered as having received the audio, this total must be at least the hearing threshold D_i.

To deliver audio to all booths, the above condition must be satisfied simultaneously for all booths i (1 \leq i \leq N). Therefore, if even one booth exists at a distance of V or more from the speaker position P, no amount of soundings can deliver volume to that booth, making it impossible to satisfy the condition.

Takahashi wants to minimize the number of times the speaker is sounded. Find the minimum number of times K the speaker must be sounded to deliver audio to all booths, when the speaker placement position P is chosen optimally.

However, if it is impossible to deliver audio to all booths regardless of which integer coordinate the speaker is placed at, output -1.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq V \leq 10^9
  • 0 \leq X_i \leq 10^9
  • 1 \leq D_i \leq 10^{18}
  • All X_i are distinct
  • All input values are integers

Input

N V
X_1 D_1
X_2 D_2
:
X_N D_N
  • The first line contains an integer N representing the number of booths and an integer V representing the output volume per sounding of the speaker, separated by a space.
  • From the 2nd line to the (N + 1)-th line, the position and hearing threshold of each booth are given.
  • The (1 + i)-th line contains the coordinate X_i and hearing threshold D_i of booth i as integers separated by a space.

Output

Output in one line the minimum number of times the speaker must be sounded to deliver audio to all booths. If it is impossible to deliver audio to all booths, output -1.


Sample Input 1

3 5
0 4
3 6
7 2

Sample Output 1

2

Sample Input 2

2 3
0 10
6 10

Sample Output 2

-1

Sample Input 3

8 10
100 15
102 80
105 21
108 100
111 70
113 42
116 18
118 55

Sample Output 3

55

Sample Input 4

30 100
5000 1000000000000
5006 123456789012
5012 987654321098
5018 555555555555
5024 314159265358
5030 271828182845
5036 777777777777
5042 888888888888
5048 999999999999
5054 111111111111
5060 222222222222
5066 333333333333
5072 444444444444
5078 666666666666
5084 135791357913
5090 246802468024
5096 101010101010
5102 909090909090
5108 123123123123
5114 456456456456
5120 789789789789
5126 100000000000
5132 200000000000
5138 300000000000
5144 400000000000
5150 500000000000
5156 600000000000
5162 700000000000
5168 800000000000
5174 900000000000

Sample Output 4

75000000000

Sample Input 5

1 1
1000000000 1000000000000000000

Sample Output 5

1000000000000000000
E - 桁の積

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 466

問題文

高橋君は、正の整数 n を先頭に 0 を付けない通常の十進法で表したときの、各桁の数字の積を f(n) と定義しました。例えば、 f(234) = 2 \times 3 \times 4 = 24 であり、 f(5) = 5 です。いずれかの桁に 0 を含む場合は f(n) = 0 となります。例えば、 f(102) = 1 \times 0 \times 2 = 0 です。

高橋君は、 L 以上 R 以下の整数 n のうち、 f(n) = K を満たすものの個数を知りたくなりました。この個数を求めてください。

制約

  • 1 \leq L \leq R \leq 10^{18}
  • 0 \leq K \leq 10^{18}
  • L, R, K は整数

入力

L R K
  • 範囲の下限を表す整数 L 、範囲の上限を表す整数 R 、各桁の数字の積の目標値を表す整数 K が、スペース区切りで 1 行に与えられる。

出力

L 以上 R 以下の整数 n のうち、 f(n) = K を満たすものの個数を 1 行で出力せよ。


入力例 1

1 20 2

出力例 1

2

入力例 2

1 30 0

出力例 2

3

入力例 3

100 10000 36

出力例 3

93

入力例 4

123456789012 987654321098765432 720

出力例 4

32671066

入力例 5

1000000000000000000 1000000000000000000 0

出力例 5

1

Score : 466 pts

Problem Statement

Takahashi defined f(n) as the product of the digits of a positive integer n when written in standard decimal notation without leading zeros. For example, f(234) = 2 \times 3 \times 4 = 24 and f(5) = 5. If any digit is 0, then f(n) = 0. For example, f(102) = 1 \times 0 \times 2 = 0.

Takahashi wants to know the number of integers n between L and R (inclusive) that satisfy f(n) = K. Find this count.

Constraints

  • 1 \leq L \leq R \leq 10^{18}
  • 0 \leq K \leq 10^{18}
  • L, R, K are integers

Input

L R K
  • An integer L representing the lower bound of the range, an integer R representing the upper bound of the range, and an integer K representing the target value of the product of digits are given on a single line separated by spaces.

Output

Output in one line the number of integers n between L and R (inclusive) that satisfy f(n) = K.


Sample Input 1

1 20 2

Sample Output 1

2

Sample Input 2

1 30 0

Sample Output 2

3

Sample Input 3

100 10000 36

Sample Output 3

93

Sample Input 4

123456789012 987654321098765432 720

Sample Output 4

32671066

Sample Input 5

1000000000000000000 1000000000000000000 0

Sample Output 5

1