A - Apple

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

果物屋さんでりんごが売られています。
あなたは次の操作を好きな順で好きなだけ繰り返すことができます。

  • X 円を払ってりんごを 1 個手に入れる。
  • Y 円を払ってりんごを 3 個手に入れる。

りんごをちょうど N 個手に入れるには最低何円必要ですか?

制約

  • 1 \leq X \leq Y \leq 100
  • 1 \leq N \leq 100
  • 入力される値はすべて整数

入力

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

X Y N

出力

答えを整数として出力せよ。


入力例 1

10 25 10

出力例 1

85

25 円払って 3 個のりんごを手に入れる操作を 3 回繰り返した後、10 円払って 1 個のりんごを手に入れると丁度 10 個のりんごを手に入れられます。このときあなたは 85 円を消費します。
これより少ない金額でちょうど 10 個のりんごを手に入れることはできないので、答えは 85 円になります。


入力例 2

10 40 10

出力例 2

100

10 円払って 1 個のりんごを手に入れる操作を 10 回繰り返すのが最適です。


入力例 3

100 100 2

出力例 3

200

100 円を払って 1 個のりんごを手に入れる操作を 2 回繰り返す以外に ちょうど 2 個のりんごを手に入れる方法はありません。


入力例 4

100 100 100

出力例 4

3400

Score : 100 points

Problem Statement

A fruit store sells apples.
You may perform the following operations as many times as you want in any order:

  • Buy one apple for X yen (the currency in Japan).
  • Buy three apples for Y yen.

How much yen do you need to pay to obtain exactly N apples?

Constraints

  • 1 \leq X \leq Y \leq 100
  • 1 \leq N \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y N

Output

Print the answer as an integer.


Sample Input 1

10 25 10

Sample Output 1

85

Buy three apples for 25 yen three times and one apple for 10 yen, and you will obtain exactly 10 apples for a total of 85 yen.
You cannot obtain exactly 10 apples for a lower cost, so the answer is 85 yen.


Sample Input 2

10 40 10

Sample Output 2

100

It is optimal to buy an apple for 10 yen 10 times.


Sample Input 3

100 100 2

Sample Output 3

200

The only way to obtain exactly 2 apples is to buy an apple for 100 yen twice.


Sample Input 4

100 100 100

Sample Output 4

3400
B - Isosceles

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

面積が正である三角形 ABC があります。
三角形 ABC の三辺の長さはそれぞれ a,b,c です。

三角形 ABC が二等辺三角形であるか判定してください。

制約

  • 1 \leq a,b,c \leq 10
  • 三辺の長さが a,b,c であるような三角形が存在し、その面積は正である。
  • a,b,c は整数

入力

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

a b c

出力

三角形 ABC が二等辺三角形であるならば Yes を、そうでないならば No を出力せよ。


入力例 1

4 2 4

出力例 1

Yes

a=c であるため、三角形 ABC は二等辺三角形です。
よって、Yes を出力します。


入力例 2

3 4 5

出力例 2

No

三角形 ABC の三辺の長さはすべて異なるため、二等辺三角形ではありません。
よって、No を出力します。


入力例 3

10 10 10

出力例 3

Yes

正三角形も二等辺三角形の一種であることに注意してください。

Score : 100 points

Problem Statement

There is a triangle ABC with positive area.
The lengths of the three sides of triangle ABC are a,b,c.

Determine whether triangle ABC is isosceles.

Constraints

  • 1 \leq a,b,c \leq 10
  • A triangle with side lengths a,b,c exists, and its area is positive.
  • a,b,c are integers.

Input

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

a b c

Output

If triangle ABC is isosceles, output Yes; otherwise, output No.


Sample Input 1

4 2 4

Sample Output 1

Yes

Since a=c, triangle ABC is isosceles. Thus, output Yes.


Sample Input 2

3 4 5

Sample Output 2

No

Since the three side lengths of triangle ABC are all different, it is not isosceles. Thus, output No.


