A - 連続晴天の最長記録

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

配点 : 233

問題文

高橋君は夏休みの自由研究として、地元の気象データを調べています。ある期間の N 日間について、毎日の最高気温(℃)が整数値で記録されています。

1 日目、2 日目、…、N 日目の最高気温はそれぞれ A_1, A_2, \ldots, A_N ℃ です。最高気温が K ℃ 以上の日を「暑い日」と呼ぶことにします。

高橋君は、連続して「暑い日」が何日続いたかに注目しています。具体的には、ある日からある日まで連続する 1 日以上の日の列であって、その中のすべての日が「暑い日」であるものを考えます。そのような日の列に含まれる日数の最大値を求めてください。「暑い日」が一日もない場合は 0 を出力してください。

制約

  • 1 \leq N \leq 10^6
  • -50 \leq K \leq 50
  • -50 \leq A_i \leq 50 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、日数を表す整数 N と、「暑い日」の基準気温を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各日の最高気温を表す N 個の整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

すべての日が「暑い日」であるような連続する日の列の日数の最大値を 1 行で出力せよ。「暑い日」が一日もない場合は 0 を出力せよ。


入力例 1

10 35
32 36 37 35 33 35 36 37 38 34

出力例 1

4

入力例 2

15 30
28 30 31 32 29 27 30 30 30 30 30 28 33 34 29

出力例 2

5

入力例 3

20 0
-5 -3 0 1 2 3 -1 -2 0 0 0 0 0 -4 1 2 3 4 5 -1

出力例 3

5

Score : 233 pts

Problem Statement

Takahashi is investigating local weather data as part of his summer vacation independent research project. For a period of N days, the daily maximum temperature (°C) has been recorded as an integer value.

The maximum temperatures on day 1, day 2, …, day N are A_1, A_2, \ldots, A_N °C, respectively. A day whose maximum temperature is K °C or higher is called a "hot day."

Takahashi is interested in how many consecutive "hot days" occurred in a row. Specifically, consider a sequence of one or more consecutive days from some day to some day such that every day in the sequence is a "hot day." Find the maximum number of days contained in such a sequence. If there are no "hot days" at all, output 0.

Constraints

  • 1 \leq N \leq 10^6
  • -50 \leq K \leq 50
  • -50 \leq A_i \leq 50 (1 \leq i \leq N)
  • All input values are integers

Input

N K
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of days and an integer K representing the threshold temperature for a "hot day," separated by a space.
  • The second line contains N integers A_1, A_2, \ldots, A_N representing the maximum temperature on each day, separated by spaces.

Output

Output in one line the maximum number of days in a sequence of consecutive days where every day is a "hot day." If there are no "hot days" at all, output 0.


Sample Input 1

10 35
32 36 37 35 33 35 36 37 38 34

Sample Output 1

4

Sample Input 2

15 30
28 30 31 32 29 27 30 30 30 30 30 28 33 34 29

Sample Output 2

5

Sample Input 3

20 0
-5 -3 0 1 2 3 -1 -2 0 0 0 0 0 -4 1 2 3 4 5 -1

Sample Output 3

5
B - 電気自動車の旅

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

配点 : 300

問題文

高橋君は電気自動車で長い一本道を走行しようとしています。この道には N 個の区間があり、左から順に区間 1、区間 2、...、区間 N と番号が付けられています。

電気自動車にはバッテリー残量があり、最初のバッテリー残量は K です。高橋君は区間 1 の手前からスタートし、区間 1, 2, \ldots, N を左から順番に走行して区間 N の先まで到達したいと考えています。

各区間の走行は以下のルールに従います。

  • 区間 i を走行するには、走行開始時点でのバッテリー残量が 1 以上でなければなりません。バッテリー残量が 0 以下の場合、それ以上走行することはできず、その時点で旅は終了します。
  • 区間 i を走行すると、バッテリー残量が D_i だけ減少します。ここで D_i はその区間の消費量を表す正の整数です。走行後のバッテリー残量が 0 以下(負の値を含む)になることもありますが、その区間の走行自体は完了します。

道沿いには M 個の充電ステーションがあります。各充電ステーションは異なる位置にあり、充電ステーション j は区間 P_j と区間 P_j + 1 の間に位置しています。高橋君が区間 P_j の走行を完了した直後(区間 P_j + 1 の走行を開始する前)に、この充電ステーションを利用することができます。バッテリー残量が 0 以下であっても充電ステーションは利用できます。

