A - お買い物の合計金額

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

配点 : 200

テストケースに誤りがあったため、リジャッジを行いました。

問題文

高橋君はスーパーマーケットで買い物をしています。

高橋君は買い物かごに K 個の商品を入れました。それぞれの商品には値札がついており、i 番目 (1 \leq i \leq K) の商品の価格は L_i 円です。

このスーパーマーケットでは、レジで精算するときに、合計金額をある正の整数 M で割った余りに等しいポイントがもらえるキャンペーンを実施しています。

すなわち、すべての商品の価格の総和を S = L_1 + L_2 + \cdots + L_K としたとき、高橋君がもらえるポイントは SM で割った余りです。なお、SM の倍数であるときにもらえるポイントは 0 です。

高橋君がもらえるポイントを求めてください。

制約

  • 1 \leq K \leq 10^5
  • 1 \leq M \leq 10^9
  • 1 \leq L_i \leq 10^4 (1 \leq i \leq K)
  • 入力はすべて整数

入力

K M
L_1 L_2 \cdots L_K
  • 1 行目には、買い物かごに入れた商品の個数を表す整数 K と、ポイント計算に用いる正の整数 M が、スペース区切りで与えられる。
  • 2 行目には、各商品の価格を表す K 個の整数 L_1, L_2, \ldots, L_K が、スペース区切りで与えられる。

出力

高橋君がもらえるポイントを整数で 1 行に出力してください。


入力例 1

3 100
150 80 45

出力例 1

75

入力例 2

5 1000
1200 350 890 460 720

出力例 2

620

入力例 3

10 500000000
9999 8765 4321 5678 1234 7777 3333 6666 2222 8888

出力例 3

58883

Score : 200 pts

A mistake was made in the test case, requiring a re-judgment.

Problem Statement

Takahashi is shopping at a supermarket.

Takahashi has placed K items in his shopping basket. Each item has a price tag, and the price of the i-th item (1 \leq i \leq K) is L_i yen.

This supermarket is running a campaign where, at checkout, customers receive points equal to the remainder when the total amount is divided by a positive integer M.

That is, if the total sum of all item prices is S = L_1 + L_2 + \cdots + L_K, the points Takahashi receives are the remainder when S is divided by M. Note that if S is a multiple of M, the points received are 0.

Find the number of points Takahashi will receive.

Constraints

  • 1 \leq K \leq 10^5
  • 1 \leq M \leq 10^9
  • 1 \leq L_i \leq 10^4 (1 \leq i \leq K)
  • All inputs are integers

Input

K M
L_1 L_2 \cdots L_K
  • The first line contains an integer K representing the number of items in the shopping basket, and a positive integer M used for the point calculation, separated by a space.
  • The second line contains K integers L_1, L_2, \ldots, L_K representing the prices of each item, separated by spaces.

Output

Print the number of points Takahashi will receive as an integer on a single line.


Sample Input 1

3 100
150 80 45

Sample Output 1

75

Sample Input 2

5 1000
1200 350 890 460 720

Sample Output 2

620

Sample Input 3

10 500000000
9999 8765 4321 5678 1234 7777 3333 6666 2222 8888

Sample Output 3

58883
B - ダンジョン探索

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

配点 : 266

問題文

高橋君はダンジョンの探索に挑戦します。

ダンジョンには N 個の部屋が一列に並んでおり、左から i 番目の部屋にはモンスターが 1 体います。i 番目のモンスターの強さは H_i で、このモンスターを倒すと体力が P_i だけ回復するアイテムを落とします。

高橋君は部屋 1 から部屋 N まで、番号の小さい順にすべての部屋をちょうど 1 回ずつ訪れます。高橋君の初期体力は S です。体力に上限はありません。

