A - Buildings

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

N 個のビルが横一列に並んでいて、左から i 番目のビルの高さは H_i です。

左から 1 番目のビルより高いビルが存在するか判定し、存在する場合その内最も左のビルは左から何番目か求めてください。

制約

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

入力

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

N 
H_1 H_2 \ldots H_N

出力

左から 1 番目のビルより高いビルが存在しない場合 -1 を出力せよ。

存在する場合、その内最も左のビルは左から何番目か出力せよ。


入力例 1

4
3 2 5 2

出力例 1

3

左から 1 番目のビルより高いビルは、左から 3 番目のビルです。


入力例 2

3
4 3 2

出力例 2

-1

左から 1 番目のビルより高いビルは存在しません。


入力例 3

7
10 5 10 2 10 13 15

出力例 3

6

左から 1 番目のビルより高いビルは、左から 6 番目のビルと左から 7 番目のビルです。その内最も左のビルは左から 6 番目のビルです。

Score: 100 points

Problem Statement

There are N buildings aligned in a row. The i-th building from the left has a height of H_i.

Determine if there is a building taller than the first one from the left. If such a building exists, find the position of the leftmost such building from the left.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq H_i \leq 100
  • All input values are integers.

Input

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

N
H_1 H_2 \ldots H_N

Output

If no building is taller than the first one from the left, print -1.

If such a building exists, print the position (index) of the leftmost such building from the left.


Sample Input 1

4
3 2 5 2

Sample Output 1

3

The building taller than the first one from the left is the third one from the left.


Sample Input 2

3
4 3 2

Sample Output 2

-1

No building is taller than the first one from the left.


Sample Input 3

7
10 5 10 2 10 13 15

Sample Output 3

6

The buildings taller than the first one from the left are the sixth and seventh ones. Among them, the leftmost is the sixth one.

B - Count Down

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

N 以下の非負整数を大きい方から順にすべて出力してください。

制約

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

入力

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

N

出力

N 以下の非負整数が X 個存在するとき、X 行出力せよ。
i=1,2,\ldots,X に対し、i 行目には N 以下の非負整数のうち大きい方から i 番目のものを出力せよ。


入力例 1

3

出力例 1

3
2
1
0

3 以下の非負整数は 0,1,2,34 個です。
1 行目に 3 を、2 行目に 2 を、3 行目に 1 を、4 行目に 0 を出力することでこれらを大きい方から順に出力したことになります。


入力例 2

22

出力例 2

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

Score : 100 points

Problem Statement

Print all non-negative integers less than or equal to N in descending order.

Constraints

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

Input

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

N

Output

Print X lines, where X is the number of non-negative integers less than or equal to N.
For each i=1, 2, \ldots, X, the i-th line should contain the i-th greatest non-negative integer less than or equal to N.


Sample Input 1

3

Sample Output 1

3
2
1
0

We have four non-negative integers less than or equal to 3, which are 0, 1, 2, and 3.
To print them in descending order, print 3 in the first line, 2 in the second, 1 in the third, and 0 in the fourth.


Sample Input 2

22

Sample Output 2

22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
C - Split?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

ボウリングのピンは 1 から 10 の番号が付けられており、上から見ると下図のように配置されます。

0

この図の二つの点線に挟まれた部分をと呼ぶことにします。
例えば、ピン 1, 5 とピン 3, 9 はそれぞれ同じ列に存在します。

いくつかのピンが倒れた状態のうち、特殊なものはスプリットと呼ばれます。
ピンの配置がスプリットであるとは、以下の条件が全て成り立つことを言います。

  • ピン 1 が倒れている。
  • ある二つの異なる列であって、次の条件を満たすものが存在する。
    • それぞれの列には、立っているピンが 1 本以上存在する。
    • それらの列の間に、ピンが全て倒れている列が存在する。

具体例は入出力例を参考にしてください。

さて、あるピンの配置が長さ 10 の文字列 S として与えられます。 i = 1, \dots, 10 について、ピン i が倒れているとき Si 文字目は 0 であり、ピン i が立っているとき Si 文字目は 1 です。
S で表されるピンの配置がスプリットかどうか判定してください。

