A - 202<s>3</s>

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

配点 : 100 点

問題文

英小文字と数字からなる文字列 S が入力されます。
S は 2023 で終わることが保証されます。
S の最後の文字を 4 に変更し、変更後の文字列を出力してください。

制約

  • S は英小文字と数字からなる長さ 4 以上 100 以下の文字列
  • S は 2023 で終わる。

入力

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

S

出力

答えを出力せよ。


入力例 1

hello2023

出力例 1

hello2024

hello2023 の最後の文字を 4 に変更すると、 hello2024 となります。


入力例 2

worldtourfinals2023

出力例 2

worldtourfinals2024

入力例 3

2023

出力例 3

2024

S が 2023 で終わることが保証されていますが、 S が 2023 そのものである場合もあります。


入力例 4

20232023

出力例 4

20232024

Score : 100 points

Problem Statement

You are given a string S consisting of lowercase English letters and digits.
S is guaranteed to end with 2023.
Change the last character of S to 4 and print the modified string.

Constraints

  • S is a string of length between 4 and 100, inclusive, consisting of lowercase English letters and digits.
  • S ends with 2023.

Input

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

S

Output

Print the answer.


Sample Input 1

hello2023

Sample Output 1

hello2024

Changing the last character of hello2023 to 4 yields hello2024.


Sample Input 2

worldtourfinals2023

Sample Output 2

worldtourfinals2024

Sample Input 3

2023

Sample Output 3

2024

S is guaranteed to end with 2023, possibly being 2023 itself.


Sample Input 4

20232023

Sample Output 4

20232024
B - Print 341

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

配点 : 100 点

問題文

正整数 N が与えられるので、 N 個の 0 と N+1 個の 1 からなる、0 と 1 が交互に並んだ文字列を出力してください。

制約

  • N は整数
  • 1 \leq N \leq 100

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

101010101

4 個の 0 と 5 個の 1 からなる、0 と 1 が交互に並んだ文字列は 101010101 です。


入力例 2

1

出力例 2

101

入力例 3

10

出力例 3

101010101010101010101

Score: 100 points

Problem Statement

Given a positive integer N, print a string of N zeros and N+1 ones where 0 and 1 alternate.

Constraints

  • N is an integer.
  • 1 \leq N \leq 100

Input

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

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

101010101

A string of four zeros and five ones where 0 and 1 alternate is 101010101.


Sample Input 2

1

Sample Output 2

101

Sample Input 3

10

Sample Output 3

101010101010101010101
C - Counting Arrays

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

配点 : 200 点

問題文

1 から N までの番号がついた N 個の数列が与えられます。
数列 i は、長さが L_i で j (1 \leq j \leq L_i) 番目の要素が a_{i,j} であるような数列です。

数列 i と 数列 j は、 L_i = L_j かつすべての k (1 \leq k \leq L_i) に対して a_{i,k} = a_{j,k} が成り立つ時に同じであるとみなします。
同じ数列は 1 種類として数えるとき、数列 1 から 数列 N の中に全部で何種類の数列がありますか?

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq 2 \times 10^5 (1 \leq i \leq N)
  • 0 \leq a_{i,j} \leq 10^{9} (1 \leq i \leq N, 1 \leq j \leq L_i)
  • すべての数列の要素の個数の和、すなわち \sum_{i=1}^N L_i は 2 \times 10^5 を超えない。
  • 入力はすべて整数である。

入力

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

N
L_1 a_{1,1} a_{1,2} \dots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \dots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \dots a_{N,L_N}

出力

数列の種類数を出力せよ。


入力例 1

4
2 1 2
2 1 1
2 2 1
2 1 2

出力例 1

3

入力例 1 で与えられている数列は以下の 4 個です。

  • 数列 1 : (1, 2)
  • 数列 2 : (1, 1)
  • 数列 3 : (2, 1)
  • 数列 4 : (1, 2)

このうち数列 1 と数列 4 は同じ数列で、それ以外は互いに異なる数列なので全部で 3 種類の数列があります。


入力例 2

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

出力例 2

4

