A - Compare dollar and yen

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

1 ドルの価値は A 円の価値と等しいです。

このとき B ドルの価値が C 円の価値より高いか判定してください。

制約

  • A,B,C は全て 1 以上 1000 以下の整数

入力

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

A B C

出力

B ドルの価値が C 円の価値より高い場合 Yes を、そうでない場合 No を一行に出力せよ。


入力例 1

5 3 14

出力例 1

Yes

5 ドルの価値は 15 円の価値と等しく、これは 14 円の価値より高いです。


入力例 2

1 1 1

出力例 2

No

価値が等しい場合 No を出力することに注意してください。


入力例 3

10 10 200

出力例 3

No

Problem Statement

A dollar is worth A yen.

Determine if B dollars are worth more than C yen.

Constraints

  • A, B, and C are integers between 1 and 1000, inclusive.

Input

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

A B C

Output

Print Yes if B dollars are worth more than C yen, and No otherwise.


Sample Input 1

5 3 14

Sample Output 1

Yes

5 dollars are worth 15 yen, which is worth more than 14 yen.


Sample Input 2

1 1 1

Sample Output 2

No

Note that No should be printed if they are equal in value.


Sample Input 3

10 10 200

Sample Output 3

No
B - Changing Trains

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

AtCoder 駅には 2 つの路線があり、片方は高橋駅ゆき、もう片方は青木駅ゆきです。

N 本の列車のデータが与えられます。 i 本目 (1\leq i\leq N) のデータは文字列と正整数の組 (S _ i,T _ i) で表されます。S _ iAoki もしくは Takahashi のどちらかと等しく、S _ i ゆきの列車が T _ i 分後に AtCoder 駅に到着することを表します。

あなたは、N 本のうち AtCoder 駅にもっとも早く到着する高橋駅ゆきの列車に乗ろうと思っています。 何本目の列車に乗ることになるか求めてください。

制約

  • 1\leq N\leq2\times10 ^ 5
  • S _ iAoki もしくは Takahashi のどちらかと等しい (1\leq i\leq N)
  • 1\leq T _ i\leq10 ^ 9\ (1\leq i\leq N)
  • i\neq j ならば T _ i\neq T _ j\ (1\leq i,j\leq N)
  • S _ i={}Takahashi であるような i が存在する
  • N および T _ i\ (1\leq i\leq N) はすべて整数

入力

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

N
S _ 1 T _ 1
S _ 2 T _ 2
\vdots
S _ N T _ N

出力

答えを出力せよ。


入力例 1

5
Aoki 2
Takahashi 3
Takahashi 5
Aoki 10
Takahashi 15

出力例 1

2

AtCoder 駅に到着する 5 本の列車の情報が与えられています。 このうち、高橋駅ゆきの列車は 3 分後に到着する 2 本目の列車、5 分後に到着する 3 本目の列車、15 分後に到着する 5 本目の列車の 3 本です。

この中で最も到着が早い列車は 2 本目の列車なので、2 を出力してください。


入力例 2

8
Aoki 748
Aoki 436
Aoki 170
Aoki 587
Aoki 972
Aoki 331
Takahashi 532
Takahashi 523

出力例 2

8

到着する時刻がソートされて与えられるとは限らないことに注意してください。

Problem Statement

Trains from AtCoder station have two destinations: Takahashi station and Aoki station.

You are given information on N trains. The i-th train (1\leq i\leq N) is described by a pair (S _ i,T _ i) of a string and a positive integer. It means that the train for station S _ i arrives at AtCoder station T _ i minutes later, where S _ i is equal to either Aoki or Takahashi.

You are going to take the train for Takahashi station that arrives earliest at AtCoder station. Which train are you taking?

Constraints

  • 1\leq N\leq2\times10 ^ 5
  • S _ i is equal to either Aoki or Takahashi (1\leq i\leq N).
  • 1\leq T _ i\leq10 ^ 9\ (1\leq i\leq N)
  • If i\neq j, then T _ i\neq T _ j\ (1\leq i,j\leq N).
  • There exists i with S _ i={}Takahashi.
  • N and T _ i\ (1\leq i\leq N) are all integers.

Input

The 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 he is taking the i-th train (1\leq i\leq N), print the integer i.


Sample Input 1

5
Aoki 2
Takahashi 3
Takahashi 5
Aoki 10
Takahashi 15

Sample Output 1

2

Information on five trains arriving at AtCoder station is given. Three of them are for Takahashi station: the 2-nd train arriving three minutes later, the 3-rd train arriving five minutes later, and the 5-th train arriving 15 minutes later.

The 2-nd train arrives earliest among them, so 2 should be printed.


Sample Input 2

8
Aoki 748
Aoki 436
Aoki 170
Aoki 587
Aoki 972
Aoki 331
Takahashi 532
Takahashi 523

Sample Output 2

8

Note that the given arrival times may not be sorted.

C - XX reverse

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

いくつかの英小文字とちょうど 2 つの X からなる文字列 S が与えられます。
次の操作を行なった後に得られる文字列を出力してください。

S のうち 2 つの X で挟まれた部分、すなわち 1 つめの X の直後の文字から 2 つめの X の直前までの文字からなる部分(これは空文字列の可能性もある)を反転させる。 その後、2 つの X を取り除き、残りの文字列を順番を変えることなく連結し 1 つの文字列とする。

ただし、文字列 T を反転させて得られる文字列 T' とは、T と同じ長さの文字列であって、 T'i (1\leq i\leq \lvert T\rvert) 文字目が T(\lvert T\rvert+1-i) 文字目と等しいようなものをさします。 ここで、\lvert T\rvert は文字列 T の長さを表します。

