Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君は、レストランで「AtCoder ドリンク」というドリンクを飲もうとしています。 AtCoder ドリンクは定価である P 円を払えば飲むことができます。
また、高橋君は割引券を持っており、それを使うと AtCoder ドリンクを定価より安い価格である Q 円で飲むことができますが、 その場合には AtCoder ドリンクの他に、N 品ある料理の中から 1 つを追加で注文しなければなりません。 i = 1, 2, \ldots, N について、i 番目の料理の価格は D_i 円です。
高橋君がドリンクを飲むため支払う合計金額の最小値を出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq Q \lt P \leq 10^5
- 1 \leq D_i \leq 10^5
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P Q D_1 D_2 \ldots D_N
出力
答えを出力せよ。
入力例 1
3 100 50 60 20 40
出力例 1
70
割引券を使用して 2 番目の料理を注文することで、ドリンク代 50 円と料理代 20 円の合計 70 円の支払いで AtCoder ドリンクを飲むことができ、支払う合計金額が最小となります。
入力例 2
3 100 50 60000 20000 40000
出力例 2
100
割引券を使用せず定価の 100 円で AtCoder ドリンクを飲むことで、支払う合計金額が最小となります。
Score : 100 points
Problem Statement
Takahashi wants a beverage called AtCoder Drink in a restaurant. It can be ordered at a regular price of P yen.
He also has a discount coupon that allows him to order it at a lower price of Q yen. However, he must additionally order one of the restaurant's N dishes to use that coupon. For each i = 1, 2, \ldots, N, the price of the i-th dish is D_i yen.
Print the minimum total amount of money that he must pay to get the drink.
Constraints
- 1 \leq N \leq 100
- 1 \leq Q \lt P \leq 10^5
- 1 \leq D_i \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P Q D_1 D_2 \ldots D_N
Output
Print the answer.
Sample Input 1
3 100 50 60 20 40
Sample Output 1
70
If he uses the coupon and orders the second dish, he can get the drink by paying 50 yen for it and 20 yen for the dish, for a total of 70 yen, which is the minimum total payment needed.
Sample Input 2
3 100 50 60000 20000 40000
Sample Output 2
100
The total payment will be minimized by not using the coupon and paying the regular price of 100 yen.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
3 種類の文字 . | * からなる、長さ N の文字列 S が与えられます。
S には | がちょうど 2 つ、* がちょうど 1 つ含まれます。
2 つの | で囲まれた部分の中に * が含まれるか判定してください。
含まれている場合 in と、含まれていない場合 out と出力してください。
より厳密には、* より前にある文字のいずれかが | であり、かつ、* より後ろにある文字のいずれかが | であるか判定してください。
制約
- 3\leq N\leq 100
- N は整数
- S は
.|*からなる長さ N の文字列 - S に
|はちょうど 2 個含まれる - S に
*はちょうど 1 個含まれる
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
2 つの | に囲まれた部分の中に * が含まれている場合 in と、含まれていない場合 out と 1 行に出力せよ。
入力例 1
10 .|..*...|.
出力例 1
in
2 つの | に囲まれた部分は |..*...| です。
この中に * が含まれているため、in と出力してください。
入力例 2
10 .|..|.*...
出力例 2
out
2 つの | に囲まれた部分は |..| です。
この中に * は含まれていないため、out と出力してください。
入力例 3
3 |*|
出力例 3
in
Score : 100 points
Problem Statement
You are given a string S of length N consisting of three kinds of characters: ., |, and *.
S contains exactly two | and exactly one *.
Determine whether the * is between the two |, and if so, print in; otherwise, print out.
More formally, determine whether one of the characters before the * is | and one of the characters after the * is |.
Constraints
- 3\leq N\leq 100
- N is an integer.
- S is a string of length N consisting of
.,|, and*. - S contains exactly two
|. - S contains exactly one
*.
Input
The input is given from Standard Input in the following format:
N S
Output
Print a single line containing in if the * is between the two |, and out otherwise.
Sample Input 1
10 .|..*...|.
Sample Output 1
in
Between the two |, we have |..*...|, which contains *, so you should print in.
Sample Input 2
10 .|..|.*...
Sample Output 2
out
Between the two |, we have |..|, which does not contain *, so you should print out.
Sample Input 3
3 |*|
Sample Output 3
in
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 A,B が与えられます。
A+B を(十進法で)計算する時、繰り上がりが生じないなら Easy 、生じるなら Hard と出力してください。
制約
- A,B は整数
- 1 \le A,B \le 10^{18}
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
繰り上がりが生じないなら Easy 、生じるなら Hard と出力せよ。
入力例 1
229 390
出力例 1
Hard
229+390 を計算する際、十の位から百の位へと繰り上がりが発生します。よって、答えは Hard です。
入力例 2
123456789 9876543210
出力例 2
Easy
繰り上がりは発生しません。答えは Easy です。
また、入力が 32bit 整数に収まらないこともあります。
Score : 200 points
Problem Statement
You are given positive integers A and B.
Let us calculate A+B (in decimal). If it does not involve a carry, print Easy; if it does, print Hard.
Constraints
- A and B are integers.
- 1 \le A,B \le 10^{18}
Input
Input is given from Standard Input in the following format:
A B
Output
If the calculation does not involve a carry, print Easy; if it does, print Hard.
Sample Input 1
229 390
Sample Output 1
Hard
When calculating 229+390, we have a carry from the tens digit to the hundreds digit, so the answer is Hard.
Sample Input 2
123456789 9876543210
Sample Output 2
Easy
We do not have a carry here; the answer is Easy.
Note that the input may not fit into a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君はAtCoderレストランの前の待ち行列の管理をしたいです。はじめ、待ち行列に並んでいる人はいません。 また、待ち行列に並ぶ人は必ず注文する料理のメニュー番号が書かれた食券を持って並びます。
Q 個のクエリが与えられるので順に処理してください。クエリは 2 種類あり、以下のいずれかの形式で与えられます。
1 X: 待ち行列の末尾に 1 人並ぶ。このとき並ぶ人はメニュー番号が X の食券を持って並ぶ。2: 待ち行列の先頭にいる人をレストランに案内する。このとき案内される人が持っている食券のメニュー番号を出力する。
制約
- 1 \leq Q \leq 100
- 1 \leq X \leq 100
- 2 つ目の形式のクエリについて、案内する前に待ち行列に並んでいる人がいる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の 2 種類のいずれかの形式で与えられる。
1 X
2
出力
問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。
入力例 1
6 1 3 1 1 1 15 2 1 3 2
出力例 1
3 1
はじめ、待ち行列に並んでいる人はいません。
- 1 つ目のクエリについて、メニュー番号が 3 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 3 です。
- 2 つ目のクエリについて、メニュー番号が 1 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 3,1 です。
- 3 つ目のクエリについて、メニュー番号が 15 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 3,1,15 です。
- 4 つ目のクエリについて、待ち行列の先頭にいる人をレストランに案内します。案内する人はメニュー番号が 3 の食券を持っているので 3 を出力します。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 1,15 です。
- 5 つ目のクエリについて、メニュー番号が 3 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 1,15,3 です。
- 6 つ目のクエリについて、待ち行列の先頭にいる人をレストランに案内します。案内する人はメニュー番号が 1 の食券を持っているので 1 を出力します。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 15,3 です。
入力例 2
7 1 3 1 1 1 4 1 1 1 5 1 9 1 2
出力例 2
2 つ目の形式のクエリがないことがあることに注意してください。
Score : 200 points
Problem Statement
Takahashi wants to manage the waiting line in front of the AtCoder Restaurant. Initially, the waiting line is empty. Each person who joins the line holds a meal ticket with the menu number of the dish they will order.
Process Q queries in order. There are two types of queries, given in the following formats:
1 X: One person joins the end of the waiting line holding a ticket with menu number X.2: Takahashi guides the person at the front of the waiting line into the restaurant. Print the menu number on that person’s ticket.
Constraints
- 1 \leq Q \leq 100
- 1 \leq X \leq 100
- For each query of the second type, there is at least one person in the line before guiding.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query has one of the following two formats:
1 X
2
Output
For each query, print the answer as specified in the problem statement, each on its own line.
Sample Input 1
6 1 3 1 1 1 15 2 1 3 2
Sample Output 1
3 1
Initially, the waiting line is empty.
- For the first query, a person holding a ticket with menu number 3 joins the end of the line. The sequence of menu numbers held by people in line from front to back is 3.
- For the second query, a person holding a ticket with menu number 1 joins the end of the line. The sequence becomes 3,1.
- For the third query, a person holding a ticket with menu number 15 joins the end of the line. The sequence becomes 3,1,15.
- For the fourth query, guide the person at the front into the restaurant. That person holds menu number 3, so print 3. The sequence becomes 1,15.
- For the fifth query, a person holding a ticket with menu number 3 joins the end of the line. The sequence becomes 1,15,3.
- For the sixth query, guide the person at the front into the restaurant. That person holds menu number 1, so print 1. The sequence becomes 15,3.
Sample Input 2
7 1 3 1 1 1 4 1 1 1 5 1 9 1 2
Sample Output 2
Note that there may be no queries of the second type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
1,2,\dots,N がちょうど 3 回ずつ現れる長さ 3N の数列 A=(A_1,A_2,\dots,A_{3N}) が与えられます。
i=1,2,\dots,N について、A の中にある i のうち真ん中にあるものの添字を f(i) と定めます。 1,2,\dots,N を f(i) の昇順に並べ替えてください。
f(i) の定義は厳密には以下の通りです。
- A_j = i を満たす j が j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma) であるとする。このとき、f(i) = \beta である。
制約
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i=1,2,\dots,N それぞれについて、A の中に i はちょうど 3 回現れる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \dots A_{3N}
出力
1,2,\dots,N を f(i) の昇順に並べ替えてできる長さ N の数列を空白区切りで出力せよ。
入力例 1
3 1 1 3 2 3 2 2 3 1
出力例 1
1 3 2
- A の中にある 1 は A_1,A_2,A_9 なので、f(1) = 2 です。
- A の中にある 2 は A_4,A_6,A_7 なので、f(2) = 6 です。
- A の中にある 3 は A_3,A_5,A_8 なので、f(3) = 5 です。
よって、f(1) < f(3) < f(2) であるため 1,3,2 の順に出力します。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
4 2 3 4 3 4 1 3 1 1 4 2 2
出力例 3
3 4 1 2
Score : 250 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_{3N}) of length 3N where each of 1,2,\dots, and N occurs exactly three times.
For i=1,2,\dots,N, let f(i) be the index of the middle occurrence of i in A. Sort 1,2,\dots,N in ascending order of f(i).
Formally, f(i) is defined as follows.
- Suppose that those j such that A_j = i are j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma). Then, f(i) = \beta.
Constraints
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i occurs in A exactly three times, for each i=1,2,\dots,N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 \dots A_{3N}
Output
Print the sequence of length N obtained by sorting 1,2,\dots,N in ascending order of f(i), separated by spaces.
Sample Input 1
3 1 1 3 2 3 2 2 3 1
Sample Output 1
1 3 2
- 1 occurs in A at A_1,A_2,A_9, so f(1) = 2.
- 2 occurs in A at A_4,A_6,A_7, so f(2) = 6.
- 3 occurs in A at A_3,A_5,A_8, so f(3) = 5.
Thus, f(1) < f(3) < f(2), so 1,3, and 2 should be printed in this order.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
4 2 3 4 3 4 1 3 1 1 4 2 2
Sample Output 3
3 4 1 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字のみからなる N 個の文字列 S_1,S_2,\dots,S_N が与えられます。
S_1,S_2,\dots,S_N から文字列を好きな個数選ぶことを考えます。
このとき、「選んだ文字列の中でちょうど K 個の文字列に登場する英小文字」の種類数としてあり得る最大値を求めてください。
制約
- 1 \le N \le 15
- 1 \le K \le N
- N,K は整数
- S_i は英小文字からなる空でない文字列である。
- 1 \le i \le N を満たす整数 i に対し、S_i に同じ文字は 2 個以上含まれない。
- i \neq j ならば S_i \neq S_j である。
入力
入力は以下の形式で標準入力から与えられる。
N K S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
4 2 abi aef bc acg
出力例 1
3
S_1,S_3,S_4 を選んだ場合、a,b,c がちょうど 2 個の文字列に含まれます。
4 個以上の文字がちょうど 2 個の文字列に含まれるような選び方は存在しないため、答えは 3 です。
入力例 2
2 2 a b
出力例 2
0
同じ文字列を複数回選ぶことはできません。
入力例 3
5 2 abpqxyz az pq bc cy
出力例 3
7
Score : 300 points
Problem Statement
You are given N strings S_1,S_2,\dots,S_N consisting of lowercase English alphabets.
Consider choosing some number of strings from S_1,S_2,\dots,S_N.
Find the maximum number of distinct alphabets that satisfy the following condition: "the alphabet is contained in exactly K of the chosen strings."
Constraints
- 1 \le N \le 15
- 1 \le K \le N
- N and K are integers.
- S_i is a non-empty string consisting of lowercase English alphabets.
- For each integer i such that 1 \le i \le N, S_i does not contain two or more same alphabets.
- If i \neq j, then S_i \neq S_j.
Input
Input is given from Standard Input in the following format:
N K S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
4 2 abi aef bc acg
Sample Output 1
3
When S_1,S_3, and S_4 are chosen, a,b, and c occur in exactly two of the strings.
There is no way to choose strings so that 4 or more alphabets occur in exactly 2 of the strings, so the answer is 3.
Sample Input 2
2 2 a b
Sample Output 2
0
You cannot choose the same string more than once.
Sample Input 3
5 2 abpqxyz az pq bc cy
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
xy -平面上の N 個の円が与えられます。 i = 1, 2, \ldots, N について、i 番目の円は点 (x_i, y_i) を中心とする半径 r_i の円です。
N 個の円のうち少なくとも 1 つ以上の円の円周上にある点のみを通って、点 (s_x, s_y) から点 (t_x, t_y) に行くことができるかどうかを判定してください。
制約
- 1 \leq N \leq 3000
- -10^9 \leq x_i, y_i \leq 10^9
- 1 \leq r_i \leq 10^9
- (s_x, s_y) は N 個の円のうち少なくとも 1 つ以上の円の円周上にある
- (t_x, t_y) は N 個の円のうち少なくとも 1 つ以上の円の円周上にある
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N s_x s_y t_x t_y x_1 y_1 r_1 x_2 y_2 r_2 \vdots x_N y_N r_N
出力
点 (s_x, s_y) から点 (t_x, t_y) に行くことができる場合は Yes を、そうでない場合は No を出力せよ。
ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。
入力例 1
4 0 -2 3 3 0 0 2 2 0 2 2 3 1 -3 3 3
出力例 1
Yes

