A - Kaiden

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題

ButCoder株式会社 は、プログラミングコンテストサイト「ButCoder」を運営しています。このサイトでは、ユーザーにはレーティングという技量を表す整数値が与えられ、その値はユーザーがコンテストに参加するたびに変動します。新規ユーザーのレーティングの初期値は 0 であり、レーティングが K 以上に達したユーザーは 皆伝 と呼ばれます。なお、レーティングは負になることもあります。

低橋くんというユーザーが ButCoder に新たに登録しました。彼のレーティングは、彼が奇数回目に参加するコンテスト(1 回目、3 回目、5 回目)では毎回 A 増加し、偶数回目に参加するコンテスト(2 回目、4 回目、6 回目)では毎回 B 減少することが予測されます。

この予測によると、彼が初めて皆伝になるのは何回のコンテストに参加した直後でしょうか、もしくは彼は永遠に皆伝になれないでしょうか?

制約

  • 1 ≤ K, A, B ≤ 10^{18}
  • 入力値はすべて整数である。

入力

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

K A B

出力

低橋くんが永遠に皆伝にならないと予測される場合は、-1 と出力せよ。そうでない場合は、彼が初めて皆伝になるまでのコンテストの回数の予測値を出力せよ。


入力例 1

4000 2000 500

出力例 1

5

低橋くんがコンテストに参加するたびに、彼のレーティングは 020001500350030005000 → … と変動することが予測されます。彼のレーティングが 4000 以上に到達するのは、5 回目の参加の直後です。


入力例 2

4000 500 2000

出力例 2

-1

低橋くんがコンテストに参加するたびに、彼のレーティングは 0500-1500-1000-3000-2500 → … と変動することが予測されます。彼が皆伝になることは永遠にありません。


入力例 3

1000000000000000000 2 1

出力例 3

1999999999999999997

入力される値や出力すべき値は 32 bit 整数に収まらないことがあります。

Score : 100 points

Problem Statement

ButCoder Inc. runs a programming competition site called ButCoder. In this site, a user is given an integer value called rating that represents his/her skill, which changes each time he/she participates in a contest. The initial value of a new user's rating is 0, and a user whose rating reaches K or higher is called Kaiden ("total transmission"). Note that a user's rating may become negative.

Hikuhashi is a new user in ButCoder. It is estimated that, his rating increases by A in each of his odd-numbered contests (first, third, fifth, ...), and decreases by B in each of his even-numbered contests (second, fourth, sixth, ...).

According to this estimate, after how many contests will he become Kaiden for the first time, or will he never become Kaiden?

Constraints

  • 1 ≤ K, A, B ≤ 10^{18}
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

K A B

Output

If it is estimated that Hikuhashi will never become Kaiden, print -1. Otherwise, print the estimated number of contests before he become Kaiden for the first time.


Sample Input 1

4000 2000 500

Sample Output 1

5

Each time Hikuhashi participates in a contest, his rating is estimated to change as 020001500350030005000 After his fifth contest, his rating will reach 4000 or higher for the first time.


Sample Input 2

4000 500 2000

Sample Output 2

-1

Each time Hikuhashi participates in a contest, his rating is estimated to change as 0500-1500-1000-3000-2500 He will never become Kaiden.


Sample Input 3

1000000000000000000 2 1

Sample Output 3

1999999999999999997

The input and output values may not fit into 32-bit integers.

B - Evergrowing Tree

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題

整数 N が与えられます。下図のような、無限に伸びる完全 N 分木を考えます。

図: N = 3 の場合の無限に伸びる完全 N 分木

図で示されているように、各頂点には重複しない正の整数の番号が付いており、どの正の整数に対してもそれを頂点番号として持つ頂点が存在します。木の根の頂点番号は 1 です。その他の頂点には、上の段にある頂点ほど小さな番号が付けられています。同じ段にある頂点には、左に位置するほど小さな番号が付けられています。

この木に関して、以下の形式の Q 個の問いに答えてください。i 番目の問い (1 ≤ i ≤ Q) は次の通りです。

  • 頂点 v_iw_i の最近共通祖先(注釈を参照)の頂点番号を求めよ。

