A - 山登りトレーニング

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

配点 : 233

問題文

高橋君は登山家を目指しており、現在トレーニング中です。高橋君が通うジムには N 人の会員がおり、各会員には到達可能な最高標高(以下、単に「標高」と呼びます)が記録されています。高橋君は会員番号 1 であり、現在の標高は P メートルです。会員番号 i (2 \leq i \leq N) の標高は S_i メートルです。

高橋君は、自分よりも高い標高を持つ先輩会員に師事することで、自分の標高を上げたいと考えています。具体的には、高橋君は以下の操作を好きな回数( 0 回でもよい)繰り返し行うことができます。

  • 現在の高橋君の標高よりも真に大きい標高を持つ会員を 1 人選び、その会員に師事する。師事した結果、高橋君の標高は、その会員の標高と同じ値になる。ただし、この操作には 1 回につき C 円のレッスン料がかかる。

なお、高橋君以外の会員の標高は操作を通じて変化しません。

高橋君は、最終的に自分の標高を T メートル以上にしたいと考えています。すでに P \geq T であれば操作を行う必要はありません。目標を達成するために必要な最小の総レッスン料を求めてください。

ただし、どのように操作を行っても目標を達成できない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P \leq 10^9
  • 1 \leq T \leq 10^9
  • 1 \leq C \leq 10^9
  • 1 \leq S_i \leq 10^9 (2 \leq i \leq N)
  • 入力はすべて整数

入力

N P T C
S_2 S_3 \cdots S_N
  • 1 行目には、会員の総数を表す整数 N 、高橋君の現在の標高を表す整数 P 、目標標高を表す整数 T1 回の操作にかかるレッスン料を表す整数 C が、スペース区切りで与えられる。
  • 2 行目には、会員番号 2 から N までの各会員の標高 S_2, S_3, \ldots, S_N が、スペース区切りで与えられる。ただし、 N = 1 の場合、 2 行目は与えられない(高橋君以外の会員がいないため)。

出力

高橋君の標高を T メートル以上にするために必要な最小の総レッスン料を 1 行で出力してください。目標を達成できない場合は -1 を出力してください。


入力例 1

5 100 500 1000
200 300 500 150

出力例 1

1000

入力例 2

4 1000 2000 500
800 900 950

出力例 2

-1

入力例 3

10 50 1000000000 100000
1000 5000 10000 100000 1000000 10000000 100000000 500000000 1000000000

出力例 3

100000

Score : 233 pts

Problem Statement

Takahashi aspires to become a mountaineer and is currently in training. The gym Takahashi attends has N members, and each member has a recorded maximum reachable altitude (hereafter simply called "altitude"). Takahashi is member number 1, and his current altitude is P meters. The altitude of member number i (2 \leq i \leq N) is S_i meters.

Takahashi wants to increase his altitude by learning from senior members who have a higher altitude than his. Specifically, Takahashi can repeat the following operation any number of times (including 0 times):

  • Choose one member whose altitude is strictly greater than Takahashi's current altitude, and learn from that member. As a result of learning, Takahashi's altitude becomes equal to that member's altitude. However, each such operation costs C yen as a lesson fee.

Note that the altitudes of members other than Takahashi do not change through these operations.

Takahashi wants his final altitude to be at least T meters. If P \geq T already holds, no operations are needed. Find the minimum total lesson fee required to achieve the goal.

If it is impossible to achieve the goal no matter how the operations are performed, output -1.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq P \leq 10^9
  • 1 \leq T \leq 10^9
  • 1 \leq C \leq 10^9
  • 1 \leq S_i \leq 10^9 (2 \leq i \leq N)
  • All input values are integers

Input

N P T C
S_2 S_3 \cdots S_N
  • The first line contains the total number of members N, Takahashi's current altitude P, the target altitude T, and the lesson fee per operation C, separated by spaces.
  • The second line contains the altitudes S_2, S_3, \ldots, S_N of members numbered 2 through N, separated by spaces. However, if N = 1, the second line is not given (since there are no members other than Takahashi).

Output