充電ステーションを利用すると、現在のバッテリー残量がいくつであるかに関わらず、バッテリーを新品に交換し、バッテリー残量がちょうど S_j になります。現在のバッテリー残量が S_j より大きい場合でも、交換後の残量は S_j になります。充電ステーションを利用するかどうかは高橋君が自由に選択でき、利用しなくても構いません。

なお、道は一方通行であり、一度通過した地点に戻ることはできません。

高橋君が充電ステーションの利用を適切に選択することで、すべての区間を走行して区間 N の先まで到達できるかどうかを判定してください。区間 N の走行完了後にバッテリー残量が 0 以下であっても、区間 N の走行が完了していれば到達に成功したものとします。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq P_j \leq N - 1 (1 \leq j \leq M)
  • 1 \leq S_j \leq 10^9 (1 \leq j \leq M)
  • P_1 < P_2 < \cdots < P_M(充電ステーションは位置の昇順で与えられ、同じ位置に複数の充電ステーションは存在しない)
  • 入力はすべて整数

入力

N M K
D_1 D_2 \ldots D_N
P_1 S_1
P_2 S_2
\vdots
P_M S_M
  • 1 行目には、区間の数 N、充電ステーションの数 M、最初のバッテリー残量 K が、スペース区切りで与えられる。
  • 2 行目には、各区間を走行する際に減少するバッテリー残量 D_1, D_2, \ldots, D_N が、スペース区切りで与えられる。
  • 3 行目から M 行にわたり、充電ステーションの情報が与えられる。M = 0 の場合、この部分は存在しない。
  • 2 + j 行目では、充電ステーション j の位置 P_j と交換後のバッテリー残量 S_j が、スペース区切りで与えられる。

出力

高橋君がすべての区間を走行して区間 N の先まで到達できる場合は Yes を、できない場合は No を出力せよ。


入力例 1

5 2 10
3 4 5 2 3
2 8
4 6

出力例 1

Yes

入力例 2

4 1 5
3 3 3 3
2 4

出力例 2

Yes

入力例 3

10 4 15
5 3 8 2 6 4 7 3 5 2
2 12
4 20
6 10
8 15

出力例 3

Yes

Score : 300 pts

Problem Statement

Takahashi is trying to drive an electric vehicle along a long straight road. This road has N segments, numbered from left to right as segment 1, segment 2, ..., segment N.

The electric vehicle has a battery level, and the initial battery level is K. Takahashi starts just before segment 1 and wants to drive through segments 1, 2, \ldots, N in order from left to right, reaching beyond segment N.

Driving through each segment follows these rules:

  • To drive through segment i, the battery level at the start of driving must be 1 or more. If the battery level is 0 or less, the vehicle cannot drive any further, and the journey ends at that point.
  • Driving through segment i decreases the battery level by D_i, where D_i is a positive integer representing the consumption of that segment. The battery level after driving may become 0 or less (including negative values), but the driving of that segment itself is completed.

There are M charging stations along the road. Each charging station is at a distinct position, and charging station j is located between segment P_j and segment P_j + 1. Takahashi can use this charging station immediately after completing segment P_j (before starting segment P_j + 1). Charging stations can be used even if the battery level is 0 or less.

When a charging station is used, regardless of the current battery level, the battery is replaced with a new one, and the battery level becomes exactly S_j. Even if the current battery level is greater than S_j, the battery level after replacement becomes S_j. Takahashi can freely choose whether or not to use a charging station; it is not mandatory to use it.

Note that the road is one-way, and it is not possible to return to a previously passed point.

Determine whether Takahashi can drive through all segments and reach beyond segment N by appropriately choosing which charging stations to use. Even if the battery level is 0 or less after completing segment N, as long as the driving of segment N is completed, the journey is considered successful.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq P_j \leq N - 1 (1 \leq j \leq M)
  • 1 \leq S_j \leq 10^9 (1 \leq j \leq M)
  • P_1 < P_2 < \cdots < P_M (charging stations are given in ascending order of position, and no two charging stations are at the same position)
  • All input values are integers

Input

