A - Glutton Takahashi

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

配点 : 100

問題文

高橋君は N 個の料理を食べようとしています。

i 番目に食べようとしている料理は、S_i = sweet のとき甘い料理であり、S_i = salty のとき塩辛い料理です。

高橋君は甘い料理を 2 つ連続で食べると気持ち悪くなってしまい、その後料理が食べられなくなってしまいます。

高橋君がすべての料理を食べることができるか判定してください。

制約

  • N1 以上 100 以下の整数
  • S_isweet または salty

入力

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

N
S_1
S_2
\vdots
S_N

出力

高橋君がすべての料理を食べることができるならば Yes を、できないならば No を出力せよ。


入力例 1

5
salty
sweet
salty
salty
sweet

出力例 1

Yes

高橋君は甘い料理を 2 つ連続で食べることがないので、気持ち悪くなることなくすべての料理を食べることができます。


入力例 2

4
sweet
salty
sweet
sweet

出力例 2

Yes

高橋君は気持ち悪くなってしまいますが、すべての料理を食べることができます。


入力例 3

6
salty
sweet
sweet
salty
sweet
sweet

出力例 3

No

高橋君は 3 番目の料理を食べると気持ち悪くなってしまい、4 番目以降の料理が食べられなくなります。

Score : 100 points

Problem Statement

Takahashi is planning to eat N dishes.

The i-th dish he plans to eat is sweet if S_i = sweet, and salty if S_i = salty.

If he eats two sweet dishes consecutively, he will feel sick and be unable to eat any more dishes.

Determine whether he can eat all the dishes.

Constraints

  • N is an integer between 1 and 100, inclusive.
  • Each S_i is sweet or salty.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print Yes if Takahashi can eat all the dishes, and No otherwise.


Sample Input 1

5
salty
sweet
salty
salty
sweet

Sample Output 1

Yes

He will not eat two sweet dishes consecutively, so he can eat all the dishes without feeling sick.


Sample Input 2

4
sweet
salty
sweet
sweet

Sample Output 2

Yes

He will feel sick but can still eat all the dishes.


Sample Input 3

6
salty
sweet
sweet
salty
sweet
sweet

Sample Output 3

No

He feels sick when eating the 3rd dish and cannot eat the 4th and subsequent dishes.

B - Grid Walk

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

配点 : 200

問題文

HW 列のグリッドがあります。グリッドの上から i 番目、左から j 番目のマスをマス (i, j) と表記します。

マス (i, j)C_{i, j}. のとき空きマスであり、# のとき空きマスではありません。

高橋君は現在マス (S_i, S_j) におり、i = 1, 2, \ldots, |X| の順に以下のルールに従って行動します。

  • Xi 文字目が L のとき、高橋君が現在いるマスの 1 つ左のマスが存在し、そのマスが空きマスならば 1 つ左のマスに移動する。そうでないならば、現在いるマスに留まる。
  • Xi 文字目が R のとき、高橋君が現在いるマスの 1 つ右のマスが存在し、そのマスが空きマスならば 1 つ右のマスに移動する。そうでないならば、現在いるマスに留まる。
  • Xi 文字目が U のとき、高橋君が現在いるマスの 1 つ上のマスが存在し、そのマスが空きマスならば 1 つ上のマスに移動する。そうでないならば、現在いるマスに留まる。
  • Xi 文字目が D のとき、高橋君が現在いるマスの 1 つ下のマスが存在し、そのマスが空きマスならば 1 つ下のマスに移動する。そうでないならば、現在いるマスに留まる。

一連の行動を終えた後高橋君がどのマスにいるか出力してください。

制約

  • 1 \leq H, W \leq 50
  • 1 \leq S_i \leq H
  • 1 \leq S_j \leq W
  • H, W, S_i, S_j は整数
  • C_{i, j}. または #
  • C_{S_i, S_j} = .
  • XL, R, U, D からなる長さ 1 以上 50 以下の文字列

入力

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

H W
S_i S_j
C_{1, 1}C_{1, 2}\ldotsC_{1, W}
C_{2, 1}C_{2, 2}\ldotsC_{2, W}
\vdots
C_{H, 1}C_{H, 2}\ldotsC_{H, W}
X

出力

高橋君が一連の行動を終えた後にいるマスをマス (x, y) として、xy をこの順に空白区切りで出力せよ。


入力例 1

2 3
2 1
.#.
...
ULDRU

出力例 1

2 2

