A - Not Acceptable

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

配点 : 100 点

問題文

高橋君は 2025 年 5 月 17 日 A 時 B 分締切のレポートを、 2025 年 5 月 17 日 C 時 D 分に提出しました。
ここで、「A 時 B 分」と「C 時 D 分」は異なる時刻であることが保証されます。

高橋君が締切前にレポートを提出しているならば Yes を、そうでないならば No を出力してください。

制約

  • 0 \leq A,C \leq 23
  • 0 \leq B,D \leq 59
  • (A,B)\neq(C,D)
  • A,B,C,D は整数

入力

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

A B C D

出力

高橋君が締切前にレポートを提出しているならば Yes を、そうでないならば No を出力せよ。


入力例 1

22 40 22 30

出力例 1

Yes

レポートの締切は 22 時 40 分であり、高橋君は 22 時 30 分に提出しているため、締切前にレポートを提出しています。
よって、Yes を出力します。


入力例 2

22 40 22 45

出力例 2

No

レポートの締切は 22 時 40 分であり、高橋君は 22 時 45 分に提出しているため、締切後にレポートを提出しています。
よって、No を出力します。


入力例 3

12 0 11 30

出力例 3

Yes

Score : 100 points

Problem Statement

Takahashi had a report whose deadline was B minutes past A o'clock on May 17, 2025. He submitted it at D minutes past C o'clock on May 17, 2025.
It is guaranteed that "B minutes past A o'clock" and "D minutes past C o'clock" are different times.

Output Yes if Takahashi submitted the report before the deadline, and No otherwise.

Constraints

  • 0 \leq A, C \leq 23
  • 0 \leq B, D \leq 59
  • (A, B) \neq (C, D)
  • A, B, C, and D are integers.

Input

The input is given from Standard Input in the following format:

A B C D

Output

If Takahashi submitted the report before the deadline, output Yes; otherwise, output No.


Sample Input 1

22 40 22 30

Sample Output 1

Yes

The deadline is 22:40, and he submitted at 22:30, so he submitted before the deadline. Hence, output Yes.


Sample Input 2

22 40 22 45

Sample Output 2

No

The deadline is 22:40, and he submitted at 22:45, so he submitted after the deadline. Hence, output No.


Sample Input 3

12 0 11 30

Sample Output 3

Yes
B - Find Multiple

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

配点 : 100 点

問題文

A 以上 B 以下であるような C の倍数を、1 つ出力してください。

条件を満たす数が存在しない場合は -1 を出力してください。

制約

  • 1 \leq A \leq B \leq 1000
  • 1 \leq C \leq 1000
  • 入力は全て整数

入力

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

A B C

出力

答えを出力せよ。
条件を満たす数が存在しない場合は -1 を出力せよ。


入力例 1

123 456 100

出力例 1

200

300 や 400 も正解です。


入力例 2

630 940 314

出力例 2

-1

Score : 100 points

Problem Statement

Print a number between A and B (inclusive) that is a multiple of C.

If there is no such number, print -1.

Constraints

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

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the answer.
If there is no number with the desired property, print -1.


Sample Input 1

123 456 100

Sample Output 1

200

300 or 400 would also be accepted.


Sample Input 2

630 940 314

Sample Output 2

-1
C - Unique Nicknames

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

配点 : 200 点

問題文

人 1, 人 2, \dots 人 N の N 人の人がいます。人 i の姓は s_i、名は t_i です。

N 人の人すべてにあだ名をつけることを考えます。人 i のあだ名 a_i は以下の条件を満たす必要があります。

  • a_i は人 i の姓あるいは名と一致する。言い換えると、a_i = s_i または a_i = t_i の少なくとも一方が成り立つ。
  • a_i は自分以外の人の姓および名のどちらとも一致しない。言い換えると、1 \leq j \leq N, i \neq j を満たすすべての整数 j について a_i \neq s_j かつ a_i \neq t_j が成り立つ。

N 人全員に条件を満たすあだ名をつけることは可能でしょうか。可能ならば Yes を、そうでないならば No を出力してください。

制約

  • 2 \leq N \leq 100
  • N は整数である。
  • s_i,t_i は英小文字からなる 1 文字以上 10 文字以下の文字列である。

入力

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

N
s_1 t_1
s_2 t_2
\vdots
s_N t_N

出力

N 人すべてにあだ名をつけることが可能ならば Yes を、そうでないならば No を出力せよ。


入力例 1