N M K
D_1 D_2 \ldots D_N
P_1 S_1
P_2 S_2
\vdots
P_M S_M
  • The first line contains the number of segments N, the number of charging stations M, and the initial battery level K, separated by spaces.
  • The second line contains the battery consumption D_1, D_2, \ldots, D_N for driving each segment, separated by spaces.
  • The following M lines contain information about the charging stations. If M = 0, this part does not exist.
  • The (2 + j)-th line contains the position P_j and the battery level after replacement S_j of charging station j, separated by spaces.

Output

If Takahashi can drive through all segments and reach beyond segment N, print Yes; otherwise, print No.


Sample Input 1

5 2 10
3 4 5 2 3
2 8
4 6

Sample Output 1

Yes

Sample Input 2

4 1 5
3 3 3 3
2 4

Sample Output 2

Yes

Sample Input 3

10 4 15
5 3 8 2 6 4 7 3 5 2
2 12
4 20
6 10
8 15

Sample Output 3

Yes
C - お買い物

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

配点 : 366

問題文

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

このスーパーマーケットには N 個の商品が並んでおり、各商品には 1 から N までの番号が付けられています。商品 i の価格は P_i 円です。なお、異なる番号の商品であっても、価格が同じであることがあります。

高橋君は、これらの商品の中から 1 個以上を選んで購入し、購入した商品の価格の合計がちょうど K 円になるようにしたいと考えています。ただし、各商品は最大 1 個しか購入できません。

購入する商品の番号の集合が異なるものを異なる方法として数えるとき、ちょうど K 円分の商品を購入する方法の数を 10^9 + 7 で割った余りを求めてください。例えば、価格が同じ商品が複数ある場合でも、選んだ商品の番号の集合が異なれば、それらは異なる方法として数えます。

制約

  • 1 \leq N \leq 100
  • 1 \leq K \leq 10000
  • 1 \leq P_i \leq K
  • 入力はすべて整数

入力

N K
P_1 P_2 \ldots P_N
  • 1 行目には、商品の個数を表す整数 N と、目標とする合計金額を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各商品の価格を表す整数 P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。

出力

ちょうど K 円分の商品を購入する方法の数を 10^9 + 7 で割った余りを 1 行で出力してください。なお、条件を満たす方法が存在しない場合は 0 を出力してください。


入力例 1

4 7
2 3 4 5

出力例 1

2

入力例 2

6 10
1 2 3 4 5 6

出力例 2

5

入力例 3

10 100
10 20 30 40 50 15 25 35 45 55

出力例 3

21

Score : 366 pts

Problem Statement

Takahashi is shopping at a supermarket.

There are N items available at this supermarket, each numbered from 1 to N. The price of item i is P_i yen. Note that different items may have the same price.

Takahashi wants to select and purchase 1 or more of these items such that the total price of the purchased items is exactly K yen. However, each item can be purchased at most once.

Counting two ways as different if the sets of item numbers purchased are different, find the number of ways to purchase items totaling exactly K yen, modulo 10^9 + 7. For example, even if multiple items have the same price, they are counted as different ways if the sets of selected item numbers differ.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq K \leq 10000
  • 1 \leq P_i \leq K
  • All inputs are integers

Input

N K
P_1 P_2 \ldots P_N
  • The first line contains an integer N representing the number of items and an integer K representing the target total price, separated by a space.
  • The second line contains integers P_1, P_2, \ldots, P_N representing the price of each item, separated by spaces.

Output

Print in one line the number of ways to purchase items totaling exactly K yen, modulo 10^9 + 7. If there is no way to satisfy the condition, print 0.


Sample Input 1

4 7
2 3 4 5

Sample Output 1

2

Sample Input 2

6 10
1 2 3 4 5 6

Sample Output 2

5

Sample Input 3

10 100
10 20 30 40 50 15 25 35 45 55

Sample Output 3

21
D - 省エネ照明計画

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

配点 : 400

問題文

高橋君は、商店街の街路灯の管理を任されています。商店街には N 個の街路灯が一直線に並んでおり、左から順に街路灯 1, 街路灯 2, \ldots, 街路灯 N と番号が付けられています。街路灯は直線状に配置されており、街路灯 N と街路灯 1 は隣接していません。高橋君は、これらの街路灯の一部を LED 照明に交換することを検討しています。

