A - Weekly Records

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君は N 週間の間、自分が歩いた歩数を記録しました。i 日目に歩いた歩数は A_i でした。

各週に高橋君が歩いた歩数の合計を求めてください。
より正確には、1 週目( 1 日目から 7 日目まで)の 7 日間の歩数の合計、2 週目( 8 日目から 14 日目まで)の 7 日間の歩数の合計、……をそれぞれ求めてください。

制約

  • 1 \leq N \leq 10
  • 0 \leq A_i \leq 10^5
  • 入力は整数

入力

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

N
A_1 A_2 \ldots A_{7N}

出力

i 週目に歩いた歩数を B_i とする。B_1,B_2,\ldots,B_N をこの順に空白区切りで出力せよ。


入力例 1

2
1000 2000 3000 4000 5000 6000 7000 2000 3000 4000 5000 6000 7000 8000

出力例 1

28000 35000

高橋君は 1 週目に 1000+2000+3000+4000+5000+6000+7000=28000 歩あるき、2 週目に 2000+3000+4000+5000+6000+7000+8000=35000 歩あるきました。


入力例 2

3
14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 6286 20899 86280 34825 34211 70679 82148

出力例 2

314333 419427 335328

Score : 100 points

Problem Statement

Takahashi has recorded the number of steps he walked for N weeks. He walked A_i steps on the i-th day.

Find the total number of steps Takahashi walked each week.
More precisely, find the sum of the steps for the first week (the 1-st through 7-th day), the sum of the steps for the second week (the 8-th through 14-th day), and so on.

Constraints

  • 1 \leq N \leq 10
  • 0 \leq A_i \leq 10^5
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_{7N}

Output

Let B_i be the number of steps walked for the i-th week. Print B_1,B_2,\ldots,B_N in this order, separated by spaces.


Sample Input 1

2
1000 2000 3000 4000 5000 6000 7000 2000 3000 4000 5000 6000 7000 8000

Sample Output 1

28000 35000

For the first week, he walked 1000+2000+3000+4000+5000+6000+7000=28000 steps, and for the second week, he walked 2000+3000+4000+5000+6000+7000+8000=35000 steps.


Sample Input 2

3
14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 6286 20899 86280 34825 34211 70679 82148

Sample Output 2

314333 419427 335328
B - ABC -> AC

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英大文字からなる文字列 S が与えられます。ここで、S の長さは奇数です。

S の中央の文字を削除して得られる文字列を出力してください。ただし、S の中央の文字とは S の長さを L として S\frac{L+1}{2} 文字目の文字を指します。

制約

  • S は英大文字からなる文字列
  • S の長さは 3 以上 9 以下の奇数

入力

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

S

出力

問題文中の指示に従い、文字列を出力せよ。


入力例 1

ABCDE

出力例 1

ABDE

ABCDE の中央の文字は 3 文字目の C であるため、ABDE を出力します。


入力例 2

OOO

出力例 2

OO

入力例 3

ATCODER

出力例 3

ATCDER

Score : 100 points

Problem Statement

You are given a string S consisting of uppercase English letters. Here, the length of S is odd.

Print the string obtained by deleting the middle character of S. The middle character of S is the \frac{L+1}{2}-th character of S, where L is the length of S.

Constraints

  • S is a string consisting of uppercase English letters.
  • The length of S is an odd number between 3 and 9, inclusive.

Input

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

S

Output

Print the string as instructed in the problem statement.


Sample Input 1

ABCDE

Sample Output 1

ABDE

The middle character of ABCDE is the 3-rd character C, so print ABDE.


Sample Input 2

OOO

Sample Output 2

OO

Sample Input 3

ATCODER

Sample Output 3

ATCDER
C - Strictly Superior

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