制約

  • S は英小文字および X からなる長さ 3 以上 100 以下の文字列
  • SX をちょうど 2 つ含む。

入力

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

S

出力

S に対して操作を行なった後に得られる文字列を出力せよ。


入力例 1

stXnirXg

出力例 1

string

2 つの X に挟まれた文字列は nir です。 よって、この部分を反転させると stXrinXg となります。
その後、2 つの X を取り除き、残りの文字列を順番を保って連結すると、string となります。


入力例 2

samXXple

出力例 2

sample

空文字列を反転させたものは空文字列であることに注意してください。


入力例 3

XesreverX

出力例 3

reverse

Problem Statement

You are given a string S consisting of lowercase English letters and exactly two X's.
Apply the following operation and print the resulting string.

Reverse the part between the two X's in S, namely the part starting from the character right after the first X through the character right before the second X, which may be empty. Then, remove the two X's and concatenate the remaining strings without changing their order to form a single string.

The string T' obtained by reversing a string T is defined as the string whose length equals that of T, where the i-th (1\leq i\leq \lvert T\rvert) character of T' equals the (\lvert T\rvert+1-i)-th character of T. Here, \lvert T\rvert denotes the length of the string T.

Constraints

  • S is a string of length between 3 and 100, inclusive, consisting of lowercase English letters and X.
  • X occurs exactly twice in S.

Input

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

S

Output

Print the resulting string after applying the operation against S.


Sample Input 1

stXnirXg

Sample Output 1

string

The substring between the two X's is nir; reversing this part makes it stXrinXg.
After removing the two X's and concatenating the remaining strings preserving the order, it results in string.


Sample Input 2

samXXple

Sample Output 2

sample

Note that reversing an empty string makes it an empty string.


Sample Input 3

XesreverX

Sample Output 3

reverse
D - Rotating v

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N マス、横 N マスのグリッドがあります。
グリッドの各マスには文字 v が様々な向きで 1 個ずつ書きこまれています。上から i 行目、左から j 列目のマスに書きこまれている文字の向きは c_{i, j} で表されて、

  • c_{i,j} = v の時は通常の v が、
  • c_{i,j} = ^ の時は 180 度回転した v が、
  • c_{i,j} = < の時は時計回りに 90 度回転した v が、
  • c_{i,j} = > の時は反時計回りに 90 度回転した v がマスに書きこまれています。

今、グリッド全体を時計回りに 90 度回転させました。
回転させた後のグリッドの各マスに書きこまれている文字の向きを出力してください。

制約

  • 1 \leq N \leq 100
  • c_{i, j}v, ^, <, > のいずれか

入力

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

N
c_{1,1}c_{1,2}\dots c_{1,N}
c_{2,1}c_{2,2}\dots c_{2,N}
\vdots
c_{N,1}c_{N,2}\dots c_{N,N}

出力

回転させた後のグリッドの各マスに書きこまれている文字の向きを以下の形式で出力せよ。

a_{1,1}a_{1,2}\dots a_{1,N}
a_{2,1}a_{2,2}\dots a_{2,N}
\vdots
a_{N,1}a_{N,2}\dots a_{N,N}

ここで a_{i, j} は上から i 行目、左から j 列目のマスに書きこまれている文字の向きを表して、

  • a_{i,j} = v の時は通常の v を、
  • a_{i,j} = ^ の時は 180 度回転した v を、
  • a_{i,j} = < の時は時計回りに 90 度回転した v を、
  • a_{i,j} = > の時は反時計回りに 90 度回転した v を意味する。

入力例 1

1
v

出力例 1

<

v を時計回りに 90 度回転させると < になります。


入力例 2

3
>>^
><>
v<^

出力例 2

<vv
^^v
>v>

入力例 3

5
^^>vv
v>^^v
<vv<v
>^><>
>>^v>

出力例 3

vv^<>
v><v>
>v<>v
<^^><
vv<<<

Problem Statement

There is a grid with N horizontal rows and N vertical columns.
Each cell in the grid has the character v written in various orientations. The orientation of v written on the cell in the i-th row from the top and j-th column from the left is described by c_{i, j}:

  • if c_{i,j} = v, an ordinary v is written;
  • if c_{i,j} = ^, an upside-down v is written;
  • if c_{i,j} = <, a v rotated 90 degrees clockwise is written;
  • if c_{i,j} = >, a v rotated 90 degrees counterclockwise is written.

Now you will rotate the grid 90 degrees clockwise.
Print the orientations of the characters in the resulting grid.

Constraints

  • 1 \leq N \leq 100
  • c_{i, j} is one of v, ^, <, or >.

Input

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

N
c_{1,1}c_{1,2}\dots c_{1,N}
c_{2,1}c_{2,2}\dots c_{2,N}
\vdots
c_{N,1}c_{N,2}\dots c_{N,N}

Output

Print the orientations of the characters in the resulting grid in the following format:

a_{1,1}a_{1,2}\dots a_{1,N}
a_{2,1}a_{2,2}\dots a_{2,N}
\vdots
a_{N,1}a_{N,2}\dots a_{N,N}

Here, a_{i, j} denotes the orientation of v written on the cell in the i-th row from the top and j-th column from the left:

  • a_{i,j} = v means an ordinary v is written;
  • a_{i,j} = ^ means an upside-down v is written;
  • a_{i,j} = < means a v rotated 90 degrees clockwise is written;
  • a_{i,j} = > means a v rotated 90 degrees counterclockwise is written.

Sample Input 1

1
v

Sample Output 1

