A - Weather Forecast

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

配点 : 100 点

問題文

明日からの 7 日間の天気予報を表す文字列 S が与えられます。
i 日後の予報は S の i 文字目が o であるとき晴れ、x であるとき雨です。

N 日後の天気予報が晴れかどうかを教えてください。

制約

  • N は 1 以上 7 以下の整数
  • S は長さ 7 の文字列であり、o と x のみからなる

入力

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

N
S

出力

N 日後の天気予報が晴れであるとき Yes を、雨であるとき No を出力せよ。


入力例 1

4
oooxoox

出力例 1

No

明日からの 7 日間の天気予報は順に、晴れ、晴れ、晴れ、雨、晴れ、晴れ、雨です。
特に、今日から 4 日後の天気予報は雨です。


入力例 2

7
ooooooo

出力例 2

Yes

Score : 100 points

Problem Statement

You are given a string S, which represents a weather forecast for the seven days starting tomorrow.
The i-th of those seven days is forecast to be sunny if the i-th character of S is o, and rainy if that character is x.

Tell us whether the N-th day is forecast to be sunny.

Constraints

  • N is an integer between 1 and 7 (inclusive).
  • S is a string of length 7 consisting of o and x.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print Yes if the N-th of the seven days starting tomorrow is forecast to be sunny, and No if that day is forecast to be rainy.


Sample Input 1

4
oooxoox

Sample Output 1

No

The forecast for each of the seven days starting tomorrow is as follows: sunny, sunny, sunny, rainy, sunny, sunny, rainy.
In particular, the fourth day is forecast to be rainy.


Sample Input 2

7
ooooooo

Sample Output 2

Yes
B - Tires

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

配点 : 100 点

問題文

末尾が er または ist であるような文字列 S が与えられます。
S の末尾が er である場合は er を、 ist である場合は ist を出力してください。

制約

  • 2 \le |S| \le 20
  • S は英小文字のみからなる。
  • S の末尾は er または ist である。

入力

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

S

出力

答えを出力せよ。


入力例 1

atcoder

出力例 1

er

S="atcoder" の末尾は er です。


入力例 2

tourist

出力例 2

ist

入力例 3

er

出力例 3

er

Score : 100 points

Problem Statement

You are given a string S ending with er or ist.
If S ends with er, print er; if it ends with ist, print ist.

Constraints

  • 2 \le |S| \le 20
  • S consists of lowercase English letters.
  • S ends with er or ist.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

atcoder

Sample Output 1

er

S="atcoder" ends with er.


Sample Input 2

tourist

Sample Output 2

ist

Sample Input 3

er

Sample Output 3

er
C - Digit Sum

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

配点 : 200 点

問題文

正整数 n の桁和を、 n を十進法で表したときの各桁の和と定めます。例えば 2026 の桁和は 2+0+2+6=10 です。

N 以下の正整数のうち、桁和が K であるものの個数を求めてください。

制約

  • 1\leq N,K \leq 10^5
  • 入力は全て整数

入力

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

N K

出力

答えを出力せよ。


入力例 1

30 4

出力例 1

3

30 以下の正整数のうち、桁和が 4 であるものは、4,13,22 の 3 個です。


入力例 2

2026 10

出力例 2

121

入力例 3

99999 45

出力例 3

1

Score : 200 points

Problem Statement

The digit sum of a positive integer n is defined as the sum of the digits when n is represented in decimal. For example, the digit sum of 2026 is 2+0+2+6=10.

Find the number of positive integers not exceeding N whose digit sum is K.

Constraints

  • 1\leq N,K \leq 10^5
  • All input values are integers.

Input

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

N K

Output

Output the answer.


Sample Input 1

30 4

Sample Output 1

3

Among the positive integers not exceeding 30, there are three integers whose digit sum is 4: 4,13,22.


Sample Input 2

2026 10

Sample Output 2

121

Sample Input 3

99999 45

Sample Output 3

1
D - Pick Two

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

配点 : 200 点

問題文