Output in one line the minimum total lesson fee required to make Takahashi's altitude at least T meters. If the goal cannot be achieved, output -1.


Sample Input 1

5 100 500 1000
200 300 500 150

Sample Output 1

1000

Sample Input 2

4 1000 2000 500
800 900 950

Sample Output 2

-1

Sample Input 3

10 50 1000000000 100000
1000 5000 10000 100000 1000000 10000000 100000000 500000000 1000000000

Sample Output 3

100000
B - りんご収穫

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

配点 : 333

問題文

高橋君は果樹園を経営しています。果樹園には N 本のりんごの木が一列に並んでおり、左から順に木 1, 木 2, \ldots, 木 N と番号が付けられています。木 i には H_i 個のりんごが実っています。

高橋君は、収穫作業の効率化のため、連続する木の番号の範囲を表す整数の組 (L, R)1 \leq L \leq R \leq N)を 1 つ選び、木 L から木 R までのすべての木からりんごを収穫します。ただし、作業員の人数の都合上、区間に含まれる木の本数 R - L + 1M 以下でなければなりません。ここで M は、一度の収穫作業で扱える木の最大本数です。

収穫したりんごのうち、市場に出荷できるのは出荷基準を満たす木のりんごだけです。具体的には、木 L から木 R までの木 i について、H_i \geq K を満たす木のりんごのみが出荷対象となります。出荷対象の木からは、その木に実っているりんご H_i 個すべてが出荷されます。出荷できるりんごの総数は、これらの出荷対象の木の H_i の合計です。出荷基準を満たす木が区間内に 1 本もない場合、出荷できるりんごの総数は 0 です。

高橋君が (L, R) を最適に選んだとき、出荷できるりんごの総数の最大値を求めてください。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq N
  • 1 \leq K \leq 10^9
  • 0 \leq H_i \leq 10^9
  • 入力はすべて整数である

入力

N M K
H_1 H_2 \ldots H_N
  • 1 行目には、木の本数を表す整数 N、一度の収穫作業で扱える木の最大本数を表す整数 M、出荷基準となるりんごの個数の下限を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各木に実っているりんごの個数を表す整数 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。

出力

出荷できるりんごの総数の最大値を 1 行で出力せよ。


入力例 1

5 3 5
3 7 2 8 6

出力例 1

15

入力例 2

4 2 10
3 5 2 1

出力例 2

0

入力例 3

10 4 3
1 5 0 4 7 2 9 3 1 6

出力例 3

20

入力例 4

20 6 5
2 8 1 10 3 7 0 15 6 4 12 5 9 2 11 3 8 1 7 6

出力例 4

47

入力例 5

1 1 1
1000000000

出力例 5

1000000000

Score : 333 pts

Problem Statement

Takahashi runs an orchard. The orchard has N apple trees lined up in a row, numbered tree 1, tree 2, \ldots, tree N from left to right. Tree i has H_i apples growing on it.

To streamline the harvesting process, Takahashi will choose one pair of integers (L, R) (1 \leq L \leq R \leq N) representing a range of consecutive tree numbers, and harvest apples from all trees from tree L to tree R. However, due to the limited number of workers, the number of trees in the range, R - L + 1, must be at most M, where M is the maximum number of trees that can be handled in a single harvesting operation.

Among the harvested apples, only apples from trees that meet the shipping standard can be shipped to market. Specifically, for tree i from tree L to tree R, only apples from trees satisfying H_i \geq K are eligible for shipping. From each eligible tree, all H_i apples growing on that tree are shipped. The total number of apples that can be shipped is the sum of H_i over all eligible trees. If there are no trees meeting the shipping standard within the range, the total number of apples that can be shipped is 0.

Find the maximum total number of apples that can be shipped when Takahashi chooses (L, R) optimally.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq N
  • 1 \leq K \leq 10^9
  • 0 \leq H_i \leq 10^9
  • All input values are integers

Input

N M K
H_1 H_2 \ldots H_N
  • The first line contains the integer N representing the number of trees, the integer M representing the maximum number of trees that can be handled in a single harvesting operation, and the integer K representing the minimum number of apples required to meet the shipping standard, separated by spaces.
  • The second line contains the integers H_1, H_2, \ldots, H_N representing the number of apples growing on each tree, separated by spaces.