街路灯 i (1 \leq i \leq N) を LED に交換すると、年間の電気代削減効果は V_i 円であり、交換にかかる費用は W_i 円です。各街路灯について、LED に交換するかしないかのいずれかを選びます(同じ街路灯を複数回交換することはありません)。高橋君に与えられた交換費用の予算の上限は K 円であり、LED に交換する街路灯の交換費用の合計が K 円以下でなければなりません。

ところが、この商店街には古い電気配線の制約があります。番号が連続する隣り合う 2 つの街路灯をともに LED にすると、配線の容量を超えてブレーカーが落ちてしまいます。すなわち、すべての i (1 \leq i \leq N-1) について、街路灯 i と街路灯 i + 1 の両方を LED に交換することは許されません。

高橋君が、予算の制約と配線の制約の両方を満たすように LED に交換する街路灯を選ぶとき、年間の電気代削減効果の合計の最大値を求めてください。なお、1 つも交換しないという選択も許されます。その場合、削減効果の合計は 0 円です。

制約

  • 1 \leq N \leq 200
  • 1 \leq K \leq 10^4
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq W_i \leq 10^4 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N K
V_1 W_1
V_2 W_2
\vdots
V_N W_N
  • 1 行目には、街路灯の個数を表す整数 N と、交換費用の予算の上限を表す整数 K(単位は円)が、スペース区切りで与えられる。
  • 続く N 行のうち i 行目 (1 \leq i \leq N) には、街路灯 i の年間電気代削減効果 V_i(単位は円)と交換費用 W_i(単位は円)が、スペース区切りで与えられる。

出力

年間の電気代削減効果の合計の最大値を整数として 1 行で出力してください。


入力例 1

4 5
100 3
200 2
150 3
80 2

出力例 1

280

入力例 2

6 10
500 4
300 3
400 5
600 4
200 2
350 3

出力例 2

1250

入力例 3

10 50
1000000000 8
500000000 12
800000000 10
300000000 5
900000000 15
700000000 9
400000000 7
600000000 11
250000000 6
450000000 8

出力例 3

3550000000

Score : 400 pts

Problem Statement

Takahashi is in charge of managing the street lights of a shopping district. The shopping district has N street lights arranged in a straight line, numbered from left to right as street light 1, street light 2, \ldots, street light N. The street lights are placed along a line, and street light N and street light 1 are not adjacent. Takahashi is considering replacing some of these street lights with LED lighting.

If street light i (1 \leq i \leq N) is replaced with LED, the annual electricity cost reduction is V_i yen, and the replacement cost is W_i yen. For each street light, you choose either to replace it with LED or not (no street light is replaced more than once). The budget limit given to Takahashi for replacement costs is K yen, so the total replacement cost of the street lights replaced with LED must be at most K yen.

However, there is an old electrical wiring constraint in this shopping district. If two consecutively numbered adjacent street lights are both replaced with LED, the wiring capacity will be exceeded and the breaker will trip. That is, for all i (1 \leq i \leq N-1), it is not allowed to replace both street light i and street light i + 1 with LED.

Find the maximum total annual electricity cost reduction when Takahashi selects which street lights to replace with LED while satisfying both the budget constraint and the wiring constraint. Note that choosing to replace none of the street lights is also allowed. In that case, the total reduction is 0 yen.

Constraints

  • 1 \leq N \leq 200
  • 1 \leq K \leq 10^4
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq W_i \leq 10^4 (1 \leq i \leq N)
  • All inputs are integers

Input

N K
V_1 W_1
V_2 W_2
\vdots
V_N W_N
  • The first line contains an integer N representing the number of street lights and an integer K representing the budget limit for replacement costs (in yen), separated by a space.
  • The i-th line (1 \leq i \leq N) of the following N lines contains the annual electricity cost reduction V_i (in yen) and the replacement cost W_i (in yen) for street light i, separated by a space.

Output

Output the maximum total annual electricity cost reduction as an integer on a single line.


Sample Input 1

4 5
100 3
200 2
150 3
80 2

Sample Output 1

280

Sample Input 2

6 10
500 4
300 3
400 5
600 4
200 2
350 3

Sample Output 2

1250

Sample Input 3

10 50
1000000000 8
500000000 12
800000000 10
300000000 5
900000000 15
700000000 9
400000000 7
600000000 11
250000000 6
450000000 8

Sample Output 3

3550000000
E - 整列の手間

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

配点 : 466

問題文