入力例 2 で与えられている数列は以下の 5 個です。

  • 数列 1 : (1)
  • 数列 2 : (1)
  • 数列 3 : (2)
  • 数列 4 : (1, 1)
  • 数列 5 : (1, 1, 1)

入力例 3

1
1 1

出力例 3

1

Score : 200 points

Problem Statement

You are given N sequences numbered 1 to N.
Sequence i has a length of L_i and its j-th element (1 \leq j \leq L_i) is a_{i,j}.

Sequence i and Sequence j are considered the same when L_i = L_j and a_{i,k} = a_{j,k} for every k (1 \leq k \leq L_i).
How many different sequences are there among Sequence 1 through Sequence N?

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq 2 \times 10^5 (1 \leq i \leq N)
  • 0 \leq a_{i,j} \leq 10^{9} (1 \leq i \leq N, 1 \leq j \leq L_i)
  • The total number of elements in the sequences, \sum_{i=1}^N L_i, does not exceed 2 \times 10^5.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 a_{1,1} a_{1,2} \dots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \dots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \dots a_{N,L_N}

Output

Print the number of different sequences.


Sample Input 1

4
2 1 2
2 1 1
2 2 1
2 1 2

Sample Output 1

3

Sample Input 1 contains four sequences:

  • Sequence 1 : (1, 2)
  • Sequence 2 : (1, 1)
  • Sequence 3 : (2, 1)
  • Sequence 4 : (1, 2)

Except that Sequence 1 and Sequence 4 are the same, these sequences are pairwise different, so we have three different sequences.


Sample Input 2

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

Sample Output 2

4

Sample Input 2 contains five sequences:

  • Sequence 1 : (1)
  • Sequence 2 : (1)
  • Sequence 3 : (2)
  • Sequence 4 : (1, 1)
  • Sequence 5 : (1, 1, 1)

Sample Input 3

1
1 1

Sample Output 3

1
D - Search and Delete

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

配点 : 200 点

問題文

高橋君は長さ N の整数列 A=(A_1,A_2,\ldots,A_N) を持っています。
A は広義単調増加であることが保証されます。

高橋君はこの整数列に対して M 回操作を行います。 i 回目 (1\leq i\leq M) の操作では、次の操作を行います。

数列 A が B_i を要素として持つならば、そのような要素を 1 つ選び、削除する。 そのような要素が存在しないならば何もしない。

A は広義単調増加であるため、操作後の列は要素の選び方によらず一意であり、再び広義単調増加となることに注意せよ。

M 回の操作を行なった後の A を求めてください。

広義単調増加 とは 数列 X=(X_1,X_2,\ldots,X_K) が広義単調増加であるとは、 任意の i (1\leq i\leq K-1) について、 X_i\leq X_{i+1} をみたすことを指します。

制約

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 1 \leq A_i,B_i \leq 10^9
  • 整数列 A は広義単調増加である。
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

操作後の A の各要素を、順に空白区切りで一行に出力せよ。
操作後の A が空ならば、何も出力しないようにせよ。


入力例 1

8 5
1 2 2 3 3 3 5 6
2 2 7 3 2

出力例 1

1 3 3 5 6

最初、A は A=(1,2,2,3,3,3,5,6) です。
操作は次のように行われます。

  • A から 2 を 1 つ削除し、操作後の A は A=(1,2,3,3,3,5,6) となります。
  • A から 2 を 1 つ削除し、操作後の A は A=(1,3,3,3,5,6) となります。
  • A は 7 を要素として持たないため、何も行いません。操作後の A は A=(1,3,3,3,5,6) のままです。
  • A から 3 を 1 つ削除し、操作後の A は A=(1,3,3,5,6) となります。
  • A は 2 を要素として持たないため、何も行いません。操作後の A は A=(1,3,3,5,6) のままです。

よって、すべての操作の後、A=(1,3,3,5,6) であるため、要素をこの順に空白区切りで出力します。


入力例 2

1 2
1
1 1

出力例 2


操作後の A は空となるため、何も出力しません。

Score : 200 points

Problem Statement

Takahashi has an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.
It is guaranteed that A is non-decreasing.