注釈

  • 根付き木において、頂点 vw最近共通祖先 とは、頂点 vw のいずれの祖先でもある頂点のうち、最も根から遠いもののことです。ここで、頂点の祖先にはその頂点自身も含まれるものとします。例えば、問題文中で図示された木において、頂点 57 の最近共通祖先は頂点 2、頂点 811 の最近共通祖先は頂点 1、頂点 39 の最近共通祖先は頂点 3 です。

制約

  • 1 ≤ N ≤ 10^9
  • 1 ≤ Q ≤ 10^5
  • 1 ≤ v_i < w_i ≤ 10^9

入力

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

N Q
v_1 w_1
:
v_Q w_Q

出力

Q 行出力せよ。i 行目 (1 ≤ i ≤ Q) に頂点 v_iw_i の最近共通祖先の頂点番号を出力すること。


入力例 1

3 3
5 7
8 11
3 9

出力例 1

2
1
3

このケースでの問いは、注釈セクションで示した例に対応します。


入力例 2

100000 2
1 2
3 4

出力例 2

1
1

Score : 100 points

Problem Statement

You are given an integer N. Consider an infinite N-ary tree as shown below:

Figure: an infinite N-ary tree for the case N = 3

As shown in the figure, each vertex is indexed with a unique positive integer, and for every positive integer there is a vertex indexed with it. The root of the tree has the index 1. For the remaining vertices, vertices in the upper row have smaller indices than those in the lower row. Among the vertices in the same row, a vertex that is more to the left has a smaller index.

Regarding this tree, process Q queries. The i-th query is as follows:

  • Find the index of the lowest common ancestor (see Notes) of Vertex v_i and w_i.

Notes

  • In a rooted tree, the lowest common ancestor (LCA) of Vertex v and w is the farthest vertex from the root that is an ancestor of both Vertex v and w. Here, a vertex is considered to be an ancestor of itself. For example, in the tree shown in Problem Statement, the LCA of Vertex 5 and 7 is Vertex 2, the LCA of Vertex 8 and 11 is Vertex 1, and the LCA of Vertex 3 and 9 is Vertex 3.

Constraints

  • 1 ≤ N ≤ 10^9
  • 1 ≤ Q ≤ 10^5
  • 1 ≤ v_i < w_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N Q
v_1 w_1
:
v_Q w_Q

Output

Print Q lines. The i-th line (1 ≤ i ≤ Q) must contain the index of the lowest common ancestor of Vertex v_i and w_i.


Sample Input 1

3 3
5 7
8 11
3 9

Sample Output 1

2
1
3

The queries in this case correspond to the examples shown in Notes.


Sample Input 2

100000 2
1 2
3 4

Sample Output 2

1
1
C - Garden

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題

あなたの家の庭には、東に果てしなく伸びる細長い花壇があります。あなたは、何も植えられていないこの花壇に N 種類の花を植えることにしました。便宜上、これらの花の種類を花 1, 2, …, N と呼びます。また、花壇の西端から p センチメートルの位置を位置 p と呼びます。

i (1 ≤ i ≤ N) は、位置 w_i に一つ植え、そこから d_i センチメートルおきに一つずつ、東へと無数に植えていくことにします。 すなわち、花 i は位置 w_i, w_i + d_i, w_i + 2 d_i, に植えられることになります。 複数の花が同じ位置に植えられることもありえます。

西から K 番目に植えられる花の位置を求めてください。なお、同じ位置に複数の花が植えられる場合、それらは個別に数えます。

制約

  • 1 ≤ N ≤ 10^5
  • 1 ≤ K ≤ 10^9
  • 1 ≤ w_i ≤ 10^{18}
  • 1 ≤ d_i ≤ 10^9
  • 入力値はすべて整数である。

入力

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

N K
w_1 d_1
:
w_N d_N

出力

西から K 番目に植えられる花の位置が位置 X であるとき、X の値を出力せよ。(最も西に植えられる花を 1 番目として数える。)