高橋君は \(N\) 人の生徒が一列に並んでいる列を管理しています。生徒にはそれぞれ \(1\) から \(N\) までの出席番号が付けられており、左から \(i\) 番目の位置には出席番号 \(P_i\) の生徒が立っています。すなわち、\((P_1, P_2, \ldots, P_N)\) は \(1\) から \(N\) の順列です。

高橋君はこの列をできるだけ出席番号順(昇順)に近づけたいと考えています。列の「乱れ度」を、\(i < j\) かつ \(P_i > P_j\) となる組 \((i, j)\) の個数(転倒数)として定義します。

高橋君は以下の操作を ちょうど \(K\) 回 行います:

  • 隣り合う \(2\) 人の生徒を選び、位置を入れ替える。すなわち、ある \(i\)(\(1 \leq i \leq N - 1\))を選んで \(P_i\) と \(P_{i+1}\) を交換する。

\(K\) 回の操作をすべて行った後の列の乱れ度(転倒数)として、あり得る最小の値を求めてください。

なお、同じ位置の組を複数回選んでもよく、一度交換したものを元に戻す操作も許されます。

制約

  • \(2 \leq N \leq 2 \times 10^5\)
  • \(0 \leq K \leq 10^{18}\)
  • \((P_1, P_2, \ldots, P_N)\) は \((1, 2, \ldots, N)\) の順列である
  • 入力はすべて整数である

入力

\(N\) \(K\)
\(P_1\) \(P_2\) \(\ldots\) \(P_N\)
  • \(1\) 行目には、生徒の人数を表す \(N\) と操作回数を表す \(K\) が、スペース区切りで与えられる。
  • \(2\) 行目には、初期の並びを表す \(P_1, P_2, \ldots, P_N\) が、スペース区切りで与えられる。

出力

\(K\) 回の操作後の並びの転倒数としてあり得る最小の値を \(1\) 行で出力せよ。


入力例 1

5 3
3 1 4 5 2

出力例 1

1

入力例 2

4 2
1 3 2 4

出力例 2

1

入力例 3

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

出力例 3

4

入力例 4

30 123456789012
17 3 25 1 29 12 8 21 5 30 14 2 19 27 10 6 23 15 28 4 11 20 7 26 13 9 24 16 22 18

出力例 4

1

入力例 5

2 1000000000000000000
1 2

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi is managing a line of \(N\) students. Each student has a unique student ID from \(1\) to \(N\), and the student standing at the \(i\)-th position from the left has student ID \(P_i\). That is, \((P_1, P_2, \ldots, P_N)\) is a permutation of \((1, 2, \ldots, N)\).

Takahashi wants to make this line as close to student ID order (ascending order) as possible. We define the "disorderliness" of the line as the number of pairs \((i, j)\) such that \(i < j\) and \(P_i > P_j\) (the number of inversions).

Takahashi will perform the following operation exactly \(K\) times:

  • Choose two adjacent students and swap their positions. That is, choose some \(i\) (\(1 \leq i \leq N - 1\)) and swap \(P_i\) and \(P_{i+1}\).

Find the minimum possible disorderliness (number of inversions) of the line after performing all \(K\) operations.

Note that the same pair of adjacent positions may be chosen multiple times, and operations that undo previous swaps are also allowed.

Constraints

  • \(2 \leq N \leq 2 \times 10^5\)
  • \(0 \leq K \leq 10^{18}\)
  • \((P_1, P_2, \ldots, P_N)\) is a permutation of \((1, 2, \ldots, N)\)
  • All input values are integers.

Input

\(N\) \(K\)
\(P_1\) \(P_2\) \(\ldots\) \(P_N\)
  • The first line contains \(N\), the number of students, and \(K\), the number of operations, separated by a space.
  • The second line contains \(P_1, P_2, \ldots, P_N\), representing the initial arrangement, separated by spaces.

Output

Print the minimum possible number of inversions of the arrangement after \(K\) operations in a single line.


Sample Input 1

5 3
3 1 4 5 2

Sample Output 1

1

Sample Input 2

4 2
1 3 2 4

Sample Output 2

1

Sample Input 3

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

Sample Output 3

4

Sample Input 4

30 123456789012
17 3 25 1 29 12 8 21 5 30 14 2 19 27 10 6 23 15 28 4 11 20 7 26 13 9 24 16 22 18

Sample Output 4

1

Sample Input 5

2 1000000000000000000
1 2

Sample Output 5

0