例えば、下記の経路で点 (0, -2) から点 (3, 3) へ行くことができます。
- 点 (0, -2) から 1 つ目の円の円周上を反時計回りに通って点 (1, -\sqrt{3}) へ行く。
- 点 (1, -\sqrt{3}) から 2 つ目の円の円周上を時計回りに通って点 (2, 2) へ行く。
- 点 (2, 2) から 3 つ目の円の円周上を反時計回りに通って点 (3, 3) へ行く。
よって、Yes を出力します。
入力例 2
3 0 1 0 3 0 0 1 0 0 2 0 0 3
出力例 2
No

少なくとも 1 つ以上の円の円周上にある点のみを通って点 (0, 1) から点 (0, 3) に行くことはできないので No を出力します。
Score : 400 points
Problem Statement
You are given N circles on the xy-coordinate plane. For each i = 1, 2, \ldots, N, the i-th circle is centered at (x_i, y_i) and has a radius of r_i.
Determine whether it is possible to get from (s_x, s_y) to (t_x, t_y) by only passing through points that lie on the circumference of at least one of the N circles.
Constraints
- 1 \leq N \leq 3000
- -10^9 \leq x_i, y_i \leq 10^9
- 1 \leq r_i \leq 10^9
- (s_x, s_y) lies on the circumference of at least one of the N circles.
- (t_x, t_y) lies on the circumference of at least one of the N circles.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N s_x s_y t_x t_y x_1 y_1 r_1 x_2 y_2 r_2 \vdots x_N y_N r_N
Output
If it is possible to get from (s_x, s_y) to (t_x, t_y), print Yes; otherwise, print No.
Note that the judge is case-sensitive.
Sample Input 1
4 0 -2 3 3 0 0 2 2 0 2 2 3 1 -3 3 3
Sample Output 1
Yes