制約

  • S01 からなる長さ 10 の文字列

入力

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

S

出力

S で表されるピンの配置がスプリットなら Yes を、そうでないなら No を出力せよ。


入力例 1

0101110101

出力例 1

Yes

倒れているピンを灰色で、立っているピンを白色で示すと下図のようになります。

ex0

ピン 5 が立っている列とピン 6 が立っている列の間にはピン 3, 9 が置かれている列が存在しますが、ピン 3, 9 はいずれも倒れているので、この配置はスプリットです。


入力例 2

0100101001

出力例 2

Yes

ex1


入力例 3

0000100110

出力例 3

No

ex2

この配置はスプリットではありません。


入力例 4

1101110101

出力例 4

No

ex3

ピン 1 が倒れていないので、スプリットではありません。

Score : 200 points

Problem Statement

Bowling pins are numbered 1 through 10. The following figure is a top view of the arrangement of the pins:

0

Let us call each part between two dotted lines in the figure a column.
For example, Pins 1 and 5 belong to the same column, and so do Pin 3 and 9.

When some of the pins are knocked down, a special situation called split may occur.
A placement of the pins is a split if both of the following conditions are satisfied:

  • Pin 1 is knocked down.
  • There are two different columns that satisfy both of the following conditions:
    • Each of the columns has one or more standing pins.
    • There exists a column between these columns such that all pins in the column are knocked down.

See also Sample Inputs and Outputs for examples.

Now, you are given a placement of the pins as a string S of length 10. For i = 1, \dots, 10, the i-th character of S is 0 if Pin i is knocked down, and is 1 if it is standing.
Determine if the placement of the pins represented by S is a split.

Constraints

  • S is a string of length 10 consisting of 0 and 1.

Input

Input is given from Standard Input in the following format:

S

Output

If the placement of the pins represented by S is a split, print Yes; otherwise, print No.


Sample Input 1

0101110101

Sample Output 1

Yes

In the figure below, the knocked-down pins are painted gray, and the standing pins are painted white:

ex0

Between the column containing a standing pin 5 and the column containing a standing pin 6 is a column containing Pins 3 and 9. Since Pins 3 and 9 are both knocked down, the placement is a split.


Sample Input 2

0100101001

Sample Output 2

Yes

ex1


Sample Input 3

0000100110

Sample Output 3

No

ex2

This placement is not a split.


Sample Input 4

1101110101

Sample Output 4

No

ex3

This is not a split because Pin 1 is not knocked down.

D - At Most 3 (Judge ver.)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

おもり 1, おもり 2, \dots, おもり NN 個のおもりがあります。おもり i の重さは A_i です。
以下の条件を満たす正整数 n良い整数 と呼びます。

  • \bf{3} 個以下 の異なるおもりを自由に選んで、選んだおもりの重さの和を n にすることができる。

W 以下の正整数のうち、良い整数は何個ありますか?

制約

  • 1 \leq N \leq 300
  • 1 \leq W \leq 10^6
  • 1 \leq A_i \leq 10^6
  • 入力される値はすべて整数

入力

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

N W
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

2 10
1 3

出力例 1

3

おもり 1 のみを選ぶと重さの和は 1 になります。よって 1 は良い整数です。
おもり 2 のみを選ぶと重さの和は 3 になります。よって 3 は良い整数です。
おもり 1 とおもり 2 を選ぶと重さの和は 4 になります。よって 4 は良い整数です。
これら以外に良い整数は存在しません。また、1,3,4 のいずれも W 以下の整数です。よって答えは 3 個になります。


入力例 2

2 1
2 3

出力例 2

0

W 以下の良い整数は存在しません。


入力例 3

4 12
3 3 3 3

出力例 3

3

良い整数は 3,6,93 個です。
たとえばおもり 1, おもり 2, おもり 3 を選ぶと重さの和は 9 になるので、9 は良い整数です。
12 は良い整数 ではない ことに注意してください。