高橋君ははじめマス (2, 1) にいます。高橋君の一連の行動は以下のようになります。

  • X1 文字目は U であり、マス (2, 1)1 つ上のマスは存在し、そのマスは空きマスであるため 1 つ上のマスであるマス (1, 1) に移動する。
  • X2 文字目は L であり、マス (1, 1)1 つ左のマスは存在しないためマス (1, 1) に留まる。
  • X3 文字目は D であり、マス (1, 1)1 つ下のマスは存在し、そのマスは空きマスであるため 1 つ下のマスであるマス (2, 1) に移動する。
  • X4 文字目は R であり、マス (2, 1)1 つ右のマスは存在し、そのマスは空きマスであるため 1 つ右のマスであるマス (2, 2) に移動する。
  • X5 文字目は U であり、マス (2, 2)1 つ上のマスは存在するが、そのマスは空きマスではないためマス (2, 2) に留まる。

したがって一連の行動を終えた後に高橋君がいるマスはマス (2, 2) です。


入力例 2

4 4
4 2
....
.#..
...#
....
DUUUURULRD

出力例 2

2 4

入力例 3

6 6
1 1
.#####
######
######
######
######
######
RURLDLULLRULRDL

出力例 3

1 1

Score : 200 points

Problem Statement

There is a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.

Cell (i, j) is empty if C_{i, j} is ., and not empty if C_{i, j} is #.

Takahashi is currently at cell (S_i, S_j), and he will act according to the following rules for i = 1, 2, \ldots, |X| in order.

  • If the i-th character of X is L, and the cell to the left of his current cell exists and is empty, he moves to the cell to the left. Otherwise, he stays in the current cell.
  • If the i-th character of X is R, and the cell to the right of his current cell exists and is empty, he moves to the cell to the right. Otherwise, he stays in the current cell.
  • If the i-th character of X is U, and the cell above his current cell exists and is empty, he moves to the cell above. Otherwise, he stays in the current cell.
  • If the i-th character of X is D, and the cell below his current cell exists and is empty, he moves to the cell below. Otherwise, he stays in the current cell.

Print the cell where he is after completing the series of actions.

Constraints

  • 1 \leq H, W \leq 50
  • 1 \leq S_i \leq H
  • 1 \leq S_j \leq W
  • H, W, S_i, S_j are integers.
  • C_{i, j} is . or #.
  • C_{S_i, S_j} = .
  • X is a string of length between 1 and 50, inclusive, consisting of L, R, U, D.

Input

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

H W
S_i S_j
C_{1, 1}C_{1, 2}\ldotsC_{1, W}
C_{2, 1}C_{2, 2}\ldotsC_{2, W}
\vdots
C_{H, 1}C_{H, 2}\ldotsC_{H, W}
X

Output

Let (x, y) be the cell where Takahashi is after completing the series of actions. Print x and y, separated by a space.


Sample Input 1

2 3
2 1
.#.
...
ULDRU

Sample Output 1

2 2

Takahashi starts at cell (2, 1). His series of actions are as follows:

  • The 1st character of X is U, and the cell above (2, 1) exists and is an empty cell, so he moves to the cell above, which is (1, 1).
  • The 2nd character of X is L, and the cell to the left of (1, 1) does not exist, so he stays at (1, 1).
  • The 3rd character of X is D, and the cell below (1, 1) exists and is an empty cell, so he moves to the cell below, which is (2, 1).
  • The 4th character of X is R, and the cell to the right of (2, 1) exists and is an empty cell, so he moves to the cell to the right, which is (2, 2).
  • The 5th character of X is U, and the cell above (2, 2) exists but is not an empty cell, so he stays at (2, 2).

Therefore, after completing the series of actions, he is at cell (2, 2).


Sample Input 2

4 4
4 2
....
.#..
...#
....
DUUUURULRD

Sample Output 2

2 4

Sample Input 3

6 6
1 1
.#####
######
######
######
######
######
RURLDLULLRULRDL

Sample Output 3

1 1
C - Minimum Glutton

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

配点 : 250

問題文

N 個の料理があり、i 個目の料理の甘さA_iしょっぱさB_i です。

高橋君はこれらの N 個の料理を好きな順番で並べ、その順に食べようとします。

高橋君は並べた順番の通りに料理を食べていきますが、食べた料理の甘さの合計が X より大きくなるかしょっぱさの合計が Y より大きくなるとその時点で食べるのをやめます。

高橋君が食べることになる料理の個数としてあり得る最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq X, Y \leq 2 \times 10^{14}
  • 1 \leq A_i, B_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N X Y
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