AtCoder 商店には N 個の商品があります。 i\ (1\leq i\leq N) 番目の商品の価格は P _ i です。 i\ (1\leq i\leq N) 番目の商品は C _ i 個の機能をもち、i\ (1\leq i\leq N) 番目の商品の j\ (1\leq j\leq C _ i) 番目の機能は 1 以上 M 以下の整数 F _ {i,j} として表されます。

高橋くんは、AtCoder 商店の商品で一方が一方の上位互換であるものがないか気になりました。 i 番目の商品と j 番目の商品 (1\leq i,j\leq N) であって、次の条件をすべて満たすものがあるとき Yes と、ないとき No と出力してください。

  • P _ i\geq P _ j である。
  • j 番目の製品は i 番目の製品がもつ機能をすべてもつ。
  • P _ i\gt P _ j であるか、j 番目の製品は i 番目の製品にない機能を 1 つ以上もつ。

制約

  • 2\leq N\leq100
  • 1\leq M\leq100
  • 1\leq P _ i\leq10^5\ (1\leq i\leq N)
  • 1\leq C _ i\leq M\ (1\leq i\leq N)
  • 1\leq F _ {i,1}\lt F _ {i,2}\lt\cdots\lt F _ {i,C _ i}\leq M\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N M
P _ 1 C _ 1 F _ {1,1} F _ {1,2} \ldots F _ {1,C _ 1}
P _ 2 C _ 2 F _ {2,1} F _ {2,2} \ldots F _ {2,C _ 2}
\vdots
P _ N C _ N F _ {N,1} F _ {N,2} \ldots F _ {N,C _ N}

出力

答えを 1 行で出力せよ。


入力例 1

5 6
10000 2 1 3
15000 3 1 2 4
30000 3 1 3 5
35000 2 1 5
100000 6 1 2 3 4 5 6

出力例 1

Yes

(i,j)=(4,3) とすると、条件を全て満たします。

他の組は条件を満たしません。例えば (i,j)=(4,5) とすると j 番目の商品は i 番目の商品の機能をすべてもっていますが、P _ i\lt P _ j なので上位互換ではありません。


入力例 2

4 4
3 1 1
3 1 2
3 1 2
4 2 2 3

出力例 2

No

まったく同じ価格と機能をもった商品がある場合もあります。


入力例 3

20 10
72036 3 3 4 9
7716 4 1 2 3 6
54093 5 1 6 7 8 10
25517 7 3 4 5 6 7 9 10
96930 8 2 3 4 6 7 8 9 10
47774 6 2 4 5 6 7 9
36959 5 1 3 4 5 8
46622 7 1 2 3 5 6 8 10
34315 9 1 3 4 5 6 7 8 9 10
54129 7 1 3 4 6 7 8 9
4274 5 2 4 7 9 10
16578 5 2 3 6 7 9
61809 4 1 2 4 5
1659 5 3 5 6 9 10
59183 5 1 2 3 4 9
22186 4 3 5 6 8
98282 4 1 4 7 10
72865 8 1 2 3 4 6 8 9 10
33796 6 1 3 5 7 9 10
74670 4 1 2 6 8

出力例 3

Yes

Score : 200 points

Problem Statement

AtCoder Shop has N products. The price of the i-th product (1\leq i\leq N) is P _ i. The i-th product (1\leq i\leq N) has C_i functions. The j-th function (1\leq j\leq C _ i) of the i-th product (1\leq i\leq N) is represented as an integer F _ {i,j} between 1 and M, inclusive.

Takahashi wonders whether there is a product that is strictly superior to another. If there are i and j (1\leq i,j\leq N) such that the i-th and j-th products satisfy all of the following conditions, print Yes; otherwise, print No.

  • P _ i\geq P _ j.
  • The j-th product has all functions of the i-th product.
  • P _ i\gt P _ j, or the j-th product has one or more functions that the i-th product lacks.

Constraints

  • 2\leq N\leq100
  • 1\leq M\leq100
  • 1\leq P _ i\leq10^5\ (1\leq i\leq N)
  • 1\leq C _ i\leq M\ (1\leq i\leq N)
  • 1\leq F _ {i,1}\lt F _ {i,2}\lt\cdots\lt F _ {i,C _ i}\leq M\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N M