Output

Print the maximum total number of apples that can be shipped, on a single line.


Sample Input 1

5 3 5
3 7 2 8 6

Sample Output 1

15

Sample Input 2

4 2 10
3 5 2 1

Sample Output 2

0

Sample Input 3

10 4 3
1 5 0 4 7 2 9 3 1 6

Sample Output 3

20

Sample Input 4

20 6 5
2 8 1 10 3 7 0 15 6 4 12 5 9 2 11 3 8 1 7 6

Sample Output 4

47

Sample Input 5

1 1 1
1000000000

Sample Output 5

1000000000
C - 積雪調査

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

配点 : 366

問題文

高橋君は気象観測所の職員です。東西に一直線に延びる道路上に、地点 1, 2, \ldots, NN 個の観測地点がこの順に並んでおり、各地点の積雪状況を調査しています。

はじめ、すべての地点の積雪回数0 です。

この冬、合計 M 回の降雪がありました。i 回目 (1 \leq i \leq M) の降雪では、地点 L_i から地点 R_i までのすべての地点(両端を含む)において、それぞれ積雪回数が 1 だけ増加しました。

すなわち、M 回の降雪がすべて終わった後の各地点の積雪回数は、その地点が降雪の範囲に含まれた回数の合計に等しくなります。

上司の青木君から「積雪回数が K 以上の地点は除雪が必要だ」と連絡がありました。高橋君は除雪計画を立てるために、M 回の降雪がすべて終わった後に積雪回数が K 以上である地点の個数を求めたいです。

積雪回数が K 以上である地点の個数を求めてください。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq M
  • 1 \leq L_i \leq R_i \leq N
  • 入力はすべて整数である。

入力

N M K
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • 1 行目には、観測地点の数 N、降雪の回数 M、除雪が必要となる基準値 K が、スペース区切りで与えられる。
  • 続く M 行のうち i 行目 (1 \leq i \leq M) には、i 回目の降雪の範囲の左端 L_i と右端 R_i が、スペース区切りで与えられる。

出力

積雪回数が K 以上である地点の個数を 1 行で出力せよ。


入力例 1

10 3 2
1 5
3 7
6 10

出力例 1

5

入力例 2

5 3 3
1 2
3 4
4 5

出力例 2

0

入力例 3

100 5 3
10 50
20 60
30 70
40 80
50 90

出力例 3

41

入力例 4

1000000 4 2
1 500000
250001 750000
500001 1000000
1 1000000

出力例 4

1000000

入力例 5

1 1 1
1 1

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi is a staff member at a meteorological observatory. Along a road extending in a straight line from east to west, there are N observation points, numbered 1, 2, \ldots, N in order, and he is surveying the snow accumulation status at each point.

Initially, the snowfall count at every point is 0.

This winter, there were a total of M snowfalls. In the i-th snowfall (1 \leq i \leq M), the snowfall count increased by 1 at every point from point L_i to point R_i (inclusive).

In other words, after all M snowfalls have finished, the snowfall count at each point equals the total number of times that point was included in the range of a snowfall.

His supervisor Aoki informed him that "points with a snowfall count of K or more require snow removal." To create a snow removal plan, Takahashi wants to determine the number of points whose snowfall count is K or more after all M snowfalls have finished.

Find the number of points whose snowfall count is K or more.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq M
  • 1 \leq L_i \leq R_i \leq N
  • All input values are integers.

Input

N M K
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • The first line contains the number of observation points N, the number of snowfalls M, and the threshold value K for requiring snow removal, separated by spaces.
  • The i-th line (1 \leq i \leq M) of the following M lines contains the left endpoint L_i and right endpoint R_i of the range of the i-th snowfall, separated by spaces.

Output

Output in one line the number of points whose snowfall count is K or more.


Sample Input 1

10 3 2
1 5
3 7
6 10

Sample Output 1

5

Sample Input 2

5 3 3
1 2
3 4
4 5