<

Rotating v 90 degrees clockwise makes it <.


Sample Input 2

3
>>^
><>
v<^

Sample Output 2

<vv
^^v
>v>

Sample Input 3

5
^^>vv
v>^^v
<vv<v
>^><>
>>^v>

Sample Output 3

vv^<>
v><v>
>v<>v
<^^><
vv<<<
E - Collecting Apples

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

数直線上にリンゴが落ちてきます。
i=1,2,\ldots,N について、座標 i には A_i 個のリンゴが落ちてきます。それ以外の場所にはリンゴは落ちてきません。

あなたは幅 W のカゴを持っています。
整数 L を自由に選び、L を左端としてカゴを設置することで、座標 L 以上 L+W 未満の範囲に落ちてくるリンゴが全てカゴに入ります。

最大で何個のリンゴがカゴに入るようにできますか?

制約

  • 1 \leq W \leq N \leq 10^6
  • 0 \leq A_i \leq 10^3
  • 入力は全て整数である

入力

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

N W
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

5 3
3 1 4 1 5

出力例 1

10

L=3 とすると、座標 3,4,5 に落ちてくる 4+1+5=10 個のリンゴがカゴに入ります。これが最大です。


入力例 2

10 10
1 1 1 1 1 1 1 1 1 1

出力例 2

10

入力例 3

12 4
314 159 265 358 979 323 846 264 338 327 950 288

出力例 3

2506

Problem Statement

Apples are dropping onto a number line.
A_i apples will drop onto the coordinate i for each i=1,2,\ldots,N; no other apples will drop.

You have a basket of width W.
You may freely choose an integer L to place the basket, aligning its left end at L, so that it collects apples falling onto the coordinates between L and L+W-1, inclusive.

At most how many apples can you collect?

Constraints

  • 1 \leq W \leq N \leq 10^6
  • 0 \leq A_i \leq 10^3
  • All input values are integers.

Input

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

N W
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

5 3
3 1 4 1 5

Sample Output 1

10

If you set L=3, the basket collects 4+1+5=10 apples falling onto coordinates 3,4,5. This is the maximum.


Sample Input 2

10 10
1 1 1 1 1 1 1 1 1 1

Sample Output 2

10

Sample Input 3

12 4
314 159 265 358 979 323 846 264 338 327 950 288

Sample Output 3

2506
F - Test Result

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N 人の生徒が期末試験を受けました。 生徒には番号が振られており、1 番から N 番までの異なる整数の番号がついています。

i(1\leq i\leq N) の生徒は、英語の試験で E _ i 点、数学の試験で M _ i 点を取りました。

試験の結果を用いて、生徒の順位が決まります。 順位は以下のようにして決められます。

  • 英語と数学の試験で取った点の合計がより大きい生徒はより小さい生徒より順位が高い。
  • 英語と数学の試験で取った点の合計が等しいとき、数学の試験で取った点がより大きい生徒はより小さい生徒より順位が高い。
  • 英語と数学の試験で取った点の合計と数学の試験で取った点のいずれも等しいとき、番号がより大きい生徒はより小さい生徒より順位が高い。

順位が高いほうから生徒の番号を出力してください。

制約

  • 1\leq N\leq100
  • 0\leq E _ i\leq100\ (1\leq i\leq N)
  • 0\leq M _ i\leq100\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N
E _ 1 M _ 1
E _ 2 M _ 2
\vdots
E _ N M _ N

出力

最も順位の高い生徒から、空白を区切りとしてすべての生徒の番号を順位の降順に出力せよ。


入力例 1

4
80 70
40 100
60 80
60 80

出力例 1

1 2 4 3

合計点数は、1 番の生徒の 150 点が最も高いです。 よって、最も順位の高い生徒は 1 番の生徒です。

他の生徒の合計点数はいずれも 140 点です。 これらの生徒のうち、2 番の生徒が取った数学の点数は 100 点で最も高いです。 よって、次に順位の高い生徒は 2 番の生徒です。

3 番の生徒と 4 番の生徒は合計点数も数学の点数も等しいです。 このとき、より番号の大きい生徒のほうが順位が上なので、次に順位の高い生徒は 4 番の生徒で、最も順位の低い生徒は 3 番の生徒です。

以上より、1 2 4 3 を出力してください。


入力例 2

1
1 1

出力例 2

1

生徒が 1 人しかいない場合もあります。


入力例 3

12
72 7
54 25
97 48
37 47
34 54
4 16
62 1
59 22
99 73
34 75
6 45
82 84

出力例 3

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

Problem Statement

N students took a final exam. Each student has a student ID, which is a unique integer between 1 and N.

The student with ID i (1\leq i\leq N) marked E _ i points in English and M _ i points in math.

The students' ranks are determined based on their scores by the following criteria:

  • Those with higher total (English and math) scores rank higher.
  • Among those with the same total score, those with higher math scores rank higher.
  • Among those with the same total score and math score, those with larger IDs rank higher.

Print the student IDs in the order of their ranks.

Constraints

  • 1\leq N\leq100
  • 0\leq E _ i\leq100\ (1\leq i\leq N)
  • 0\leq M _ i\leq100\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N
E _ 1 M _ 1
E _ 2 M _ 2
\vdots
E _ N M _ N

Output

Print all the student IDs, separated by spaces, in order of their ranks, with higher-ranked students coming first.


Sample Input 1

4
80 70
40 100
60 80
60 80

Sample Output 1

1 2 4 3

Student 1 marked the highest total score of 150 points. Thus, student 1 ranks highest.