Takahashi performs M operations on this integer sequence. In the i-th operation (1\leq i\leq M), he performs the following operation:

If the sequence A contains B_i as an element, select one such element and delete it. If no such element exists, do nothing.

Note that since A is non-decreasing, the sequence after the operation is uniquely determined regardless of the choice of element, and remains non-decreasing.

Find A after performing M operations.

What is non-decreasing? A sequence X=(X_1,X_2,\ldots,X_K) is non-decreasing if X_i\leq X_{i+1} holds for all i (1\leq i\leq K-1).

Constraints

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 1 \leq A_i,B_i \leq 10^9
  • The integer sequence A is non-decreasing.
  • 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
B_1 B_2 \ldots B_M

Output

Output the elements of A after the operations, in order, separated by spaces on a single line.
If A is empty after the operations, output nothing.


Sample Input 1

8 5
1 2 2 3 3 3 5 6
2 2 7 3 2

Sample Output 1

1 3 3 5 6

Initially, A is A=(1,2,2,3,3,3,5,6).
The operations are performed as follows:

  • Delete one 2 from A, and A becomes A=(1,2,3,3,3,5,6) after the operation.
  • Delete one 2 from A, and A becomes A=(1,3,3,3,5,6) after the operation.
  • Since A does not contain 7 as an element, do nothing. A remains A=(1,3,3,3,5,6) after the operation.
  • Delete one 3 from A, and A becomes A=(1,3,3,5,6) after the operation.
  • Since A does not contain 2 as an element, do nothing. A remains A=(1,3,3,5,6) after the operation.

Therefore, after all operations, A=(1,3,3,5,6), so output the elements in this order separated by spaces.


Sample Input 2

1 2
1
1 1

Sample Output 2


A becomes empty after the operations, so output nothing.

E - Socks 2

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

配点 : 350 点

問題文

高橋君は N 組の靴下を持っており、i 番目の組は色 i の靴下 2 枚からなります。 ある日タンスの中を整理した高橋君は、色 A_1,A_2,\dots,A_K の靴下を 1 枚ずつなくしてしまったことに気づいたので、残っている 2N-K 枚の靴下を使って、靴下 2 枚ずつからなる \lfloor\frac{2N-K}{2}\rfloor 個の組を新たに作り直すことにしました。 色 i の靴下と色 j の靴下からなる組の奇妙さは |i-j| として定義され、高橋君は奇妙さの総和をできるだけ小さくしたいです。

残っている靴下をうまく組み合わせて \lfloor\frac{2N-K}{2}\rfloor 個の組を作ったとき、奇妙さの総和が最小でいくつになるか求めてください。 なお、2N-K が奇数のとき、どの組にも含まれない靴下が 1 枚存在することに注意してください。

制約

  • 1\leq K\leq N \leq 2\times 10^5
  • 1\leq A_1 < A_2 < \dots < A_K \leq N
  • 入力は全て整数

入力

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

N K
A_1 A_2 \dots A_K

出力

奇妙さの総和の最小値を整数として出力せよ。


入力例 1

4 2
1 3

出力例 1

2

以下、色 i の靴下と色 j の靴下からなる組を (i,j) と表記します。

色 1,2,3,4 の靴下がそれぞれ 1,2,1,2 枚ずつあります。 (1,2),(2,3),(4,4) の 3 組を作ると、奇妙さの総和は |1-2|+|2-3|+|4-4|=2 となり、これが最小です。


入力例 2

5 1
2

出力例 2

0

(1,1),(3,3),(4,4),(5,5) の 4 組を作り、色 2 の靴下を 1 枚余らせる(どの組にも入れない)のが最適です。


入力例 3

8 5
1 2 4 7 8

出力例 3

2

Score : 350 points

Problem Statement

Takahashi has N pairs of socks, and the i-th pair consists of two socks of color i. One day, after organizing his chest of drawers, Takahashi realized that he had lost one sock each of colors A_1, A_2, \dots, A_K, so he decided to use the remaining 2N-K socks to make \lfloor\frac{2N-K}{2}\rfloor new pairs of socks, each pair consisting of two socks. The weirdness of a pair of a sock of color i and a sock of color j is defined as |i-j|, and Takahashi wants to minimize the total weirdness.