入力例 4

7 251
202 20 5 1 4 2 100

出力例 4

48

Score : 200 points

Problem Statement

There are N weights called Weight 1, Weight 2, \dots, Weight N. Weight i has a mass of A_i.
Let us say a positive integer n is a good integer if the following condition is satisfied:

  • We can choose at most 3 different weights so that they have a total mass of n.

How many positive integers less than or equal to W are good integers?

Constraints

  • 1 \leq N \leq 300
  • 1 \leq W \leq 10^6
  • 1 \leq A_i \leq 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N W
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

2 10
1 3

Sample Output 1

3

If we choose only Weight 1, it has a total mass of 1, so 1 is a good integer.
If we choose only Weight 2, it has a total mass of 3, so 3 is a good integer.
If we choose Weights 1 and 2, they have a total mass of 4, so 4 is a good integer.
No other integer is a good integer. Also, all of 1, 3, and 4 are integers less than or equal to W. Therefore, the answer is 3.


Sample Input 2

2 1
2 3

Sample Output 2

0

There are no good integers less than or equal to W.


Sample Input 3

4 12
3 3 3 3

Sample Output 3

3

There are 3 good integers: 3, 6, and 9.
For example, if we choose Weights 1, 2, and 3, they have a total mass of 9, so 9 is a good integer.
Note that 12 is not a good integer.


Sample Input 4

7 251
202 20 5 1 4 2 100

Sample Output 4

48
E - Merge the balls

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

空の列と、N 個のボールがあります。i 個目 (1\leq i\leq N) のボールの大きさは 2^{A_i} です。

これから N 回の操作を行います。
i 回目の操作では、i 個目のボールを列の一番右に付け加えた後、次の手順を繰り返します。

  1. 列にあるボールが 1 つ以下ならば操作を終了する。
  2. 列にあるボールのうち右から 1 番目のものと 2 番目のものの大きさが 異なる ならば操作を終了する。
  3. 列にあるボールのうち右から 1 番目のものと 2 番目のものの大きさが 等しい ならば、2 つのボールを取り除き、「取り除かれた 2 つのボールの大きさの和」の大きさのボール 1 つを列の一番右に付け加える。その後、1. に戻り、手順を繰り返す。

N 回の操作の後で、列にあるボールの数を求めてください。

制約

  • 1\leq N \leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

N 回の操作の後で、列にあるボールの数を出力せよ。


入力例 1

7
2 1 1 3 5 3 3

出力例 1

3

操作は次のように行われます。

  • 1 回目の操作の後、列にあるボールは 1 つで、大きさは 2^2 です。
  • 2 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^2, 2^1 です。
  • 3 回目の操作の後、列にあるボールは 1 つで、大きさは 2^3 です。これは次のようにして得ることができます。
    • 3 回目の操作において 3 個目のボールを付け加えたとき、列にあるボールの大きさは順に 2^2,2^1,2^1 となります。
    • 右から 1 番目のボールと 2 番目のボールの大きさが等しいため、これらのボールが取り除かれ、大きさが 2^1+2^1=2^2 のボールが追加されます。このとき、列にあるボールの大きさは 2^2, 2^2 となります。
    • さらに、再び右から 1 番目のボールと 2 番目のボールの大きさが等しいため、これらのボールが取り除かれ、大きさが 2^2+2^2=2^3 のボールが追加され、列にあるボールの大きさは 2^3 となります。
  • 4 回目の操作の後、列にあるボールは 1 つで、大きさは 2^4 です。
  • 5 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^4, 2^5 です。
  • 6 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^4, 2^5, 2^3 です。
  • 7 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^4, 2^5, 2^4 です。

よって、最後に列にあるボールの数である 3 を出力します。


入力例 2

5
0 0 0 1 2

出力例 2

4

操作は次のように行われます。

  • 1 回目の操作の後、列にあるボールは 1 つで、大きさは 2^0 です。
  • 2 回目の操作の後、列にあるボールは 1 つで、大きさは 2^1 です。
  • 3 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^1, 2^0 です。
  • 4 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^1, 2^0, 2^1 です。
  • 5 回目の操作の後、列にあるボールは 4 つで、大きさは順に 2^1, 2^0, 2^1, 2^2 です。

