A - 売れ残るフルーツ

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

配点 : 166 点

問題文

高橋君は果物屋で、今日はフルーツを A 個仕入れました。今日の客は B 人で、客は 1 人につきちょうど 1 個ずつフルーツを買います。フルーツが足りなくなった場合、残りの客は何も買わずに帰ります。

今日の営業が終わったとき、フルーツが 1 個以上売れ残るなら Yes を、売れ残らないなら No を出力してください。

制約

  • 1 \leq A \leq 1000
  • 1 \leq B \leq 1000
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

A B
  • 1 行目には、仕入れたフルーツの個数 A と客の人数 B がスペース区切りで与えられる。

出力

フルーツが売れ残るなら Yes を、売れ残らないなら No を 1 行で出力せよ。


入力例 1

10 7

出力例 1

Yes

入力例 2

5 8

出力例 2

No

入力例 3

1000 1

出力例 3

Yes

入力例 4

236 20

出力例 4

Yes

入力例 5

540 973

出力例 5

No

Score : 166 pts

Problem Statement

Takahashi runs a fruit shop, and today he stocked A fruits. There are B customers today, and each customer buys exactly one fruit. If the fruits run out, the remaining customers leave without buying anything.

At the end of today's business, print Yes if at least one fruit remains unsold, and No otherwise.

Constraints

  • 1 \leq A \leq 1000
  • 1 \leq B \leq 1000
  • All input values are integers.

Input

The input is given from standard input in the following format:

A B
  • The first line contains the number of stocked fruits A and the number of customers B, separated by a space.

Output

Print Yes on one line if a fruit remains unsold, and No otherwise.


Sample Input 1

10 7

Sample Output 1

Yes

Sample Input 2

5 8

Sample Output 2

No

Sample Input 3

1000 1

Sample Output 3

Yes

Sample Input 4

236 20

Sample Output 4

Yes

Sample Input 5

540 973

Sample Output 5

No
B - 山寺の札

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

配点 : 233 点

問題文

山寺の石段には N 枚の札が左から順に並んでおり、それぞれに英小文字が 1 文字ずつ書かれています。左から i 番目の札の文字は、文字列 S の i 文字目です。

高橋君はこれから Q 回、札を入れ替えます。 i 回目には、左から a_i 番目の札と左から b_i 番目の札の位置を入れ替えます。 a_i = b_i のときは何も起こりません。

Q 回の入れ替えがすべて終わったあと、札の文字を左から順に並べた文字列を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq Q \leq 100
  • S は英小文字のみからなる長さ N の文字列
  • 1 \leq a_i \leq N
  • 1 \leq b_i \leq N
  • 入力のうち S 以外はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N Q
S
a_1 b_1
a_2 b_2
\vdots
a_Q b_Q
  • 1 行目には、札の枚数 N と入れ替えの回数 Q がスペース区切りで与えられる。
  • 2 行目には、札の文字を表す文字列 S が与えられる。
  • 3 行目から Q 行にわたり、 2 + i 行目には i 回目に入れ替える札の位置 a_i と b_i がスペース区切りで与えられる。

出力

すべての入れ替えが終わったあとの札の文字を左から順に並べた文字列を 1 行で出力せよ。


入力例 1

5 2
abcde
1 3
2 5

出力例 1

ceadb

入力例 2

3 1
xyz
2 2

出力例 2

xyz

入力例 3

4 3
abcd
1 4
2 3
1 4

出力例 3

acbd

入力例 4

3 1
xyz
1 2

出力例 4

yxz

入力例 5

4 3
abcd
1 4
2 1
1 4

出力例 5

adcb

Score : 233 pts

Problem Statement

There are N tags lined up in order from left to right on the stone steps of a mountain temple, each with one lowercase English letter written on it. The letter on the i-th tag from the left is the i-th character of the string S.

Takahashi will now swap tags Q times. On the i-th swap, he swaps the positions of the a_i-th tag from the left and the b_i-th tag from the left. When a_i = b_i, nothing happens.

After all Q swaps are complete, output the string formed by the letters on the tags arranged from left to right.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq Q \leq 100
  • S is a string of length N consisting only of lowercase English letters
  • 1 \leq a_i \leq N
  • 1 \leq b_i \leq N
  • All values in the input except S are integers

Input

The input is given from standard input in the following format:

N Q
S
a_1 b_1
a_2 b_2
\vdots
a_Q b_Q
  • The first line contains the number of tags N and the number of swaps Q, separated by a space.
  • The second line contains the string S representing the letters on the tags.
  • Over Q lines starting from the third line, the (2 + i)-th line contains the positions a_i and b_i of the tags swapped in the i-th operation, separated by a space.