各部屋 i を訪れたとき、以下のルールが自動的に適用されます。高橋君が行動を選択する余地はありません。

  • 現在の体力が H_i 以上の場合、高橋君はそのモンスターを倒す。まず体力が H_i 減少し、その直後に P_i 回復する。すなわち、体力の変化量は -H_i + P_i である。戦闘の直前に体力が H_i 以上あるため、H_i 減少した直後の体力は 0 以上であることが保証される。
  • 現在の体力が H_i 未満の場合、高橋君はそのモンスターを倒すことができず、その部屋を素通りする。この場合、体力は変化しない。

モンスターを倒せなかった部屋 1 つにつき、罰金として C を支払わなければなりません。

すべての N 個の部屋を訪れ終えたとき、高橋君が支払う罰金の合計額を求めてください。すなわち、モンスターを倒せなかった部屋の数を k としたとき、k \times C を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq S \leq 10^9
  • 1 \leq C \leq 10^9
  • 1 \leq H_i \leq 10^9
  • 0 \leq P_i \leq 10^9
  • 入力はすべて整数である。

入力

N S C
H_1 P_1
H_2 P_2
\vdots
H_N P_N
  • 1 行目には、部屋の数を表す N 、高橋君の初期体力を表す S 、部屋 1 つあたりの罰金を表す C が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各部屋の情報が N 行で与えられる。
  • 1 + i 行目では、i 番目のモンスターの強さ H_i と、そのモンスターを倒したときの体力回復量 P_i が、スペース区切りで与えられる。

出力

高橋君が支払う罰金の合計額を 1 行で出力せよ。


入力例 1

3 10 5
3 1
12 5
5 3

出力例 1

5

入力例 2

5 7 100
5 2
3 10
20 5
11 0
1 0

出力例 2

200

入力例 3

8 15 1000000000
10 8
5 3
20 0
8 7
10 10
100 50
1 0
9 5

出力例 3

2000000000

Score : 266 pts

Problem Statement

Takahashi challenges himself to explore a dungeon.

The dungeon has N rooms arranged in a row. The i-th room from the left contains 1 monster. The i-th monster has strength H_i, and upon being defeated, it drops an item that restores P_i health points.

Takahashi visits all rooms exactly once, from room 1 to room N in order of increasing room number. Takahashi's initial health is S. There is no upper limit on health.

When visiting each room i, the following rules are automatically applied. Takahashi has no choice in the matter.

  • If his current health is greater than or equal to H_i, Takahashi defeats the monster. First, his health decreases by H_i, and immediately after, it recovers by P_i. That is, the net change in health is -H_i + P_i. Since his health was at least H_i just before the battle, it is guaranteed that his health is non-negative immediately after the H_i decrease.
  • If his current health is less than H_i, Takahashi cannot defeat the monster and passes through the room. In this case, his health does not change.

For each room where a monster was not defeated, Takahashi must pay a penalty of C.

After visiting all N rooms, determine the total penalty Takahashi must pay. That is, if the number of rooms where the monster was not defeated is k, output k \times C.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq S \leq 10^9
  • 1 \leq C \leq 10^9
  • 1 \leq H_i \leq 10^9
  • 0 \leq P_i \leq 10^9
  • All input values are integers.

Input

N S C
H_1 P_1
H_2 P_2
\vdots
H_N P_N
  • The first line contains N representing the number of rooms, S representing Takahashi's initial health, and C representing the penalty per room, separated by spaces.
  • From the 2nd line to the (N + 1)-th line, information about each room is given over N lines.
  • The (1 + i)-th line contains the strength H_i of the i-th monster and the health recovery amount P_i upon defeating that monster, separated by spaces.

Output

Output the total penalty Takahashi must pay, on a single line.


Sample Input 1

3 10 5
3 1
12 5
5 3

Sample Output 1

5

Sample Input 2

5 7 100
5 2
3 10
20 5
11 0
1 0

Sample Output 2

200

Sample Input 3

8 15 1000000000
10 8
5 3
20 0
8 7
10 10
100 50
1 0
9 5

Sample Output 3

2000000000
C - りんご収穫

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

配点 : 333

問題文

高橋君は果樹園でりんごの収穫アルバイトをしています。果樹園には N 個のりんごがなっており、それぞれのりんごは木の異なる高さの位置にあります。