よって、最後に列にあるボールの数である 4 を出力します。

Score: 250 points

Problem Statement

You have an empty sequence and N balls. The size of the i-th ball (1 \leq i \leq N) is 2^{A_i}.

You will perform N operations.
In the i-th operation, you add the i-th ball to the right end of the sequence, and repeat the following steps:

  1. If the sequence has one or fewer balls, end the operation.
  2. If the rightmost ball and the second rightmost ball in the sequence have different sizes, end the operation.
  3. If the rightmost ball and the second rightmost ball in the sequence have the same size, remove these two balls and add a new ball to the right end of the sequence with a size equal to the sum of the sizes of the two removed balls. Then, go back to step 1 and repeat the process.

Determine the number of balls remaining in the sequence after the N operations.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 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

Print the number of balls in the sequence after the N operations.


Sample Input 1

7
2 1 1 3 5 3 3

Sample Output 1

3

The operations proceed as follows:

  • After the first operation, the sequence has one ball, of size 2^2.
  • After the second operation, the sequence has two balls, of sizes 2^2 and 2^1 in order.
  • After the third operation, the sequence has one ball, of size 2^3. This is obtained as follows:
    • When the third ball is added during the third operation, the sequence has balls of sizes 2^2, 2^1, 2^1 in order.
    • The first and second balls from the right have the same size, so these balls are removed, and a ball of size 2^1 + 2^1 = 2^2 is added. Now, the sequence has balls of sizes 2^2, 2^2.
    • Again, the first and second balls from the right have the same size, so these balls are removed, and a ball of size 2^2 + 2^2 = 2^3 is added, leaving the sequence with a ball of size 2^3.
  • After the fourth operation, the sequence has one ball, of size 2^4.
  • After the fifth operation, the sequence has two balls, of sizes 2^4 and 2^5 in order.
  • After the sixth operation, the sequence has three balls, of sizes 2^4, 2^5, 2^3 in order.
  • After the seventh operation, the sequence has three balls, of sizes 2^4, 2^5, 2^4 in order.

Therefore, you should print 3, the final number of balls in the sequence.


Sample Input 2

5
0 0 0 1 2

Sample Output 2

4

The operations proceed as follows:

  • After the first operation, the sequence has one ball, of size 2^0.
  • After the second operation, the sequence has one ball, of size 2^1.
  • After the third operation, the sequence has two balls, of sizes 2^1 and 2^0 in order.
  • After the fourth operation, the sequence has three balls, of sizes 2^1, 2^0, 2^1 in order.
  • After the fifth operation, the sequence has four balls, of sizes 2^1, 2^0, 2^1, 2^2 in order.

Therefore, you should print 4, the final number of balls in the sequence.

F - Collision 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

xy 座標平面上に N 人の人がいます。人 i(X_i, Y_i) にいます。すべての人は異なる地点にいます。

L, R からなる長さ N の文字列 S があります。
iS_i = R ならば右向きに、S_i = L ならば左向きに、一斉に同じ速度で歩き始めます。ここで、右は x 軸の正の向き、左は x 軸の負の向きです。

