A - AtCoder Group Contest

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 300

問題文

AtCoder Group Contestの参加者に 3N 人が参加します。 i 番目の参加者の 強さ は整数 a_i で表されます。 参加者が 31 組となるようにチームを N 組作ることにしました。1 人の参加者が複数のチームに所属することはできません。

チームの強さはチームメンバーの強さのうち 2 番目に大きい値で表されます。 例えば、強さが 1,5,2 のメンバーからなるチームの強さは 2 になり、強さが 3,2,3 のメンバーからなるチームの強さは 3 になります。

N 組のチームの強さの和としてありうる値のうち、最大の値を求めてください。

制約

  • 1 ≦ N ≦ 10^5
  • 1 ≦ a_i ≦ 10^{9}
  • a_i は整数

入力

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

N
a_1 a_2 ... a_{3N}

出力

答えを出力せよ。


入力例 1

2
5 2 8 5 1 5

出力例 1

10

例えば以下のようにチームを作ったとき、チームの強さの和が最大となります。

  • チーム 11,4,5 番目の参加者からなる。
  • チーム 22,3,6 番目の参加者からなる。

入力例 2

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

出力例 2

10000000000

チームの強さの和は非常に大きくなることがあります。

Score : 300 points

Problem Statement

There are 3N participants in AtCoder Group Contest. The strength of the i-th participant is represented by an integer a_i. They will form N teams, each consisting of three participants. No participant may belong to multiple teams.

The strength of a team is defined as the second largest strength among its members. For example, a team of participants of strength 1, 5, 2 has a strength 2, and a team of three participants of strength 3, 2, 3 has a strength 3.

Find the maximum possible sum of the strengths of N teams.

Constraints

  • 1 ≤ N ≤ 10^5
  • 1 ≤ a_i ≤ 10^{9}
  • a_i are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the answer.


Sample Input 1

2
5 2 8 5 1 5

Sample Output 1

10

The following is one formation of teams that maximizes the sum of the strengths of teams:

  • Team 1: consists of the first, fourth and fifth participants.
  • Team 2: consists of the second, third and sixth participants.

Sample Input 2

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2

10000000000

The sum of the strengths can be quite large.

B - Splatter Painting

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 700

問題文

イカはグラフの頂点に色を塗るのが好きです。

1 から N までの番号がついた N 個の頂点と M 本の辺からなる単純無向グラフが与えられます。 全ての頂点ははじめ、色 0 で塗られています。i 番目の辺は頂点 a_i と頂点 b_i を双方向につなぐ長さ 1 の辺です。

イカはこのグラフに対して Q 回の操作を行いました。 i 回目の操作では、頂点 v_i から距離 d_i 以内にあるような頂点たち全ての色を色 c_i で上書きしました。

Q 回の操作後において、各頂点がどの色で塗られているか調べてください。

制約

  • 1 ≦ N,M,Q ≦ 10^5
  • 1 ≦ a_i,b_i,v_i ≦ N
  • a_i ≠ b_i
  • 0 ≦ d_i ≦ 10
  • 1 ≦ c_i ≦10^5
  • d_i, c_i いずれも整数
  • 与えられるグラフに自己ループや多重辺は存在しない

部分点

  • 1 ≦ N,M,Q ≦ 2{,}000 を満たすデータセットに正解した場合は、部分点として 200 点が与えられる。

入力

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

N M
a_1 b_1
:
a_{M} b_{M}
Q
v_1 d_1 c_1
:
v_{Q} d_{Q} c_{Q}

出力

答えを N 行に出力せよ。 i 行目では Q 回の操作後における頂点 i の色を出力せよ。


入力例 1

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

出力例 1

2
2
2
2
2
1
0

はじめ、各頂点は色 0 で塗られています。 1 回目の操作により、頂点 5,6 が色 1 で塗られます。 2 回目の操作により、頂点 1,2,3,4,5 が色 2 で塗られます。