P _ 1 C _ 1 F _ {1,1} F _ {1,2} \ldots F _ {1,C _ 1}
P _ 2 C _ 2 F _ {2,1} F _ {2,2} \ldots F _ {2,C _ 2}
\vdots
P _ N C _ N F _ {N,1} F _ {N,2} \ldots F _ {N,C _ N}

Output

Print the answer in a single line.


Sample Input 1

5 6
10000 2 1 3
15000 3 1 2 4
30000 3 1 3 5
35000 2 1 5
100000 6 1 2 3 4 5 6

Sample Output 1

Yes

(i,j)=(4,3) satisfies all of the conditions.

No other pair satisfies them. For instance, for (i,j)=(4,5), the j-th product has all functions of the i-th one, but P _ i\lt P _ j, so it is not strictly superior.


Sample Input 2

4 4
3 1 1
3 1 2
3 1 2
4 2 2 3

Sample Output 2

No

Multiple products may have the same price and functions.


Sample Input 3

20 10
72036 3 3 4 9
7716 4 1 2 3 6
54093 5 1 6 7 8 10
25517 7 3 4 5 6 7 9 10
96930 8 2 3 4 6 7 8 9 10
47774 6 2 4 5 6 7 9
36959 5 1 3 4 5 8
46622 7 1 2 3 5 6 8 10
34315 9 1 3 4 5 6 7 8 9 10
54129 7 1 3 4 6 7 8 9
4274 5 2 4 7 9 10
16578 5 2 3 6 7 9
61809 4 1 2 4 5
1659 5 3 5 6 9 10
59183 5 1 2 3 4 9
22186 4 3 5 6 8
98282 4 1 4 7 10
72865 8 1 2 3 4 6 8 9 10
33796 6 1 3 5 7 9 10
74670 4 1 2 6 8

Sample Output 3

Yes
D - KEYENCE building

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

1 から N の番号がついた N 人の人がいます。

i はキーエンス本社ビルの建築面積を S_i 平方メートルであると予想しました。

キーエンス本社ビルは下図のような形をしています。ただし、a,b はある 正の整数 です。
つまり、キーエンス本社ビルの建築面積は 4ab+3a+3b 平方メートルと表されます。

N 人のうち、この情報のみによって、予想した面積が確実に誤りであるとわかる人数を求めてください。

キーエンス本社ビル見取り図

制約

  • 1 \leq N \leq 20
  • 1 \leq S_i \leq 1000
  • 入力に含まれる値は全て整数である

入力

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

N
S_1 \ldots S_N

出力

答えを出力せよ。


入力例 1

3
10 20 39

出力例 1

1

a=1,b=1 のとき面積は 10 平方メートル、a=2,b=3 のとき面積は 39 平方メートルとなります。

しかし a,b がどのような正の整数であったとしても面積が 20 平方メートルになることはありません。

よって、人 2 の予想だけは確実に誤りであることがわかります。


入力例 2

5
666 777 888 777 666

出力例 2

3

Score : 200 points

Problem Statement

There are N people numbered 1 to N.

Person i guessed the building area of KEYENCE headquarters building to be S_i square meters.

The shape of KEYENCE headquarters building is shown below, where a and b are some positive integers.
That is, the building area of the building can be represented as 4ab+3a+3b.

Based on just this information, how many of the N people are guaranteed to be wrong in their guesses?

Sketch of KEYENCE headquarters building

Constraints

  • 1 \leq N \leq 20
  • 1 \leq S_i \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
S_1 \ldots S_N

Output

Print the answer.


Sample Input 1

3
10 20 39

Sample Output 1

1

The area would be 10 square meters if a=1,b=1, and 39 square meters if a=2,b=3.

However, no pair of positive integers a and b would make the area 20 square meters.