Find the minimum possible total weirdness when making \lfloor\frac{2N-K}{2}\rfloor pairs from the remaining socks. Note that if 2N-K is odd, there will be one sock that is not included in any pair.

Constraints

  • 1\leq K\leq N \leq 2\times 10^5
  • 1\leq A_1 < A_2 < \dots < A_K \leq N
  • All input values are integers.

Input

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

N K
A_1 A_2 \dots A_K

Output

Print the minimum total weirdness as an integer.


Sample Input 1

4 2
1 3

Sample Output 1

2

Below, let (i,j) denote a pair of a sock of color i and a sock of color j.

There are 1, 2, 1, 2 socks of colors 1, 2, 3, 4, respectively. Creating the pairs (1,2),(2,3),(4,4) results in a total weirdness of |1-2|+|2-3|+|4-4|=2, which is the minimum.


Sample Input 2

5 1
2

Sample Output 2

0

The optimal solution is to make the pairs (1,1),(3,3),(4,4),(5,5) and leave one sock of color 2 as a surplus (not included in any pair).


Sample Input 3

8 5
1 2 4 7 8

Sample Output 3

2
F - AtCoder AAC Contest

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

配点 : 300 点

問題文

高橋くんは、文字をいくつか持っています。 高橋くんが持っている文字は A, B, C のいずれかです。 はじめ、高橋くんは A を n _ A 個、B を n _ B 個、C を n _ C 個持っています。

高橋くんは、A を 1 つ、C を 1 つ、さらに追加で任意の 1 つの文字の合計 3 文字を使うことで、コンテストを 1 つ開催することができます。 具体的には、A を 2 つ、C を 1 つ使うと AAC を、A, B, C を 1 つずつ使うと ABC を、A を 1 つ、C を 2 つ使うと ACC を開催することができます。

高橋くんは、現在持っている文字を使ってコンテストをなるべく多く開催したいです。 高橋くんが最大でいくつのコンテストを開催することができるか求めてください。

T 個のテストケースが与えられるので、それぞれについて答えてください。

制約

  • 1\le T\le2\times10 ^ 5
  • それぞれのテストケースについて、
    • 0\le n _ A\le10 ^ 9
    • 0\le n _ B\le10 ^ 9
    • 0\le n _ C\le10 ^ 9
  • 入力はすべて整数

入力

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

T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T

\mathrm{testcase} _ i\ (1\le i\le T) は i 番目のテストケースを表しており、次の形式で与えられる。

n _ A n _ B n _ C

出力

T 行にわたって出力せよ。 i 行目 (1\le i\le T) には、i 番目のテストケースの答えを出力せよ。


入力例 1

5
3 1 2
100 0 0
1000000 1000000 1000000
31 41 59
1000000000 10000 1

出力例 1

2
0
1000000
31
1

1 つめのテストケースでは、AAC を 1 回、ABC を 1 回の計 2 回のコンテストを開催することができます。 よって、1 行目には 2 を出力してください。

Score : 300 points

Problem Statement

Takahashi has some letters. Each letter he has is A, B, or C. Initially, he has n _ A letters A, n _ B letters B, and n _ C letters C.

He can hold one contest by using one letter A, one letter C, and additionally any one letter, for a total of three letters. Specifically, he can hold AAC by using two letters A and one letter C, ABC by using one letter each of A, B, and C, and ACC by using one letter A and two letters C.

He wants to hold as many contests as possible using the letters he currently has. Find the maximum number of contests he can hold.

T test cases are given, so report the answer for each of them.

Constraints

  • 1\le T\le2\times10 ^ 5
  • For each test case,
    • 0\le n _ A\le10 ^ 9
    • 0\le n _ B\le10 ^ 9
    • 0\le n _ C\le10 ^ 9
  • All input values are integers.

Input

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

T
\mathrm{testcase} _ 1
\mathrm{testcase} _ 2
\vdots
\mathrm{testcase} _ T

\mathrm{testcase} _ i\ (1\le i\le T) represents the i-th test case and is given in the following format:

n _ A n _ B n _ C