The other students marked the same total score of 140 points. Among them, student 2 marked the highest math score of 100 points. Thus, student 2 ranks the next.

Student 3 and 4 marked the same total and math scores. Among them, those with larger IDs rank higher. Thus, student 4 ranks the next, and student 3 ranks lowest.

Hence, 1 2 4 3 should be printed.


Sample Input 2

1
1 1

Sample Output 2

1

There may be only one student.


Sample Input 3

12
72 7
54 25
97 48
37 47
34 54
4 16
62 1
59 22
99 73
34 75
6 45
82 84

Sample Output 3

9 12 3 10 5 4 8 2 1 7 11 6
G - Rice or Bread

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N 個のライスと M 個のパンがあります。i 番目のライスの美味しさは R_i で、i 番目のパンの美味しさは P_i です。

あなたはこれから K 日間、ライスかパンのいずれか一つを選んで食べることを繰り返します。同じ種類の料理を連続で食べると飽きるので、ライスを二日連続で食べることや、パンを二日連続で食べることは禁じられています。

K 日間の食事が可能か判定し、可能な場合食べる料理のおいしさの総和の最大値を求めてください。

制約

  • 1\leq N,M \leq 1000
  • 1\leq K \leq N+M
  • 1\leq R_i, P_i \leq 1000
  • 入力は全て整数

入力

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

N M K
R_1 \ldots R_N
P_1 \ldots P_M

出力

K 日間の食事が不可能な場合 -1 を出力せよ。可能な場合食べる料理のおいしさの総和の最大値を出力せよ。


入力例 1

3 3 3
2 5 4
3 6 1

出力例 1

15

1 日目に 3 番目のライスを食べ、2 日目に 2 番目のパンを食べ、3 日目に 2 番目のライスを食べることで、食べる料理のおいしさの総和を 15 にできます。


入力例 2

2 4 6
2 4
10 4 4 2

出力例 2

-1

条件を満たす K 日間の食事は不可能です。

Problem Statement

There are N bowls of rice and M slices of bread. The i-th bowl of rice has a deliciousness of R_i, and the i-th slice of bread has a deliciousness of P_i.

For the next K days, you will choose to eat either rice or bread. You will get tired of it if you eat the same dish in a row, so you will never eat rice two days in a row, or eat bread two days in a row.

Determine if this plan is feasible. If it is, find the maximum possible total deliciousness.

Constraints

  • 1\leq N,M \leq 1000
  • 1\leq K \leq N+M
  • 1\leq R_i, P_i \leq 1000
  • All input values are integers.

Input

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

N M K
R_1 \ldots R_N
P_1 \ldots P_M

Output

Print -1 if the plan is infeasible. Otherwise, print the maximum possible total deliciousness of the dishes that you eat.


Sample Input 1

3 3 3
2 5 4
3 6 1

Sample Output 1

15

By eating the third bowl of rice on day 1, the second slice of bread on day 2, and the second bowl of rice on day 3, the total deliciousness will be 15.


Sample Input 2

2 4 6
2 4
10 4 4 2

Sample Output 2

-1

The meal plan for the next K days is infeasible.

H - Temperature of Bath

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N 人の人が温泉に入りに来ました。i 番目の人は、 L_i 度以上 R_i 度以下の温度の温泉に入ることができると満足します。

あなたはどんな温度の温泉をいくつでも用意することができます。ただし、1 つの温泉は 1 つの温度にしか設定できません。

N 人全員が満足できるように温泉を用意するとき、用意する必要がある温泉の数の最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^{5}
  • 1 \leq L_i \leq R_i \leq 10^{9}
  • 入力は全て整数である。

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

用意する必要がある温泉の数の最小値を出力せよ。


入力例 1

5
37 40
43 45
40 42
35 39
39 43

出力例 1

3

39 度, 41 度, 45 度の 3 つの温泉を用意すれば、全員が満足することができます。2 つ以下の温泉を用意して全員が満足することはできません。よって、3 を出力します。


入力例 2

7
27 50
40 43
55 60
25 30
33 49
35 39
32 45

出力例 2

4

Problem Statement

N people came to use a hot spring. The i-th person will be satisfied if they bathe in a hot spring with temperature between L_i and R_i degrees, inclusive.

You can prepare any number of hot springs at any temperature. However, each hot spring can have only one set temperature.

At least how many hot springs do you need to prepare so that all the N people are satisfied?

Constraints

  • 1 \leq N \leq 2 \times 10^{5}
  • 1 \leq L_i \leq R_i \leq 10^{9}
  • All input values are integers.

Input

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the minimum number of hot springs you need to prepare.


Sample Input 1

5
37 40
43 45
40 42
35 39
39 43

Sample Output 1

3

If you prepare three hot springs at 39, 41, and 45 degrees, everyone will be satisfied. However, you can never satisfy everyone by preparing two hot springs or less. Thus, 3 should be printed.


Sample Input 2

7
27 50
40 43
55 60
25 30
33 49
35 39
32 45

Sample Output 2

4
I - Swap Puzzle

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

2 マス、横 4 マスのグリッドがあります。
各マスには数が書きこまれていて、上から i 行目、左から j 列目には (i-1) \times 4 + j が書きこまれています。
あなたは次の 2 種類の操作を好きな順番で何回でも行うことが出来ます。

  • 縦に隣接している 2 つのマスを選び、書かれている数を入れ替える。
  • 横に隣接している 2 つのマスを選び、書かれている数を入れ替える。

A_{i,j} (1 \leq i \leq 2, 1 \leq j \leq 4) が入力で与えられます。
グリッドが次の条件を満たした状態になるには最小で何回の操作が必要ですか?

  • 全ての i, j に対して、上から i 行目、左から j 列目のマスに A_{i,j} が書きこまれている。