Thus, we can only be sure that Person 2 guessed wrong.


Sample Input 2

5
666 777 888 777 666

Sample Output 2

3
E - 343

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

正整数 N が与えられます。

N 以下の正整数であって回文立方数であるものの最大値を求めてください。

ただし、正整数 K は以下の 2 つの条件を満たすとき、またそのときに限り回文立方数であると定義します。

  • ある正整数 x が存在し、x^3 = K を満たす。
  • K を先頭に 0 をつけずに 10 進表記した文字列が回文となる。より厳密には、0 以上 9 以下の整数 A_0, A_1, \ldots, A_{L-2} および 1 以上 9 以下の整数 A_{L-1} を用いて K = \sum_{i = 0}^{L-1} A_i10^i と表記したときに i = 0, 1, \ldots, L-1 に対して A_i = A_{L-1-i} を満たす。

制約

  • N10^{18} 以下の正整数

入力

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

N

出力

答えを出力せよ。


入力例 1

345

出力例 1

343

343 は回文立方数であり、344, 345 は回文立方数ではありません。したがって、343 が答えとなります。


入力例 2

6

出力例 2

1

入力例 3

123456789012345

出力例 3

1334996994331

Score: 250 points

Problem Statement

You are given a positive integer N.

Find the maximum value of a palindromic cube number not greater than N.

Here, a positive integer K is defined to be a palindromic cube number if and only if it satisfies the following two conditions:

  • There is a positive integer x such that x^3 = K.
  • The decimal representation of K without leading zeros is a palindrome. More precisely, if K is represented as K = \sum_{i = 0}^{L-1} A_i10^i using integers A_0, A_1, \ldots, A_{L-2} between 0 and 9, inclusive, and an integer A_{L-1} between 1 and 9, inclusive, then A_i = A_{L-1-i} for all i = 0, 1, \ldots, L-1.

Constraints

  • N is a positive integer not greater than 10^{18}.

Input

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

N

Output

Print the answer.


Sample Input 1

345

Sample Output 1

343

343 is a palindromic cube number, while 344 and 345 are not. Thus, the answer is 343.


Sample Input 2

6

Sample Output 2

1

Sample Input 3

123456789012345

Sample Output 3

1334996994331
F - Remembering the Days

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

ある地方に、1 から N の番号がついた N 個の街と、1 から M の番号がついた M 本の道路があります。

i 番目の道路は街 A_i と街 B_i を双方向に結び、長さは C_i です。

好きな街からスタートして同じ街を二度以上通らずに別の街へ移動するときの、通る道路の長さの和としてありえる最大値を求めてください。

制約

  • 2 \leq N \leq 10
  • 1 \leq M \leq \frac{N(N-1)}{2}
  • 1\leq A_i < B_i \leq N
  • (A_i,B_i) は相異なる
  • 1\leq C_i \leq 10^8
  • 入力は全て整数である

入力

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

N M
A_1 B_1 C_1
\vdots
A_M B_M C_M

出力

答えを出力せよ。


入力例 1

4 4
1 2 1
2 3 10
1 3 100
1 4 1000

出力例 1

1110

4\to 1\to 3\to 2 と移動すると、通る道路の長さの和は 1110 となります。


入力例 2

10 1
5 9 1

出力例 2

1

道路と繋がっていない街が存在するかもしれません。


入力例 3

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

出力例 3

20

図

Score : 300 points

Problem Statement

A region has N towns numbered 1 to N, and M roads numbered 1 to M.

The i-th road connects town A_i and town B_i bidirectionally with length C_i.

Find the maximum possible total length of the roads you traverse when starting from a town of your choice and getting to another town without passing through the same town more than once.

Constraints

  • 2 \leq N \leq 10
  • 1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i < B_i \leq N
  • The pairs (A_i,B_i) are distinct.
  • 1\leq C_i \leq 10^8
  • All input values are integers.

Input

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

N M
A_1 B_1 C_1
\vdots
A_M B_M C_M