たとえば (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL の場合は次の図のように動きます。

image

反対の向きに歩いている人同士が同じ地点に来ることを「衝突」と呼びます。すべての人が歩き続けたとき、衝突は発生しますか?

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i \leq 10^9
  • 0 \leq Y_i \leq 10^9
  • i \neq j ならば (X_i, Y_i) \neq (X_j, Y_j) である。
  • X_i, Y_i はすべて整数である。
  • SL および R からなる長さ N の文字列である。

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
S

出力

衝突が発生するならば Yes を、発生しないならば No を出力せよ。


入力例 1

3
2 3
1 1
4 1
RRL

出力例 1

Yes

この入力は問題文にある例と同じケースです。
すべての人が歩き続けると人 2 と人 3 が衝突します。よって Yes を出力します。


入力例 2

2
1 1
2 1
RR

出力例 2

No

1 と人 2 は同じ向きに歩いているので衝突することはありません。


入力例 3

10
1 3
1 4
0 0
0 2
0 4
3 1
2 4
4 2
4 4
3 3
RLRRRLRLRR

出力例 3

Yes

Score : 300 points

Problem Statement

There are N people in an xy-plane. Person i is at (X_i, Y_i). The positions of all people are different.

We have a string S of length N consisting of L and R.
If S_i = R, Person i is facing right; if S_i = L, Person i is facing left. All people simultaneously start walking in the direction they are facing. Here, right and left correspond to the positive and negative x-direction, respectively.

For example, the figure below shows the movement of people when (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL.

image

We say that there is a collision when two people walking in opposite directions come to the same position. Will there be a collision if all people continue walking indefinitely?

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i \leq 10^9
  • 0 \leq Y_i \leq 10^9
  • (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
  • All X_i and Y_i are integers.
  • S is a string of length N consisting of L and R.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
S

Output

If there will be a collision, print Yes; otherwise, print No.


Sample Input 1

3
2 3
1 1
4 1
RRL

Sample Output 1

Yes

This input corresponds to the example in the Problem Statement.
If all people continue walking, Person 2 and Person 3 will collide. Thus, Yes should be printed.


Sample Input 2

2
1 1
2 1
RR

Sample Output 2

No

Since Person 1 and Person 2 walk in the same direction, they never collide.


Sample Input 3

10
1 3
1 4
0 0
0 2
0 4
3 1
2 4
4 2
4 4
3 3
RLRRRLRLRR

Sample Output 3

Yes
G - General Weighted Max Matching

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

頂点に 1 から N の番号が付いた N 頂点の重み付き無向完全グラフが与えられます。頂点 i と頂点 j\ (i< j) を結ぶ辺の重みは D_{i,j} です。

以下の条件を満たすように何本かの辺を選ぶとき、選んだ辺の重みの総和としてあり得る最大値を求めてください。

  • 選んだ辺の端点はどの 2 個も相異なる。

制約

  • 2\leq N\leq 16
  • 1\leq D_{i,j} \leq 10^9
  • 入力される数値は全て整数

入力

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

N 
D_{1,2} D_{1,3} \ldots D_{1,N}
D_{2,3} \ldots D_{2,N}
\vdots
D_{N-1,N}

出力

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


入力例 1

4
1 5 4
7 8
6

出力例 1

13

頂点 1 と頂点 3 を結ぶ辺、頂点 2 と頂点 4 を結ぶ辺を選ぶと、辺の重みの総和が 5+8=13 となります。

これが達成可能な最大値であることが示せます。


入力例 2

3
1 2
3

出力例 2

3

N が奇数の場合もあります。


入力例 3

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

出力例 3

75

Score : 400 points

Problem Statement

You are given a weighted undirected complete graph with N vertices numbered from 1 to N. The edge connecting vertices i and j (i< j) has a weight of D_{i,j}.

When choosing some number of edges under the following condition, find the maximum possible total weight of the chosen edges.

  • The endpoints of the chosen edges are pairwise distinct.

Constraints

  • 2\leq N\leq 16
  • 1\leq D_{i,j} \leq 10^9
  • All input values are integers.

Input

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

N 
D_{1,2} D_{1,3} \ldots D_{1,N}
D_{2,3} \ldots D_{2,N}
\vdots
D_{N-1,N}

Output

Print the answer as an integer.


Sample Input 1

4
1 5 4
7 8
6

Sample Output 1

13

If you choose the edge connecting vertices 1 and 3, and the edge connecting vertices 2 and 4, the total weight of the edges is 5+8=13.

It can be shown that this is the maximum achievable value.


Sample Input 2

3
1 2
3

Sample Output 2

3

N can be odd.


Sample Input 3

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

Sample Output 3

75
H - Minimize Sum of Distances

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

N 頂点からなる木が与えられます。頂点は 1 から N までの番号がついており、 i 番目の辺は頂点 A_i, B_i を結んでいます。

長さ N の正整数列 C = (C_1, C_2, \ldots ,C_N) が与えられます。d(a, b) を頂点 a, b の間にある辺の数とし、 x = 1, 2, \ldots ,N に対して \displaystyle f(x) = \sum_{i=1}^{N} (C_i \times d(x, i)) とします。\displaystyle \min_{1 \leq v \leq N} f(v) を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i, B_i \leq N
  • 与えられるグラフは木である。
  • 1 \leq C_i \leq 10^9

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}
C_1 C_2 \cdots C_N

出力

答えを一行に出力せよ。


入力例 1

4
1 2
1 3
2 4
1 1 1 2

出力例 1

5

例として、 f(1) を求めることを考えます。d(1, 1) = 0, d(1, 2) = 1, d(1, 3) = 1, d(1, 4) = 2 です。
よって、 f(1) = 0 \times 1 + 1 \times 1 + 1 \times 1 + 2 \times 2 = 6 となります。

同様に、 f(2) = 5, f(3) = 9, f(4) = 6 です。f(2) が最小なので 5 を出力します。


入力例 2

2
2 1
1 1000000000

出力例 2

1

f(2) = 1 で、これが最小です。


入力例 3

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

出力例 3

56

Score: 475 points

Problem Statement

You are given a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects vertices A_i and B_i.

You are also given a sequence of positive integers C = (C_1, C_2, \ldots ,C_N) of length N. Let d(a, b) be the number of edges between vertices a and b, and for x = 1, 2, \ldots, N, let \displaystyle f(x) = \sum_{i=1}^{N} (C_i \times d(x, i)). Find \displaystyle \min_{1 \leq v \leq N} f(v).

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i, B_i \leq N
  • The given graph is a tree.
  • 1 \leq C_i \leq 10^9

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_{N - 1} B_{N - 1}
C_1 C_2 \cdots C_N

Output

Print the answer in one line.


Sample Input 1

4
1 2
1 3
2 4
1 1 1 2

Sample Output 1

5

For example, consider calculating f(1). We have d(1, 1) = 0, d(1, 2) = 1, d(1, 3) = 1, d(1, 4) = 2.
Thus, f(1) = 0 \times 1 + 1 \times 1 + 1 \times 1 + 2 \times 2 = 6.

Similarly, f(2) = 5, f(3) = 9, f(4) = 6. Since f(2) is the minimum, print 5.


Sample Input 2

2
2 1
1 1000000000

Sample Output 2

1

f(2) = 1, which is the minimum.


Sample Input 3

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

Sample Output 3

56
I - Score of Permutations

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

(1,2, \dots, N) を並び替えた長さ N の順列 P = (p_1, p_2, \dots, p_N) に対して、 P のスコア S(P) を次のように定めます。

  • N 人の人とすぬけ君がいて、N 人の人には 1,2,\dots,N の番号がついています。はじめ、人 i (1 \leq i \leq N) はボール i を持っています。
    すぬけ君が叫ぶたびに、i \neq p_i であるようなすべての人 i は人 p_i に持っているボールを同時に渡します。
    すぬけ君は、1 回以上叫んだ後にすべての人 i がボール i を持っている状態になると叫ぶのをやめます。
    すぬけ君が叫ぶのをやめるまでに叫んだ回数が順列のスコアとなります。ここでスコアは有限の値を取ることが保証されます。

P としてあり得るものは N! 通りありますが、それらのスコアを K 乗した値の総和を 998244353 で割ったあまりを計算してください。

  • 厳密に言い換えると、(1,2, \dots, N) を並び替えた長さ N の順列全体の集合を S_N として

    \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}

    を計算してください。

制約

  • 2 \leq N \leq 50
  • 1 \leq K \leq 10^4
  • 入力はすべて整数である。

入力

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

N K

出力

\displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353} を出力せよ。


