E - Lining Up 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

1,2,\ldots,NN 人が一列に並んでいます。

並び方の情報が長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) として与えられます。

A _ i\ (1\leq i\leq N) は次のような情報を表しています。

  • A _ i=-1 のとき、人 i は先頭に並んでいる。
  • A _ i\neq -1 のとき、人 i は人 A _ i のすぐ後ろに並んでいる。

列に並んでいる人の番号を先頭から順番に出力してください。

制約

  • 1\leq N\leq3\times10 ^ 5
  • A _ i=-1 もしくは 1\leq A _ i\leq N\ (1\leq i\leq N)
  • 情報と矛盾しないような N 人の並び方がただ 1 通り存在する
  • 入力はすべて整数

入力

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

N
A _ 1 A _ 2 \ldots A _ N

出力

s _ 1,s _ 2,\ldots,s _ N がこの順に列に並んでいるとき、s _ 1,s _ 2,\ldots,s _ N をこの順に空白を区切りとして出力せよ。


入力例 1

6
4 1 -1 5 3 2

出力例 1

3 5 4 1 2 6

先頭から、人 3,5,4,1,2,6 がこの順に列に並んでいるとき、与えられた情報と一致しています。

実際、

  • 1 は人 4 のすぐ後ろに並んでいます。
  • 2 は人 1 のすぐ後ろに並んでいます。
  • 3 は先頭に並んでいます。
  • 4 は人 5 のすぐ後ろに並んでいます。
  • 5 は人 3 のすぐ後ろに並んでいます。
  • 6 は人 2 のすぐ後ろに並んでいます。

となり、与えられた情報と一致していることが確認できます。

よって、3,5,4,1,2,6 をこの順に空白区切りで出力してください。


入力例 2

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

出力例 2

1 2 3 4 5 6 7 8 9 10

入力例 3

30
3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7

出力例 3

10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14

Score: 300 points

Problem Statement

There are N people standing in a line: person 1, person 2, \ldots, person N.

You are given the arrangement of the people as a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.

A _ i\ (1\leq i\leq N) represents the following information:

  • if A _ i=-1, person i is at the front of the line;
  • if A _ i\neq -1, person i is right behind person A _ i.

Print the people's numbers in the line from front to back.

Constraints

  • 1\leq N\leq3\times10 ^ 5
  • A _ i=-1 or 1\leq A _ i\leq N\ (1\leq i\leq N)
  • There is exactly one way to arrange the N people consistent with the information given.
  • 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

If person s _ 1, person s _ 2, \ldots, person s _ N are standing in the line in this order, print s _ 1, s _ 2, \ldots, and s _ N in this order, separated by spaces.


Sample Input 1

6
4 1 -1 5 3 2

Sample Output 1

3 5 4 1 2 6

If person 3, person 5, person 4, person 1, person 2, and person 6 stand in line in this order from front to back, the arrangement matches the given information.

Indeed, it can be seen that:

  • person 1 is standing right behind person 4,
  • person 2 is standing right behind person 1,
  • person 3 is at the front of the line,
  • person 4 is standing right behind person 5,
  • person 5 is standing right behind person 3, and
  • person 6 is standing right behind person 2.

Thus, print 3, 5, 4, 1, 2, and 6 in this order, separated by spaces.


Sample Input 2

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

Sample Output 2

1 2 3 4 5 6 7 8 9 10

Sample Input 3

30
3 25 20 6 18 12 26 1 29 -1 21 17 23 9 8 30 10 15 22 27 4 13 5 11 16 24 28 2 19 7

Sample Output 3

10 17 12 6 4 21 11 24 26 7 30 16 25 2 28 27 20 3 1 8 15 18 5 23 13 22 19 29 9 14
F - Many Replacement

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

英小文字からなる長さ N の文字列 S が与えられます。

文字列 S に対して操作を Q 回行います。 i 回目 (1\leq i\leq Q) の操作は文字の組 (c _ i,d _ i) で表され、次のような操作に対応します。

  • S に含まれる文字 c _ i をすべて文字 d _ i で置き換える。

すべての操作が終わったあとの文字列 S を出力してください。

制約

  • 1\leq N\leq2\times10^5
  • S は英小文字からなる長さ N の文字列
  • 1\leq Q\leq2\times10^5
  • c _ i,d _ i は英小文字 (1\leq i\leq Q)
  • N,Q は整数