制約

  • 1 \leq A_{i,j} \leq 8
  • A_{i,j} は全て異なる
  • 入力される値は全て整数

入力

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

A_{1,1} A_{1,2} A_{1,3} A_{1,4}
A_{2,1} A_{2,2} A_{2,3} A_{2,4}

出力

問題文の条件を満たすために必要な最小の操作回数を出力せよ。


入力例 1

5 2 4 3
1 6 7 8

出力例 1

2

例えば以下の手順で 2 回の操作を行うことで問題文の条件を満たすことができます。

  • 上から 1 行目、左から 1 列目に書かれた数と、上から 2 行目、左から 1 列目に書かれた数を入れ替える。
  • 上から 1 行目、左から 3 列目に書かれた数と、上から 1 行目、左から 4 列目に書かれた数を入れ替える。

1 回以下の操作で問題文の条件を満たすことはできないので、答えは 2 です。


入力例 2

1 2 3 4
5 6 7 8

出力例 2

0

入力例 3

7 4 5 8
2 6 3 1

出力例 3

8

Problem Statement

There is a grid with two horizontal rows and four vertical columns.
Each cell has a number written on it; the cell in the i-th row from the top and j-th column from the left has (i-1) \times 4 + j written on it.
You may perform the following two kinds of operations any number of times in any order.

  • Choose two horizontally adjacent cells, and swap the numbers written on them.
  • Choose two vertically adjacent cells, and swap the numbers written on them.

You are given A_{i,j} (1 \leq i \leq 2, 1 \leq j \leq 4) as the input.
At least how many operations are required to make the grid satisfy the following condition?

  • For all i and j, A_{i,j} is written on the cell in the i-th row from the top and j-th column from the left.

Constraints

  • 1 \leq A_{i,j} \leq 8
  • A_{i,j} are distinct.
  • All input values are integers.

Input

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

A_{1,1} A_{1,2} A_{1,3} A_{1,4}
A_{2,1} A_{2,2} A_{2,3} A_{2,4}

Output

Print the minimum number of operations required to satisfy the condition in the problem statement.


Sample Input 1

5 2 4 3
1 6 7 8

Sample Output 1

2

For example, the condition in the problem statement can be satisfied with two operations as follows:

  • Swap the number written on the cell in the 1-st row from the top and 1-st column from the left, and the number written on the cell in the 2-nd row from the top and 1-st column from the left.
  • Swap the number written on the cell in the 1-st row from the top and 3-rd column from the left, and the number written on the cell in the 1-st row from the top and 4-th column from the left.

The condition cannot be satisfied with one operation or less, so 2 is the answer.


Sample Input 2

1 2 3 4
5 6 7 8

Sample Output 2

0

Sample Input 3

7 4 5 8
2 6 3 1

Sample Output 3

8
J - Prohibited Pattern on Grid

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

縦横それぞれ N マスからなる N \times N のグリッドがあります。上から i マス目、右から j マス目のマスのことを、マス (i, j) と呼びます。はじめ各マスは黒色か白色で塗られていて、色は入力で与えられる文字 S_{i, j} で表されます。マス (i, j)S_{i, j}# なら黒色、 . なら白色で塗られています。

あなたは、白色で塗られたマスを 1 つ選び黒色で塗る、という操作を繰り返すことで、マス目が以下の条件を満たすようにします。

  • どの白色で塗られたマス (x, y) についても、マス (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1) (ただし存在しないマスは除く) のうち、黒色で塗られたマスは 2 つ以下である。

最小で何回の操作でマス目が条件を満たすようにできるか、求めてください。

制約

  • 1 \leq N \leq 2000
  • S_{i, j}.#

入力

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

N
S_{1, 1} S_{1, 2} \cdots S_{1, N}
S_{2, 1} S_{2, 2} \cdots S_{2, N}
\vdots
S_{N, 1} S_{N, 2} \cdots S_{N, N}

出力

答えを 1 行に出力せよ。


入力例 1

3
###
#.#
###

出力例 1

1

マス (2, 2) は白色で塗られており、また上下左右のマスがいずれも黒色で塗られています。マス (2, 2) を黒色で塗り直すと、マス目が条件を満たすようになります。


入力例 2

3
...
#.#
.#.

出力例 2

1

マス (2, 2) を黒色で塗り直すと、マス目が条件を満たすようになります。


入力例 3

5
#####
#....
#.###
#...#
#####

出力例 3

8

どの白く塗られたマスも黒く塗らないと、最終的に条件を満たすマス目にはなりません。

Problem Statement

There is an N \times N grid with N horizontal rows and N vertical columns. Let (i, j) denote the cell in the i-th row from the top and j-th column from the left. Initially, each cell is painted black or white; cell (i, j) is painted black if S_{i, j} is #, and white if S_{i, j} is ..

You will repeatedly choose one cell to paint black so that the grid satisfies the following condition:

  • for any cell (x, y) painted white, at most two of cells (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1) (except for non-existent ones) are painted black.

At least how many operations are required to meet the condition?

Constraints

  • 1 \leq N \leq 2000
  • S_{i, j} is . or #.

Input

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

N
S_{1, 1} S_{1, 2} \cdots S_{1, N}
S_{2, 1} S_{2, 2} \cdots S_{2, N}
\vdots
S_{N, 1} S_{N, 2} \cdots S_{N, N}

Output

Print the answer in one line.


Sample Input 1

3
###
#.#
###

Sample Output 1

1