Output

Output over T lines. On the i-th line (1\le i\le T), output the answer to the i-th test case.


Sample Input 1

5
3 1 2
100 0 0
1000000 1000000 1000000
31 41 59
1000000000 10000 1

Sample Output 1

2
0
1000000
31
1

In the first test case, he can hold AAC once and ABC once, for a total of two contests. Therefore, output 2 on the first line.

G - Coefficient Stair

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

配点 : 400 点

問題文

非負整数からなる長さ N の列 A=(A _ 1,A _ 2,\ldots,A _ N) であって、\displaystyle\sum _ {i=1} ^ Ni\times A _ i=K を満たすものを辞書順で小さいほうからすべて出力してください。

ここで、条件を満たす数列が 3\times10 ^ 5 個以下であるような入力のみが与えられます。

数列の辞書順とは?

数列 S = (S_1,S_2,\ldots,S_{|S|}) が数列 T = (T_1,T_2,\ldots,T_{|T|}) より辞書順で小さいとは,下記の 1. と 2. のどちらかが成り立つことを言います. ここで,|S|, |T| はそれぞれ S, T の長さを表します.

  1. |S| \lt |T| かつ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}).
  2. ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して,下記の 2 つがともに成り立つ.
    • (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
    • S_i が T_i より(数として)小さい.

制約

  • 1\le N\le 10
  • 1\le K\le 2\times10 ^ 5
  • 条件を満たす数列は 3\times10 ^ 5 個以下
  • 入力はすべて整数

入力

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

N K

出力

条件を満たす非負整数列が q 個あるとして、q 行にわたって出力せよ。 それぞれの行には条件を満たす非負整数列の要素を空白を区切りとして順に出力せよ。 どの数列についても、その数列より前に出力された数列はどれもその数列より辞書順で小さくなければならない。


入力例 1

3 8

出力例 1

0 1 2
0 4 0
1 2 1
2 0 2
2 3 0
3 1 1
4 2 0
5 0 1
6 1 0
8 0 0

例えば、列 (0,1,2) は 0\times1+1\times2+2\times3=0+2+6=8 となるため条件を満たします。 条件を満たす列であってこれより辞書順で小さいものは存在しないため、1 行目には 0 1 2 を出力してください。

(0,1,2) とあわせて 10 個の列が条件を満たします。 これらを辞書順で小さいほうから出力してください。


入力例 2

1 200000

出力例 2

200000

入力例 3

8 9

出力例 3

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

Score : 400 points

Problem Statement

Output all length-N sequences A=(A _ 1,A _ 2,\ldots,A _ N) consisting of non-negative integers that satisfy \displaystyle\sum _ {i=1} ^ Ni\times A _ i=K, in lexicographic order from smallest to largest.

Here, you will only receive inputs such that the number of sequences satisfying the condition is at most 3\times10 ^ 5.

What is lexicographic order for sequences?

A sequence S = (S_1,S_2,\ldots,S_{|S|}) is said to be lexicographically smaller than a sequence T = (T_1,T_2,\ldots,T_{|T|}) if either 1. or 2. below holds. Here, |S| and |T| denote the lengths of S and T, respectively.

  1. |S| \lt |T| and (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}).
  2. There exists an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace such that both of the following hold.
    • (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
    • S_i is (numerically) smaller than T_i.

Constraints

  • 1\le N\le 10
  • 1\le K\le 2\times10 ^ 5
  • The number of sequences satisfying the condition is at most 3\times10 ^ 5.
  • All input values are integers.

Input

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

N K

Output

Let q be the number of sequences of non-negative integers satisfying the condition; output them over q lines. Each line should contain the elements of a sequence of non-negative integers satisfying the condition, in order, separated by spaces. For every sequence, all sequences outputed before it must be lexicographically smaller than it.


Sample Input 1

3 8

Sample Output 1

0 1 2
0 4 0
1 2 1
2 0 2
2 3 0
3 1 1
4 2 0
5 0 1
6 1 0
8 0 0

For example, for the sequence (0,1,2) we have 0\times1+1\times2+2\times3=0+2+6=8, so it satisfies the condition. There is no sequence satisfying the condition that is lexicographically smaller than this one, so output 0 1 2 on the first line.

Including (0,1,2), ten sequences satisfy the condition. Output these in lexicographic order from smallest to largest.


Sample Input 2

1 200000

Sample Output 2

200000

Sample Input 3

8 9

Sample Output 3

0 0 0 1 1 0 0 0
0 0 1 0 0 1 0 0
0 0 3 0 0 0 0 0
0 1 0 0 0 0 1 0
0 1 1 1 0 0 0 0
0 2 0 0 1 0 0 0
0 3 1 0 0 0 0 0
1 0 0 0 0 0 0 1
1 0 0 2 0 0 0 0
1 0 1 0 1 0 0 0
1 1 0 0 0 1 0 0
1 1 2 0 0 0 0 0
1 2 0 1 0 0 0 0
1 4 0 0 0 0 0 0
2 0 0 0 0 0 1 0
2 0 1 1 0 0 0 0
2 1 0 0 1 0 0 0
2 2 1 0 0 0 0 0
3 0 0 0 0 1 0 0
3 0 2 0 0 0 0 0
3 1 0 1 0 0 0 0
3 3 0 0 0 0 0 0
4 0 0 0 1 0 0 0
4 1 1 0 0 0 0 0
5 0 0 1 0 0 0 0
5 2 0 0 0 0 0 0
6 0 1 0 0 0 0 0
7 1 0 0 0 0 0 0
9 0 0 0 0 0 0 0
H - Sort Arrays

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

配点 : 450 点

問題文

N+1 個の数列 A_0, A_1, \ldots, A_{N} があります。A_i は以下のように定義されます。

  • A_0 は空の数列
  • A_i\ (1\leq i\leq N) は数列 A_{x_i}\ (0\leq x_i\lt i) の末尾に整数 y_i を追加することで得られる数列

以下の条件を満たす (1,2,\ldots,N) の順列 P=(P_1, P_2,\ldots,P_N) を求めてください。

  • i = 1,2,\ldots,N-1 に対して、以下のいずれかが成り立つ。
    • A_{P_i} は辞書順で A_{P_{i+1}} より小さい。
    • A_{P_i}= A_{P_{i+1}} かつ P_i\lt P_{i+1}

言い換えると、A_1,A_2,\ldots,A_N を辞書順で小さいものから順に並べたとき(等しい列が複数ある場合には添え字が小さいものを先にするように並べる)、その並びに現れる添え字の列が P です。

数列の辞書順とは?

数列 S = (S_1,S_2,\ldots,S_{|S|}) が数列 T = (T_1,T_2,\ldots,T_{|T|}) より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、|S|, |T| はそれぞれ S, T の長さを表します。

  1. |S| \lt |T| かつ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})。
  2. ある整数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存在して、下記の 2 つがともに成り立つ。
    • (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
    • S_i が T_i より(数として)小さい。

制約

  • 1\leq N\leq 3\times 10^5
  • 0\leq x_i\lt i
  • 1\leq y_i\leq 10^9
  • 入力は全て整数

入力

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

N
x_1 y_1
x_2 y_2
\vdots
x_N y_N

出力

P_1, P_2, \ldots, P_N を空白区切りで一行で出力せよ。


入力例 1

4
0 2
0 1
2 2
0 1

出力例 1

2 4 3 1

A_1 = (2), A_2 = (1), A_3 = (1, 2), A_4 = (1) なので P=(2,4,3,1) です。


入力例 2

5
0 1
0 1
0 1
0 1
0 1

出力例 2

1 2 3 4 5

A_1 = A_2 = A_3 = A_4 = A_5 = (1) です。


入力例 3

10
0 305186313
1 915059758
0 105282054
1 696409999
3 185928366
3 573289179
6 254538849
3 105282054
5 696409999
8 168629803

出力例 3

3 8 10 5 9 6 7 1 4 2

Score : 450 points

Problem Statement

There are N+1 sequences A_0, A_1, \ldots, A_{N}. A_i is defined as follows:

  • A_0 is an empty sequence.
  • A_i\ (1\leq i\leq N) is a sequence obtained by appending an integer y_i to the end of the sequence A_{x_i}\ (0\leq x_i\lt i).

Find the permutation P=(P_1, P_2,\ldots,P_N) of (1,2,\ldots,N) that satisfies the following condition:

  • For i = 1,2,\ldots,N-1, one of the following holds:
    • A_{P_i} is lexicographically smaller than A_{P_{i+1}}.
    • A_{P_i}= A_{P_{i+1}} and P_i\lt P_{i+1}

In other words, when A_1,A_2,\ldots,A_N are arranged in lexicographical order (when there are multiple equal sequences, arrange those with smaller indices first), P is the sequence of indices that appears in that arrangement.

What is the lexicographical order of sequences?

A sequence S = (S_1,S_2,\ldots,S_{|S|}) is lexicographically smaller than a sequence T = (T_1,T_2,\ldots,T_{|T|}) if one of the following two conditions holds. Here, |S| and |T| represent the lengths of S and T, respectively.

  1. |S| \lt |T| and (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}).
  2. There exists an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace such that both of the following hold:
    • (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
    • S_i is (numerically) smaller than T_i.

Constraints

  • 1\leq N\leq 3\times 10^5
  • 0\leq x_i\lt i
  • 1\leq y_i\leq 10^9
  • All input values are integers.

Input

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

N
x_1 y_1
x_2 y_2
\vdots
x_N y_N

Output

Output P_1, P_2, \ldots, P_N in one line, separated by spaces.


Sample Input 1

4
0 2
0 1
2 2
0 1

Sample Output 1

2 4 3 1

A_1 = (2), A_2 = (1), A_3 = (1, 2), A_4 = (1), so P=(2,4,3,1).


Sample Input 2

5
0 1
0 1
0 1
0 1
0 1

Sample Output 2

1 2 3 4 5

A_1 = A_2 = A_3 = A_4 = A_5 = (1).


Sample Input 3

10
0 305186313
1 915059758
0 105282054
1 696409999
3 185928366
3 573289179
6 254538849
3 105282054
5 696409999
8 168629803

Sample Output 3

3 8 10 5 9 6 7 1 4 2
I - Palindromic Expression

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

配点 : 500 点

問題文

整数 N が与えられます。 次の条件を全て満たす文字列 S としてあり得るものを 1 個出力してください。そのような文字列が存在しなければ -1 を出力してください。

  • S は 1, 2, 3, 4, 5, 6, 7, 8, 9 および * (乗算記号) からなる長さ 1 以上 1000 以下の文字列である。
  • S は回文である。
  • S の先頭の文字は数字である。
  • S を式として評価した値が N と一致する。

制約

  • 1 \leq N \leq 10^{12}
  • N は整数

入力

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

N

出力

問題文の条件を満たす文字列が存在する場合はその文字列を、そうでない場合は -1 を出力せよ。


入力例 1

363

出力例 1

11*3*11

S = 11*3*11 は問題文の条件を満たします。他に条件を満たす文字列として S= 363 があります。


入力例 2

101

出力例 2

-1

S は 0 を含んではいけない点に注意してください。


入力例 3

3154625100

出力例 3

2*57*184481*75*2

Score : 500 points

Problem Statement

You are given an integer N. Print a string S that satisfies all of the following conditions. If no such string exists, print -1.

  • S is a string of length between 1 and 1000, inclusive, consisting of the characters 1, 2, 3, 4, 5, 6, 7, 8, 9, and * (multiplication symbol).
  • S is a palindrome.
  • The first character of S is a digit.
  • The value of S when evaluated as a formula equals N.

Constraints

  • 1 \leq N \leq 10^{12}
  • N is an integer.

Input

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

N

Output

If there is a string S that satisfies the conditions exists, print such a string. Otherwise, print -1.


Sample Input 1

363

Sample Output 1

11*3*11

S = 11*3*11 satisfies the conditions in the problem statement. Another string that satisfies the conditions is S= 363.


Sample Input 2

101

Sample Output 2

-1

Note that S must not contain the digit 0.


Sample Input 3

3154625100

Sample Output 3

2*57*184481*75*2