入力

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

N
S
Q
c _ 1 d _ 1
c _ 2 d _ 2
\vdots
c _ Q d _ Q

出力

すべての操作が終わったあとの S を出力せよ。


入力例 1

7
atcoder
4
r a
t e
d v
a r

出力例 1

recover

Satcoderatcodeaaecodeaaecovearecover と変化します。 たとえば、4 番目の操作では S={}aecovea に含まれる a1 文字目と 7 文字目)をすべて r に置き換えるので S={}recover となります。

すべての操作が終わったときには S={}recover となっているため、recover を出力してください。


入力例 2

3
abc
4
a a
s k
n n
z b

出力例 2

abc

c _ i=d _ i であるような操作や Sc _ i が含まれないような操作もあります。


入力例 3

34
supercalifragilisticexpialidocious
20
g c
l g
g m
c m
r o
s e
a a
o f
f s
e t
t l
d v
p k
v h
x i
h n
n j
i r
s i
u a

出力例 3

laklimamriiamrmrllrmlrkramrjimrial

Score: 350 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

You will perform an operation Q times on the string S. The i-th operation (1\leq i\leq Q) is represented by a pair of characters (c _ i,d _ i), which corresponds to the following operation:

  • Replace all occurrences of the character c _ i in S with the character d _ i.

Print the string S after all operations are completed.

Constraints

  • 1\leq N\leq2\times10^5
  • S is a string of length N consisting of lowercase English letters.
  • 1\leq Q\leq2\times10^5
  • c _ i and d _ i are lowercase English letters (1\leq i\leq Q).
  • N and Q are integers.

Input

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

N
S
Q
c _ 1 d _ 1
c _ 2 d _ 2
\vdots
c _ Q d _ Q

Output

Print the string S after all operations are completed.


Sample Input 1

7
atcoder
4
r a
t e
d v
a r

Sample Output 1

recover

S changes as follows: atcoderatcodeaaecodeaaecovearecover. For example, in the fourth operation, all occurrences of a in S={}aecovea (the first and seventh characters) are replaced with r, resulting in S={}recover.

After all operations are completed, S={}recover, so print recover.


Sample Input 2

3
abc
4
a a
s k
n n
z b

Sample Output 2

abc

There may be operations where c _ i=d _ i or S does not contain c _ i.


Sample Input 3

34
supercalifragilisticexpialidocious
20
g c
l g
g m
c m
r o
s e
a a
o f
f s
e t
t l
d v
p k
v h
x i
h n
n j
i r
s i
u a

Sample Output 3

laklimamriiamrmrllrmlrkramrjimrial
G - Sum of Maximum Weights

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

N 頂点の木があり、頂点は 1, 2, \dots, N と番号付けられています。
i \, (1 \leq i \leq N - 1) 番目の辺は頂点 u_i と頂点 v_i を結び、重みは w_i です。

異なる頂点 u, v に対し、頂点 u から頂点 v までの最短パスに含まれる辺の重みの最大値を f(u, v) とおきます。

\displaystyle \sum_{i = 1}^{N - 1} \sum_{j = i + 1}^N f(i, j) を求めてください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq u_i, v_i \leq N
  • 1 \leq w_i \leq 10^7
  • 与えられるグラフは木である。
  • 入力は全て整数である。

入力

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

N
u_1 v_1 w_1
\vdots
u_{N - 1} v_{N - 1} w_{N - 1}

出力

答えを出力せよ。


入力例 1

3
1 2 10
2 3 20

出力例 1

50

f(1, 2) = 10, f(2, 3) = 20, f(1, 3) = 20 であるので、これらの和である 50 を出力します。


入力例 2

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

出力例 2

76

Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1, 2, \dots, N.
The i-th edge (1 \leq i \leq N - 1) connects Vertex u_i and Vertex v_i and has a weight w_i.

For different vertices u and v, let f(u, v) be the greatest weight of an edge contained in the shortest path from Vertex u to Vertex v.

Find \displaystyle \sum_{i = 1}^{N - 1} \sum_{j = i + 1}^N f(i, j).

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq u_i, v_i \leq N
  • 1 \leq w_i \leq 10^7
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
\vdots
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print the answer.