2ab7e180230b159d42d35ea7e555b3b0.png


入力例 2

14 10
1 4
5 7
7 11
4 10
14 7
14 3
6 14
8 11
5 13
8 3
8
8 6 2
9 7 85
6 9 3
6 7 5
10 3 1
12 9 4
9 6 6
8 2 3

出力例 2

1
0
3
1
5
5
3
3
6
1
3
4
5
3

与えられるグラフは連結とは限りません。

Score : 700 points

Problem Statement

Squid loves painting vertices in graphs.

There is a simple undirected graph consisting of N vertices numbered 1 through N, and M edges. Initially, all the vertices are painted in color 0. The i-th edge bidirectionally connects two vertices a_i and b_i. The length of every edge is 1.

Squid performed Q operations on this graph. In the i-th operation, he repaints all the vertices within a distance of d_i from vertex v_i, in color c_i.

Find the color of each vertex after the Q operations.

Constraints

  • 1 ≤ N,M,Q ≤ 10^5
  • 1 ≤ a_i,b_i,v_i ≤ N
  • a_i ≠ b_i
  • 0 ≤ d_i ≤ 10
  • 1 ≤ c_i ≤10^5
  • d_i and c_i are all integers.
  • There are no self-loops or multiple edges in the given graph.

Partial Score

  • 200 points will be awarded for passing the testset satisfying 1 ≤ N,M,Q ≤ 2{,}000.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_{M} b_{M}
Q
v_1 d_1 c_1
:
v_{Q} d_{Q} c_{Q}

Output

Print the answer in N lines. In the i-th line, print the color of vertex i after the Q operations.


Sample Input 1

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

Sample Output 1

2
2
2
2
2
1
0

Initially, each vertex is painted in color 0. In the first operation, vertices 5 and 6 are repainted in color 1. In the second operation, vertices 1, 2, 3, 4 and 5 are repainted in color 2.

2ab7e180230b159d42d35ea7e555b3b0.png


Sample Input 2

14 10
1 4
5 7
7 11
4 10
14 7
14 3
6 14
8 11
5 13
8 3
8
8 6 2
9 7 85
6 9 3
6 7 5
10 3 1
12 9 4
9 6 6
8 2 3

Sample Output 2

1
0
3
1
5
5
3
3
6
1
3
4
5
3

The given graph may not be connected.

C - Tautonym Puzzle

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1000

問題文

文字列 x が以下の条件を満たすとき、x良い文字列 と呼びます。

  • 条件:x はある長さ 1 以上の文字列 y2 回繰り返した文字列 yy で表すことができる。

例えば aa,bubobubo などは良い文字列ですが、空文字列、a,abcabcabc,abba などは良い文字列ではありません。

ワシとフクロウが良い文字列に関するパズルを作りました。 以下の条件を満たす文字列 s をどれか 1 つ求めてください。この問題の制約下で、そのような文字列が必ず存在することが証明できます。

  • 1 ≦ |s| ≦ 200
  • s1 から 100 までの整数で表される 100 種類の文字のみからなる。
  • s2^{|s|} 個ある部分列のうち、良い文字列であるようなものは N 個ある。

制約

  • 1 ≦ N ≦ 10^{12}

入力

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

N

出力

1 行目には s の長さ |s| を出力せよ。 2 行目に s の各要素を 1 文字目から順に空白区切りで出力せよ。s が上記の条件を満たすならば正解となる。


入力例 1

7

出力例 1

4
1 1 1 1

s の部分列であって良い文字列となるようなものは (1,1) と (1,1,1,1) の 2 種類があります。(1,1) であるような部分列は 6 個、(1,1,1,1) であるような部分列は 1 個あるため、合計 7 個です。


入力例 2

299

出力例 2

23
32 11 11 73 45 8 11 83 83 8 45 32 32 10 100 73 32 83 45 73 32 11 10

Score : 1000 points