入力例 1

2 6
20 10
25 15

出力例 1

50

2 種類の花が以下の位置に植えられます。

  • 1: 位置 20, 30, 40, 50, 60,
  • 2: 位置 25, 40, 55, 70, 85,

西から 6 番目の花は、位置 50 に植えられた花 1 です。位置 40 に植えられた 2 本の花を個別に数えていることに注意してください。


入力例 2

3 9
10 10
10 10
10 10

出力例 2

30

位置 10, 20, 30, のそれぞれに花が 3 本ずつ植えられます。したがって、西から 9 番目の花は位置 30 に植えられます。


入力例 3

1 1000000000
1000000000000000000 1000000000

出力例 3

1999999999000000000

Score : 100 points

Problem Statement

In your garden, there is a long and narrow flowerbed that stretches infinitely to the east. You have decided to plant N kinds of flowers in this empty flowerbed. For convenience, we will call these N kinds of flowers Flower 1, 2, …, N. Also, we will call the position that is p centimeters from the west end of the flowerbed Position p.

You will plant Flower i (1 ≤ i ≤ N) as follows: first, plant one at Position w_i, then plant one every d_i centimeters endlessly toward the east. That is, Flower i will be planted at the positions w_i, w_i + d_i, w_i + 2 d_i, Note that more than one flower may be planted at the same position.

Find the position at which the K-th flower from the west is planted. If more than one flower is planted at the same position, they are counted individually.

Constraints

  • 1 ≤ N ≤ 10^5
  • 1 ≤ K ≤ 10^9
  • 1 ≤ w_i ≤ 10^{18}
  • 1 ≤ d_i ≤ 10^9
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
w_1 d_1
:
w_N d_N

Output

When the K-th flower from the west is planted at Position X, print the value of X. (The westmost flower is counted as the 1-st flower.)


Sample Input 1

2 6
20 10
25 15

Sample Output 1

50

Two kinds of flowers are planted at the following positions:

  • Flower 1: Position 20, 30, 40, 50, 60,
  • Flower 2: Position 25, 40, 55, 70, 85,

The sixth flower from the west is the Flower 1 planted at Position 50. Note that the two flowers planted at Position 40 are counted individually.


Sample Input 2

3 9
10 10
10 10
10 10

Sample Output 2

30

Three flowers are planted at each of the positions 10, 20, 30, Thus, the ninth flower from the west is planted at Position 30.


Sample Input 3

1 1000000000
1000000000000000000 1000000000

Sample Output 3

1999999999000000000
D - Shock

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題

無向グラフ G が与えられます。GN 個の頂点と M 本の辺を持ちます。G の頂点には 1 から N までの番号が付けられており、Gi 番目の辺 (1 ≤ i ≤ M) は頂点 a_ib_i を結びます。G は自己辺や多重辺を持ちません。

あなたは、G の二頂点間に辺を付け足す操作を繰り返し行うことができます。ただし、その結果 G が自己辺や多重辺を持ってはなりません。また、頂点 12 が直接的または間接的に辺で結ばれてしまうと、あなたの身体を 1000000007 ボルトの電圧が襲います。これも避けなければなりません。

これらの条件のもとで、最大で何本の辺を付け足すことができるでしょうか?なお、頂点 1 と頂点 2 がはじめから直接的または間接的に辺で結ばれていることはありません。

制約

  • 2 ≤ N ≤ 10^5
  • 0 ≤ M ≤ 10^5
  • 1 ≤ a_i < b_i ≤ N
  • (a_i, b_i) はすべて異なる。
  • G の頂点 1, 2 は直接的にも間接的にも辺で結ばれていない。

入力

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

N M
a_1 b_1
:
a_M b_M

出力

付け足すことのできる辺の本数の最大値を出力せよ。


入力例 1

4 1
1 3

出力例 1

2

上図のように、2 本の辺を付け足すことができます。3 本以上の辺を付け足すことはできません。


入力例 2

2 0

出力例 2

0

辺を付け足すことはできません。