Output

Output, in one line, the string formed by the letters on the tags from left to right after all swaps are complete.


Sample Input 1

5 2
abcde
1 3
2 5

Sample Output 1

ceadb

Sample Input 2

3 1
xyz
2 2

Sample Output 2

xyz

Sample Input 3

4 3
abcd
1 4
2 3
1 4

Sample Output 3

acbd

Sample Input 4

3 1
xyz
1 2

Sample Output 4

yxz

Sample Input 5

4 3
abcd
1 4
2 1
1 4

Sample Output 5

adcb
C - 畑の均し作業

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

配点 : 333 点

問題文

高橋君は農家を営んでおり、横一列に並んだ N 区画の畑を持っています。各区画には土が積まれており、左から i 番目 (1 \leq i \leq N) の区画の土の高さは A_i です。

高橋君は作物を植える前に、すべての区画の土の高さを均一にしたいと考えています。そのために、以下の作業を好きな回数(0 回でもよい)行うことができます。

作業: 整数 l, r(1 \leq l \leq r \leq N)を選び、左から l 番目から r 番目までのすべての区画の土の高さをそれぞれ 1 だけ増やす。

作業ごとに選ぶ l, r は自由に決めることができ、以前と同じ値を再び選ぶこともできます。なお、土を取り除く作業はできないため、土の高さを減らすことはできません。

すべての区画の土の高さが同じになれば目標達成です。最終的な土の高さがいくらであっても構いません(ただし、土の高さを減らすことはできないため、最終的な高さは元の高さの最大値 \max(A_1, A_2, \ldots, A_N) 以上になります)。

高橋君が目標を達成するために必要な作業の最小回数を求めてください。

制約

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

入力

N
A_1 A_2 \ldots A_N
  • 1 行目には、畑の区画数を表す整数 N が与えられる。
  • 2 行目には、各区画の土の高さを表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。

出力

すべての区画の土の高さを同じにするために必要な作業の最小回数を 1 行で出力せよ。


入力例 1

5
3 1 4 1 5

出力例 1

7

入力例 2

8
2 5 3 7 4 7 1 6

出力例 2

16

入力例 3

12
100 85 90 70 95 60 80 75 88 92 65 100

出力例 3

102

Score : 333 pts

Problem Statement

Takahashi is a farmer who owns N plots of land arranged in a horizontal row. Each plot has soil piled on it, and the height of soil in the i-th plot from the left (1 \leq i \leq N) is A_i.

Before planting crops, Takahashi wants to make the soil height of all plots uniform. To achieve this, he can perform the following operation any number of times (possibly 0 times).

Operation: Choose integers l, r (1 \leq l \leq r \leq N), and increase the soil height of every plot from the l-th to the r-th from the left by 1.

For each operation, l and r can be chosen freely, and the same values as before may be chosen again. Note that there is no operation to remove soil, so the soil height cannot be decreased.

The goal is achieved when all plots have the same soil height. The final soil height can be any value (however, since soil height cannot be decreased, the final height must be at least \max(A_1, A_2, \ldots, A_N)).

Find the minimum number of operations Takahashi needs to achieve his goal.

Constraints

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

Input

N
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of plots.
  • The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the soil height of each plot.

Output

Print in one line the minimum number of operations required to make the soil height of all plots the same.


Sample Input 1

5
3 1 4 1 5

Sample Output 1

7

Sample Input 2

8
2 5 3 7 4 7 1 6

Sample Output 2

16

Sample Input 3

12
100 85 90 70 95 60 80 75 88 92 65 100

Sample Output 3

102
D - 休暇の計画

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

配点 : 400 点

問題文

高橋君は 1 日目から N 日目までの N 日間の休暇を過ごします。

高橋君はこの休暇の中から、ちょうど K 日を選んで外出することにしました。

ただし、体力を維持するため、連続する 2 日間(1 \le i \le N - 1 を満たす任意の整数 i についての i 日目と i + 1 日目)の両方に外出することはできません。

i 日目 (1 \le i \le N) に外出すると、満足度 A_i が得られます。

高橋君が条件を満たすように外出する日を選んだとき、得られる満足度の合計の最大値を求めてください。

なお、本問題の制約下において、条件を満たす外出日の選び方が少なくとも 1 つ存在することが保証されています。

制約

  • 1 \le N \le 5000
  • 1 \le K \le \lceil N / 2 \rceil(\lceil x \rceil は x 以上の最小の整数を表す)
  • 1 \le A_i \le 10^9
  • 入力はすべて整数