Sample Output 2

0

Sample Input 3

100 5 3
10 50
20 60
30 70
40 80
50 90

Sample Output 3

41

Sample Input 4

1000000 4 2
1 500000
250001 750000
500001 1000000
1 1000000

Sample Output 4

1000000

Sample Input 5

1 1 1
1 1

Sample Output 5

1
D - 配達ルートの最短時間

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

配点 : 400

問題文

高橋君は宅配便の配達員として働いています。今日の仕事では、配送センターから出発して、途中で特定の中継拠点に立ち寄り、最終目的地まで荷物を届ける必要があります。

高橋君の担当エリアには N 個の地点があり、それぞれ 1 から N までの番号が付けられています。地点 1 は配送センター、地点 N は最終目的地です。地点同士を結ぶ M 本の道路があり、各道路は双方向に通行可能で、どちらの向きに通行しても所要時間は同じです。i 番目の道路(1 \leq i \leq M)は地点 U_i と地点 V_i を結んでおり、この道路を通行するのに C_i 分かかります。

今日の配達では、中継拠点である地点 K で別の荷物を受け取る必要があるため、高橋君は必ず地点 K を経由しなければなりません。すなわち、高橋君は地点 1 から地点 K まで移動し、その後地点 K から地点 N まで移動します。移動中、同じ地点を何度訪れても、同じ道路を何度通ってもかまいません。なお、K = 1 の場合は配送センターが中継拠点を兼ね、K = N の場合は最終目的地が中継拠点を兼ねます。

高橋君が配送センター(地点 1)から中継拠点(地点 K)を経由して最終目的地(地点 N)に到着するまでの最短の所要時間(分)を求めてください。地点 1 から地点 K への経路と地点 K から地点 N への経路のうち、少なくとも一方が存在しない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i(自己ループは存在しない)
  • 1 \leq C_i \leq 10^9
  • 同じ2地点間を結ぶ道路が複数存在することがある
  • 入力はすべて整数
  • 答えが存在する場合、その値は 2 \times 10^{14} 以下であることが保証される

入力

N M K
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_M V_M C_M
  • 1 行目には、地点の数 N、道路の数 M、中継拠点の番号 K が、スペース区切りで与えられる。
  • 2 行目から M + 1 行目には、各道路の情報が与えられる。
  • 1 + i 行目には、i 番目の道路が結ぶ地点の番号 U_iV_i、およびその道路の所要時間 C_i(分)が、スペース区切りで与えられる。

出力

高橋君が地点 1 から地点 K を経由して地点 N に到着するまでの最短の所要時間(分)を 1 行に出力してください。到着が不可能な場合は -1 を出力してください。


入力例 1

5 6 3
1 2 2
2 3 3
1 3 10
3 4 4
4 5 2
3 5 7

出力例 1

11

入力例 2

4 2 3
1 2 5
3 4 3

出力例 2

-1

入力例 3

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

出力例 3

12

Score : 400 pts

Problem Statement

Takahashi works as a delivery driver for a courier service. For today's job, he needs to depart from the distribution center, stop by a specific relay point along the way, and deliver a package to the final destination.

Takahashi's assigned area contains N locations, each numbered from 1 to N. Location 1 is the distribution center, and location N is the final destination. There are M roads connecting locations, each of which is bidirectional and takes the same time to traverse in either direction. The i-th road (1 \leq i \leq M) connects location U_i and location V_i, and it takes C_i minutes to travel along this road.

For today's delivery, Takahashi needs to pick up another package at location K, which serves as a relay point, so he must pass through location K. Specifically, Takahashi travels from location 1 to location K, and then from location K to location N. During his travel, he may visit the same location multiple times and use the same road multiple times. Note that if K = 1, the distribution center also serves as the relay point, and if K = N, the final destination also serves as the relay point.

Find the minimum total travel time (in minutes) for Takahashi to travel from the distribution center (location 1) via the relay point (location K) to the final destination (location N). If at least one of the paths — from location 1 to location K or from location K to location N — does not exist, output -1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq N
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i (no self-loops)
  • 1 \leq C_i \leq 10^9
  • There may be multiple roads connecting the same pair of locations
  • All input values are integers
  • When a solution exists, it is guaranteed that the answer is at most 2 \times 10^{14}