Cell (2, 2) is painted white, and all of the four adjacent cells are painted black. By repainting cell (2, 2) black, the grid satisfies the condition.


Sample Input 2

3
...
#.#
.#.

Sample Output 2

1

By repainting cell (2, 2) black, the grid satisfies the condition.


Sample Input 3

5
#####
#....
#.###
#...#
#####

Sample Output 3

8

The grid satisfies the condition only when all the white cells are repainted black.

K - Lucky Grid

Time Limit: 3 sec / Memory Limit: 1024 MiB

問題文

各マスに数が書かれた 44 列のグリッドについて考えます。 上から i 行目、左から j 列目のマスを (i,j) とし、そこに書かれた数を a_{i,j} とします。 このとき、このグリッドが良いグリッドであるとは以下の条件が共に満たされることをいいます:

  • 各行に書かれた数が左から右に狭義単調増加している。すなわち、すべての i,j\ (1\leq i\leq N, 1\leq j< N) について a_{i,j} < a_{i,j+1}
  • 各列に書かれた数が上から下に狭義単調増加している。すなわち、すべての i,j\ (1\leq i< N, 1\leq j\leq N) について a_{i,j} < a_{i+1,j}

1\leq i,j\leq 4 に対して整数 A_{i,j} が与えられます。 以下の条件を共に満たす良いグリッドの数を 998244353 で割った余りを求めてください。

  • 各マスには 1 以上 M 以下の整数が書かれている
  • 1\leq i,j\leq 4 に対し、A_{i,j}\neq -1 ならばマス (i,j) には A_{i,j} が書かれている

制約

  • 1\leq M \leq 30
  • A_{i,j}=-1 または 1\leq A_{i,j}\leq M
  • 入力は全て整数

入力

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

M
A_{1,1} A_{1,2} A_{1,3} A_{1,4}
A_{2,1} A_{2,2} A_{2,3} A_{2,4}
A_{3,1} A_{3,2} A_{3,3} A_{3,4}
A_{4,1} A_{4,2} A_{4,3} A_{4,4}

出力

条件を満たす良いグリッドの数を 998244353 で割った余りを出力せよ。


入力例 1

10
1 2 3 4
2 -1 -1 5
3 -1 -1 7
4 5 8 9

出力例 1

2

条件を満たすグリッドは以下の 2 通りです。

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

入力例 2

30
1 1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1

出力例 2

0

入力例 3

21
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1

出力例 3

248851853

998244353 で割った余りを求めることに注意してください。

Problem Statement

Consider a 4\times 4 grid with a number written on each cell. Let (i,j) be the cell in the i-th row from the top and j-th column from the left, and a_{i,j} be the number written on it. We say this grid is good if both of the following conditions are met:

  • The numbers written on each row increase strictly monotonically from left to right. That is, a_{i,j} < a_{i,j+1} for all i,j\ (1\leq i\leq N, 1\leq j< N).
  • The numbers written on each column increase strictly monotonically from top to bottom. That is, a_{i,j} < a_{i+1,j} for all i,j\ (1\leq i< N, 1\leq j\leq N).

You are given an integer A_{i,j} for each 1\leq i,j\leq 4. Find the number, modulo 998244353, of good grids such that:

  • an integer between 1 and M, inclusive, is written on each cell, and
  • for 1\leq i,j\leq 4, if A_{i,j}\neq -1, then A_{i,j} is written on cell (i,j).

Constraints

  • 1\leq M \leq 30
  • A_{i,j}=-1 or 1\leq A_{i,j}\leq M.
  • All input values are integers.

Input

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

M
A_{1,1} A_{1,2} A_{1,3} A_{1,4}
A_{2,1} A_{2,2} A_{2,3} A_{2,4}
A_{3,1} A_{3,2} A_{3,3} A_{3,4}
A_{4,1} A_{4,2} A_{4,3} A_{4,4}

Output

Print the number, modulo 998244353, of good grids that satisfy the conditions.


Sample Input 1

10
1 2 3 4
2 -1 -1 5
3 -1 -1 7
4 5 8 9

Sample Output 1

2

Two grids satisfy the conditions:

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

Sample Input 2

30
1 1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1

Sample Output 2

0

Sample Input 3

21
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1

Sample Output 3

248851853

Be sure to find the count modulo 998244353.

L - Diameter Pairs

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N 頂点からなる木が与えられます。i 番目の辺は頂点 A_i, B_i を結んでいます。

木の 2 頂点 x, y の距離は、 xy を端点とするパスに含まれる辺数として得られる値のうち最小のものです。また、木の直径は任意の 2 頂点間の距離の最大値です。

この木の直径を D とします。頂点の組 (a, b) \ (a < b) であって、頂点 a, b の距離が D であるものの個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • 与えられるグラフは木である

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}

出力

答えを 1 行に出力せよ。


入力例 1

5
1 2
1 3
1 4
4 5

出力例 1

2

この木の直径は 3 です。距離が 3 である頂点の組として、 (2, 5), (3, 5) があります。よって 2 を出力します。


入力例 2

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

出力例 2

4

Problem Statement

You are given a tree with N vertices. The i-th edge connects vertices A_i and B_i.

The distance between two vertices x and y on the tree is the minimum possible number of edges in a path from x to y. The diameter of the tree is the maximum distance between two vertices.

Let D be the diameter of this tree. Find the number of pairs of vertices (a, b) \ (a < b) such that the distance between a and b equals D.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • The given graph is a tree.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N-1} B_{N-1}

Output

Print the answer in a single line.


Sample Input 1

5
1 2
1 3
1 4
4 5

Sample Output 1

2