3
tanaka taro
tanaka jiro
suzuki hanako

出力例 1

Yes

a_1 = taro, a_2 = jiro, a_3 = hanako とすれば、これは問題文にあるあだ名の条件を満たしています。(a_3 は suzuki でもよいです。)
ここで、a_1 = tanaka とはできないことに注意してください。なぜならば 人 2 の姓 s_2 もまた tanaka であるため、あだ名の条件の 2 つ目を満たさなくなるからです。


入力例 2

3
aaa bbb
xxx aaa
bbb yyy

出力例 2

No

問題文の条件を満たすあだ名のつけ方は存在しません。


入力例 3

2
tanaka taro
tanaka taro

出力例 3

No

同姓同名である人の組が存在する場合もあります。


入力例 4

3
takahashi chokudai
aoki kensho
snu ke

出力例 4

Yes

a_1 = chokudai, a_2 = kensho, a_3 = ke とすればよいです。

Score : 200 points

Problem Statement

There are N people numbered Person 1, Person 2, \dots, and Person N. Person i has a family name s_i and a given name t_i.

Consider giving a nickname to each of the N people. Person i's nickname a_i should satisfy all the conditions below.

  • a_i coincides with Person i's family name or given name. In other words, a_i = s_i and/or a_i = t_i holds.
  • a_i does not coincide with the family name and the given name of any other person. In other words, for all integer j such that 1 \leq j \leq N and i \neq j, it holds that a_i \neq s_j and a_i \neq t_j.

Is it possible to give nicknames to all the N people? If it is possible, print Yes; otherwise, print No.

Constraints

  • 2 \leq N \leq 100
  • N is an integer.
  • s_i and t_i are strings of lengths between 1 and 10 (inclusive) consisting of lowercase English alphabets.

Input

Input is given from Standard Input in the following format:

N
s_1 t_1
s_2 t_2
\vdots
s_N t_N

Output

If it is possible to give nicknames to all the N people, print Yes; otherwise, print No.


Sample Input 1

3
tanaka taro
tanaka jiro
suzuki hanako

Sample Output 1

Yes

The following assignment satisfies the conditions of nicknames described in the Problem Statement: a_1 = taro, a_2 = jiro, a_3 = hanako. (a_3 may be suzuki, too.)
However, note that we cannot let a_1 = tanaka, which violates the second condition of nicknames, since Person 2's family name s_2 is tanaka too.


Sample Input 2

3
aaa bbb
xxx aaa
bbb yyy

Sample Output 2

No

There is no way to give nicknames satisfying the conditions in the Problem Statement.


Sample Input 3

2
tanaka taro
tanaka taro

Sample Output 3

No

There may be a pair of people with the same family name and the same given name.


Sample Input 4

3
takahashi chokudai
aoki kensho
snu ke

Sample Output 4

Yes

We can let a_1 = chokudai, a_2 = kensho, and a_3 = ke.

D - Foreign Exchange

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

配点 : 150 点

問題文

1 から N までの番号がつけられた N 個の国があり、i = 1, 2, \ldots, N について、高橋君は国 i の通貨を A_i 単位持っています。

高橋君は、下記の操作を好きな回数( 0 回でも良い)繰り返します。

  • まず、1 以上 N-1 以下の整数 i を選ぶ。
  • その後、国 i の通貨を S_i 単位以上持っているなら、下記の行動を 1 回行う。
    • 国 i の通貨を S_i 単位だけ支払い、国 (i+1) の通貨を T_i 単位だけ獲得する。

最終的に高橋君が持っている国 N の通貨の単位数としてあり得る最大値を出力してください。

制約

  • 入力される値はすべて整数
  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 \leq T_i \leq S_i \leq 10^9

入力

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

N
A_1 A_2 \ldots A_N
S_1 T_1
S_2 T_2
\vdots
S_{N-1} T_{N-1}

出力

答えを出力せよ。


入力例 1

4
5 7 0 3
2 2
4 3
5 2

出力例 1

5

以下の説明では、高橋君が持っている各国の通貨の単位数を、数列 A = (A_1, A_2, A_3, A_4) として表します。はじめ、A = (5, 7, 0, 3) です。