Input

N M K
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_M V_M C_M
  • The first line contains the number of locations N, the number of roads M, and the relay point number K, separated by spaces.
  • From the 2nd line to the (M + 1)-th line, information about each road is given.
  • The (1 + i)-th line contains the location numbers U_i and V_i connected by the i-th road, and the travel time C_i (in minutes) for that road, separated by spaces.

Output

Output on a single line the minimum total travel time (in minutes) for Takahashi to travel from location 1 via location K to location N. If it is impossible to arrive, output -1.


Sample Input 1

5 6 3
1 2 2
2 3 3
1 3 10
3 4 4
4 5 2
3 5 7

Sample Output 1

11

Sample Input 2

4 2 3
1 2 5
3 4 3

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

12
E - 気温の安定した期間

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

配点 : 466

問題文

高橋君は気象データの分析を行っています。手元には N 日間の気温記録があり、i 日目 (1 \leq i \leq N) の気温は A_i 度です。

高橋君は、気温が安定していた期間を調べたいと考えています。整数の組 (l, r)1 \leq l \leq r \leq N を満たすとき、l 日目から r 日目までの連続する r - l + 1 日間を「期間」と呼びます。期間内の気温の最大値と最小値の差が K 以下であるとき、すなわち

\max(A_l, A_{l+1}, \ldots, A_r) - \min(A_l, A_{l+1}, \ldots, A_r) \leq K

が成り立つとき、その期間は「安定している」といいます。

l = r である1日間の期間では最大値と最小値が等しく差が 0 となるため、K \geq 0 である本問の制約のもとで必ず安定しています。したがって、安定している期間は少なくとも N 個存在します。安定している期間のうち、最も長いものの日数を求めてください。

制約

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

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、気温記録の日数 N と、安定とみなす気温差の上限 K が空白区切りで与えられる。
  • 2 行目には、各日の気温 A_1, A_2, \ldots, A_N が空白区切りで与えられる。

出力

安定している期間の最大の日数を 1 行で出力せよ。


入力例 1

7 3
20 22 21 23 19 25 24

出力例 1

4

入力例 2

10 5
15 18 12 14 16 20 19 17 22 25

出力例 2

4

入力例 3

15 10
-5 0 3 -2 5 8 2 -1 4 20 25 22 28 30 18

出力例 3

8

Score : 466 pts

Problem Statement

Takahashi is analyzing weather data. He has temperature records for N days, where the temperature on day i (1 \leq i \leq N) is A_i degrees.

Takahashi wants to find periods during which the temperature was stable. When a pair of integers (l, r) satisfies 1 \leq l \leq r \leq N, the consecutive r - l + 1 days from day l to day r are called a "period." A period is said to be "stable" when the difference between the maximum and minimum temperatures within the period is at most K, that is, when

\max(A_l, A_{l+1}, \ldots, A_r) - \min(A_l, A_{l+1}, \ldots, A_r) \leq K

holds.

For a period of 1 day where l = r, the maximum and minimum values are equal, so the difference is 0. Under the constraints of this problem where K \geq 0, such a period is always stable. Therefore, there are at least N stable periods. Find the number of days in the longest stable period.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • -10^9 \leq A_i \leq 10^9
  • All inputs are integers

Input

N K
A_1 A_2 \ldots A_N
  • The first line contains the number of days N in the temperature record and the upper limit K of the temperature difference to be considered stable, separated by a space.
  • The second line contains the temperatures for each day A_1, A_2, \ldots, A_N, separated by spaces.

Output

Print the maximum number of days of a stable period in one line.


Sample Input 1

7 3
20 22 21 23 19 25 24

Sample Output 1

4

Sample Input 2

10 5
15 18 12 14 16 20 19 17 22 25

Sample Output 2

4

Sample Input 3

15 10
-5 0 3 -2 5 8 2 -1 4 20 25 22 28 30 18

Sample Output 3

8