Sample Input 3

10 10 10

Sample Output 3

Yes

Note that an equilateral triangle is a kind of isosceles triangle.

C - Nearest Taller

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 人の人が左右一列に並んでいます。左から i 番目 (1\le i\le N) の人を人 i と呼びます。人 i (1\le i\le N) の身長は A_i です。

i=1,2,\ldots,N に対し、人 i より左にいる人であって人 i より身長の高い人が存在するか判定し、存在する場合はその中で番号が人 i に最も近い人を求めてください。

制約

  • 1\le N\le 100
  • 1\le A_i\le 100
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

N 行出力せよ。

i 行目 (1\le i\le N) には、人 i より左にいる人であって人 i より身長の高い人が存在しない場合は -1 を、存在する場合はその中で番号が人 i に最も近い人の番号を出力せよ。


入力例 1

4
4 3 2 5

出力例 1

-1
1
2
-1
  • 1 より左側に人はいません。したがって、 1 行目には -1 を出力してください。
  • 2 より左側にいる人で人 2 より身長が高いのは人 1 のみです。したがって、 2 行目には 1 を出力してください。
  • 3 より左側にいる人で人 3 より身長が高いのは人 1,2 で、このうち番号が人 3 に最も近いのは人 2 です。したがって、 3 行目には 2 を出力してください。
  • 4 より左側に人 4 より身長が高い人はいません。したがって、 4 行目には -1 を出力してください。

入力例 2

3
7 7 7

出力例 2

-1
-1
-1

同じ身長の人が複数人いる場合もあります。


入力例 3

6
31 9 17 10 2 9

出力例 3

-1
1
1
3
4
4

Score : 200 points

Problem Statement

There are N people standing in a row from left to right. The i-th person from the left (1\le i\le N) is called person i. The height of person i (1\le i\le N) is A_i.

For each i=1,2,\ldots,N, determine whether there exists a person to the left of person i who is taller than person i, and if so, find the person standing closest to person i among them.

Constraints

  • 1\le N\le 100
  • 1\le A_i\le 100
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Output N lines.

The i-th line (1\le i\le N) should contain -1 if there is no person to the left of person i who is taller than person i, and otherwise, the number representing the person standing closest to person i among such people.


Sample Input 1

4
4 3 2 5

Sample Output 1

-1
1
2
-1
  • There is no person to the left of person 1. Thus, output -1 on the first line.
  • Among the people to the left of person 2, only person 1 is taller than person 2. Thus, output 1 on the second line.
  • Among the people to the left of person 3, persons 1,2 are taller than person 3, and the person standing closest to person 3 is person 2. Thus, output 2 on the third line.
  • There is no person to the left of person 4 who is taller than person 4. Thus, output -1 on the fourth line.

Sample Input 2

3
7 7 7

Sample Output 2

-1
-1
-1

There may be multiple people with the same height.


Sample Input 3

6
31 9 17 10 2 9

Sample Output 3

-1
1
1
3
4
4
D - Bird Watching

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

空に M 種類の鳥が合わせて N 羽飛んでいます。
鳥の種類には 1,2,\dots,M の番号が付けられています。
N 羽の鳥には 1,2,\dots,N の番号が付けられており、鳥 i の種類は A_i で、大きさは B_i です。

全ての k=1,2,\dots,M について、飛んでいる種類 k の鳥の大きさの平均値を求めてください。
ただし、全ての k=1,2,\dots,M について、種類 k の鳥が 1 羽以上飛んでいることが保証されます。

制約

  • 1 \le M \le N \le 100
  • 1 \le A_i \le M
  • 1 \le B_i \le 100
  • 種類 k の鳥が少なくとも 1 羽存在する ( 1 \le k \le M )
  • 入力される値は全て整数

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

M 行出力せよ。
k ( 1 \le k \le M ) 行目には、種類 k の鳥の大きさの平均値を出力せよ。
真の解との絶対誤差または相対誤差が 10^{-5} 以下であるとき、正解とみなされる。