一直線状の倉庫があります。この倉庫に 偶数個 の荷物が保管されています。
倉庫の情報は文字列 S として与えらます。
倉庫は区画 1,2,\dots,|S| の |S| 個の区画からなり、 S の各文字は以下の情報を表します。

  • S の i 文字目が # であるとき、区画 i に荷物が 1 つ置かれている。
  • S の i 文字目が . であるとき、区画 i に荷物が置かれていない。

この倉庫にはロボットがあり、このロボットは以下の行動を倉庫から荷物が無くなるまで繰り返します。

  • 荷物をその荷物がある区画番号の小さい方から 2 つ回収する。回収された荷物は倉庫の外へ運び出される。

繰り返しの各回で運び出された 2 つの荷物が倉庫のどの区画にあったものかを求めてください。

制約

  • S は # と . からなる長さ 2 以上 1000 以下の文字列
  • 倉庫には偶数個の荷物が保管されている
  • 倉庫には 2 つ以上の荷物が保管されている

入力

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

S

出力

倉庫に保管されている荷物の総数を m とすると、 m/2 行出力せよ。
そのうち i 行目には、 i 回目の繰り返しで運び出された荷物が元々あった区画の番号を小さい方から順に カンマ区切り で出力せよ。


入力例 1

.#.##..##.#.###....#

出力例 1

2,4
5,8
9,11
13,14
15,20

区画 2,4,5,8,9,11,13,14,15,20 に荷物が保管されています。

  • 1 回目の繰り返しでは区画 2,4 にあった荷物が運び出されます。
  • 2 回目の繰り返しでは区画 5,8 にあった荷物が運び出されます。
  • 3 回目の繰り返しでは区画 9,11 にあった荷物が運び出されます。
  • 4 回目の繰り返しでは区画 13,14 にあった荷物が運び出されます。
  • 5 回目の繰り返しでは区画 15,20 にあった荷物が運び出されます。

Score : 200 points

Problem Statement

There is a linear warehouse, which stores an even number of packages.
The layout of the warehouse is represented by a string S.
The warehouse has |S| sections, numbered 1,2,\dots,|S|, and the string S describes them as follows:

  • When the i-th character of S is #, one package is placed in section i.
  • When the i-th character of S is ., no package is placed in section i.

The warehouse has a robot, which repeats the following action until there are no more packages in the warehouse:

  • Collect two packages from the sections with the smallest section numbers among the sections containing packages. The collected packages are carried out of the warehouse.

For each iteration, determine which sections of the warehouse the two packages that were carried out were originally located.

Constraints

  • S is a string consisting of # and . with length between 2 and 1000, inclusive.
  • The warehouse stores an even number of packages.
  • The warehouse stores at least two packages.

Input

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

S

Output

Let m be the total number of packages stored in the warehouse. Output m/2 lines.
The i-th line should contain the section numbers where the packages carried out in the i-th iteration were originally located, in ascending order, separated by a comma.


Sample Input 1

.#.##..##.#.###....#

Sample Output 1

2,4
5,8
9,11
13,14
15,20

Packages are stored in sections 2,4,5,8,9,11,13,14,15,20.

  • In the 1st iteration, packages from sections 2,4 are carried out.
  • In the 2nd iteration, packages from sections 5,8 are carried out.
  • In the 3rd iteration, packages from sections 9,11 are carried out.
  • In the 4th iteration, packages from sections 13,14 are carried out.
  • In the 5th iteration, packages from sections 15,20 are carried out.
E - Ladder Takahashi

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

配点 : 300 点

問題文

10^9 階建てのビルがあり、N 本のはしごがかかっています。
ビルの 1 階にいる高橋君ははしごを繰り返し使って(0 回でもよい)できるだけ高い階へ上りたいと考えています。
はしごには 1 から N までの番号がついており、はしご i は A_i 階と B_i 階を結んでいます。はしご i を利用すると A_i 階から B_i 階へ、または B_i 階から A_i 階へ双方向に移動することができますが、それ以外の階の間の移動は行うことはできません。
また、高橋君は同じ階での移動は自由に行うことができますが、はしご以外の方法で他の階へ移動することはできません。
高橋君は最高で何階へ上ることができますか?

制約

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

入力

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

N
A_1 B_1
A_2 B_2
\ldots
A_N B_N