The diameter of this tree is 3. The pairs of vertices distant by 3 are (2, 5) and (3, 5). Thus, 2 should be printed.


Sample Input 2

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

Sample Output 2

4
M - Median Concatenation

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。A の異なる 2 要素を選び、それらを文字列として結合して得られる N(N-1) 個(重複を含む)の整数の中央値、すなわち N(N-1)/2 番目に小さい値を求めてください。

制約

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

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
1 23 45

出力例 1

231

A=(1, 23, 45) の異なる 2 要素を選び、それらを文字列として結合して得られる整数は、 (123, 145, 231, 451, 2345, 4523)6 個です。3 番目に小さい 231 を出力します。


入力例 2

3
2 2 4

出力例 2

24

A=(2, 2, 4) の異なる 2 要素を選び、それらを文字列として結合して得られる整数は、 (22, 22, 24, 24, 42, 42)6 個です。3 番目に小さい 24 を出力します。


入力例 3

2
100000000 100000000

出力例 3

100000000100000000

答えは 32-bit 整数に収まらないことがあることに注意してください。

Problem Statement

You are given a length-N sequence of positive integers A=(A_1,A_2,\dots,A_N). Among the N(N-1) integers (including duplicates) obtained by choosing two distinct elements from A and concatenating them as strings, find the median, or the \left( N(N-1)/2 \right)-th smallest value.

Constraints

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

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

3
1 23 45

Sample Output 1

231

By choosing two distinct elements from A=(1, 23, 45) and concatenating them as strings, six integers can be obtained: (123, 145, 231, 451, 2345, 4523). Print the 3-rd smallest integer 231.


Sample Input 2

3
2 2 4

Sample Output 2

24

By choosing two distinct elements from A=(2, 2, 4) and concatenating them as strings, six integers can be obtained: (22, 22, 24, 24, 42, 42). Print the 3-rd smallest integer 24.


Sample Input 3

2
100000000 100000000

Sample Output 3

100000000100000000

Note that the answer may not fit into a 32-bit integer type.

N - Power Up

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

10^9 マス、横 10^9 マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と呼びます。
はじめ、(1, 1) に主人公がいます。主人公は現在いるマスから上下左右の隣接するマスに移動できます。主人公はマス (A, B) に移動するとパワーアップして、それ以降は現在いるマスから 8 近傍のマスに移動できるようになります。

  • ここで (i, j)8 近傍とは (i-1, j-1), (i-1, j), (i-1, j+1), (i,j-1), (i,j+1), (i+1,j-1), (i+1,j), (i+1,j+1) を意味します。(ただし存在しないマスは除く)

また、N 枚のコインがあります。コイン i はマス (C_i, D_i) にあります。主人公はコインのあるマスに移動するとコインを拾えます。
主人公は全てのコインを拾いたいです。最小で何回の移動で全てのコインを拾うことができますか?

制約

  • 1 \leq N \leq 16
  • 1 \leq A, B, C_i, D_i \leq 10^9
  • (1, 1), (A, B), (C_1, D_1), \dots, (C_N, D_N) は全て異なる
  • 入力される値は全て整数

入力

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

N A B
C_1 D_1
C_2 D_2
\vdots
C_N D_N

出力

答えを出力せよ。


入力例 1

3 5 2
4 1
1 7
6 3

出力例 1

11

主人公は次の手順で移動することで、11 回の移動により全てのコインを拾うことが出来ます。

  • 主人公ははじめ (1, 1) にいる。
  • 3 回の移動により (4, 1) にあるコインを拾う。
  • 2 回の移動により (5, 2) に着き、パワーアップする。
  • 1 回の移動により (6, 3) にあるコインを拾う。
  • 5 回の移動により (1, 7) にあるコインを拾う。

10 回以下の移動で全てのコインを拾うことはできないので、答えは 11 回になります。


入力例 2

3 500000000 500000000
1 1000000000
1000000000 1
1000000000 1000000000

出力例 2

2999999997

入力例 3

8 36 49
73 52
38 86
30 52
85 48
27 60
45 40
65 98
71 37

出力例 3

228

Problem Statement

There is a 10^9 \times 10^9 grid. Let (i, j) denote the cell in the i-th row from the top and j-th column from the left.
The hero is initially at (1, 1). He can move to one of the four adjacent cells: up, down, left, or right. Once he reaches cell (A, B), the hero powers up, after which he can move to one of the eight adjacent cells.

  • The eight cells adjacent to (i, j) are (i-1, j-1), (i-1, j), (i-1, j+1), (i,j-1), (i,j+1), (i+1,j-1), (i+1,j), (i+1,j+1) (except for non-existent ones).

Besides, there are N coins. Coin i is at cell (C_i, D_i). He can collect the coin when he reaches the cell.
He wants to collect all the coins. At least how many moves are required to collect them?

Constraints

  • 1 \leq N \leq 16
  • 1 \leq A, B, C_i, D_i \leq 10^9
  • (1, 1), (A, B), (C_1, D_1), \dots, (C_N, D_N) are pairwise distinct.
  • All input values are integers.

Input

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

N A B
C_1 D_1
C_2 D_2
\vdots
C_N D_N

Output

Print the answer.


Sample Input 1

3 5 2
4 1
1 7
6 3

Sample Output 1

11

The hero can make the following 11 moves to collect all the coins.

  • The hero is initially at (1, 1).
  • He makes three moves to collect the coin at (4, 1).
  • He makes two moves to reach (5, 2) and power up.
  • He makes one move to collect the coin at (6, 3).
  • He makes five moves to collect the coin at (1, 7).