Output

Print the answer.


Sample Input 1

4 4
1 2 1
2 3 10
1 3 100
1 4 1000

Sample Output 1

1110

If you travel as 4\to 1\to 3\to 2, the total length of the roads you traverse is 1110.


Sample Input 2

10 1
5 9 1

Sample Output 2

1

There may be a town that is not connected to a road.


Sample Input 3

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

Sample Output 3

20

Figure

G - String Bags

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

あなたは最初、空文字列 S を持っています。
さらに、文字列がいくつか入った袋 1,2,\dots,N があります。
i には A_i 個の文字列 S_{i,1},S_{i,2},\dots,S_{i,A_i} が入っています。

これから、以下の手順を i=1,2,\dots,N について繰り返します。

  • 以下のふたつの行動のうち、どちらかを選択して行う。
    • 1 円を支払い、袋 i からちょうどひとつの文字列を選択して S の末尾に連結する。
    • 何もしない。

文字列 T が与えられるとき、最終的に ST を一致させるために必要な最小の金額を求めてください。
但し、どのようにしても最終的な ST に一致させることができない場合、 -1 と出力してください。

制約

  • T は長さ 1 以上 100 以下の英小文字からなる文字列
  • N1 以上 100 以下の整数
  • A_i1 以上 10 以下の整数
  • S_{i,j} は長さ 1 以上 10 以下の英小文字からなる文字列

入力

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

T
N
A_1 S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2 S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N S_{N,1} S_{N,2} \dots S_{N,A_N}

出力

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


入力例 1

abcde
3
3 ab abc abcd
4 f c cd bcde
2 e de

出力例 1

2

例えば、以下のようにすると 2 円で最終的な ST を一致させることができ、これが必要な金額の最低値であることが示せます。

  • i=1 について、袋 1 から abc を選択し S の末尾に連結する。 S= abc となる。
  • i=2 について、何もしない。
  • i=3 について、袋 3 から de を選択し S の末尾に連結する。 S= abcde となる。

入力例 2

abcde
3
2 ab abc
3 f c bcde
1 e

出力例 2

-1

どのようにしても最終的な ST を一致させることができないので、 -1 と出力してください。


入力例 3

aaabbbbcccc
6
2 aa aaa
2 dd ddd
2 ab aabb
4 bbaa bbbc bbb bbcc
2 cc bcc
3 ccc cccc ccccc

出力例 3

4

Score: 425 points

Problem Statement

You initially have an empty string S.
Additionally, there are bags 1, 2, \dots, N, each containing some strings.
Bag i contains A_i strings S_{i,1}, S_{i,2}, \dots, S_{i,A_i}.

You will repeat the following steps for i = 1, 2, \dots, N:

  • Choose and perform one of the following two actions:
    • Pay 1 yen, select exactly one string from bag i, and concatenate it to the end of S.
    • Do nothing.

Given a string T, find the minimum amount of money required to make the final S equal T.
If there is no way to make the final S equal T, print -1.

Constraints

  • T is a string consisting of lowercase English letters with length between 1 and 100, inclusive.
  • N is an integer between 1 and 100, inclusive.
  • A_i is an integer between 1 and 10, inclusive.
  • S_{i,j} is a string consisting of lowercase English letters with length between 1 and 10, inclusive.

Input

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

T
N
A_1 S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2 S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N S_{N,1} S_{N,2} \dots S_{N,A_N}

Output

Print the answer as an integer.


Sample Input 1

abcde
3
3 ab abc abcd
4 f c cd bcde
2 e de

Sample Output 1

2

For example, doing the following makes the final S equal T with two yen, which can be shown to be the minimum amount required.

  • For i=1, select abc from bag 1 and concatenate it to the end of S, making S= abc.
  • For i=2, do nothing.
  • For i=3, select de from bag 3 and concatenate it to the end of S, making S= abcde.

Sample Input 2

abcde
3
2 ab abc
3 f c bcde
1 e