出力

答えを出力せよ。


入力例 1

4
1 4
4 3
4 10
8 3

出力例 1

10

はしご 1 で 4 階に進み、はしご 3 で 10 階に進むことにより、10 階にたどり着くことができます。


入力例 2

6
1 3
1 5
1 12
3 5
3 12
5 12

出力例 2

12

入力例 3

3
500000000 600000000
600000000 700000000
700000000 800000000

出力例 3

1

他の階への移動ができない場合もあります。

Score : 300 points

Problem Statement

There is a 10^9-story building with N ladders.
Takahashi, who is on the 1-st (lowest) floor, wants to reach the highest floor possible by using ladders (possibly none).
The ladders are numbered from 1 to N, and ladder i connects the A_i-th and B_i-th floors. One can use ladder i in either direction to move from the A_i-th floor to the B_i-th floor or vice versa, but not between other floors.
Takahashi can freely move within the same floor, but cannot move between floors without using ladders.
What is the highest floor Takahashi can reach?

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 10^9
  • A_i \neq B_i
  • All values in the input are integers.

Input

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

N
A_1 B_1
A_2 B_2
\ldots
A_N B_N

Output

Print an integer representing the answer.


Sample Input 1

4
1 4
4 3
4 10
8 3

Sample Output 1

10

He can reach the 10-th floor by using ladder 1 to get to the 4-th floor and then ladder 3 to get to the 10-th floor.


Sample Input 2

6
1 3
1 5
1 12
3 5
3 12
5 12

Sample Output 2

12

Sample Input 3

3
500000000 600000000
600000000 700000000
700000000 800000000

Sample Output 3

1

He may be unable to move between floors.

F - Dash

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

配点 : 300 点

問題文

二次元平面の点 (0,0) に高橋君がいます。初め、高橋君の体力は H です。また、二次元平面には M 個の体力を回復するアイテムがあり、i 個目のアイテムは点 (x_i,y_i) に置いてあります。

高橋君は、これから N 回の移動をします。i 回目の移動は以下の方法で行われます。

  1. 今高橋君がいる点を (x,y) とする。体力を 1 消費し、S の i 番目の文字 S_i に応じて以下の点に移動する。

    • S_i が R のとき: (x+1,y)
    • S_i が L のとき: (x-1,y)
    • S_i が U のとき: (x,y+1)
    • S_i が D のとき: (x,y-1)
  2. 高橋君の体力が負になった場合、高橋君は倒れてしまい、移動をやめる。そうでない場合、移動した点にアイテムがあり、かつ高橋君の体力が K 未満ならば、移動した点に置かれたアイテムを消費し、高橋君の体力が K になる。

高橋君が一度も倒れることなく N 回の移動を行えるか判定してください。

制約

  • 1\leq N,M,H,K\leq 2\times 10^5
  • S は R, L, U, D からなる長さ N の文字列
  • |x_i|,|y_i| \leq 2\times 10^5
  • (x_i,y_i) は互いに異なる
  • S 以外の入力は全て整数

入力

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

N M H K
S
x_1 y_1
\vdots
x_M y_M

出力

高橋君が一度も倒れることなく N 回の移動を行える場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

4 2 3 1
RUDL
-1 -1
1 0

出力例 1

Yes

初め高橋君の体力は 3 です。以下で移動を説明します。

  • 1 回目の移動: S_i が R なので点 (1,0) に移動する。高橋君の体力は 2 に減る。点 (1,0) にはアイテムが置いてあるが、高橋君の体力は K=1 以上なのでアイテムは消費されない。

  • 2 回目の移動: S_i が U なので点 (1,1) に移動する。高橋君の体力は 1 に減る。

  • 3 回目の移動: S_i が D なので点 (1,0) に移動する。高橋君の体力は 0 に減る。点 (1,0) にはアイテムが置いてあり、体力は K=1 未満なのでアイテムを消費し、体力が 1 になる。

  • 4 回目の移動: S_i が L なので点 (0,0) に移動する。高橋君の体力は 0 に減る。

以上より、高橋君は倒れずに 4 回の移動を行えるので、Yes を出力してください。体力は 0 になってもいいことに注意してください。


