A - New Generation ABC

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

配点 : 100

問題文

AtCoder Beginner Contest は、今回で 214 回目の開催となりました。

今までの AtCoder Beginner Contest において、出題される問題数は次のように変化しました。

  • 1 回目から 125 回目までは 4
  • 126 回目から 211 回目までは 6
  • 212 回目から 214 回目までは 8

N 回目の AtCoder Beginner Contest において出題された問題数を求めてください。

制約

  • 1 \leq N \leq 214
  • 入力は全て整数である。

入力

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

N

出力

答えを出力せよ。


入力例 1

214

出力例 1

8

入力例 2

1

出力例 2

4

入力例 3

126

出力例 3

6

Score : 100 points

Problem Statement

This is the 214-th AtCoder Beginner Contest (ABC).

The ABCs so far have had the following number of problems.

  • The 1-st through 125-th ABCs had 4 problems each.
  • The 126-th through 211-th ABCs had 6 problems each.
  • The 212-th through 214-th ABCs have 8 problems each.

Find the number of problems in the N-th ABC.

Constraints

  • 1 \leq N \leq 214
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

214

Sample Output 1

8

Sample Input 2

1

Sample Output 2

4

Sample Input 3

126

Sample Output 3

6
B - Weather Forecast

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

配点 : 100

問題文

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

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

制約

  • N1 以上 7 以下の整数
  • S は長さ 7 の文字列であり、ox のみからなる

入力

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

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
C - Count Distinct Integers

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

配点 : 200

問題文

長さ N の正整数列 a = (a_1, a_2, \dots, a_N) には何種類の整数が現れますか?

制約

  • 1 \leq N \leq 1000
  • 1 \leq a_i \leq 10^9 \, (1 \leq i \leq N)
  • 入力は全て整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

6
1 4 1 2 2 1

出力例 1

3

1, 2, 43 種類の整数が現れます。


入力例 2

1
1

出力例 2

1

入力例 3

11
3 1 4 1 5 9 2 6 5 3 5

出力例 3

7

Score : 200 points

Problem Statement

In a sequence of N positive integers a = (a_1, a_2, \dots, a_N), how many different integers are there?