入力例 3

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

出力例 3

12

Score : 100 points

Problem Statement

You are given an undirected graph G. G has N vertices and M edges. The vertices are numbered from 1 through N, and the i-th edge (1 ≤ i ≤ M) connects Vertex a_i and b_i. G does not have self-loops and multiple edges.

You can repeatedly perform the operation of adding an edge between two vertices. However, G must not have self-loops or multiple edges as the result. Also, if Vertex 1 and 2 are connected directly or indirectly by edges, your body will be exposed to a voltage of 1000000007 volts. This must also be avoided.

Under these conditions, at most how many edges can you add? Note that Vertex 1 and 2 are never connected directly or indirectly in the beginning.

Constraints

  • 2 ≤ N ≤ 10^5
  • 0 ≤ M ≤ 10^5
  • 1 ≤ a_i < b_i ≤ N
  • All pairs (a_i, b_i) are distinct.
  • Vertex 1 and 2 in G are not connected directly or indirectly by edges.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M

Output

Print the maximum number of edges that can be added.


Sample Input 1

4 1
1 3

Sample Output 1

2

As shown above, two edges can be added. It is not possible to add three or more edges.


Sample Input 2

2 0

Sample Output 2

0

No edge can be added.


Sample Input 3

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

Sample Output 3

12
E - White and Blue

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

りんご王国議会で、ある法案の採決が行われています。

N 人の議員が出席しており、i 人目の議員 (1 ≤ i ≤ N)w_i 枚の白票と b_i 枚の青票を持っています。それぞれの議員 i は、法案に賛成であれば持っている w_i 枚の白票すべてを投票箱に入れ、法案に反対であれば持っている b_i 枚の青票すべてを投票箱に入れます。これら以外の行為は認められていません。例えば、議員は投票を放棄したり、持っている白票の一部または青票の一部のみを投票箱に入れてはなりません。

すべての議員の投票後に、投票箱に入っている票のうち P パーセント以上が白票であれば法案が可決され、白票が P パーセント未満であれば否決されます。

法案が可決されるためには、少なくとも何人の議員の賛成が必要でしょうか?

制約

  • 1 ≤ N ≤ 10^5
  • 1 ≤ P ≤ 100
  • 1 ≤ w_i ≤ 10^9
  • 1 ≤ b_i ≤ 10^9
  • 入力値はすべて整数である。

入力

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

N P
w_1 b_1
w_2 b_2
:
w_N b_N

出力

法案の可決に必要な最小の賛成議員の数を出力せよ。


入力例 1

4 75
1 1
1 1
1 1
1 1

出力例 1

3

4 人の議員がそれぞれ白票 1 枚と青票 1 枚を持っている「普通」の投票です。法案の可決には、4 人のうち 75 パーセント、すなわち 3 人以上の賛成が必要です。


入力例 2

4 75
1 1
1 1
1 1
100 1

出力例 2

1

100 枚の白票を持っている議員 1 人の賛成で法案が可決されます。


入力例 3

5 60
6 3
5 9
3 4
7 8
4 7

出力例 3

3

Score : 100 points

Problem Statement

Ringo Kingdom Congress is voting on a bill.

N members are present, and the i-th member (1 ≤ i ≤ N) has w_i white ballots and b_i blue ballots. Each member i will put all the w_i white ballots into the box if he/she is in favor of the bill, and put all the b_i blue ballots into the box if he/she is not in favor of the bill. No other action is allowed. For example, a member must not forfeit voting, or put only a part of his/her white ballots or a part of his/her blue ballots into the box.

After all the members vote, if at least P percent of the ballots in the box is white, the bill is passed; if less than P percent of the ballots is white, the bill is rejected.

In order for the bill to pass, at least how many members must be in favor of it?

Constraints

  • 1 ≤ N ≤ 10^5
  • 1 ≤ P ≤ 100
  • 1 ≤ w_i ≤ 10^9
  • 1 ≤ b_i ≤ 10^9
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N P
w_1 b_1
w_2 b_2
:
w_N b_N

Output