入力

入力は以下の形式で与えられる。

N K
A_1
A_2
\vdots
A_N

出力

高橋君が得られる満足度の合計の最大値を 1 行で出力してください。


入力例 1

5 2
3
5
2
4
1

出力例 1

9

入力例 2

4 2
10
5
8
10

出力例 2

20

入力例 3

10 4
1000000000
500000000
800000000
200000000
900000000
100000000
700000000
300000000
600000000
400000000

出力例 3

3400000000

入力例 4

20 7
84
32
95
42
12
67
91
23
58
74
39
88
50
61
99
17
45
78
83
29

出力例 4

614

入力例 5

1 1
1000000000

出力例 5

1000000000

Score : 400 pts

Problem Statement

Takahashi will spend a vacation of N days, from Day 1 to Day N.

He has decided to choose exactly K days during this vacation to go out.

However, to maintain his stamina, he cannot go out on two consecutive days (both Day i and Day i + 1 for any integer i satisfying 1 \le i \le N - 1).

If he goes out on Day i (1 \le i \le N), he gains a satisfaction of A_i.

Find the maximum possible total satisfaction Takahashi can obtain by choosing the days to go out such that the condition is satisfied.

Under the constraints of this problem, it is guaranteed that there is at least one valid way to choose the days.

Constraints

  • 1 \le N \le 5000
  • 1 \le K \le \lceil N / 2 \rceil (\lceil x \rceil represents the smallest integer greater than or equal to x)
  • 1 \le A_i \le 10^9
  • All input values are integers.

Input

The input is given in the following format:

N K
A_1
A_2
\vdots
A_N

Output

Print the maximum total satisfaction Takahashi can obtain in a single line.


Sample Input 1

5 2
3
5
2
4
1

Sample Output 1

9

Sample Input 2

4 2
10
5
8
10

Sample Output 2

20

Sample Input 3

10 4
1000000000
500000000
800000000
200000000
900000000
100000000
700000000
300000000
600000000
400000000

Sample Output 3

3400000000

Sample Input 4

20 7
84
32
95
42
12
67
91
23
58
74
39
88
50
61
99
17
45
78
83
29

Sample Output 4

614

Sample Input 5

1 1
1000000000

Sample Output 5

1000000000
E - 伝言リレー

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

配点 : 466 点

問題文

高橋君のクラスには N 人の生徒がおり、生徒には 1 から N までの番号が付けられています。

このクラスでは「伝言リレー」というゲームを行います。ゲームの準備として、各生徒 i (1 \leq i \leq N) には受け渡し先として自分以外のちょうど1人の生徒 P_i が決められています(P_i \neq i)。受け渡し先とは、自分がクイズを解けなかったときにそのクイズを次に渡す相手のことです。

ゲームの開始時に、各生徒 i にクイズが1問ずつ配られます。生徒 i に配られるクイズの難易度は D_i です。また、各生徒 i には学力 S_i が定められており、生徒 i は難易度が S_i 以下のクイズであれば解くことができます。

配られた N 問のクイズは、それぞれ独立に以下のルールに従って処理されます。各クイズの処理ごとに訪問済みの記録は独立に管理され、あるクイズの処理で訪問済みとなった記録が他のクイズの処理に影響することはありません。 そのため、同じ生徒が異なるクイズの処理においてそれぞれ訪問されることがあり、複数のクイズを解くこともあり得ます。

生徒 i に配られたクイズの処理は、以下のように行われます。

はじめに、j = i とし、生徒 i を訪問済みとします。その後、以下の手順を繰り返します:

  1. クイズの難易度が S_j 以下であれば、生徒 j がそのクイズを解く。このクイズの処理はここで終了する。
  2. そうでなければ、クイズを受け渡し先の生徒 P_j に回そうとする。
  • 生徒 P_j がすでに訪問済みであれば、受け渡しがループするため、そのクイズは誰にも解かれず未回答となる。このクイズの処理はここで終了する。
  • 生徒 P_j がまだ訪問済みでなければ、生徒 P_j を訪問済みとし、j を P_j に置き換えて手順 1 から繰り返す。

すべてのクイズについて、そのクイズを最終的に解く生徒の番号を求めてください。未回答となる場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq N
  • P_i \neq i (1 \leq i \leq N)
  • 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である。

入力