入力例 1

2 2

出力例 1

5

N = 2 のとき P としてあり得る順列は (1,2),(2,1)2 つです。

順列 (1,2) のスコアは次のように決まります。

  • はじめ人 1 はボール 1 を、人 2 はボール 2 を持っています。
    すぬけ君が 1 回目に叫んだ後に、人 1 はボール 1 を、人 2 はボール 2 を持っています。
    このとき、すべての人が自身の番号と同じ番号が書かれたボールを持っているので、すぬけ君は叫ぶのをやめます。
    よってスコアは 1 となります。

順列 (2,1) のスコアは次のように決まります。

  • はじめ人 1 はボール 1 を、人 2 はボール 2 を持っています。
    すぬけ君が 1 回目に叫んだ後に、人 1 はボール 2 を、人 2 はボール 1 を持っています。
    すぬけ君が 2 回目に叫んだ後に、人 1 はボール 1 を、人 2 はボール 2 を持っています。
    このとき、すべての人が自身の番号と同じ番号が書かれたボールを持っているので、すぬけ君は叫ぶのをやめます。
    よってスコアは 2 となります。

よって 1^2 + 2^2 = 5 がこの問題の答えになります。


入力例 2

3 3

出力例 2

79

すべての順列とスコアの組を列挙すると以下のようになります。

  • 順列 : (1,2,3), スコア : 1
  • 順列 : (1,3,2), スコア : 2
  • 順列 : (2,1,3), スコア : 2
  • 順列 : (2,3,1), スコア : 3
  • 順列 : (3,1,2), スコア : 3
  • 順列 : (3,2,1), スコア : 2