Problem Statement

We will call a string x good if it satisfies the following condition:

  • Condition: x can be represented as a concatenation of two copies of another string y of length at least 1.

For example, aa and bubobubo are good; an empty string, a, abcabcabc and abba are not good.

Eagle and Owl created a puzzle on good strings. Find one string s that satisfies the following conditions. It can be proved that such a string always exists under the constraints in this problem.

  • 1 ≤ |s| ≤ 200
  • Each character of s is one of the 100 characters represented by the integers 1 through 100.
  • Among the 2^{|s|} subsequences of s, exactly N are good strings.

Constraints

  • 1 ≤ N ≤ 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

In the first line, print |s|, the length of s. In the second line, print the elements in s in order, with spaces in between. Any string that satisfies the above conditions will be accepted.


Sample Input 1

7

Sample Output 1

4
1 1 1 1

There are two good strings that appear as subsequences of s: (1,1) and (1,1,1,1). There are six occurrences of (1,1) and one occurrence of (1,1,1,1), for a total of seven.


Sample Input 2

299

Sample Output 2

23
32 11 11 73 45 8 11 83 83 8 45 32 32 10 100 73 32 83 45 73 32 11 10
D - Colorful Balls

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1000

問題文

すぬけくんは N 個の色が塗られたボールを一列に並べました。 左から i 番目のボールは色 c_i で塗られていて、その重さは w_i です。

すぬけくんは以下の 2 種類の操作を任意の順序で何回でも繰り返してボールの配置を変更することができます。

  • 操作 1:色が同じであるような 2 つのボールを選ぶ。2 つのボールの重さの和が X 以下なら、2 つのボールの位置を入れ替える。
  • 操作 2:色が異なるような 2 つのボールを選ぶ。2 つのボールの重さの和が Y 以下なら、2 つのボールの位置を入れ替える。

最終的なボールの色の並びとしてありうるような数列の数を modulo 10^9 + 7 で求めてください。

制約

  • 1 ≦ N ≦ 2 × 10^5
  • 1 ≦ X, Y ≦ 10^9
  • 1 ≦ c_i ≦ N
  • 1 ≦ w_i ≦ 10^9
  • X,Y,c_i, w_i はいずれも整数

入力

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

N X Y
c_1 w_1
:
c_N w_N

出力

答えを出力せよ。


入力例 1

4 7 3
3 2
4 3
2 1
4 4

出力例 1

2
  • 操作 2 により左から 1 番目のボールの位置と左から 3 番目のボールの位置を入れ替えることで、(2,4,3,4) という色の並びを作ることが可能です。
  • 操作 1 により左から 2 番目のボールの位置と左から 4 番目のボールの位置を入れ替えることも可能ですが、色の並びは変化しません。

入力例 2

1 1 1
1 1

出力例 2

1

入力例 3

21 77 68
16 73
16 99
19 66
2 87
2 16
7 17
10 36
10 68
2 38
10 74
13 55
21 21
3 7
12 41
13 88
18 6
2 12
13 87
1 9
2 27
13 15

出力例 3

129729600

Score : 1000 points

Problem Statement

Snuke arranged N colorful balls in a row. The i-th ball from the left has color c_i and weight w_i.

He can rearrange the balls by performing the following two operations any number of times, in any order:

  • Operation 1: Select two balls with the same color. If the total weight of these balls is at most X, swap the positions of these balls.
  • Operation 2: Select two balls with different colors. If the total weight of these balls is at most Y, swap the positions of these balls.

How many different sequences of colors of balls can be obtained? Find the count modulo 10^9 + 7.

Constraints

  • 1 ≤ N ≤ 2 × 10^5
  • 1 ≤ X, Y ≤ 10^9
  • 1 ≤ c_i ≤ N
  • 1 ≤ w_i ≤ 10^9
  • X, Y, c_i, w_i are all integers.

Input

Input is given from Standard Input in the following format:

N X Y
c_1 w_1
:
c_N w_N

Output

Print the answer.


Sample Input 1

4 7 3
3 2
4 3
2 1
4 4

Sample Output 1

2
  • The sequence of colors (2,4,3,4) can be obtained by swapping the positions of the first and third balls by operation 2.
  • It is also possible to swap the positions of the second and fourth balls by operation 1, but it does not affect the sequence of colors.

Sample Input 2

1 1 1
1 1

Sample Output 2

1

Sample Input 3

21 77 68
16 73
16 99
19 66
2 87
2 16
7 17
10 36
10 68
2 38
10 74
13 55
21 21
3 7
12 41
13 88
18 6
2 12
13 87
1 9
2 27
13 15

Sample Output 3

129729600
E - Camel and Oases

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1000

問題文

N 箇所のオアシスが数直線上に並んでおり、左から i 番目のオアシスは座標 x_i にあります。

ラクダはこれらのオアシス全てを訪れたいです。 ラクダははじめ、体積 V のこぶを持っています。こぶの体積を v としたとき、ラクダは水を最大で v 蓄えることができます。ラクダはオアシスにいるときのみ、水を補給することができます。オアシスでは蓄えられるだけ水を補給することができ、同じオアシスで何回でも水を補給することができます。

ラクダは数直線上を歩くか、ジャンプするかのどちらかの方法で移動することができます。

  • ラクダが距離 d だけ歩いたとき、蓄えている水を d だけ消費します。ただし、蓄えられた水の量が負になるようには移動できません。
  • 蓄えられた水の量を v として v>0 のとき、ラクダはジャンプをすることで数直線上の任意の地点へと移動することができます。その後、こぶの体積が v/2(小数点以下は切り捨て) となり、蓄えられた水の量が 0 になります。

ラクダが 1,2,3,...,N 番目のオアシスから出発したとき、全てのオアシスを訪れることが可能かどうかをそれぞれ判定してください。

制約

  • 2 ≦ N,V ≦ 2 × 10^5
  • -10^9≦ x_1 < x_2 < ... < x_N ≦10^9
  • V, x_i はいずれも整数

入力

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

N V
x_1 x_2 ... x_{N}

出力

答えを N 行に出力せよ。i 行目では i 番のオアシスから出発して全てのオアシスを訪れることが可能ならば Possible と、不可能ならば Impossible と出力せよ。


入力例 1

3 2
1 3 6

出力例 1

Possible
Possible
Possible

以下のように移動することで、1 番のオアシスから出発して全てのオアシスを訪れることが可能です。

  • 1 番のオアシスから 2 番のオアシスへと歩いて移動する。蓄えられた水の量は 0 となる。
  • 2 番のオアシスで水を補給する。蓄えられた水の量は 2 となる。
  • 2 番のオアシスから 3 番のオアシスへとジャンプして移動する。蓄えられた水の量は 0 となり、こぶの体積は 1 となる。

入力例 2

7 2
-10 -4 -2 0 2 4 10

出力例 2

Impossible
Possible
Possible
Possible
Possible
Possible
Impossible

オアシスは何度訪れても構いません。


入力例 3

16 19
-49 -48 -33 -30 -21 -14 0 15 19 23 44 52 80 81 82 84

出力例 3

Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Impossible
Impossible
Impossible
Impossible

Score : 1000 points

Problem Statement

There are N oases on a number line. The coordinate of the i-th oases from the left is x_i.

Camel hopes to visit all these oases. Initially, the volume of the hump on his back is V. When the volume of the hump is v, water of volume at most v can be stored. Water is only supplied at oases. He can get as much water as he can store at a oasis, and the same oasis can be used any number of times.

Camel can travel on the line by either walking or jumping:

  • Walking over a distance of d costs water of volume d from the hump. A walk that leads to a negative amount of stored water cannot be done.
  • Let v be the amount of water stored at the moment. When v>0, Camel can jump to any point on the line of his choice. After this move, the volume of the hump becomes v/2 (rounded down to the nearest integer), and the amount of stored water becomes 0.