N
P_1 P_2 \cdots P_N
S_1 S_2 \cdots S_N
D_1 D_2 \cdots D_N
  • 1 行目には、生徒の人数を表す整数 N が与えられる。
  • 2 行目には、各生徒 i (1 \leq i \leq N) のクイズの受け渡し先を表す整数 P_i が、スペース区切りで N 個与えられる。
  • 3 行目には、各生徒 i (1 \leq i \leq N) の学力を表す整数 S_i が、スペース区切りで N 個与えられる。
  • 4 行目には、各生徒 i (1 \leq i \leq N) に配られるクイズの難易度を表す整数 D_i が、スペース区切りで N 個与えられる。

出力

N 個の整数をスペース区切りで1行に出力せよ。i 番目 (1 \leq i \leq N) の整数は、生徒 i に最初に配られたクイズを最終的に解く生徒の番号とする。誰にも解かれない場合は -1 とする。


入力例 1

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

出力例 1

1 4 4 4

入力例 2

3
2 3 1
1 1 1
5 5 5

出力例 2

-1 -1 -1

入力例 3

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

出力例 3

1 4 4 -1 5 8 8 -1 9 8

入力例 4

25
2 3 4 5 6 3 8 9 10 7 12 13 14 15 16 17 18 19 15 21 22 23 24 25 20
12 5 30 18 7 25 40 10 22 35 6 14 50 9 27 31 3 45 20 100 1 60 15 80 33
4 26 19 8 50 24 36 11 41 2 55 13 49 28 32 44 5 70 21 90 61 16 79 34 101

出力例 4

1 3 3 4 -1 6 7 9 -1 10 -1 12 13 16 18 18 18 -1 15 20 24 22 24 24 -1

入力例 5

2
2 1
1 1000000000
1000000000 1

出力例 5

2 2

Score : 466 pts

Problem Statement

There are N students in Takahashi's class, numbered 1 to N.

In this class, they will play a game called "Message Relay". As preparation for the game, each student i (1 \leq i \leq N) is assigned exactly one other student P_i as their recipient (P_i \neq i). The recipient is the person to whom a student passes a quiz when they cannot solve it.

At the start of the game, each student i is given one quiz. The difficulty of the quiz given to student i is D_i. Additionally, each student i has an ability S_i, and student i can solve a quiz if its difficulty is at most S_i.

The N quizzes are processed independently according to the following rules. The record of visited students is managed independently for each quiz's process; the visited records of one quiz's process do not affect those of other quizzes. Thus, the same student can be visited in different quiz processes and can solve multiple quizzes.

The process for the quiz given to student i is as follows:

Initially, set j = i, and mark student i as visited. Then, repeat the following steps:

  1. If the difficulty of the quiz is at most S_j, student j solves the quiz. The process for this quiz ends.
  2. Otherwise, they attempt to pass the quiz to their recipient, student P_j.
  • If student P_j is already visited, the passing would loop, so the quiz remains unsolved by anyone. The process for this quiz ends.
  • If student P_j is not yet visited, mark student P_j as visited, replace j with P_j, and repeat from Step 1.

For each quiz, find the student number of the student who finally solves it. If a quiz remains unsolved, output -1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq P_i \leq N
  • P_i \neq i (1 \leq i \leq N)
  • 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N
P_1 P_2 \cdots P_N
S_1 S_2 \cdots S_N
D_1 D_2 \cdots D_N
  • The first line contains an integer N, representing the number of students.
  • The second line contains N space-separated integers representing the recipient P_i for each student i (1 \leq i \leq N).
  • The third line contains N space-separated integers representing the ability S_i of each student i (1 \leq i \leq N).
  • The fourth line contains N space-separated integers representing the difficulty D_i of the quiz initially given to each student i (1 \leq i \leq N).

Output

Print N integers in a single line, separated by spaces. The i-th integer (1 \leq i \leq N) should be the student number of the student who finally solves the quiz initially given to student i, or -1 if the quiz remains unsolved.


Sample Input 1

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

Sample Output 1

1 4 4 4

Sample Input 2

3
2 3 1
1 1 1
5 5 5

Sample Output 2

-1 -1 -1

Sample Input 3

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

Sample Output 3

1 4 4 -1 5 8 8 -1 9 8

Sample Input 4

25
2 3 4 5 6 3 8 9 10 7 12 13 14 15 16 17 18 19 15 21 22 23 24 25 20
12 5 30 18 7 25 40 10 22 35 6 14 50 9 27 31 3 45 20 100 1 60 15 80 33
4 26 19 8 50 24 36 11 41 2 55 13 49 28 32 44 5 70 21 90 61 16 79 34 101

Sample Output 4

1 3 3 4 -1 6 7 9 -1 10 -1 12 13 16 18 18 18 -1 15 20 24 22 24 24 -1

Sample Input 5

2
2 1
1 1000000000
1000000000 1

Sample Output 5

2 2