Since he cannot collect all the coins with 10 moves or less, the answer is 11.


Sample Input 2

3 500000000 500000000
1 1000000000
1000000000 1
1000000000 1000000000

Sample Output 2

2999999997

Sample Input 3

8 36 49
73 52
38 86
30 52
85 48
27 60
45 40
65 98
71 37

Sample Output 3

228
O - Exceed Query

Time Limit: 10 sec / Memory Limit: 1024 MiB

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) があります。はじめ A の各要素は 0 です。時刻 i=1,2,\ldots,T において、A に対して以下の操作が行われます。

  • A_{L_i},A_{L_i+1},\dots,A_{R_i} にそれぞれ V_i を加える

Q 個のクエリが与えられるので順に処理してください。i 番目のクエリは以下で説明されます。

  • 整数 l_i,r_i,x_i が与えられる。A_{l_i}+A_{l_i+1}+\dots+A_{r_i}x_i 以上になる最初の時刻を出力する。ただし、時刻 T における A_{l_i}+A_{l_i+1}+\dots+A_{r_i}x_i 未満のときは -1 を出力する。

制約

  • 1\leq N\leq 10^5
  • 1\leq T,Q\leq 10^5
  • 1\leq L_i\leq R_i\leq N
  • 1\leq V_i\leq 10^5
  • 1\leq l_i\leq r_i\leq N
  • 1\leq x_i\leq 10^{15}
  • 入力は全て整数

入力

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

N T Q
L_1 R_1 V_1 
L_2 R_2 V_2
\vdots
L_T R_T V_T 
l_1 r_1 x_1 
l_2 r_2 x_2
\vdots
l_Q r_Q x_Q

出力

Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。


入力例 1

6 3 4
3 5 3
1 4 2
2 6 4
2 5 15
3 4 5
6 6 5
1 6 30

出力例 1

2
1
-1
3

数列 A は以下のように変化します。

  • 時刻 1(0,0,3,3,3,0)
  • 時刻 2(2,2,5,5,3,0)
  • 時刻 3(2,6,9,9,7,4)

各クエリに対する答えは以下のようになります。

  • クエリ 1A_2+A_3+A_4+A_5 の値が初めて 15 以上になるのは時刻 2 です。
  • クエリ 2A_4+A_5 の値が初めて 5 以上になるのは時刻 1 です。
  • クエリ 3:時刻 3 における A_6 の値が 5 未満であるため -1 を出力します。
  • クエリ 4A_1+A_2+A_3+A_4+A_5+A_6 の値が初めて 30 以上になるのは時刻 3 です。

入力例 2

100 10 10
54 77 89179
3 23 9999
35 58 58890
38 66 74496
27 49 86313
12 59 86105
23 91 9688
55 74 55468
53 61 36846
4 46 25191
23 48 6659862
6 59 2233031
38 73 9618591
65 86 801679
20 73 3689737
2 19 1320865
27 100 13177888
27 65 762017
80 96 6436
35 66 7954927

出力例 2

-1
4
9
1
4
-1
-1
1
7
6

Problem Statement

There is an integer sequence A=(A_1,A_2,\ldots,A_N) of length N. Initially, all elements of A are 0. At time i=1,2,\ldots,T, the following operation is performed against A:

  • add V_i to each of A_{L_i},A_{L_i+1},\dots, and A_{R_i}.

Given Q queries, process them in order. The i-th query is described as follows.

  • Given integers l_i,r_i, and x_i, print the first time that A_{l_i}+A_{l_i+1}+\dots+A_{r_i} becomes x_i or greater. If A_{l_i}+A_{l_i+1}+\dots+A_{r_i} is less than x_i at time T, print -1.

Constraints

  • 1\leq N\leq 10^5
  • 1\leq T,Q\leq 10^5
  • 1\leq L_i\leq R_i\leq N
  • 1\leq V_i\leq 10^5
  • 1\leq l_i\leq r_i\leq N
  • 1\leq x_i\leq 10^{15}
  • All input values are integers.

Input

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

N T Q
L_1 R_1 V_1 
L_2 R_2 V_2
\vdots
L_T R_T V_T 
l_1 r_1 x_1 
l_2 r_2 x_2
\vdots
l_Q r_Q x_Q

Output

Print Q lines. The i-th lines should contain the answer to the i-th query.


Sample Input 1

6 3 4
3 5 3
1 4 2
2 6 4
2 5 15
3 4 5
6 6 5
1 6 30

Sample Output 1

2
1
-1
3

The sequence A transitions as follows:

  • Time 1: (0,0,3,3,3,0).
  • Time 2: (2,2,5,5,3,0).
  • Time 3: (2,6,9,9,7,4).

The answer to each query is as follows.

  • Query 1: the value A_2+A_3+A_4+A_5 becomes 15 or greater for the first time at time 2.
  • Query 2: the value A_4+A_5 becomes 5 or greater for the first time at time 1.
  • Query 3: the value A_6 at time 3 is less than 5, so -1 should be printed.
  • Query 4: the value A_1+A_2+A_3+A_4+A_5+A_6 becomes 30 or greater for the first time at time 3.

Sample Input 2

100 10 10
54 77 89179
3 23 9999
35 58 58890
38 66 74496
27 49 86313
12 59 86105
23 91 9688
55 74 55468
53 61 36846
4 46 25191
23 48 6659862
6 59 2233031
38 73 9618591
65 86 801679
20 73 3689737
2 19 1320865
27 100 13177888
27 65 762017
80 96 6436
35 66 7954927

Sample Output 2

-1
4
9
1
4
-1
-1
1
7
6