Print the minimum number of members in favor of the bill required for passage.


Sample Input 1

4 75
1 1
1 1
1 1
1 1

Sample Output 1

3

This is a "normal" vote, where each of the four members has one white ballot and one blue ballot. For the bill to pass, at least 75 percent of the four, that is, at least three must be in favor of it.


Sample Input 2

4 75
1 1
1 1
1 1
100 1

Sample Output 2

1

The "yes" vote of the member with 100 white ballots alone is enough to pass the bill.


Sample Input 3

5 60
6 3
5 9
3 4
7 8
4 7

Sample Output 3

3
F - Capture

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

東西方向に伸びる細長い森に N 匹のけものが生息しています。以下では、森の西端から p メートルの地点を地点 p と呼びます。西から i 匹目にいるけもの (1 ≤ i ≤ N) は地点 x_i におり、捕獲すると s_i 円で売れます。

あなたは整数 L, R (L ≤ R) を選び、地点 L から地点 R までの両端を含む範囲、[L, R] に網を放ちます。すると、その範囲内のけものがすべて捕獲されます。ただし、網に R - L 円のコストがかかり、あなたの利益は (捕獲されたすべてのけもの i に対する s_i の合計) - (R - L) 円となります。

網を一度だけ放つとき、得られる最大の利益はいくらでしょうか?

制約

  • 1 ≤ N ≤ 2 × 10^5
  • 1 ≤ x_1 < x_2 < ... < x_N ≤ 10^{15}
  • 1 ≤ s_i ≤ 10^9
  • 入力値はすべて整数である。

入力

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

N
x_1 s_1
x_2 s_2
:
x_N s_N

出力

得られる最大の利益が X 円のとき、X の値を出力せよ。


入力例 1

5
10 20
40 50
60 30
70 40
90 10

出力例 1

90

範囲 [L = 40, R = 70] に網を放つと西から 2, 3, 4 匹目のけものを捕獲でき、s_2 + s_3 + s_4 - (R - L) = 90 円の利益が得られます。91 円以上の利益を得ることはできません。


入力例 2

5
10 2
40 5
60 3
70 4
90 1

出力例 2

5

けものたちは入力例 1 と同じ位置にいますが、彼らの売値が大幅に下がっているため、二匹以上を狙うべきではありません。範囲 [L = 40, R = 40] に網を放つことで s_2 - (R - L) = 5 円の利益が得られ、これが最大です。


入力例 3

4
1 100
3 200
999999999999999 150
1000000000000000 150

出力例 3

299

Score : 100 points

Problem Statement

In a long narrow forest stretching east-west, there are N beasts. Below, we will call the point that is p meters from the west end Point p. The i-th beast from the west (1 ≤ i ≤ N) is at Point x_i, and can be sold for s_i yen (the currency of Japan) if captured.

You will choose two integers L and R (L ≤ R), and throw a net to cover the range from Point L to Point R including both ends, [L, R]. Then, all the beasts in the range will be captured. However, the net costs R - L yen and your profit will be (the sum of s_i over all captured beasts i) - (R - L) yen.

What is the maximum profit that can be earned by throwing a net only once?

Constraints

  • 1 ≤ N ≤ 2 × 10^5
  • 1 ≤ x_1 < x_2 < ... < x_N ≤ 10^{15}
  • 1 ≤ s_i ≤ 10^9
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 s_1
x_2 s_2
:
x_N s_N

Output

When the maximum profit is X yen, print the value of X.


Sample Input 1

5
10 20
40 50
60 30
70 40
90 10

Sample Output 1

90

If you throw a net covering the range [L = 40, R = 70], the second, third and fourth beasts from the west will be captured, generating the profit of s_2 + s_3 + s_4 - (R - L) = 90 yen. It is not possible to earn 91 yen or more.


Sample Input 2

5
10 2
40 5
60 3
70 4
90 1

Sample Output 2

5

The positions of the beasts are the same as Sample 1, but the selling prices dropped significantly, so you should not aim for two or more beasts. By throwing a net covering the range [L = 40, R = 40], you can earn s_2 - (R - L) = 5 yen, and this is the maximum possible profit.