入力例 1

10 5
4 92
1 16
3 77
4 99
2 89
3 8
1 40
5 56
1 40
4 77

出力例 1

32.00000000000000000000
89.00000000000000000000
42.50000000000000000000
89.33333333333333333333
56.00000000000000000000
  • 種類 1 の鳥の大きさの平均値は (16+40+40)/3 = 32 です。
  • 種類 2 の鳥の大きさの平均値は 89 です。
  • 種類 3 の鳥の大きさの平均値は (77+8)/2 = 42.5 です。
  • 種類 4 の鳥の大きさの平均値は (92+99+77)/3 \approx 89.3333 です。
  • 種類 5 の鳥の大きさの平均値は 56 です。

Score : 200 points

Problem Statement

There are N birds of M types flying in the sky.
The bird types are numbered 1,2,\dots,M.
The N birds are numbered 1,2,\dots,N, and bird i is of type A_i and has size B_i.

For every k=1,2,\dots,M, find the average size of the flying birds of type k.
It is guaranteed that for every k=1,2,\dots,M, there is at least one bird of type k flying.

Constraints

  • 1 \le M \le N \le 100
  • 1 \le A_i \le M
  • 1 \le B_i \le 100
  • There exists at least one bird of type k ( 1 \le k \le M ).
  • All input values 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_N B_N

Output

Output M lines.
The k-th line ( 1 \le k \le M ) should contain the average size of birds of type k.
Your answer will be considered correct if the absolute or relative error from the true value is at most 10^{-5}.


Sample Input 1

10 5
4 92
1 16
3 77
4 99
2 89
3 8
1 40
5 56
1 40
4 77

Sample Output 1

32.00000000000000000000
89.00000000000000000000
42.50000000000000000000
89.33333333333333333333
56.00000000000000000000
  • The average size of birds of type 1 is (16+40+40)/3 = 32.
  • The average size of birds of type 2 is 89.
  • The average size of birds of type 3 is (77+8)/2 = 42.5.
  • The average size of birds of type 4 is (92+99+77)/3 \approx 89.3333.
  • The average size of birds of type 5 is 56.
E - Number Place

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

9\times 9 のマス目 A があり、各マスには 1 以上 9 以下の整数が書き込まれています。
具体的には、 A の上から i 行目、左から j 列目のマスには A_{i,j} が書き込まれています。

A が次の条件をすべてみたしているならば Yes を、そうでないならば No を出力してください。

  • A の各行について、その行に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
  • A の各列について、その列に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
  • A の行を上から 3 行ずつ 3 つに分け、同様に列も左から 3 列ずつ 3 つに分ける。 これによって A9 つの 3\times 3 のマス目に分けたとき、それぞれの 3\times 3 のマス目には 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。

制約

  • 1\leq A_{i,j}\leq 9
  • 入力はすべて整数

入力

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

A_{1,1} A_{1,2} \ldots A_{1,9}
A_{2,1} A_{2,2} \ldots A_{2,9}
\vdots
A_{9,1} A_{9,2} \ldots A_{9,9}

出力

マス目 A が問題文の条件をすべてみたすならば Yes を、 そうでないならば No を出力せよ。


入力例 1

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

出力例 1

Yes

マス目 A は次のようになっています。

マス目 A3 つの条件をすべてみたしているため、Yes を出力します。


入力例 2

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

出力例 2

No

マス目 A は次のようになっています。

例えば左上の 3\times 3 のマス目に注目すると 3 つめの条件をみたしていないことが分かるため、No を出力します。


入力例 3

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

出力例 3

No

マス目 A は次のようになっています。

例えば一番左の列に注目すると 2 つめの条件をみたしていないことが分かるため、No を出力します。

Score : 250 points

Problem Statement

There is a 9\times 9 grid A, where each cell contains an integer between 1 and 9, inclusive.
Specifically, the cell at the i-th row from the top and j-th column from the left contains A_{i,j}.