Sample Output 2

-1

There is no way to make the final S equal T, so print -1.


Sample Input 3

aaabbbbcccc
6
2 aa aaa
2 dd ddd
2 ab aabb
4 bbaa bbbc bbb bbcc
2 cc bcc
3 ccc cccc ccccc

Sample Output 3

4
H - K-th Largest Connected Components

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

N 頂点 0 辺の無向グラフがあります。頂点には 1 から N の番号がつけられています。

Q 個のクエリが与えられるので、与えられた順に処理してください。各クエリは以下の 2 種類のいずれかです。

  • タイプ 1 : 1 u v の形式で与えられる。頂点 u と頂点 v の間に辺を追加する。
  • タイプ 2 : 2 v k の形式で与えられる。頂点 v と連結な頂点の中で、k 番目に頂点番号が大きいものを出力する。ただし、頂点 v と連結な頂点が k 個未満のときは -1 を出力する。

制約

  • 1\leq N,Q\leq 2\times 10^5
  • タイプ 1 のクエリにおいて、1\leq u\lt v\leq N
  • タイプ 2 のクエリにおいて、1\leq v\leq N, 1\leq k\leq 10
  • 入力は全て整数

入力

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

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

ただし、\mathrm{query}_ii 個目のクエリであり、以下のいずれかの形式で与えられる。

1 u v
2 v k

出力

タイプ 2 のクエリの個数を q として、q 行出力せよ。i 行目には i 個目のタイプ 2 に対するクエリの答えを出力せよ。


入力例 1

4 10
1 1 2
2 1 1
2 1 2
2 1 3
1 1 3
1 2 3
1 3 4
2 1 1
2 1 3
2 1 5

出力例 1

2
1
-1
4
2
-1
  • 1 個目のクエリについて、頂点 1 と頂点 2 の間に辺が追加されます。
  • 2 個目のクエリについて、頂点 1 と連結な頂点は 1,22 つです。この中で 1 番目に大きい 2 を出力します。
  • 3 個目のクエリについて、頂点 1 と連結な頂点は 1,22 つです。この中で 2 番目に大きい 1 を出力します。
  • 4 個目のクエリについて、頂点 1 と連結な頂点は 1,22 つです。3 個未満なので -1 を出力します。
  • 5 個目のクエリについて、頂点 1 と頂点 3 の間に辺が追加されます。
  • 6 個目のクエリについて、頂点 2 と頂点 3 の間に辺が追加されます。
  • 7 個目のクエリについて、頂点 3 と頂点 4 の間に辺が追加されます。
  • 8 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,44 つです。この中で 1 番目に大きい 4 を出力します。
  • 9 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,44 つです。この中で 3 番目に大きい 2 を出力します。
  • 10 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,44 つです。5 個未満なので -1 を出力します。

入力例 2

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

出力例 2

1
5
-1
3
6
2
5
-1
5
3
6
4
4

Score : 475 points

Problem Statement

There is an undirected graph with N vertices and 0 edges. The vertices are numbered 1 to N.

You are given Q queries to process in order. Each query is of one of the following two types:

  • Type 1: Given in the format 1 u v. Add an edge between vertices u and v.
  • Type 2: Given in the format 2 v k. Print the k-th largest vertex number among the vertices connected to vertex v. If there are fewer than k vertices connected to v, print -1.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • In a Type 1 query, 1 \leq u < v \leq N.
  • In a Type 2 query, 1 \leq v \leq N, 1 \leq k \leq 10.
  • All input values are integers.

Input

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

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_i is the i-th query and is given in one of the following formats:

1 u v
2 v k

Output

Let q be the number of Type 2 queries. Print q lines. The i-th line should contain the answer to the i-th Type 2 query.


Sample Input 1

4 10
1 1 2
2 1 1
2 1 2
2 1 3
1 1 3
1 2 3
1 3 4
2 1 1
2 1 3
2 1 5