Constraints

  • 1 \leq N \leq 1000
  • 1 \leq a_i \leq 10^9 \, (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

6
1 4 1 2 2 1

Sample Output 1

3

There are three different integers: 1, 2, 4.


Sample Input 2

1
1

Sample Output 2

1

Sample Input 3

11
3 1 4 1 5 9 2 6 5 3 5

Sample Output 3

7
D - Same Name

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

配点 : 200

問題文

N 人の人がいます。i \, (1 \leq i \leq N) 人目の人の姓は S_i、名は T_i です。

同姓同名であるような人の組が存在するか、すなわち 1 \leq i \lt j \leq N かつ S_i=S_j かつ T_i=T_j を満たすような整数対 (i,j) が存在するか判定してください。

制約

  • 2 \leq N \leq 1000
  • N は整数
  • S_i,T_i は英小文字のみからなる長さ 1 以上 10 以下の文字列

入力

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

N
S_1 T_1
S_2 T_2
\hspace{0.6cm}\vdots
S_N T_N

出力

同姓同名であるような人の組が存在するなら Yes を、存在しないなら No を出力せよ。


入力例 1

3
tanaka taro
sato hanako
tanaka taro

出力例 1

Yes

1 人目の人と 3 人目の人が同姓同名です。


入力例 2

3
saito ichiro
saito jiro
saito saburo

出力例 2

No

同姓同名であるような人の組は存在しません。


入力例 3

4
sypdgidop bkseq
bajsqz hh
ozjekw mcybmtt
qfeysvw dbo

出力例 3

No

Score : 200 points

Problem Statement

There are N people. The family name and given name of the i-th person (1 \leq i \leq N) are S_i and T_i, respectively.

Determine whether there is a pair of people with the same family and given names. In other words, determine whether there is a pair of integers (i,j) such that 1 \leq i \lt j \leq N, S_i=S_j, and T_i=T_j.

Constraints

  • 2 \leq N \leq 1000
  • N is an integer.
  • Each of S_i and T_i is a string of length between 1 and 10 (inclusive) consisting of English lowercase letters.

Input

Input is given from Standard Input in the following format:

N
S_1 T_1
S_2 T_2
\hspace{0.6cm}\vdots
S_N T_N

Output

If there is a pair of people with the same family and given names, print Yes; otherwise, print No.


Sample Input 1

3
tanaka taro
sato hanako
tanaka taro

Sample Output 1

Yes

The first and third persons have the same family and given names.


Sample Input 2

3
saito ichiro
saito jiro
saito saburo

Sample Output 2

No

No two persons have the same family and given names.


Sample Input 3

4
sypdgidop bkseq
bajsqz hh
ozjekw mcybmtt
qfeysvw dbo

Sample Output 3

No
E - String Delimiter

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

配点 : 300

問題文

英小文字、," からなる長さ N の文字列 S が与えられます。S に含まれる " の個数は偶数であることが保証されています。

S に含まれる " の個数を 2K 個とすると、各 i=1,2,\ldots,K について 2i-1 番目の " から 2i 番目の " までの文字のことを 括られた文字 と呼びます。

あなたの仕事は、 S に含まれる , のうち、括られた文字 でないもの. で置き換えて得られる文字列を答えることです。

制約

  • N1 以上 2\times 10^5 以下の整数
  • S は英小文字、," からなる長さ N の文字列
  • S に含まれる " の個数は偶数

入力

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

N
S

出力

答えを出力せよ。


入力例 1

8
"a,b"c,d

出力例 1

"a,b"c.d

S のうち "a,b" が括られた文字であり、c,d は括られた文字ではありません。

S に含まれる , のうち、括られた文字でないのは S の左から 7 番目の文字なので、7 番目の文字を . で置き換えたものが答えとなります。


入力例 2

5
,,,,,

出力例 2

.....

入力例 3

20
a,"t,"c,"o,"d,"e,"r,

出力例 3

a."t,"c."o,"d."e,"r.

Score : 300 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, ,, and ". It is guaranteed that S contains an even number of ".

Let 2K be the number of " in S. For each i=1,2,\ldots,K, the characters from the (2i-1)-th " through the (2i)-th " are said to be enclosed.

Your task is to replace each , in S that is not an enclosed character with . and print the resulting string.

Constraints

  • N is an integer between 1 and 2\times 10^5, inclusive.
  • S is a string of length N consisting of lowercase English letters, ,, and ".
  • S contains an even number of ".

Input

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

N
S

Output

Print the answer.


Sample Input 1

8
"a,b"c,d

Sample Output 1

"a,b"c.d

In S, "a,b" are enclosed characters, and c,d are not.

The , in S that is not an enclosed character is the seventh character from the left in S, so replace that character with . to get the answer.


Sample Input 2

5
,,,,,

Sample Output 2

.....

Sample Input 3

20
a,"t,"c,"o,"d,"e,"r,

Sample Output 3

a."t,"c."o,"d."e,"r.
F - Don’t be cycle

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

配点 : 300

問題文

N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1 から N の番号がついており、i 番目の辺は頂点 A_i と頂点 B_i を結んでいます。 このグラフから 0 本以上のいくつかの辺を削除してグラフが閉路を持たないようにするとき、削除する辺の本数の最小値を求めてください。

単純無向グラフとは 単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。

閉路とは 単純無向グラフが閉路を持つとは、i \neq j ならば v_i \neq v_j を満たす長さ 3 以上の頂点列 (v_0, v_1, \ldots, v_{n-1}) であって、各 0 \leq i < n に対し v_iv_{i+1 \bmod n} の間に辺が存在するものがあることをいいます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • 与えられるグラフは単純
  • 入力はすべて整数

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

2

頂点 1 と頂点 2 を結ぶ辺・頂点 4 と頂点 5 を結ぶ辺の 2 本を削除するなどの方法でグラフが閉路を持たないようにすることができます。
1 本以下の辺の削除でグラフが閉路を持たないようにすることはできないので、2 を出力します。


入力例 2

4 2
1 2
3 4

出力例 2

0

入力例 3

5 3
1 2
1 3
2 3

出力例 3

1

Score : 300 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects vertex A_i and vertex B_i. Let us delete zero or more edges to remove cycles from the graph. Find the minimum number of edges that must be deleted for this purpose.

What is a simple undirected graph? A simple undirected graph is a graph without self-loops or multi-edges whose edges have no direction.

What is a cycle? A cycle in a simple undirected graph is a sequence of vertices (v_0, v_1, \ldots, v_{n-1}) of length at least 3 satisfying v_i \neq v_j if i \neq j such that for each 0 \leq i < n, there is an edge between v_i and v_{i+1 \bmod n}.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • The given graph is simple.
  • All values in the input are integers.

Input

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

2

One way to remove cycles from the graph is to delete the two edges between vertex 1 and vertex 2 and between vertex 4 and vertex 5.
There is no way to remove cycles from the graph by deleting one or fewer edges, so you should print 2.


Sample Input 2

4 2
1 2
3 4

Sample Output 2

0

Sample Input 3

5 3
1 2
1 3
2 3

Sample Output 3

1
G - Union of Interval

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

配点 : 400

問題文

実数 L,R に対して、L 以上 R 未満からなる実数の集合を [L,R) と表します。このような形で表される集合を右半開区間といいます。

N 個の右半開区間 [L_i,R_i) が与えられます。これらの和集合を S とします。S を最小の個数の右半開区間の和集合として表してください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq L_i \lt R_i \leq 2\times 10^5
  • 入力に含まれる値は全て整数である

入力

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

N
L_1 R_1
\vdots
L_N R_N

出力

S が最小で k 個の右半開区間の和集合で表せるとする。そのような k 個の右半開区間 [X_i,Y_i)X_i の昇順で以下のように k 行出力せよ。

X_1 Y_1
\vdots
X_k Y_k

入力例 1

3
10 20
20 30
40 50

出力例 1

10 30
40 50

3 つの右半開区間 [10,20),[20,30),[40,50) の和集合は 2 つの右半開区間 [10,30),[40,50) の和集合と等しくなります。


入力例 2

3
10 40
30 60
20 50

出力例 2

10 60

3 つの右半開区間 [10,40),[30,60),[20,50) の和集合は 1 つの右半開区間 [10,60) と等しくなります。

Score : 400 points

Problem Statement

For real numbers L and R, let us denote by [L,R) the set of real numbers greater than or equal to L and less than R. Such a set is called a right half-open interval.

You are given N right half-open intervals [L_i,R_i). Let S be their union. Represent S as a union of the minimum number of right half-open intervals.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq L_i \lt R_i \leq 2\times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
\vdots
L_N R_N

Output

Let k be the minimum number of right half-open intervals needed to represent S as their union. Print k lines containing such k right half-open intervals [X_i,Y_i) in ascending order of X_i, as follows:

X_1 Y_1
\vdots
X_k Y_k

Sample Input 1

3
10 20
20 30
40 50

Sample Output 1

10 30
40 50

The union of the three right half-open intervals [10,20),[20,30),[40,50) equals the union of two right half-open intervals [10,30),[40,50).


Sample Input 2

3
10 40
30 60
20 50

Sample Output 2

10 60

The union of the three right half-open intervals [10,40),[30,60),[20,50) equals the union of one right half-open interval [10,60).

H - Last Train

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

配点 : 450

問題文

AtCoder 国には駅 1,2,\ldots,NN 個の駅があります。

AtCoder 国に存在する電車の情報が M 個与えられます。 i 番目 (1\leq i\leq M) の情報は正整数の 6 つ組 (l _ i,d _ i,k _ i,c _ i,A _ i,B _ i) で表され、次のような情報に対応しています。

  • t=l _ i,l _ i+d _ i,l _ i+2d _ i,\ldots,l _ i+(k _ i-1)d _ i それぞれについて、次のような電車が存在する。
    • 時刻 t に 駅 A _ i を出発し、時刻 t+c _ i に駅 B _ i に到着する。

これらの情報にあてはまらない電車は存在せず、電車以外の方法である駅から異なる駅へ移動することはできません。
また、乗り換えにかかる時間は無視できるとします。

S から駅 N に到着できる最終時刻を f(S) とします。
より厳密には、整数の 4 つ組の列 \big((t _ i,c _ i,A _ i,B _ i)\big) _ {i=1,2,\ldots,k} であって、次のすべての条件を満たすものが存在するような t の最大値を f(S) とします。

  • t\leq t _ 1
  • A _ 1=S,B _ k=N
  • すべての 1\leq i\lt k について、B _ i=A _ {i+1}
  • すべての 1\leq i\leq k について、時刻 t _ i に駅 A _ i を出発して時刻 t _ i+c _ i に駅 B _ i に到着する電車が存在する。
  • すべての 1\leq i\lt k について、t _ i+c _ i\leq t _ {i+1}

ただし、そのような t が存在しないとき f(S)=-\infty とします。

f(1),f(2),\ldots,f(N-1) を求めてください。

制約

  • 2\leq N\leq2\times10 ^ 5
  • 1\leq M\leq2\times10 ^ 5
  • 1\leq l _ i,d _ i,k _ i,c _ i\leq10 ^ 9\ (1\leq i\leq M)
  • 1\leq A _ i,B _ i\leq N\ (1\leq i\leq M)
  • A _ i\neq B _ i\ (1\leq i\leq M)
  • 入力はすべて整数

入力

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

N M
l _ 1 d _ 1 k _ 1 c _ 1 A _ 1 B _ 1
l _ 2 d _ 2 k _ 2 c _ 2 A _ 2 B _ 2
\vdots
l _ M d _ M k _ M c _ M A _ M B _ M

出力

N-1 行出力せよ。 k 行目には f(k)\neq-\infty ならば f(k) を、f(k)=-\infty ならば Unreachable を出力せよ。


入力例 1

6 7
10 5 10 3 1 3
13 5 10 2 3 4
15 5 10 7 4 6
3 10 2 4 2 5
7 10 2 3 5 6
5 3 18 2 2 3
6 3 20 4 2 1

出力例 1

55
56
58
60
17

AtCoder 国に走っている電車は以下の図のようになります(発着時間の情報は図には含まれていません)。

2 から駅 6 に到着できる最終時刻について考えます。 次の図のように、駅 2 を時刻 56 に出発して駅 2\rightarrow3\rightarrow4\rightarrow6 のように移動することで駅 6 に到着することができます。

2 を時刻 56 より遅く出発して駅 6 に到着することはできないため、f(2)=56 です。


入力例 2

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

出力例 2

1000000000000000000
Unreachable
1
Unreachable

1 を時刻 10 ^ {18} に出発して駅 5 に時刻 10 ^ {18}+10 ^ 9 に到着するような電車が存在します。それ以降に駅 1 を出発する電車はないため、f(1)=10 ^ {18} です。 このように、答えが 32\operatorname{bit} 整数におさまらない場合もあります。

また、時刻 14 に駅 2 を出発して時刻 20 に駅 3 に到着するような電車は 2 番目の情報と 3 番目の情報の両方で存在が保証されています。 このように、複数の情報に重複している電車もあります。


入力例 3

16 20
4018 9698 2850 3026 8 11
2310 7571 7732 1862 13 14
2440 2121 20 1849 11 16
2560 5115 190 3655 5 16
1936 6664 39 8822 4 16
7597 8325 20 7576 12 5
5396 1088 540 7765 15 1
3226 88 6988 2504 13 5
1838 7490 63 4098 8 3
1456 5042 4 2815 14 7
3762 6803 5054 6994 10 9
9526 6001 61 8025 7 8
5176 6747 107 3403 1 5
2014 5533 2031 8127 8 11
8102 5878 58 9548 9 10
3788 174 3088 5950 3 13
7778 5389 100 9003 10 15
556 9425 9458 109 3 11
5725 7937 10 3282 2 9
6951 7211 8590 1994 15 12

出力例 3

720358
77158
540926
255168
969295
Unreachable
369586
466218
343148
541289
42739
165772
618082
16582
591828

Score: 450 points

Problem Statement

In the country of AtCoder, there are N stations: station 1, station 2, \ldots, station N.

You are given M pieces of information about trains in the country. The i-th piece of information (1\leq i\leq M) is represented by a tuple of six positive integers (l _ i,d _ i,k _ i,c _ i,A _ i,B _ i), which corresponds to the following information:

  • For each t=l _ i,l _ i+d _ i,l _ i+2d _ i,\ldots,l _ i+(k _ i-1)d _ i, there is a train as follows:
    • The train departs from station A _ i at time t and arrives at station B _ i at time t+c _ i.

No trains exist other than those described by this information, and it is impossible to move from one station to another by any means other than by train.
Also, assume that the time required for transfers is negligible.

Let f(S) be the latest time at which one can arrive at station N from station S.
More precisely, f(S) is defined as the maximum value of t for which there is a sequence of tuples of four integers \big((t _ i,c _ i,A _ i,B _ i)\big) _ {i=1,2,\ldots,k} that satisfies all of the following conditions:

  • t\leq t _ 1
  • A _ 1=S,B _ k=N
  • B _ i=A _ {i+1} for all 1\leq i\lt k,
  • For all 1\leq i\leq k, there is a train that departs from station A _ i at time t _ i and arrives at station B _ i at time t _ i+c _ i.
  • t _ i+c _ i\leq t _ {i+1} for all 1\leq i\lt k.

If no such t exists, set f(S)=-\infty.

Find f(1),f(2),\ldots,f(N-1).

Constraints

  • 2\leq N\leq2\times10 ^ 5
  • 1\leq M\leq2\times10 ^ 5
  • 1\leq l _ i,d _ i,k _ i,c _ i\leq10 ^ 9\ (1\leq i\leq M)
  • 1\leq A _ i,B _ i\leq N\ (1\leq i\leq M)
  • A _ i\neq B _ i\ (1\leq i\leq M)
  • All input values are integers.

Input

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

N M
l _ 1 d _ 1 k _ 1 c _ 1 A _ 1 B _ 1
l _ 2 d _ 2 k _ 2 c _ 2 A _ 2 B _ 2
\vdots
l _ M d _ M k _ M c _ M A _ M B _ M

Output

Print N-1 lines. The k-th line should contain f(k) if f(k)\neq-\infty, and Unreachable if f(k)=-\infty.


Sample Input 1

6 7
10 5 10 3 1 3
13 5 10 2 3 4
15 5 10 7 4 6
3 10 2 4 2 5
7 10 2 3 5 6
5 3 18 2 2 3
6 3 20 4 2 1

Sample Output 1

55
56
58
60
17

The following diagram shows the trains running in the country (information about arrival and departure times is omitted).

Consider the latest time at which one can arrive at station 6 from station 2. As shown in the following diagram, one can arrive at station 6 by departing from station 2 at time 56 and moving as station 2\rightarrow station 3\rightarrow station 4\rightarrow station 6.

It is impossible to depart from station 2 after time 56 and arrive at station 6, so f(2)=56.


Sample Input 2

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

Sample Output 2

1000000000000000000
Unreachable
1
Unreachable

There is a train that departs from station 1 at time 10 ^ {18} and arrives at station 5 at time 10 ^ {18}+10 ^ 9. There are no trains departing from station 1 after that time, so f(1)=10 ^ {18}. As seen here, the answer may not fit within a 32\operatorname{bit} integer.

Also, both the second and third pieces of information guarantee that there is a train that departs from station 2 at time 14 and arrives at station 3 at time 20. As seen here, some trains may appear in multiple pieces of information.


Sample Input 3

16 20
4018 9698 2850 3026 8 11
2310 7571 7732 1862 13 14
2440 2121 20 1849 11 16
2560 5115 190 3655 5 16
1936 6664 39 8822 4 16
7597 8325 20 7576 12 5
5396 1088 540 7765 15 1
3226 88 6988 2504 13 5
1838 7490 63 4098 8 3
1456 5042 4 2815 14 7
3762 6803 5054 6994 10 9
9526 6001 61 8025 7 8
5176 6747 107 3403 1 5
2014 5533 2031 8127 8 11
8102 5878 58 9548 9 10
3788 174 3088 5950 3 13
7778 5389 100 9003 10 15
556 9425 9458 109 3 11
5725 7937 10 3282 2 9
6951 7211 8590 1994 15 12

Sample Output 3

720358
77158
540926
255168
969295
Unreachable
369586
466218
343148
541289
42739
165772
618082
16582
591828
I - Insert

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

配点 : 500

問題文

空の配列 A があります。 i=1,2,\ldots,N の順に以下の操作を行います。

  • i を、A の前から P_i 番目の位置になるように挿入する。
    • より正確には、「AP_i-1 項目まで」「 i 」「AP_i 項目以降」をこの順に連結したもので A を置き換える。

全ての操作を終えた後の A を出力してください。

制約

  • 1 \leq N \leq 5\times 10^5
  • 1 \leq P_i \leq i
  • 入力は全て整数である

入力

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

N
P_1 P_2 \ldots P_N

出力

全ての操作を終えた後の A(A_1,\ldots,A_N) とするとき、A_1,\ldots,A_N をこの順に空白区切りで出力せよ。


入力例 1

4
1 1 2 1

出力例 1

4 2 3 1

操作は以下のように行われます。

  • 1 を、A の前から 1 番目の位置になるように挿入する。 A=(1) となる。
  • 2 を、A の前から 1 番目の位置になるように挿入する。 A=(2,1) となる。
  • 3 を、A の前から 2 番目の位置になるように挿入する。 A=(2,3,1) となる。
  • 4 を、A の前から 1 番目の位置になるように挿入する。 A=(4,2,3,1) となる。

入力例 2

5
1 2 3 4 5

出力例 2

1 2 3 4 5

Score : 500 points

Problem Statement

There is an empty array A. For i = 1,2,\ldots,N, perform the following operation in order:

  • Insert the number i into A so that it becomes the P_i-th element from the beginning.
    • More precisely, replace A with the concatenation of the first P_i-1 elements of A, then i, then the remaining elements of A starting from the P_i-th element, in this order.

Output the final array A after all operations have been completed.

Constraints

  • 1 \leq N \leq 5\times 10^5
  • 1 \leq P_i \leq i
  • All input values are integers.

Input

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

N
P_1 P_2 \ldots P_N

Output

Let the final array be A = (A_1, A_2, \ldots, A_N). Print A_1, A_2, \ldots, A_N in this order, separated by spaces.


Sample Input 1

4
1 1 2 1

Sample Output 1

4 2 3 1

The operations are performed as follows:

  • Insert the number 1 so that it becomes the 1st element of A. Now, A = (1).
  • Insert the number 2 so that it becomes the 1st element of A. Now, A = (2, 1).
  • Insert the number 3 so that it becomes the 2nd element of A. Now, A = (2, 3, 1).
  • Insert the number 4 so that it becomes the 1st element of A. Now, A = (4, 2, 3, 1).

Sample Input 2

5
1 2 3 4 5

Sample Output 2

1 2 3 4 5