For each of the oases, determine whether it is possible to start from that oasis and visit all the oases.

Constraints

  • 2 ≤ N,V ≤ 2 × 10^5
  • -10^9 ≤ x_1 < x_2 < ... < x_N ≤ 10^9
  • V and x_i are all integers.

Input

Input is given from Standard Input in the following format:

N V
x_1 x_2 ... x_{N}

Output

Print N lines. The i-th line should contain Possible if it is possible to start from the i-th oasis and visit all the oases, and Impossible otherwise.


Sample Input 1

3 2
1 3 6

Sample Output 1

Possible
Possible
Possible

It is possible to start from the first oasis and visit all the oases, as follows:

  • Walk from the first oasis to the second oasis. The amount of stored water becomes 0.
  • Get water at the second oasis. The amount of stored water becomes 2.
  • Jump from the second oasis to the third oasis. The amount of stored water becomes 0, and the volume of the hump becomes 1.

Sample Input 2

7 2
-10 -4 -2 0 2 4 10

Sample Output 2

Impossible
Possible
Possible
Possible
Possible
Possible
Impossible

A oasis may be visited any number of times.


Sample Input 3

16 19
-49 -48 -33 -30 -21 -14 0 15 19 23 44 52 80 81 82 84

Sample Output 3

Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Impossible
Impossible
Impossible
Impossible
F - Prefix Median

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 2000

問題文

すぬけくんは長さ 2N-1 の数列 a をもらいました。

すぬけくんは a を自由に並び替えたあと、a を使って新しい数列 b を作りました。b は以下に示されるような長さ N の数列です。

  • b_1 = (a_1) の中央値
  • b_2 = (a_1, a_2, a_3) の中央値
  • b_3 = (a_1,a_2,a_3,a_4,a_5) の中央値
  • :
  • b_N = (a_1,a_2,a_3, ..., a_{2N-1}) の中央値

b としてありうる数列の数を modulo 10^{9} + 7 で求めてください。

制約

  • 1 ≦ N ≦ 50
  • 1 ≦ a_i ≦ 2N-1
  • a_i は整数

入力

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

N
a_1 a_2 ... a_{2N-1}

出力

答えを出力せよ。


入力例 1

2
1 3 2

出力例 1

3

b としてありうる数列は (1,2),(2,2),(3,2)3 通りです。これらはそれぞれ (1,2,3),(2,1,3),(3,1,2) から作ることが可能です。


入力例 2

4
1 3 2 3 2 4 3

出力例 2

16

入力例 3

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

出力例 3

911828634

10^{9} + 7 で割ったあまりを求めてください。

Score : 2000 points

Problem Statement

Snuke received an integer sequence a of length 2N-1.

He arbitrarily permuted the elements in a, then used it to construct a new integer sequence b of length N, as follows:

  • b_1 = the median of (a_1)
  • b_2 = the median of (a_1, a_2, a_3)
  • b_3 = the median of (a_1, a_2, a_3, a_4, a_5)
  • :
  • b_N = the median of (a_1, a_2, a_3, ..., a_{2N-1})

How many different sequences can be obtained as b? Find the count modulo 10^{9} + 7.

Constraints

  • 1 ≤ N ≤ 50
  • 1 ≤ a_i ≤ 2N-1
  • a_i are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{2N-1}

Output

Print the answer.


Sample Input 1

2
1 3 2

Sample Output 1

3

There are three sequences that can be obtained as b: (1,2), (2,2) and (3,2). Each of these can be constructed from (1,2,3), (2,1,3) and (3,1,2), respectively.


Sample Input 2

4
1 3 2 3 2 4 3

Sample Output 2

16

Sample Input 3

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

Sample Output 3

911828634

Print the count modulo 10^{9} + 7.