入力例 2

5 2 1 5
LDRLD
0 0
-1 -1

出力例 2

No

初め高橋君の体力は 1 です。以下で移動を説明します。

  • 1 回目の移動: S_i が L なので点 (-1,0) に移動する。高橋君の体力は 0 に減る。

  • 2 回目の移動: S_i が D なので点 (-1,-1) に移動する。高橋君の体力は -1 に減る。体力が -1 になってしまったので、高橋君は倒れてしまい、移動をやめる。

以上より、高橋君は倒れてしまうので、No を出力してください。

高橋君がはじめいる点 (0,0) にはアイテムが置いてありますが、移動後にアイテムは消費されるので、1 回目の移動前にアイテムを消費しないことに注意してください。

Score : 300 points

Problem Statement

On a two-dimensional plane, Takahashi is initially at point (0, 0), and his initial health is H. M items to recover health are placed on the plane; the i-th of them is placed at (x_i,y_i).

Takahashi will make N moves. The i-th move is as follows.

  1. Let (x,y) be his current coordinates. He consumes a health of 1 to move to the following point, depending on S_i, the i-th character of S:

    • (x+1,y) if S_i is R;
    • (x-1,y) if S_i is L;
    • (x,y+1) if S_i is U;
    • (x,y-1) if S_i is D.
  2. If Takahashi's health has become negative, he collapses and stops moving. Otherwise, if an item is placed at the point he has moved to, and his health is strictly less than K, then he consumes the item there to make his health K.

Determine if Takahashi can complete the N moves without being stunned.

Constraints

  • 1\leq N,M,H,K\leq 2\times 10^5
  • S is a string of length N consisting of R, L, U, and D.
  • |x_i|,|y_i| \leq 2\times 10^5
  • (x_i, y_i) are pairwise distinct.
  • All values in the input are integers, except for S.

Input

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

N M H K
S
x_1 y_1
\vdots
x_M y_M

Output

Print Yes if he can complete the N moves without being stunned; print No otherwise.


Sample Input 1

4 2 3 1
RUDL
-1 -1
1 0

Sample Output 1

Yes

Initially, Takahashi's health is 3. We describe the moves below.

  • 1-st move: S_i is R, so he moves to point (1,0). His health reduces to 2. Although an item is placed at point (1,0), he do not consume it because his health is no less than K=1.

  • 2-nd move: S_i is U, so he moves to point (1,1). His health reduces to 1.

  • 3-rd move: S_i is D, so he moves to point (1,0). His health reduces to 0. An item is placed at point (1,0), and his health is less than K=1, so he consumes the item to make his health 1.

  • 4-th move: S_i is L, so he moves to point (0,0). His health reduces to 0.

Thus, he can make the 4 moves without collapsing, so Yes should be printed. Note that the health may reach 0.


Sample Input 2

5 2 1 5
LDRLD
0 0
-1 -1

Sample Output 2

No

Initially, Takahashi's health is 1. We describe the moves below.

  • 1-st move: S_i is L, so he moves to point (-1,0). His health reduces to 0.

  • 2-nd move: S_i is D, so he moves to point (-1,-1). His health reduces to -1. Now that the health is -1, he collapses and stops moving.

Thus, he will be stunned, so No should be printed.

Note that although there is an item at his initial point (0,0), he does not consume it before the 1-st move, because items are only consumed after a move.

G - Unicyclic Components

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

配点 : 400 点

問題文

頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結んでいます。

このグラフのすべての連結成分が次の条件を満たすかどうかを判定してください。

  • その連結成分に含まれる頂点の個数と辺の本数が等しい。

注釈

無向グラフ とは、辺に向きの無いグラフのことをいいます。
あるグラフの 部分グラフ とは、元のグラフのいくつかの頂点といくつかの辺を選んでできるグラフのことをいいます。
グラフが 連結 であるとは、グラフに含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
連結成分 とは、連結な部分グラフのうち、そのグラフを含んだより大きい連結な部分グラフが存在しないものをいいます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i \leq v_i \leq N
  • 入力はすべて整数

入力

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

N M
u_1 v_1
\vdots
u_M v_M