i 番目のりんごは地面から H_i センチメートルの高さにあります。高橋君の身長は T センチメートルですが、背伸びをすることで、最大 T + K センチメートルの高さまで手が届きます。

りんごを収穫するためには、りんごの高さが手の届く範囲内( T + K センチメートル以下)でなければなりません。手が届かないりんごには、脚立を使う必要があります。

高橋君は脚立を使うのが面倒なので、背伸びだけで収穫できるりんごの個数を最大化したいと考えています。幸い、この果樹園の木は特殊な移動式プランターに植えられており、プランターごと地面に D センチメートルの深さの穴を掘って沈めることができます( D0 以上の任意の整数)。木を沈めると、すべてのりんごの高さが一律に D だけ低くなります。

ただし、最も低い位置にあるりんごの高さが 1 センチメートル未満になってはいけないという制約があります。つまり、すべての i について H_i - D \geq 1 を満たす必要があります。

高橋君が背伸びだけで収穫できるりんごの最大個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 0 \leq K \leq 10^9
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N T K
H_1 H_2 \ldots H_N
  • 1 行目には、りんごの個数 N 、高橋君の身長 T 、背伸びで追加で届く高さ K が、スペース区切りで与えられる。
  • 2 行目には、各りんごの高さ H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。

出力

高橋君が背伸びだけで収穫できるりんごの最大個数を 1 行で出力せよ。


入力例 1

5 150 30
100 160 180 200 250

出力例 1

5

入力例 2

5 100 10
50 80 120 200 300

出力例 2

3

入力例 3

10 200 50
10 30 50 100 150 200 260 300 350 400

出力例 3

6

入力例 4

15 500 100
1 50 100 200 300 400 500 550 580 590 600 650 700 800 1000

出力例 4

11

入力例 5

1 1 0
1

出力例 5

1

Score : 333 pts

Problem Statement

Takahashi is working a part-time job harvesting apples at an orchard. The orchard has N apples, each located at a different height on the tree.

The i-th apple is at a height of H_i centimeters from the ground. Takahashi's height is T centimeters, but by stretching on his toes, he can reach up to a maximum height of T + K centimeters.

To harvest an apple, the apple's height must be within his reach (at most T + K centimeters). For apples he cannot reach, he would need to use a stepladder.

Since Takahashi finds using a stepladder troublesome, he wants to maximize the number of apples he can harvest by stretching alone. Fortunately, the trees in this orchard are planted in special mobile planters, and he can dig a hole of depth D centimeters in the ground to sink an entire planter (D is any non-negative integer). When a tree is sunk, the heights of all its apples are uniformly reduced by D.

However, there is a constraint that the height of the lowest apple must not become less than 1 centimeter. In other words, for all i, the condition H_i - D \geq 1 must be satisfied.

Find the maximum number of apples Takahashi can harvest by stretching alone.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 0 \leq K \leq 10^9
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

Input

N T K
H_1 H_2 \ldots H_N
  • The first line contains the number of apples N, Takahashi's height T, and the additional height K he can reach by stretching, separated by spaces.
  • The second line contains the heights of each apple H_1, H_2, \ldots, H_N, separated by spaces.

Output

Print the maximum number of apples Takahashi can harvest by stretching alone, in a single line.


Sample Input 1

5 150 30
100 160 180 200 250

Sample Output 1

5

Sample Input 2

5 100 10
50 80 120 200 300

Sample Output 2

3

Sample Input 3

10 200 50
10 30 50 100 150 200 260 300 350 400

Sample Output 3

6

Sample Input 4

15 500 100
1 50 100 200 300 400 500 550 580 590 600 650 700 800 1000

Sample Output 4

11

Sample Input 5

1 1 0
1

Sample Output 5

1
D - 花の種まき

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

配点 : 366

問題文

高橋君は庭に N 個の花の種を植えようとしています。

日付は第 1 日目、第 2 日目、…と続きます。高橋君は第 1 日目から順に各日について、種を植えられるかどうかを判断していきます。