Sample Input 3

4
1 100
3 200
999999999999999 150
1000000000000000 150

Sample Output 3

299
G - Coinage

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

英小文字からなる二つの文字列 s, t と整数 L が与えられます。

s, t を任意の順に一個以上並べて長さ L の文字列を生成することを考えます。このとき、同じ文字列を複数回用いることもできます。

例えば、s = at, t = code, L = 6 のとき、文字列 atatat, atcode, codeat を生成することができます。

このようにして生成される長さ L の文字列のうち、辞書順最小のものを求めてください。なお、入力として与えられるケースでは、長さ L の文字列を生成することは必ず可能です。

制約

  • 1 ≤ L ≤ 2 × 10^5
  • 1 ≤ |s|, |t| ≤ L
  • s, t は英小文字からなる。
  • 問題文で述べた方法で、s, t から長さ L の文字列を生成することが可能である。

入力

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

L
s
t

出力

問題文で述べた方法で生成される長さ L の文字列のうち、辞書順最小のものを出力せよ。


入力例 1

6
at
code

出力例 1

atatat

この入力は問題文中で示した例に対応します。


入力例 2

8
coding
festival

出力例 2

festival

s, t のうち一方が長さ L の文字列を生成する上でまったく役に立たないことがあります。


入力例 3

8
same
same

出力例 3

samesame

s = t であることもあります。


入力例 4

10
coin
age

出力例 4

ageagecoin

Score : 100 points

Problem Statement

You are given two strings s and t consisting of lowercase English letters and an integer L.

We will consider generating a string of length L by concatenating one or more copies of s and t. Here, it is allowed to use the same string more than once.

For example, when s = at, t = code and L = 6, the strings atatat, atcode and codeat can be generated.

Among the strings that can be generated in this way, find the lexicographically smallest one. In the cases given as input, it is always possible to generate a string of length L.

Constraints

  • 1 ≤ L ≤ 2 × 10^5
  • 1 ≤ |s|, |t| ≤ L
  • s and t consist of lowercase English letters.
  • It is possible to generate a string of length L in the way described in Problem Statement.

Input

Input is given from Standard Input in the following format:

N
x_1 s_1
x_2 s_2
:
x_N s_N

Output

Print the lexicographically smallest string among the ones that can be generated in the way described in Problem Statement.


Sample Input 1

6
at
code

Sample Output 1

atatat

This input corresponds to the example shown in Problem Statement.


Sample Input 2

8
coding
festival

Sample Output 2

festival

It is possible that either s or t cannot be used at all in generating a string of length L.


Sample Input 3

8
same
same

Sample Output 3

samesame

It is also possible that s = t.


Sample Input 4

10
coin
age

Sample Output 4

ageagecoin
H - Akashic Records

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

無限個の項からなる数列 a_1, a_2, を考えます。はじめ、すべての項の値は 0 であり、この状態から Q 回の操作を続けて行います。i 回目の操作 (1 ≤ i ≤ Q) は次の通りです。

  • すべての正の整数 j に対し、a_{j × m_i} の値に x_i を加える。

これらの Q 回の操作後の最も大きい項の値を求めてください。

制約

  • 1 ≤ Q ≤ 299
  • 2 ≤ m_i ≤ 300
  • -10^6 ≤ x_i ≤ 10^6
  • m_i はすべて異なる。
  • 入力値はすべて整数である。

入力

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

Q
m_1 x_1
:
m_Q x_Q

出力

Q 回の操作後の最も大きい項の値を出力せよ。


入力例 1

3
2 10
3 -20
6 15

出力例 1

10

数列の各項 a_1, a_2, の値は以下のように変化します。

  • 操作前: 0, 0, 0, 0, 0, 0,
  • 1 回目の操作後: 0, 10, 0, 10, 0, 10,
  • 2 回目の操作後: 0, 10, -20, 10, 0, -10,
  • 3 回目の操作後: 0, 10, -20, 10, 0, 5,