答えを出力せよ。


入力例 1

4 7 18
2 3 5 1
8 8 1 4

出力例 1

2

i 個目の料理のことを料理 i と書きます。

高橋君が 4 個の料理を料理 2, 3, 1, 4 の順に並べ替えたとき、料理 2, 3 を食べた時点での食べた料理の甘さの合計が 8 となり 7 より大きくなります。したがってこの場合は高橋君が食べることになる料理の個数は 2 個です。

高橋君が食べる料理の個数が 1 個以下になることはないため、2 を出力します。


入力例 2

5 200000000000000 200000000000000
1 1 1 1 1
2 2 2 2 2

出力例 2

5

入力例 3

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

出力例 3

6

Score : 250 points

Problem Statement

There are N dishes, and the i-th dish has a sweetness of A_i and a saltiness of B_i.

Takahashi plans to arrange these N dishes in any order he likes and eat them in that order.

He will eat the dishes in the arranged order, but he will stop eating as soon as the total sweetness of the dishes he has eaten exceeds X or the total saltiness exceeds Y.

Find the minimum possible number of dishes that he will end up eating.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq X, Y \leq 2 \times 10^{14}
  • 1 \leq A_i, B_i \leq 10^9
  • All input values are integers.

Input

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

N X Y
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print the answer.


Sample Input 1

4 7 18
2 3 5 1
8 8 1 4

Sample Output 1

2

The i-th dish will be denoted as dish i.

If he arranges the four dishes in the order 2, 3, 1, 4, as soon as he eats dishes 2 and 3, their total sweetness is 8, which is greater than 7. Therefore, in this case, he will end up eating two dishes.

The number of dishes he will eat cannot be 1 or less, so print 2.


Sample Input 2

5 200000000000000 200000000000000
1 1 1 1 1
2 2 2 2 2

Sample Output 2

5

Sample Input 3

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

Sample Output 3

6
D - K-th Nearest

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

配点 : 425

問題文

数直線上に N+Q 個の点 A_1,\dots,A_N,B_1,\dots,B_Q があり、点 A_i の座標は a_i、点 B_j の座標は b_j です。