If A satisfies all of the following conditions, print Yes. Otherwise, print No.

  • For each row of A, the nine cells in that row contain each integer from 1 to 9 exactly once.
  • For each column of A, the nine cells in that column contain each integer from 1 to 9 exactly once.
  • Divide the rows of A into three groups, each of three rows, from top to bottom, and similarly divide the columns into three groups, each of three columns, from left to right. Each 3\times 3 grid obtained from A in this way contains each integer from 1 to 9 exactly once.

Constraints

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

Input

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

A_{1,1} A_{1,2} \ldots A_{1,9}
A_{2,1} A_{2,2} \ldots A_{2,9}
\vdots
A_{9,1} A_{9,2} \ldots A_{9,9}

Output

If the grid A satisfies all the conditions in the problem statement, print Yes; otherwise, print No.


Sample Input 1

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

Sample Output 1

Yes

The grid A is shown below.

The grid A satisfies all three conditions, so print Yes.


Sample Input 2

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

Sample Output 2

No

The grid A is shown below.

For example, if you look at the top left 3\times 3 grid, you can see that the third condition is unsatisfied, so print No.


Sample Input 3

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

Sample Output 3

No

The grid A is shown below.

For example, if you look at the leftmost column, you can see that the second condition is unsatisfied, so print No.

F - Enumerate Sequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

以下の条件を満たす長さ N の整数列を辞書順で小さい方から順に全て出力して下さい。

  • i 番目の要素は 1 以上 R_i 以下
  • 総和が K の倍数
数列の辞書順とは? 数列 A = (A_1, \ldots, A_{|A|})B = (B_1, \ldots, B_{|B|}) より辞書順で真に小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。
  1. |A|<|B| かつ (A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|}) である。
  2. ある整数 1\leq i\leq \min\{|A|,|B|\} が存在して、下記の 2 つがともに成り立つ。
    • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
    • A_i < B_i

制約

  • 入力は全て整数
  • 1 \le N \le 8
  • 2 \le K \le 10
  • 1 \le R_i \le 5

入力

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

N K
R_1 R_2 \dots R_N

出力

出力すべき数列が X 個あり、そのうち i 個目が A_i=(A_{i,1},A_{i,2},\dots,A_{i,N}) であったとき、答えを以下の形式で出力せよ。

A_{1,1} A_{1,2} \dots A_{1,N}
A_{2,1} A_{2,2} \dots A_{2,N}
\vdots
A_{X,1} A_{X,2} \dots A_{X,N}

入力例 1

3 2
2 1 3

出力例 1

1 1 2
2 1 1
2 1 3

出力すべき数列は 3 個で、辞書順で (1,1,2),(2,1,1),(2,1,3) です。


入力例 2

1 2
1

出力例 2


出力すべき数列が無い場合もあります。
この場合、出力は空で構いません。


入力例 3

5 5
2 3 2 3 2

出力例 3

1 1 1 1 1
1 2 2 3 2
1 3 1 3 2
1 3 2 2 2
1 3 2 3 1
2 1 2 3 2
2 2 1 3 2
2 2 2 2 2
2 2 2 3 1
2 3 1 2 2
2 3 1 3 1
2 3 2 1 2
2 3 2 2 1

Score : 300 points

Problem Statement

Print all integer sequences of length N that satisfy the following conditions, in ascending lexicographical order.

  • The i-th element is between 1 and R_i, inclusive.
  • The sum of all elements is a multiple of K.
What is lexicographical order for sequences? A sequence A = (A_1, \ldots, A_{|A|}) is lexicographically smaller than B = (B_1, \ldots, B_{|B|}) if either 1. or 2. below holds:
  1. |A|<|B| and (A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|}).
  2. There exists an integer 1\leq i\leq \min\{|A|,|B|\} such that both of the following are true:
    • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
    • A_i < B_i