すべての操作後の最も大きい項の値は 10 です。


入力例 2

3
10 -3
50 4
100 -5

出力例 2

1

入力例 3

5
56 114834
72 -149861
100 190757
192 -132693
240 133108

出力例 3

438699

Score : 100 points

Problem Statement

Consider an infinite sequence a_1, a_2, Initially, the values of all the terms are 0, and from this state we will sequentially perform Q operations. The i-th operation (1 ≤ i ≤ Q) is as follows:

  • For every positive integer j, add x_i to the value of a_{j × m_i}.

Find the value of the largest term after these Q operations.

Constraints

  • 1 ≤ Q ≤ 299
  • 2 ≤ m_i ≤ 300
  • -10^6 ≤ x_i ≤ 10^6
  • All m_i are distinct.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
m_1 x_1
:
m_Q x_Q

Output

Print the value of the largest term after the Q operations.


Sample Input 1

3
2 10
3 -20
6 15

Sample Output 1

10

The values of each terms in the sequence a_1, a_2, change as follows:

  • Before the operations: 0, 0, 0, 0, 0, 0,
  • After the 1-st operation: 0, 10, 0, 10, 0, 10,
  • After the 2-nd operation: 0, 10, -20, 10, 0, -10,
  • After the 3-rd operation: 0, 10, -20, 10, 0, 5,

The value of the largest term after all the operations is 10.


Sample Input 2

3
10 -3
50 4
100 -5

Sample Output 2

1

Sample Input 3

5
56 114834
72 -149861
100 190757
192 -132693
240 133108

Sample Output 3

438699
I - Nice to Meet You

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

りんご海に N 個の島が浮かんでおり、M 社の業者がこれらの島を結ぶ船便を運行しています。便宜上、これらの島を島 1, 2, …, N と呼び、これらの業者を業者 1, 2, …, M と呼びます。

りんご海の海流は毎日大きく変わります。業者 i (1 ≤ i ≤ M) は、その日の海の状況に応じて、島 a_i から b_i への便または島 b_i から a_i への便のどちらか一方のみを運行します。どちらの方向の便が運行されるかは、それぞれの業者について独立に、等確率で決定されるものとします。

いま、島 1 に高橋くんが、島 2 に低橋くんがいます。M 社の業者による船便を用いて、高橋くんと低橋くんがその日のうちに同じ島に移動することができる確率を P とします。ただし、船の所要時間などは無視します。このとき、P × 2^M は整数となります。P × 2^M10^9 + 7 で割ったあまりを求めてください。

制約

  • 2 ≤ N ≤ 15
  • 1 ≤ M ≤ N(N-1)/2
  • 1 ≤ a_i < b_i ≤ N
  • (a_i, b_i) はすべて異なる。

入力

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

N M
a_1 b_1
:
a_M b_M

出力

P × 2^M10^9 + 7 で割ったあまりを出力せよ。


入力例 1

4 3
1 3
2 3
3 4

出力例 1

6
36cba65088d9b1224a6ce9665aa44048.png

上図に示した 2^M = 8 通りの状況が等確率で発生し、そのうち高橋くんと低橋くんが同じ島で出会える状況は 6 通りです。したがって、P = 6/2^M, P × 2^M = 6 となります。


入力例 2

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

出力例 2

18

入力例 3

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

出力例 3

64

Score : 100 points

Problem Statement

There are N islands floating in Ringo Sea, and M travel agents operate ships between these islands. For convenience, we will call these islands Island 1, 2, …, N, and call these agents Agent 1, 2, …, M.

The sea currents in Ringo Sea change significantly each day. Depending on the state of the sea on the day, Agent i (1 ≤ i ≤ M) operates ships from Island a_i to b_i, or Island b_i to a_i, but not both at the same time. Assume that the direction of the ships of each agent is independently selected with equal probability.

Now, Takahashi is on Island 1, and Hikuhashi is on Island 2. Let P be the probability that Takahashi and Hikuhashi can travel to the same island in the day by ships operated by the M agents, ignoring the factors such as the travel time for ships. Then, P × 2^M is an integer. Find P × 2^M modulo 10^9 + 7.