よって 1^3 + 2^3 + 2^3 + 3^3 + 3^3 + 2^3 = 79 を出力します。


入力例 3

50 10000

出力例 3

77436607

Score : 500 points

Problem Statement

For a permutation P = (p_1, p_2, \dots, p_N) of (1,2, \dots, N), let us define the score S(P) of P as follows.

  • There are N people, numbered 1,2,\dots,N. Additionally, Snuke is there. Initially, Person i (1 \leq i \leq N) has Ball i.
    Each time Snuke screams, every Person i such that i \neq p_i gives their ball to Person p_i simultaneously.
    If, after screaming at least once, every Person i has Ball i, Snuke stops screaming.
    The score is the number of times Snuke screams until he stops. Here, it is guaranteed that the score will be a finite value.

There are N! permutations P of (1,2, \dots, N). Find the sum, modulo 998244353, of the scores of those permutations, each raised to the K-th power.

  • Formally, let S_N be the set of the permutations of (1,2, \dots, N). Compute the following: \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}.

Constraints

  • 2 \leq N \leq 50
  • 1 \leq K \leq 10^4
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}.


Sample Input 1

2 2

Sample Output 1

5

When N = 2, there are two possible permutations P: (1,2),(2,1).

The score of the permutation (1,2) is found as follows.

  • Initially, Person 1 has Ball 1, and Person 2 has Ball 2.
    After Snuke's first scream, Person 1 has Ball 1, and Person 2 has Ball 2.
    Here, every Person i has Ball i, so he stops screaming.
    Thus, the score is 1.

The score of the permutation (2,1) is found as follows.

  • Initially, Person 1 has Ball 1, and Person 2 has Ball 2.
    After Snuke's first scream, Person 1 has Ball 2, and Person 2 has Ball 1.
    After Snuke's second scream, Person 1 has Ball 1, and Person 2 has Ball 2.
    Here, every Person i has Ball i, so he stops screaming.
    Thus, the score is 2.

Therefore, the answer in this case is 1^2 + 2^2 = 5.


Sample Input 2

3 3

Sample Output 2

79

All permutations and their scores are listed below.

  • (1,2,3): The score is 1.
  • (1,3,2): The score is 2.
  • (2,1,3): The score is 2.
  • (2,3,1): The score is 3.
  • (3,1,2): The score is 3.
  • (3,2,1): The score is 2.

Thus, we should print 1^3 + 2^3 + 2^3 + 3^3 + 3^3 + 2^3 = 79.


Sample Input 3

50 10000

Sample Output 3

77436607