E - Merge the balls

実行時間制限: 2 sec / メモリ制限: 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

実行時間制限: 2 sec / メモリ制限: 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

実行時間制限: 2 sec / メモリ制限: 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

実行時間制限: 2 sec / メモリ制限: 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

実行時間制限: 2 sec / メモリ制限: 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