Sample Input 1

3
1 2 10
2 3 20

Sample Output 1

50

We have f(1, 2) = 10, f(2, 3) = 20, and f(1, 3) = 20, so we should print their sum, or 50.


Sample Input 2

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

Sample Output 2

76
H - Square Price

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

N 種類の商品がそれぞれ 10^{100} 個ずつあります。

あなたはこれらの商品を各種類について 0 個以上買うことが出来ます。i 番目の商品を k 個買うには k^2P_i 円かかります。

購入金額の合計を M 円以下にするとき、最大何個の商品を買うことができるか求めてください。

制約

  • 1\leq N\leq 2\times 10^{5}
  • 1\leq M\leq 10^{18}
  • 1\leq P_i\leq 2\times 10^{9}
  • 入力される数値は全て整数

入力

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

N M
P_1 \ldots P_N

出力

答えを出力せよ。


入力例 1

3 9
4 1 9

出力例 1

3

1 種類目の商品を 1 個、2 種類目の商品を 2 個買うとき、購入金額の合計は 1^2 \times 4+2^2\times 1=8 です。 購入金額の合計が 9 円以下で 4 個以上の商品を買うことはできないため、3 が答えとなります。


入力例 2

10 1000
2 15 6 5 12 1 7 9 17 2

出力例 2

53

Score : 475 points

Problem Statement

There are N types of products, each having 10^{100} units in stock.

You can buy any non-negative number of units of each product. To buy k units of the i-th product, it costs k^2 P_i yen.

If your total purchase cost is at most M yen, what is the maximum number of units you can buy in total?

Constraints

  • 1 \leq N \leq 2 \times 10^{5}
  • 1 \leq M \leq 10^{18}
  • 1 \leq P_i \leq 2 \times 10^{9}
  • All input values are integers.

Input

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

N M
P_1 \ldots P_N

Output

Print the answer.


Sample Input 1

3 9
4 1 9

Sample Output 1

3

If you buy one unit of the 1st product and two units of the 2nd product, the total purchase cost is 1^2 \times 4 + 2^2 \times 1 = 8. It is impossible to buy four or more units in total with a total cost of at most 9 yen, so the answer is 3.


Sample Input 2

10 1000
2 15 6 5 12 1 7 9 17 2

Sample Output 2

53
I - Product Equality

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

N 個の整数 A_1,A_2,\dots,A_N が与えられます。
以下の条件を満たす整数の組 (i,j,k) の個数を求めてください。

  • 1 \le i,j,k \le N
  • A_i \times A_j = A_k

制約

  • 1 \le N \le 1000
  • \color{red}{1 \le A_i < 10^{1000}}

入力

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

N
A_1
A_2
\vdots
A_N

出力

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


入力例 1

5
2
3
6
12
24

出力例 1

6

問題文中の条件を満たす (i,j,k) の組は以下の 6 通りです。

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

入力例 2

11
1
2
3
4
5
6
123456789123456789
123456789123456789
987654321987654321
987654321987654321
121932631356500531347203169112635269

出力例 2

40

各整数 A_i の値が非常に大きくなりうることに注意してください。


入力例 3

9
4
4
4
2
2
2
1
1
1

出力例 3

162

A_i の値に重複がありうることに注意してください。

Score: 550 points

Problem Statement

You are given N integers A_1, A_2, \dots, A_N.
Find the number of triples of integers (i, j, k) that satisfy the following conditions:

  • 1 \le i, j, k \le N
  • A_i \times A_j = A_k

Constraints

  • 1 \le N \le 1000
  • \color{red}{1 \le A_i < 10^{1000}}

Input

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

N
A_1
A_2
\vdots
A_N

Output

Print the answer as an integer.


Sample Input 1

5
2
3
6
12
24

Sample Output 1

6

The following six triples (i, j, k) satisfy the conditions in the problem statement:

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

Sample Input 2

11
1
2
3
4
5
6
123456789123456789
123456789123456789
987654321987654321
987654321987654321
121932631356500531347203169112635269

Sample Output 2

40

Note that the values of each integer A_i can be huge.


Sample Input 3

9
4
4
4
2
2
2
1
1
1

Sample Output 3

162

Note that there may be duplicates among the values of A_i.