Constraints

  • All input values are integers.
  • 1 \le N \le 8
  • 2 \le K \le 10
  • 1 \le R_i \le 5

Input

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

N K
R_1 R_2 \dots R_N

Output

Print the answer in the following format, where X is the number of sequences to print, the i-th of which is A_i=(A_{i,1},A_{i,2},\dots,A_{i,N}):

A_{1,1} A_{1,2} \dots A_{1,N}
A_{2,1} A_{2,2} \dots A_{2,N}
\vdots
A_{X,1} A_{X,2} \dots A_{X,N}

Sample Input 1

3 2
2 1 3

Sample Output 1

1 1 2
2 1 1
2 1 3

There are three sequences to be printed, which are (1,1,2),(2,1,1),(2,1,3) in lexicographical order.


Sample Input 2

1 2
1

Sample Output 2


There may be no sequences to print.
In this case, the output can be empty.


Sample Input 3

5 5
2 3 2 3 2

Sample Output 3

1 1 1 1 1
1 2 2 3 2
1 3 1 3 2
1 3 2 2 2
1 3 2 3 1
2 1 2 3 2
2 2 1 3 2
2 2 2 2 2
2 2 2 3 1
2 3 1 2 2
2 3 1 3 1
2 3 2 1 2
2 3 2 2 1
G - Sequence Query

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

空の数列 A があります。
クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。

  • 1 xAx を追加する。

  • 2 x kAx 以下の要素のうち、大きい方から k 番目の値を出力する。(k\bf{5} 以下)
    ただし、Ax 以下の要素が k 個以上存在しないときは -1 と出力する。

  • 3 x kAx 以上の要素のうち、小さい方から k 番目の値を出力する。(k\bf{5} 以下)
    ただし、Ax 以上の要素が k 個以上存在しないときは -1 と出力する。

制約

  • 1\leq Q \leq 2\times 10^5
  • 1\leq x\leq 10^{18}
  • 1\leq k\leq 5
  • 入力は全て整数である

入力

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

i 番目のクエリ \text{query}_i では、まずクエリの種類 c_i (1,2,3 のいずれか) が与えられる。
c_i=1 の場合は x が追加で与えられ、c_i=2,3 の場合は x,k が追加で与えられる。

すなわち、各クエリは以下に示す 3 つの形式のいずれかである。

1 x
2 x k
3 x k

出力

c_i=2,3 を満たすクエリの個数を q として q 行出力せよ。
j(1\leq j\leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

11
1 20
1 10
1 30
1 20
3 15 1
3 15 2
3 15 3
3 15 4
2 100 5
1 1
2 100 5

出力例 1

20
20
30
-1
-1
1

\text{query}_{1,2,3,4} が終了した段階で、A=(20,10,30,20) となっています。

\text{query}_{5,6,7} について、A15 以上の要素は (20,30,20) です。
このうち小さい方から 1 番目の値は 202 番目の値は 203 番目の値は 30 です。

Score : 400 points

Problem Statement

We have an empty sequence A.
Given Q queries, process them in order.
Each query is of one of the following three types.

  • 1 x : Insert x to A.

  • 2 x k : Among the elements of A that are less than or equal to x, print the k-th largest value. (k is no more than \bf{5})
    If there are less than k elements of A that are less than or equal to x, then print -1.

  • 3 x k : Among the elements of A that are greater than or equal to x, print the k-th smallest value. (k is no more than \bf{5})
    If there are less than k elements of A that are greater than or equal to x, then print -1.

Constraints

  • 1\leq Q \leq 2\times 10^5
  • 1\leq x\leq 10^{18}
  • 1\leq k\leq 5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

In the i-th query \text{query}_i, the type of query c_i (which is either 1, 2, or 3) is given first.
If c_i=1, then x is additionally given; if c_i=2, 3, then x and k are additionally given.

In other words, each query is given in one of the following three formats:

1 x
2 x k
3 x k

Output

Print q lines, where q is the number of queries such that c_i=2,3.
The j-th line (1\leq j\leq q) should contain the answer for the j-th such query.


Sample Input 1

11
1 20
1 10
1 30
1 20
3 15 1
3 15 2
3 15 3
3 15 4
2 100 5
1 1
2 100 5

Sample Output 1

20
20
30
-1
-1
1

After \text{query}_{1,2,3,4} have been processed, we have A=(20,10,30,20).

For \text{query}_{5,6,7}, the elements of A greater than or equal to 15 are (20,30,20).
The 1-st smallest value of them is 20; the 2-nd is 20; the 3-rd is 30.

H - Sorting Queries

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

空の列 A があります。クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。

  • 1 x : A の最後尾に x を追加する。
  • 2 : A の最初の要素を出力する。その後、その要素を削除する。このクエリが与えられるとき、A は空でないことが保証される。
  • 3 : A を昇順にソートする。

制約

  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq x \leq 10^9
  • クエリ 2 が与えられるとき、A は空でない。
  • 入力は全て整数である。

入力

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

Q
\mathrm{query} 1
\mathrm{query} 2
\vdots
\mathrm{query} Q

i 番目のクエリ \mathrm{query} i では、まずクエリの種類 c_i1, 2, 3 のいずれか)が与えられる。 c_i = 1 の場合はさらに整数 x が追加で与えられる。