j=1,2,\dots,Q それぞれについて、以下の問題に答えてください。

  • A_1,A_2,\dots,A_N のうち点 B_j との距離が k_j 番目に近い点を X としたとき、点 X と点 B_j との距離を求めよ。 より厳密には、点 A_i と点 B_j との距離を d_i として、(d_1,d_2,\dots,d_N) を昇順に並び替えてできる列を (d_1',d_2',\dots,d_N') としたとき、d_{k_j}' を求めよ。

制約

  • 1\leq N,Q \leq 10^5
  • -10^8\leq a_i,b_j \leq 10^8
  • 1\leq k_j\leq N
  • 入力は全て整数

入力

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

N Q
a_1 a_2 \dots a_N
b_1 k_1
b_2 k_2
\vdots
b_Q k_Q

出力

Q 行出力せよ。 l\ (1\leq l \leq Q) 行目には、j=l としたときの問題に対する答えを整数として出力せよ。


入力例 1

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

出力例 1

7
3
13

1 番目のクエリについて説明します。

A_1,A_2,A_3,A_4 と点 B_1 との距離は順に 1,1,7,8 なので、点 B_1 との距離が 3 番目に近いのは点 A_3 です。 よって、点 A_3 と点 B_1 との距離である 7 を出力します。


入力例 2

2 2
0 0
0 1
0 2

出力例 2

0
0

同じ座標に複数の点がある可能性もあります。


入力例 3

10 5
-84 -60 -41 -100 8 -8 -52 -62 -61 -76
-52 5
14 4
-2 6
46 2
26 7

出力例 3

11
66
59
54
88

Score : 425 points

Problem Statement

There are N+Q points A_1,\dots,A_N,B_1,\dots,B_Q on a number line, where point A_i has a coordinate a_i and point B_j has a coordinate b_j.

For each j=1,2,\dots,Q, answer the following question:

  • Let X be the point among A_1,A_2,\dots,A_N that is the k_j-th closest to point B_j. Find the distance between points X and B_j. More formally, let d_i be the distance between points A_i and B_j. Sort (d_1,d_2,\dots,d_N) in ascending order to get the sequence (d_1',d_2',\dots,d_N'). Find d_{k_j}'.

Constraints

  • 1 \leq N, Q \leq 10^5
  • -10^8 \leq a_i, b_j \leq 10^8
  • 1 \leq k_j \leq N
  • All input values are integers.

Input

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

N Q
a_1 a_2 \dots a_N
b_1 k_1
b_2 k_2
\vdots
b_Q k_Q

Output

Print Q lines. The l-th line (1 \leq l \leq Q) should contain the answer to the question for j=l as an integer.


Sample Input 1

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

Sample Output 1

7
3
13

Let us explain the first query.

The distances from points A_1, A_2, A_3, A_4 to point B_1 are 1, 1, 7, 8, respectively, so the 3rd closest to point B_1 is point A_3. Therefore, print the distance between point A_3 and point B_1, which is 7.


Sample Input 2

2 2
0 0
0 1
0 2

Sample Output 2

0
0

There may be multiple points with the same coordinates.


Sample Input 3

10 5
-84 -60 -41 -100 8 -8 -52 -62 -61 -76
-52 5
14 4
-2 6
46 2
26 7

Sample Output 3

11
66
59
54
88
E - Maximum Glutton

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

配点 : 475

問題文

高橋君はすぬけ君のために N 個の料理を作りました。 料理には 1 から N までの番号がつけられていて、料理 i甘さA_iしょっぱさB_i です。

高橋君はこれらの料理を好きな順番で並べることができます。 すぬけ君は料理を並べられた順に食べていきますが、ある時点においてそれまでに食べた料理の甘さの合計が X を超えるかしょっぱさの合計が Y を超えた場合、それ以降の料理は食べません。

高橋君は、すぬけ君にできるだけ多くの料理を食べてほしいと思っています。 高橋君がうまく料理を並べたとき、すぬけ君が最大で何個の料理を食べることになるか求めてください。

制約

  • 1\leq N \leq 80
  • 1\leq A_i,B_i \leq 10000
  • 1\leq X,Y \leq 10000
  • 入力は全て整数

入力

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

N X Y
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

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


入力例 1

4 8 4
1 5
3 2
4 1
5 3

出力例 1

3

高橋君が料理を 2,3,1,4 の順番で並べた場合のすぬけ君の行動を考えます。

  • まず料理 2 を食べる。ここまでに食べた料理の甘さの合計は 3、しょっぱさの合計は 2 である。
  • 次に料理 3 を食べる。ここまでに食べた料理の甘さの合計は 7、しょっぱさの合計は 3 である。
  • 次に料理 1 を食べる。ここまでに食べた料理の甘さの合計は 8、しょっぱさの合計は 8 である。
  • しょっぱさの合計が Y=4 を超えたので、これ以降の料理は食べない。

よって、この並び方の場合すぬけ君は 3 個の料理を食べることになります。

高橋君が料理をどのように並べてもすぬけ君が 4 つ全ての料理を食べることはないので、答えは 3 です。


入力例 2

2 1 1
3 2
3 2

出力例 2

1

入力例 3

2 100 100
3 2
3 2

出力例 3

2

入力例 4

6 364 463
230 381
154 200
328 407
339 94
193 10
115 309

出力例 4

3

Score : 475 points

Problem Statement

Takahashi has prepared N dishes for Snuke. The dishes are numbered from 1 to N, and dish i has a sweetness of A_i and a saltiness of B_i.

Takahashi can arrange these dishes in any order he likes. Snuke will eat the dishes in the order they are arranged, but if at any point the total sweetness of the dishes he has eaten so far exceeds X or the total saltiness exceeds Y, he will not eat any further dishes.

Takahashi wants Snuke to eat as many dishes as possible. Find the maximum number of dishes Snuke will eat if Takahashi arranges the dishes optimally.

Constraints

  • 1 \leq N \leq 80
  • 1 \leq A_i, B_i \leq 10000
  • 1 \leq X, Y \leq 10000
  • All input values are integers.

Input

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

N X Y
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answer as an integer.


Sample Input 1

4 8 4
1 5
3 2
4 1
5 3

Sample Output 1

3

Consider the scenario where Takahashi arranges the dishes in the order 2, 3, 1, 4.

  • First, Snuke eats dish 2. The total sweetness so far is 3, and the total saltiness is 2.
  • Next, Snuke eats dish 3. The total sweetness so far is 7, and the total saltiness is 3.
  • Next, Snuke eats dish 1. The total sweetness so far is 8, and the total saltiness is 8.
  • The total saltiness has exceeded Y=4, so Snuke will not eat any further dishes.

Thus, in this arrangement, Snuke will eat three dishes.

No matter how Takahashi arranges the dishes, Snuke will not eat all four dishes, so the answer is 3.


Sample Input 2

2 1 1
3 2
3 2

Sample Output 2

1

Sample Input 3

2 100 100
3 2
3 2

Sample Output 3

2

Sample Input 4

6 364 463
230 381
154 200
328 407
339 94
193 10
115 309

Sample Output 4

3
F - Range Connect MST

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

配点 : 500

問題文

N + Q 頂点のグラフがあり、頂点には 1, 2, \ldots, N + Q の番号がついています。グラフにははじめ辺がありません。

このグラフに対して i = 1, 2, \ldots, Q の順に以下の操作を行います。

  • L_i \leq j \leq R_i を満たす各整数 j について頂点 N + i と頂点 j の間にコスト C_i の無向辺を追加する

すべての操作を終えた後グラフは連結であるか判定し、連結である場合はこのグラフの最小全域木のコストを求めてください。

ただし、最小全域木とはコストが最小の全域木のことを指し、全域木のコストとは全域木に使われた辺のコストの和のことを指します。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq N
  • 1 \leq C_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N Q
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_Q R_Q C_Q

出力

グラフが連結である場合は最小全域木のコストを出力せよ。そうでない場合は -1 を出力せよ。


入力例 1

4 3
1 2 2
1 3 4
2 4 5

出力例 1

22

以下の辺からなる全域木が最小全域木のひとつとなります。

  • 頂点 15 を結ぶコスト 2 の辺
  • 頂点 25 を結ぶコスト 2 の辺
  • 頂点 16 を結ぶコスト 4 の辺
  • 頂点 36 を結ぶコスト 4 の辺
  • 頂点 37 を結ぶコスト 5 の辺
  • 頂点 47 を結ぶコスト 5 の辺

2 + 2 + 4 + 4 + 5 + 5 = 22 であるため、22 を出力します。


入力例 2

6 2
1 2 10
4 6 10

出力例 2

-1

グラフは非連結です。


入力例 3

200000 4
1 200000 1000000000
1 200000 998244353
1 200000 999999999
1 200000 999999999

出力例 3

199651870599998

Score : 500 points

Problem Statement

There is a graph with N + Q vertices, numbered 1, 2, \ldots, N + Q. Initially, the graph has no edges.

For this graph, perform the following operation for i = 1, 2, \ldots, Q in order:

  • For each integer j satisfying L_i \leq j \leq R_i, add an undirected edge with cost C_i between vertices N + i and j.

Determine if the graph is connected after all operations are completed. If it is connected, find the cost of a minimum spanning tree of the graph.

A minimum spanning tree is a spanning tree with the smallest possible cost, and the cost of a spanning tree is the sum of the costs of the edges used in the spanning tree.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq N
  • 1 \leq C_i \leq 10^9
  • All input values are integers.

Input

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

N Q
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_Q R_Q C_Q

Output

If the graph is connected, print the cost of a minimum spanning tree. Otherwise, print -1.


Sample Input 1

4 3
1 2 2
1 3 4
2 4 5

Sample Output 1

22

The following edges form a minimum spanning tree:

  • An edge with cost 2 connecting vertices 1 and 5
  • An edge with cost 2 connecting vertices 2 and 5
  • An edge with cost 4 connecting vertices 1 and 6
  • An edge with cost 4 connecting vertices 3 and 6
  • An edge with cost 5 connecting vertices 3 and 7
  • An edge with cost 5 connecting vertices 4 and 7

Since 2 + 2 + 4 + 4 + 5 + 5 = 22, print 22.


Sample Input 2

6 2
1 2 10
4 6 10

Sample Output 2

-1

The graph is disconnected.


Sample Input 3

200000 4
1 200000 1000000000
1 200000 998244353
1 200000 999999999
1 200000 999999999

Sample Output 3

199651870599998
G - Last Major City

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

配点 : 600

問題文

AtCoder 国は N 個の都市およびそれらを結ぶ M 本の道路からなり、どの都市間もいくつかの道路を辿ることで行き来可能です。 都市には 1 から N までの番号が、道路には 1 から M までの番号がつけられていて、道路 i は都市 A_i,B_i を双方向に繋いでいます。

交通量が年々増加している AtCoder 国では、いくつかの道路に拡張工事を行うことが予定されています。 現在はまだどの道路も拡張されておらず、道路 i を拡張するのにかかるコストは C_i です。

一度に全ての道路を拡張するのは大変なので、まずは N 個の都市のうち K 個の都市を主要都市に指定し、主要都市間が拡張された道路のみを辿って行き来可能となるような最低限の拡張工事を行うことになりました。 都市 1,2,\dots,K-1 が主要都市に指定されることは既に確定していますが、最後の 1 つの主要都市はまだ決まっていません。

i=K,K+1,\dots,N それぞれについて以下の問いに答えてください。

  • 都市 i が最後の主要都市として指定された場合、どの主要都市間も拡張された道路のみを辿って行き来可能となるようにするための拡張工事のコストの総和の最小値はいくつか。

制約

  • 2\leq N \leq 4000
  • N-1\leq M \leq 8000
  • 2\leq K \leq \min(N,\,10)
  • 1\leq A_i < B_i \leq N
  • 1\leq C_i \leq 10^9
  • どの都市間もいくつかの道路を辿ることで行き来可能である
  • 入力は全て整数

入力

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

N M K
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

出力

N-K+1 行出力せよ。 l\ (1\leq l \leq N-K+1) 行目には、i=l+K-1 のときの問いの答えを整数として出力せよ。


入力例 1

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

出力例 1

3
6

上図において、数が書かれた丸はその番号の都市を、数が書かれた線は拡張のためのコストがその数であるような道路を表しています。 また、左右の図はそれぞれ i=3,4 の場合に対応しており、色のついた丸が主要都市を、色のついた太線が最適解において拡張される道路を表しています。

  • i=3 のとき、道路 4,5 を拡張するとコストの総和が 2+1=3 となり、これが最小値です。
  • i=4 のとき、道路 1,4,5 を拡張するとコストの総和が 3+2+1=6 となり、これが最小値です。

入力例 2

4 3 2
2 4 28
1 4 56
1 3 82

出力例 2

84
82
56

入力例 3

6 12 4
2 6 68
2 5 93
4 6 28
2 4 89
3 6 31
1 3 10
1 2 53
3 5 1
3 5 74
3 4 22
4 5 80
3 4 35

出力例 3

85
64
94

同じ都市のペアを結ぶ道路が複数存在することもあります。

Score : 600 points

Problem Statement

The Country of AtCoder consists of N cities and M roads connecting them, and one can travel between any two cities by traversing some roads. The cities are numbered from 1 to N, and the roads are numbered from 1 to M. Road i connects cities A_i and B_i bidirectionally.

Due to increasing traffic in the country, some roads are planned to be expanded. Currently, no roads have been expanded, and the cost to expand road i is C_i.

Since it is difficult to expand all roads at once, the plan is to first designate K out of the N cities as major cities and perform the minimum necessary expansion work so that one can travel between any two major cities using only expanded roads. It has already been decided that cities 1, 2, \dots, K-1 will be major cities, but the last major city has not yet been decided.

For each i=K, K+1, \dots, N, answer the following question:

  • If city i is designated as the last major city, what is the minimum total cost of the expansion work required to ensure that one can travel between any two major cities using only expanded roads?

Constraints

  • 2 \leq N \leq 4000
  • N-1 \leq M \leq 8000
  • 2\leq K \leq \min(N,\,10)
  • 1 \leq A_i < B_i \leq N
  • 1 \leq C_i \leq 10^9
  • One can travel between any two cities by traversing some roads.
  • All input values are integers.

Input

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

N M K
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

Output

Print N-K+1 lines. The l-th line (1 \leq l \leq N-K+1) should contain the answer to the question for i=l+K-1, as an integer.


Sample Input 1

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

Sample Output 1

3
6

In the above figure, circles with numbers represent cities with those numbers, and lines with numbers represent roads with the cost of expansion being that number. The left and right figures correspond to the cases i=3 and i=4, respectively. The colored circles represent the major cities, and the colored thick lines represent the roads that are expanded in an optimal solution.

  • For i=3, expanding roads 4 and 5 results in a total cost of 2+1=3, which is the minimum.
  • For i=4, expanding roads 1, 4, and 5 results in a total cost of 3+2+1=6, which is the minimum.

Sample Input 2

4 3 2
2 4 28
1 4 56
1 3 82

Sample Output 2

84
82
56

Sample Input 3

6 12 4
2 6 68
2 5 93
4 6 28
2 4 89
3 6 31
1 3 10
1 2 53
3 5 1
3 5 74
3 4 22
4 5 80
3 4 35

Sample Output 3

85
64
94

There may be multiple roads connecting the same pair of cities.