下記の通りに 4 回操作を行うことを考えます。

  • i = 2 を選び、国 2 の通貨 4 単位を支払って、国 3 の通貨 3 単位を獲得する。その結果、A = (5, 3, 3, 3) となる。
  • i = 1 を選び、国 1 の通貨 2 単位を支払って、国 2 の通貨 2 単位を獲得する。その結果、A = (3, 5, 3, 3) となる。
  • i = 2 を選び、国 2 の通貨 4 単位を支払って、国 3 の通貨 3 単位を獲得する。その結果、A = (3, 1, 6, 3) となる。
  • i = 3 を選び、国 3 の通貨 5 単位を支払って、国 4 の通貨 2 単位を獲得する。その結果、A = (3, 1, 1, 5) となる。

このとき、最終的に高橋君が持っている国 4 の通貨の単位数は 5 であり、これが考えられる最大値です。


入力例 2

10
32 6 46 9 37 8 33 14 31 5
5 5
3 1
4 3
2 2
3 2
3 2
4 4
3 3
3 1

出力例 2

45

Score: 150 points

Problem Statement

There are N countries numbered 1 to N. For each i = 1, 2, \ldots, N, Takahashi has A_i units of the currency of country i.

Takahashi can repeat the following operation any number of times, possibly zero:

  • First, choose an integer i between 1 and N-1, inclusive.
  • Then, if Takahashi has at least S_i units of the currency of country i, he performs the following action once:
    • Pay S_i units of the currency of country i and gain T_i units of the currency of country (i+1).

Print the maximum possible number of units of the currency of country N that Takahashi could have in the end.

Constraints

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

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N
S_1 T_1
S_2 T_2
\vdots
S_{N-1} T_{N-1}

Output

Print the answer.


Sample Input 1

4
5 7 0 3
2 2
4 3
5 2

Sample Output 1

5

In the following explanation, let the sequence A = (A_1, A_2, A_3, A_4) represent the numbers of units of the currencies of the countries Takahashi has. Initially, A = (5, 7, 0, 3).

Consider performing the operation four times as follows:

  • Choose i = 2, pay four units of the currency of country 2, and gain three units of the currency of country 3. Now, A = (5, 3, 3, 3).
  • Choose i = 1, pay two units of the currency of country 1, and gain two units of the currency of country 2. Now, A = (3, 5, 3, 3).
  • Choose i = 2, pay four units of the currency of country 2, and gain three units of the currency of country 3. Now, A = (3, 1, 6, 3).
  • Choose i = 3, pay five units of the currency of country 3, and gain two units of the currency of country 4. Now, A = (3, 1, 1, 5).

At this point, Takahashi has five units of the currency of country 4, which is the maximum possible number.


Sample Input 2

10
32 6 46 9 37 8 33 14 31 5
5 5
3 1
4 3
2 2
3 2
3 2
4 4
3 3
3 1

Sample Output 2

45
E - Fishbones

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

配点 : 300 点

問題文

アーティストの高砂君は、魚の骨をかたどったオブジェを作りました。

オブジェは N 本の肋骨と 1 本の脊椎からなります。 肋骨には 1 から N までの番号が付けられています。

高砂君は、以下の条件をすべて満たすように N+1 本の骨に 1 つずつ文字列を書こうと考えています。

  • 脊椎に書く文字列の長さは N である。
  • 肋骨 i = 1, \dots, N に対して、以下が成り立つ。
    • 肋骨 i に書く文字列の長さは A_i である。
    • 肋骨 i に書く文字列の B_i 文字目は、脊椎に書く文字列の i 文字目に一致する。
  • N+1 本の骨に書く文字列はいずれも、S_1, \cdots, S_M のいずれかである(重複してもよい)。

S_1, \cdots, S_M は英小文字からなる文字列であり、互いに異なります。

j = 1, \cdots, M に対して、以下の質問に答えてください。

  • 条件を満たす書き方のうち、脊椎に書く文字列が S_j であるものは存在しますか?

制約

  • N は整数
  • 1 \leq N \leq 10
  • A_i, B_i は整数 (1 \leq i \leq N)
  • 1 \leq B_i \leq A_i \leq 10 (1 \leq i \leq N)
  • M は整数
  • 1 \leq M \leq 200\,000
  • S_j は英小文字からなる文字列 (1 \leq j \leq M)
  • 1 \leq |S_j| \leq 10 (1 \leq j \leq M)
  • S_1, \cdots, S_M は相異なる

入力

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

N
A_1 B_1
\vdots
A_N B_N
M
S_1
\vdots
S_M

出力

M 行出力せよ。

j 行目 (1 \leq j \leq M) には、条件を満たす書き方のうち脊椎に書く文字列が S_j のものが存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