出力

すべての連結成分が条件を満たすならば Yes と、そうでなければ No と出力せよ。


入力例 1

3 3
2 3
1 1
2 3

出力例 1

Yes

このグラフには頂点 1 のみからなる連結成分と頂点 2,3 からなる連結成分があります。
前者には 1 本の辺(辺 2 )が、後者には 2 本の辺(辺 1,3 )が含まれており、条件を満たします。


入力例 2

5 5
1 2
2 3
3 4
3 5
1 5

出力例 2

Yes

入力例 3

13 16
7 9
7 11
3 8
1 13
11 11
6 11
8 13
2 11
3 3
8 12
9 11
1 11
5 13
3 12
6 9
1 10

出力例 3

No

Score : 400 points

Problem Statement

You are given an undirected graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i connects vertex u_i and vertex v_i.

Determine whether every connected component in this graph satisfies the following condition.

  • The connected component has the same number of vertices and edges.

Notes

An undirected graph is a graph with edges without direction.
A subgraph of a graph is a graph formed from a subset of vertices and edges of that graph.
A graph is connected when one can travel between every pair of vertices in the graph via edges.
A connected component is a connected subgraph that is not part of any larger connected subgraph.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i \leq v_i \leq N
  • All values in the input are integers.

Input

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

N M
u_1 v_1
\vdots
u_M v_M

Output

If every connected component satisfies the condition, print Yes; otherwise, print No.


Sample Input 1

3 3
2 3
1 1
2 3

Sample Output 1

Yes

The graph has a connected component formed from just vertex 1, and another formed from vertices 2 and 3.
The former has one edge (edge 2), and the latter has two edges (edges 1 and 3), satisfying the condition.


Sample Input 2

5 5
1 2
2 3
3 4
3 5
1 5

Sample Output 2

Yes

Sample Input 3

13 16
7 9
7 11
3 8
1 13
11 11
6 11
8 13
2 11
3 3
8 12
9 11
1 11
5 13
3 12
6 9
1 10

Sample Output 3

No
H - Farthest Vertex

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

配点 : 475 点

問題文

頂点に 1 から N の番号がついた N 頂点の木があります。i 番目の辺は頂点 A_i と頂点 B_i を結ぶ辺です。
頂点 u と頂点 v の距離を、頂点 u と頂点 v を両端点とするパスに含まれる辺の本数として定義します。(このパスは一意に定まります)

v = 1, 2, \dots, N について次の問題を解いてください。

  • 頂点 1, 2, \dots, N のうち頂点 v からの距離が最大となる頂点の番号を出力してください。ただし、条件を満たす頂点が複数存在する場合は 最も番号が大きい頂点 を出力してください。

制約

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \lt B_i \leq N
  • 入力で与えられるグラフは木
  • 入力される値は全て整数

入力

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

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

出力

N 行出力せよ。i 行目には v=i の時の答えを出力せよ。


入力例 1

3
1 2
2 3

出力例 1

3
3
1

頂点 1 からの距離が最大となる点は頂点 3 です。
頂点 2 からの距離が最大となる点は頂点 1 および頂点 3 です。このうち番号が大きい頂点である頂点 3 が答えとなります。
頂点 3 からの距離が最大となる点は頂点 1 です。


入力例 2

5
1 2
2 3
2 4
1 5

出力例 2

4
5
5
5
4

Score : 475 points

Problem Statement

There is a tree with N vertices numbered 1 to N. The i-th edge connects vertices A_i and B_i.
Define the distance between vertices u and v as the number of edges in the path with endpoints at vertices u and v. (This path is uniquely determined.)

Solve the following problem for v = 1, 2, \dots, N.

  • Among vertices 1, 2, \dots, N, output the number of the vertex that has the maximum distance from vertex v. If there are multiple vertices that satisfy the condition, output the vertex with the largest number.

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \lt B_i \leq N
  • The graph given in the input is a tree.
  • All input values are integers.

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

Output N lines. The i-th line should contain the answer for v=i.


Sample Input 1

3
1 2
2 3

Sample Output 1

3
3
1