すなわち、各クエリは以下に示す 3 つの形式のいずれかである。

1 x
2
3

出力

c_i = 2 を満たすクエリの回数を q として q 行出力せよ。
j (1 \leq j \leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

8
1 4
1 3
1 2
1 1
3
2
1 0
2

出力例 1

1
2

入力例 1 において、 i 番目のクエリを処理した後の A の状態を i 行目に示すと以下のようになります。

  • (4)
  • (4, 3)
  • (4, 3, 2)
  • (4, 3, 2, 1)
  • (1, 2, 3, 4)
  • (2, 3, 4)
  • (2, 3, 4, 0)
  • (3, 4, 0)

入力例 2

9
1 5
1 5
1 3
2
3
2
1 6
3
2

出力例 2

5
3
5

入力例 2 において、 i 番目のクエリを処理した後の A の状態を i 行目に示すと以下のようになります。

  • (5)
  • (5, 5)
  • (5, 5, 3)
  • (5, 3)
  • (3, 5)
  • (5)
  • (5, 6)
  • (5, 6)
  • (6)

Score : 500 points

Problem Statement

We have an empty sequence A. You will be given Q queries, which should be processed in the order they are given. Each query is of one of the three kinds below:

  • 1 x : Append x to the end of A.
  • 2 : Print the element at the beginning of A. Then, delete that element. It is guaranteed that A will not empty when this query is given.
  • 3 : Sort A in ascending order.

Constraints

  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq x \leq 10^9
  • A will not be empty when a query 2 is given.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
\mathrm{query} 1
\mathrm{query} 2
\vdots
\mathrm{query} Q

The i-th query, \mathrm{query} i, begins with the kind of query c_i (1, 2, or 3). If c_i = 1, the line additionally has an integer x.

In other words, each query is in one of the three formats below.

1 x
2
3

Output

Print q lines, where q is the number of queries with c_i = 2.
The j-th line (1 \leq j \leq q) should contain the response for the j-th such query.


Sample Input 1

8
1 4
1 3
1 2
1 1
3
2
1 0
2

Sample Output 1

1
2

The i-th line below shows the contents of A after the i-th query is processed in Sample Input 1.

  • (4)
  • (4, 3)
  • (4, 3, 2)
  • (4, 3, 2, 1)
  • (1, 2, 3, 4)
  • (2, 3, 4)
  • (2, 3, 4, 0)
  • (3, 4, 0)

Sample Input 2

9
1 5
1 5
1 3
2
3
2
1 6
3
2

Sample Output 2

5
3
5

The i-th line below shows the contents of A after the i-th query is processed in Sample Input 2.

  • (5)
  • (5, 5)
  • (5, 5, 3)
  • (5, 3)
  • (3, 5)
  • (5)
  • (5, 6)
  • (5, 6)
  • (6)
I - Starry Landscape Photo

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

AtCoder 星から見える夜空には N 個の星があり、これらの N 個の星は東から西へ一直線上に並んでいます。 東から i 番目 (1\le i\le N) の星は、これらの星の中で B _ i 番目に明るい星です。

高橋くんは、次のような手順で夜空の写真を撮ることにしました。

  1. 1\le l\le r\le N を満たす整数の組 (l,r) を選び、東から l 番目、l+1 番目、\ldotsr 番目の星が全てフレームに収まり、他の星がフレームに入らないようにカメラを設置する。
  2. 1\le b\le N を満たす整数 b を選び、N 個の星のうち明るさが 1 番目から b 番目に入る(かつフレームに収まっている)星がすべて写り、そうでない星が写らないようにシャッターを開放する。

ただし、星が 1 つも写らないように写真を撮ることはできません。

このようにして撮影された夜空の写真に写っている星の集合としてありえるものが何通りあるか求めてください。

制約

  • 1\le N\le5\times10 ^ 5
  • 1\le B _ i\le N\ (1\le i\le N)
  • B _ i\ne B _ j\ (1\le i\lt j\le N)
  • 入力はすべて整数

入力

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

N
B _ 1 B _ 2 \ldots B _ N

出力

答えを出力せよ。


入力例 1

4
3 1 4 2

出力例 1

12

例えば (l,r)=(2,4),b=3 とすると、東から 2 番目の星と東から 4 番目の星の 2 つの星が写った写真を撮ることができます。

これを含め、以下の 12 通りの星の集合が写った写真を撮ることができます。 それぞれの写真ではより東にある星がより左側に並んでおり、i 番目に明るい星に整数 i が書かれています。

これ以外の集合を撮ることはできないため、12 を出力してください。


入力例 2

7
1 2 3 4 5 6 7

出力例 2

28

入力例 3

20
15 5 13 17 9 11 20 4 14 16 6 3 8 19 12 7 10 18 2 1

出力例 3

627

Score : 500 points

Problem Statement

In the night sky seen from planet AtCoder, there are N stars, and these N stars are arranged in a line from east to west. The i-th star from the east (1\le i\le N) is the B _ i-th brightest among these stars.

Takahashi decided to take a picture of the night sky using the following procedure:

  1. Choose a pair of integers (l,r) satisfying 1\le l\le r\le N, and set up the camera so that the l-th, (l+1)-th, \ldots, r-th stars from the east all fit in the frame, and no other stars enter the frame.
  2. Choose an integer b satisfying 1\le b\le N, and open the shutter so that all stars among the N stars whose brightness ranks from 1st through b-th (and that fit in the frame) are captured, and no other stars are captured.

However, he may not take a picture with no stars captured.

Find the number of different sets of stars that can be captured in pictures taken in this way.

Constraints

  • 1\le N\le5\times10 ^ 5
  • 1\le B _ i\le N\ (1\le i\le N)
  • B _ i\ne B _ j\ (1\le i\lt j\le N)
  • All input values are integers.

Input

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

N
B _ 1 B _ 2 \ldots B _ N

Output

Print the answer.


Sample Input 1

4
3 1 4 2

Sample Output 1

12

For example, with (l,r)=(2,4),b=3, you can take a picture with two stars: the 2nd star from the east and the 4th star from the east.

Including this, you can take pictures with the following 12 different sets of stars. In each picture, stars further east are arranged further left, and the i-th brightest star is labeled with integer i.

No other sets can be captured, so print 12.


Sample Input 2

7
1 2 3 4 5 6 7

Sample Output 2

28

Sample Input 3

20
15 5 13 17 9 11 20 4 14 16 6 3 8 19 12 7 10 18 2 1

Sample Output 3

627