Sample Output 1

2
1
-1
4
2
-1
  • In the first query, an edge is added between vertices 1 and 2.
  • In the second query, two vertices are connected to vertex 1: 1 and 2. Among them, the 1-st largest vertex number is 2, which should be printed.
  • In the third query, two vertices are connected to vertex 1: 1 and 2. Among them, the 2-nd largest vertex number is 1, which should be printed.
  • In the fourth query, two vertices are connected to vertex 1: 1 and 2, which is fewer than 3, so print -1.
  • In the fifth query, an edge is added between vertices 1 and 3.
  • In the sixth query, an edge is added between vertices 2 and 3.
  • In the seventh query, an edge is added between vertices 3 and 4.
  • In the eighth query, four vertices are connected to vertex 1: 1,2,3,4. Among them, the 1-st largest vertex number is 4, which should be printed.
  • In the ninth query, four vertices are connected to vertex 1: 1,2,3,4. Among them, the 3-rd largest vertex number is 2, which should be printed.
  • In the tenth query, four vertices are connected to vertex 1: 1,2,3,4, which is fewer than 5, so print -1.

Sample Input 2

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

Sample Output 2

1
5
-1
3
6
2
5
-1
5
3
6
4
4
I - MST Query

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

頂点に 1 から N の番号が、辺に 1 から N-1 の番号が付いた N 頂点 N-1 辺の重み付き無向連結グラフ G が与えられます。辺 i は頂点 a_i と頂点 b_i を結んでおり、その重みは c_i です。

Q 個のクエリが与えられるので順に処理してください。i 番目のクエリは以下で説明されます。

  • 整数 u_i,v_i,w_i が与えられる。G の頂点 u_i,v_i の間に重み w_i の辺を追加する。その後、G の最小全域木に含まれる辺の重みの和を出力する。

制約

  • 2\leq N\leq 2 \times 10^5
  • 1\leq Q\leq 2 \times 10^5
  • 1\leq a_i\lt b_i\leq N
  • 1\leq u_i\lt v_i\leq N
  • 1\leq c_i,w_i\leq 10
  • クエリを処理する前のグラフは連結
  • 入力はすべて整数

入力

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

N Q
a_1 b_1 c_1
\vdots
a_{N-1} b_{N-1} c_{N-1}
u_1 v_1 w_1
\vdots
u_Q v_Q w_Q

出力

Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。


入力例 1

4 4
1 2 6
2 3 5
2 4 4
1 3 3
1 2 3
1 4 10
3 4 1

出力例 1

12
10
10
7

各クエリで辺を追加した後のグラフを示しています。最小全域木に含まれる辺は赤色で塗られています。


入力例 2

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

出力例 2

49
46
45
38
34
33

Score : 550 points

Problem Statement

You are given a weighted undirected connected graph G with N vertices and N-1 edges, where vertices are numbered 1 to N and edges are numbered 1 to N-1. Edge i connects vertices a_i and b_i with a weight of c_i.

You are given Q queries to process sequentially. The i-th query is described as follows:

  • You are given integers u_i, v_i, w_i. Add an edge with weight w_i between vertices u_i and v_i in G. Then, print the sum of the weights of the edges in a minimum spanning tree of G.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq a_i < b_i \leq N
  • 1 \leq u_i < v_i \leq N
  • 1 \leq c_i, w_i \leq 10
  • The graph is connected before processing the queries.
  • All input values are integers.

Input

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

N Q
a_1 b_1 c_1
\vdots
a_{N-1} b_{N-1} c_{N-1}
u_1 v_1 w_1
\vdots
u_Q v_Q w_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

4 4
1 2 6
2 3 5
2 4 4
1 3 3
1 2 3
1 4 10
3 4 1

Sample Output 1

12
10
10
7

Here is the graph after adding the edge for each query. The edges included in the minimum spanning tree are colored red.


Sample Input 2

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

Sample Output 2

49
46
45
38
34
33