天気予報によると M 回の雨の期間が予定されており、i 番目の雨の期間は第 L_i 日から第 R_i 日まで(両端を含む)続きます。ある日が M 回の雨の期間のうち少なくとも 1 つに含まれている場合、その日は雨の日です。雨の日は地面がぬかるんで作業ができないため、種を植えることができません。雨の日でない日には、種をちょうど 1 個植えます。

なお、雨の期間同士が重なったり一方が他方に含まれたりすることもあります。その場合も、いずれかの雨の期間に含まれる日はすべて雨の日として扱います。

雨の期間は有限であるため、最後の雨の期間が終わった後にはすべての日が晴れとなり、十分な日数が経てば必ずすべての種を植え終えることができます。

N 個すべての種を植え終えるのは第何日目になるかを求めてください。

制約

  • 1 \leq N \leq 10^9
  • 0 \leq M \leq 10^5
  • 1 \leq L_i \leq R_i \leq 10^{18}
  • 雨の期間はソートされているとは限らない
  • 入力はすべて整数である

入力

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • 1 行目には、植える種の数を表す整数 N と、雨の期間の数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目から M 行にわたって、各雨の期間の情報が与えられる。1 + i 行目には、i 番目の雨の期間の開始日 L_i と終了日 R_i が、スペース区切りで与えられる。これは、第 L_i 日から第 R_i 日まで(両端を含む)雨が降ることを意味する。

出力

N 個すべての種を植え終える日の番号を 1 行で出力せよ。


入力例 1

5 2
2 4
7 7

出力例 1

9

入力例 2

10 3
3 8
5 12
20 25

出力例 2

26

入力例 3

1000000000 3
1 500000000
500000000001 999999999999
1000000000002 1000000000002

出力例 3

1500000000

Score : 366 pts

Problem Statement

Takahashi is trying to plant N flower seeds in his garden.

Days are numbered as day 1, day 2, and so on. Starting from day 1, Takahashi decides for each day whether he can plant a seed or not.

According to the weather forecast, there are M rainy periods scheduled. The i-th rainy period lasts from day L_i to day R_i (inclusive). A day is a rainy day if it is included in at least one of the M rainy periods. On rainy days, the ground is muddy and work cannot be done, so no seeds can be planted. On days that are not rainy, exactly 1 seed is planted.

Note that rainy periods may overlap with each other or one may be entirely contained within another. Even in such cases, any day that falls within any rainy period is treated as a rainy day.

Since the rainy periods are finite, all days after the last rainy period ends will be sunny, and given enough days, all seeds will eventually be planted.

Determine on which day all N seeds will have been planted.

Constraints

  • 1 \leq N \leq 10^9
  • 0 \leq M \leq 10^5
  • 1 \leq L_i \leq R_i \leq 10^{18}
  • The rainy periods are not necessarily sorted
  • All input values are integers

Input

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • The first line contains an integer N representing the number of seeds to plant and an integer M representing the number of rainy periods, separated by a space.
  • The following M lines provide information about each rainy period. The (1 + i)-th line contains the start day L_i and end day R_i of the i-th rainy period, separated by a space. This means it rains from day L_i to day R_i (inclusive).

Output

Print on a single line the day number on which all N seeds have been planted.


Sample Input 1

5 2
2 4
7 7

Sample Output 1

9

Sample Input 2

10 3
3 8
5 12
20 25

Sample Output 2

26

Sample Input 3

1000000000 3
1 500000000
500000000001 999999999999
1000000000002 1000000000002

Sample Output 3

1500000000
E - 気温の変動調査

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

配点 : 433

問題文

高橋君は気象観測所でアルバイトをしています。彼の仕事は、過去の気温データを分析することです。

観測所には N 日分の気温記録があります。各日には 1 から N までの番号が付けられており、i 日目の気温は A_i ℃でした。