5
5 3
5 2
4 1
5 1
3 2
8
retro
chris
itchy
tuna
crab
rock
cod
ash

出力例 1

Yes
Yes
No
No
No
No
No
No

肋骨 1,2,3,4,5 にそれぞれ chris, retro, tuna, retro, cod と書くことで、脊椎に retro を書いたときに条件を満たします。

  • retro の長さは 5 文字。
  • 各肋骨について、以下が成り立つ。
    • 肋骨 1 に書かれる文字列 chris の長さは 5 文字である。その 3 文字目は r であり、retro の 1 文字目に一致する。
    • 肋骨 2 に書かれる文字列 retro の長さは 5 文字である。その 2 文字目は e であり、retro の 2 文字目に一致する。
    • 肋骨 3 に書かれる文字列 tuna の長さは 4 文字である。その 1 文字目は t であり、retro の 3 文字目に一致する。
    • 肋骨 4 に書かれる文字列 retro の長さは 5 文字である。その 1 文字目は r であり、retro の 4 文字目に一致する。
    • 肋骨 5 に書かれる文字列 cod の長さは 3 文字である。その 2 文字目は o であり、retro の 5 文字目に一致する。

肋骨 1,2,3,4,5 にそれぞれ itchy, chris, rock, itchy, ash と書くことで、脊椎に chris を書いたときに条件を満たします。


入力例 2

5
5 1
5 2
5 3
5 4
5 5
8
retro
chris
itchy
tuna
crab
rock
cod
ash

出力例 2

Yes
Yes
Yes
No
No
No
No
No

Score : 300 points

Problem Statement

Artist Takasago has created an object in the shape of a fish skeleton.

The object consists of N ribs and one spine. The ribs are numbered 1 through N.

He wants to write one string on each of the N+1 bones, satisfying all of the following conditions.

  • The length of the string written on the spine is N.
  • For each rib i = 1, \dots, N, the following hold.
    • The length of the string written on rib i is A_i.
    • The B_i-th character of the string written on rib i equals the i-th character of the string written on the spine.
  • Each of the strings written on the N+1 bones is one of S_1, \cdots, S_M (duplicates allowed).

S_1, \cdots, S_M are strings consisting of lowercase English letters, and they are all distinct.

For each j = 1, \cdots, M, answer the following question.

  • Among the ways to write strings satisfying the conditions, is there one where the string written on the spine is S_j?

Constraints

  • N is an integer.
  • 1 \leq N \leq 10
  • A_i and B_i are integers. (1 \leq i \leq N)
  • 1 \leq B_i \leq A_i \leq 10 (1 \leq i \leq N)
  • M is an integer.
  • 1 \leq M \leq 200\,000
  • S_j is a string consisting of lowercase English letters. (1 \leq j \leq M)
  • 1 \leq |S_j| \leq 10 (1 \leq j \leq M)
  • S_1, \cdots, S_M are pairwise distinct.

Input

The input is given from Standard Input in the following format:

N
A_1 B_1
\vdots
A_N B_N
M
S_1
\vdots
S_M

Output

Output M lines.

The j-th line (1 \leq j \leq M) should contain Yes if there exists a way to write strings satisfying the conditions with S_j written on the spine, and No otherwise.


Sample Input 1

5
5 3
5 2
4 1
5 1
3 2
8
retro
chris
itchy
tuna
crab
rock
cod
ash

Sample Output 1

Yes
Yes
No
No
No
No
No
No

By writing chris, retro, tuna, retro, cod on ribs 1,2,3,4,5 respectively, the conditions are satisfied with retro written on the spine.

  • The length of retro is 5.
  • For each rib, the following hold.
    • The string written on rib 1 is chris, which has length 5. Its third character is r, which equals the first character of retro.
    • The string written on rib 2 is retro, which has length 5. Its second character is e, which equals the second character of retro.
    • The string written on rib 3 is tuna, which has length 4. Its first character is t, which equals the third character of retro.
    • The string written on rib 4 is retro, which has length 5. Its first character is r, which equals the fourth character of retro.
    • The string written on rib 5 is cod, which has length 3. Its second character is o, which equals the fifth character of retro.

By writing itchy, chris, rock, itchy, ash on ribs 1,2,3,4,5 respectively, the conditions are satisfied with chris written on the spine.


Sample Input 2

5
5 1
5 2
5 3
5 4
5 5
8
retro
chris
itchy
tuna
crab
rock
cod
ash

Sample Output 2

Yes
Yes
Yes
No
No
No
No
No