Here is one way to get from (0, -2) to (3, 3).
- From (0, -2), pass through the circumference of the 1-st circle counterclockwise to reach (1, -\sqrt{3}).
- From (1, -\sqrt{3}), pass through the circumference of the 2-nd circle clockwise to reach (2, 2).
- From (2, 2), pass through the circumference of the 3-rd circle counterclockwise to reach (3, 3).
Thus, Yes should be printed.
Sample Input 2
3 0 1 0 3 0 0 1 0 0 2 0 0 3
Sample Output 2
No

It is impossible to get from (0, 1) to (0, 3) by only passing through points on the circumference of at least one of the circles, so No should be printed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
整数 N,M と各要素が 1 以上 M 以下である長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
この整数列 A に対して以下の操作を 10^{100} 回行います。
- 1 以上 M 以下の整数のうち A における出現回数が最も少ないものを v とする。ただし、そのような v が複数存在する場合はその中で値が最小のものとする。そして、A の末尾に v を追加する。
Q 個のクエリが与えられます。 i 番目のクエリでは整数 X_i が与えられるので、操作を 10^{100} 回行った後の A_{X_i} の値を求めてください。
制約
- 1\le N,M\le 5\times 10^5
- 1\le A_i \le M
- 1\le Q\le 2\times 10^5
- 1\le X_i \le 10^{18}
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N Q X_1 X_2 \vdots X_Q
出力
Q 行出力せよ。
i 行目には、i 番目のクエリに対する答えを出力せよ。
入力例 1
3 3 1 1 2 8 1 2 3 4 5 6 7 8
出力例 1
1 1 2 3 2 3 1 2
はじめ A=(1,1,2) です。各操作で A は以下のように変化します。
- 1 回目:A に含まれる 1,2,3 の個数はそれぞれ 2,1,0 であるため、v=3 とする。v を A の末尾に追加し、A=(1,1,2,3) となる。
- 2 回目:A に含まれる 1,2,3 の個数はそれぞれ 2,1,1 であるため、v=2 とする。v を A の末尾に追加し、A=(1,1,2,3,2) となる。
- 3 回目:A に含まれる 1,2,3 の個数はそれぞれ 2,2,1 であるため、v=3 とする。v を A の末尾に追加し、A=(1,1,2,3,2,3) となる。
- \vdots
操作を 10^{100} 回行った後の A は A=(1,1,2,3,2,3,1,2,\ldots) となります。
入力例 2
7 30 20 26 3 14 4 4 9 10 31 9 21 23 97 99 30 79 57 3
出力例 2
30 2 18 21 7 9 29 19 27 3
Score : 475 points
Problem Statement
You are given integers N, M and an integer sequence A=(A_1,A_2,\ldots,A_N) of length N where each element is between 1 and M, inclusive.
The following operation is performed 10^{100} times on this integer sequence A:
- Let v be the integer between 1 and M, inclusive, with the fewest occurrences in A. If there are multiple such v, take the smallest among them. Then, append v to the end of A.
You are given Q queries. The i-th query gives an integer X_i, so find the value of A_{X_i} after performing the operation 10^{100} times.
Constraints
- 1\le N,M\le 5\times 10^5
- 1\le A_i \le M
- 1\le Q\le 2\times 10^5
- 1\le X_i \le 10^{18}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N Q X_1 X_2 \vdots X_Q
Output
Output Q lines.
The i-th line should contain the answer to the i-th query.
Sample Input 1
3 3 1 1 2 8 1 2 3 4 5 6 7 8
Sample Output 1
1 1 2 3 2 3 1 2
Initially, A=(1,1,2). With each operation, A changes as follows:
- First operation: The counts of 1, 2, 3 in A are 2, 1, 0 respectively, so v=3. Append v to the end of A, giving A=(1,1,2,3).
- Second operation: The counts of 1, 2, 3 in A are 2, 1, 1 respectively, so v=2. Append v to the end of A, giving A=(1,1,2,3,2).
- Third operation: The counts of 1, 2, 3 in A are 2, 2, 1 respectively, so v=3. Append v to the end of A, giving A=(1,1,2,3,2,3).
- \vdots
After performing the operation 10^{100} times, A becomes A=(1,1,2,3,2,3,1,2,\ldots).
Sample Input 2
7 30 20 26 3 14 4 4 9 10 31 9 21 23 97 99 30 79 57 3
Sample Output 2
30 2 18 21 7 9 29 19 27 3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
空の配列 A があります。 i=1,2,\ldots,N の順に以下の操作を行います。
- 数 i を、A の前から P_i 番目の位置になるように挿入する。
- より正確には、「A の P_i-1 項目まで」「 i 」「A の P_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