高橋君は上司から、指定された期間内での気温の変動幅を調べるように頼まれました。ここで、ある期間の 気温の変動幅 とは、その期間内の気温の最大値と最小値の差のことを指します。例えば、期間が 1 日だけの場合、変動幅は 0 です。

Q 回の問い合わせが与えられます。i 番目(i = 1, 2, \ldots, Q)の問い合わせでは 2 つの整数 L_i, R_i が指定されます。高橋君は L_i 日目から R_i 日目まで(両端を含む)の範囲における気温の変動幅を求めて報告しなければなりません。

各問い合わせに対して、指定された期間内の気温の変動幅を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • -10^9 \leq A_i \leq 10^9
  • 1 \leq L_i \leq R_i \leq N
  • 入力はすべて整数

入力

N Q
A_1 A_2 \ldots A_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q
  • 1 行目には、日数を表す整数 N と、問い合わせの回数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、各日の気温を表す N 個の整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 続く Q 行の i 行目(i = 1, 2, \ldots, Q)には、i 番目の問い合わせの期間の開始日 L_i と終了日 R_i がスペース区切りで与えられる。

出力

Q 行出力せよ。i 行目(i = 1, 2, \ldots, Q)には、i 番目の問い合わせに対する答え、すなわち A_{L_i}, A_{L_i+1}, \ldots, A_{R_i} の最大値と最小値の差を整数で出力せよ。


入力例 1

5 3
20 18 25 22 19
1 3
2 5
4 4

出力例 1

7
7
0

入力例 2

7 5
-5 12 8 -3 15 7 10
1 7
3 6
1 2
2 4
5 7

出力例 2

20
18
17
15
8

入力例 3

10 8
1000000000 -1000000000 500 -200 300 0 999999999 -999999999 100 -100
1 2
1 10
3 6
7 8
5 5
2 8
4 9
1 1

出力例 3

2000000000
2000000000
700
1999999998
0
1999999999
1999999998
0

Score : 433 pts

Problem Statement

Takahashi is working a part-time job at a weather observatory. His job is to analyze past temperature data.

The observatory has temperature records for N days. Each day is numbered from 1 to N, and the temperature on day i was A_i ℃.

Takahashi's supervisor asked him to investigate the temperature fluctuation range within specified periods. Here, the temperature fluctuation range of a certain period refers to the difference between the maximum and minimum temperatures within that period. For example, if the period consists of only 1 day, the fluctuation range is 0.

Q queries are given. In the i-th query (i = 1, 2, \ldots, Q), two integers L_i, R_i are specified. Takahashi must determine and report the temperature fluctuation range in the period from day L_i to day R_i (inclusive).

For each query, find the temperature fluctuation range within the specified period.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • -10^9 \leq A_i \leq 10^9
  • 1 \leq L_i \leq R_i \leq N
  • All input values are integers

Input

N Q
A_1 A_2 \ldots A_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q
  • The first line contains an integer N representing the number of days and an integer Q representing the number of queries, separated by a space.
  • The second line contains N integers A_1, A_2, \ldots, A_N representing the temperature on each day, separated by spaces.
  • The i-th of the following Q lines (i = 1, 2, \ldots, Q) contains the start day L_i and end day R_i of the period for the i-th query, separated by a space.

Output

Print Q lines. The i-th line (i = 1, 2, \ldots, Q) should contain the answer to the i-th query, that is, the difference between the maximum and minimum values among A_{L_i}, A_{L_i+1}, \ldots, A_{R_i}, as an integer.


Sample Input 1

5 3
20 18 25 22 19
1 3
2 5
4 4

Sample Output 1

7
7
0

Sample Input 2

7 5
-5 12 8 -3 15 7 10
1 7
3 6
1 2
2 4
5 7

Sample Output 2

20
18
17
15
8

Sample Input 3

10 8
1000000000 -1000000000 500 -200 300 0 999999999 -999999999 100 -100
1 2
1 10
3 6
7 8
5 5
2 8
4 9
1 1

Sample Output 3

2000000000
2000000000
700
1999999998
0
1999999999
1999999998
0