The vertex with the maximum distance from vertex 1 is vertex 3.
The vertices with the maximum distance from vertex 2 are vertices 1 and 3. Among them, vertex 3, which has the larger number, is the answer.
The vertex with the maximum distance from vertex 3 is vertex 1.


Sample Input 2

5
1 2
2 3
2 4
1 5

Sample Output 2

4
5
5
5
4
I - Pre-order and In-order

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

配点 : 500 点

問題文

1, 2, \ldots, N と番号づけられた N 個の頂点を持つ二分木を考えます。 ここで、二分木とは各頂点が高々 2 個の子を持つ根付き木です。より具体的には、二分木の各頂点は高々 1 個の左の子と高々 1 個の右の子を持ちます。

頂点 1 を根とする二分木であって、下記の条件を満たすものが存在するかを判定し、存在する場合はその一例を示してください。

  • すべての頂点を深さ優先探索における行きがけ順(pre-order)で並べた列が (P_1, P_2, \ldots, P_N) である。
  • すべての頂点を深さ優先探索における通りがけ順(in-order)で並べた列が (I_1, I_2, \ldots, I_N) である。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N は整数
  • (P_1, P_2, \ldots, P_N) は (1, 2, \ldots, N) の順列
  • (I_1, I_2, \ldots, I_N) は (1, 2, \ldots, N) の順列

入力

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

N
P_1 P_2 \ldots P_N
I_1 I_2 \ldots I_N

出力

問題文中の条件を満たすような頂点 1 を根とする二分木が存在しない場合は -1 を出力せよ。
存在する場合は、条件を満たす二分木の一例を下記の形式にしたがって N 行にわたって出力せよ。 すなわち、i = 1, 2, \ldots, N について、i 行目には頂点 i の左の子の番号 L_i と右の子の番号 R_i を出力せよ。 ただし、左の子(または右の子)を持たない場合は L_i(または R_i )として 0 を出力せよ。
条件を満たすような頂点 1 を根とする二分木が複数存在する場合は、そのうちどれを出力しても正解となる。

L_1 R_1
L_2 R_2
\vdots
L_N R_N

入力例 1

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

出力例 1

3 6
0 0
0 5
0 0
0 0
4 2

次の画像に示す、頂点 1 を根とする二分木が問題文中の条件を満たします。


入力例 2

2
2 1
1 2

出力例 2

-1

問題文中の条件を満たすような頂点 1 を根とする二分木は存在しません。よって -1 を出力します。

Score : 500 points

Problem Statement

Consider a binary tree with N vertices numbered 1, 2, \ldots, N. Here, a binary tree is a rooted tree where each vertex has at most two children. Specifically, each vertex in a binary tree has at most one left child and at most one right child.

Determine whether there exists a binary tree rooted at Vertex 1 satisfying the conditions below, and present such a tree if it exists.

  • The depth-first traversal of the tree in pre-order is (P_1, P_2, \ldots, P_N).
  • The depth-first traversal of the tree in in-order is (I_1, I_2, \ldots, I_N).

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N is an integer.
  • (P_1, P_2, \ldots, P_N) is a permutation of (1, 2, \ldots, N).
  • (I_1, I_2, \ldots, I_N) is a permutation of (1, 2, \ldots, N).

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N
I_1 I_2 \ldots I_N

Output

If there is no binary tree rooted at Vertex 1 satisfying the conditions in Problem Statement, print -1.
Otherwise, print one such tree in N lines as follows. For each i = 1, 2, \ldots, N, the i-th line should contain L_i and R_i, the indices of the left and right children of Vertex i, respectively. Here, if Vertex i has no left (right) child, L_i (R_i) should be 0.
If there are multiple binary trees rooted at Vertex 1 satisfying the conditions, any of them will be accepted.

L_1 R_1
L_2 R_2
\vdots
L_N R_N

Sample Input 1

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

Sample Output 1

3 6
0 0
0 5
0 0
0 0
4 2

The binary tree rooted at Vertex 1 shown in the following image satisfies the conditions.


Sample Input 2

2
2 1
1 2

Sample Output 2

-1

No binary tree rooted at Vertex 1 satisfies the conditions, so -1 should be printed.