Constraints

  • 2 ≤ N ≤ 15
  • 1 ≤ M ≤ N(N-1)/2
  • 1 ≤ a_i < b_i ≤ N
  • All pairs (a_i, b_i) are distinct.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M

Output

Print the value P × 2^M modulo 10^9 + 7.


Sample Input 1

4 3
1 3
2 3
3 4

Sample Output 1

6
36cba65088d9b1224a6ce9665aa44048.png

The 2^M = 8 scenarios shown above occur with equal probability, and Takahashi and Hikuhashi can meet on the same island in 6 of them. Thus, P = 6/2^M and P × 2^M = 6.


Sample Input 2

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

Sample Output 2

18

Sample Input 3

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

Sample Output 3

64
J - Indifferent

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

2N 個の壺があります。このうち i 個目の壺 (1 ≤ i ≤ 2N) の市場価格は p_i 円です。

これから、あなたとダックスフンドのルンルンは交互に壺を一つずつ取っていきます。あなたから始めて、すべての壺があなたかルンルンに取られるまで続けます。ルンルンは壺の市場価格がわからないので、毎回、残っている壺の中から無作為に等確率で一つを選びます。あなたはこのルンルンの行動と、壺の市場価格を知っています。

あなたの目的は、取る壺の市場価格の合計を S 円としたとき、S の期待値を最大化することです。最適な戦略を取ったときの S の期待値を求めてください。

制約

  • 1 ≤ N ≤ 10^5
  • 1 ≤ p_i ≤ 2 × 10^5
  • 入力値はすべて整数である。

入力

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

N
p_1
:
p_{2N}

出力

S の期待値を最大化する戦略を取ったときの S の期待値を出力せよ。出力は、ジャッジの出力からの絶対誤差または相対誤差が 10^{-9} 以下であれば正答とみなされる。


入力例 1

1
150000
108

出力例 1

150000.0

当然、15 万円の壺を選ぶべきです。


入力例 2

2
50000
50000
100000
100000

出力例 2

183333.3333333333

あなたはまず、10 万円の壺のうち一つを取ります。もう一つの 10 万円の壺は、次のルンルンの番で取られなければ手に入り、その確率は 2/3 です。もしその壺を取られてしまった場合は、5 万円の壺で我慢することになります。以上から、最適な戦略を取ったときの S の期待値は 2/3 × (100000 + 100000) + 1/3 × (100000 + 50000) = 183333.3333… となります。

Score : 100 points

Problem Statement

We have 2N pots. The market price of the i-th pot (1 ≤ i ≤ 2N) is p_i yen (the currency of Japan).

Now, you and Lunlun the dachshund will alternately take one pot. You will go first, and we will continue until all the pots are taken by you or Lunlun. Since Lunlun does not know the market prices of the pots, she will always choose a pot randomly from the remaining pots with equal probability. You know this behavior of Lunlun, and the market prices of the pots.

Let the sum of the market prices of the pots you take be S yen. Your objective is to maximize the expected value of S. Find the expected value of S when the optimal strategy is adopted.

Constraints

  • 1 ≤ N ≤ 10^5
  • 1 ≤ p_i ≤ 2 × 10^5
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
p_1
:
p_{2N}

Output

Print the expected value of S when the strategy to maximize the expected value of S is adopted. The output is considered correct if its absolute or relative error from the judge's output is at most 10^{-9}.


Sample Input 1

1
150000
108

Sample Output 1

150000.0

Naturally, you should choose the 150000 yen pot.


Sample Input 2

2
50000
50000
100000
100000

Sample Output 2

183333.3333333333

First, you will take one of the 100000 yen pots. The other 100000 yen pot will become yours if it is not taken in Lunlun's next turn, with probability 2/3. If it is taken, you will have to settle for a 50000 yen pot. Thus, the expected value of S when the optimal strategy is adopted is 2/3 × (100000 + 100000) + 1/3 × (100000 + 50000) = 183333.3333…