A - Frog 1

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 個の足場があります。 足場には 1, 2, \ldots, N と番号が振られています。 各 i (1 \leq i \leq N) について、足場 i の高さは h_i です。

最初、足場 1 にカエルがいます。 カエルは次の行動を何回か繰り返し、足場 N まで辿り着こうとしています。

  • 足場 i にいるとき、足場 i + 1 または i + 2 へジャンプする。 このとき、ジャンプ先の足場を j とすると、コスト |h_i - h_j| を支払う。

カエルが足場 N に辿り着くまでに支払うコストの総和の最小値を求めてください。

制約

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

入力

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

N
h_1 h_2 \ldots h_N

出力

カエルが支払うコストの総和の最小値を出力せよ。


入力例 1

4
10 30 40 20

出力例 1

30

足場 124 と移動すると、コストの総和は |10 - 30| + |30 - 20| = 30 となります。


入力例 2

2
10 10

出力例 2

0

足場 12 と移動すると、コストの総和は |10 - 10| = 0 となります。


入力例 3

6
30 10 60 10 60 50

出力例 3

40

足場 1356 と移動すると、コストの総和は |30 - 60| + |60 - 60| + |60 - 50| = 40 となります。

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:

  • If the frog is currently on Stone i, jump to Stone i + 1 or Stone i + 2. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

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

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.


Sample Input 1

4
10 30 40 20

Sample Output 1

30

If we follow the path 124, the total cost incurred would be |10 - 30| + |30 - 20| = 30.


Sample Input 2

2
10 10

Sample Output 2

0

If we follow the path 12, the total cost incurred would be |10 - 10| = 0.


Sample Input 3

6
30 10 60 10 60 50

Sample Output 3

40

If we follow the path 1356, the total cost incurred would be |30 - 60| + |60 - 60| + |60 - 50| = 40.

B - Frog 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 個の足場があります。 足場には 1, 2, \ldots, N と番号が振られています。 各 i (1 \leq i \leq N) について、足場 i の高さは h_i です。

最初、足場 1 にカエルがいます。 カエルは次の行動を何回か繰り返し、足場 N まで辿り着こうとしています。

  • 足場 i にいるとき、足場 i + 1, i + 2, \ldots, i + K のどれかへジャンプする。 このとき、ジャンプ先の足場を j とすると、コスト |h_i - h_j| を支払う。

カエルが足場 N に辿り着くまでに支払うコストの総和の最小値を求めてください。

制約

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

入力

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

N K
h_1 h_2 \ldots h_N

出力

カエルが支払うコストの総和の最小値を出力せよ。


入力例 1

5 3
10 30 40 50 20

出力例 1

30

足場 125 と移動すると、コストの総和は |10 - 30| + |30 - 20| = 30 となります。


入力例 2

3 1
10 20 10

出力例 2

20

足場 123 と移動すると、コストの総和は |10 - 20| + |20 - 10| = 20 となります。


入力例 3

2 100
10 10

出力例 3

0

足場 12 と移動すると、コストの総和は |10 - 10| = 0 となります。


入力例 4

10 4
40 10 20 70 80 10 20 70 80 60

出力例 4

40

足場 14810 と移動すると、コストの総和は |40 - 70| + |70 - 70| + |70 - 60| = 40 となります。

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:

  • If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, i + K. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

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

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.


Sample Input 1

5 3
10 30 40 50 20

Sample Output 1

30

If we follow the path 125, the total cost incurred would be |10 - 30| + |30 - 20| = 30.


Sample Input 2

3 1
10 20 10

Sample Output 2

20

If we follow the path 123, the total cost incurred would be |10 - 20| + |20 - 10| = 20.


Sample Input 3

2 100
10 10

Sample Output 3

0

If we follow the path 12, the total cost incurred would be |10 - 10| = 0.


Sample Input 4

10 4
40 10 20 70 80 10 20 70 80 60

Sample Output 4

40

If we follow the path 14810, the total cost incurred would be |40 - 70| + |70 - 70| + |70 - 60| = 40.

C - Vacation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

明日から太郎君の夏休みが始まります。 太郎君は夏休みの計画を立てることにしました。

夏休みは N 日からなります。 各 i (1 \leq i \leq N) について、i 日目には太郎君は次の活動のうちひとつを選んで行います。

  • A: 海で泳ぐ。 幸福度 a_i を得る。
  • B: 山で虫取りをする。 幸福度 b_i を得る。
  • C: 家で宿題をする。 幸福度 c_i を得る。

太郎君は飽き性なので、2 日以上連続で同じ活動を行うことはできません。

太郎君が得る幸福度の総和の最大値を求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 10^5
  • 1 \leq a_i, b_i, c_i \leq 10^4

入力

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

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

出力

太郎君が得る幸福度の総和の最大値を出力せよ。


入力例 1

3
10 40 70
20 50 80
30 60 90

出力例 1

210

C, B, C の順に活動を行うと、幸福度の総和は 70 + 50 + 90 = 210 となります。


入力例 2

1
100 10 1

出力例 2

100

入力例 3

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

出力例 3

46

C, A, B, A, C, B, A の順に活動を行えばよいです。

Score : 100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of N days. For each i (1 \leq i \leq N), Taro will choose one of the following activities and do it on the i-th day:

  • A: Swim in the sea. Gain a_i points of happiness.
  • B: Catch bugs in the mountains. Gain b_i points of happiness.
  • C: Do homework at home. Gain c_i points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 10^5
  • 1 \leq a_i, b_i, c_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the maximum possible total points of happiness that Taro gains.


Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.


Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

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

Sample Output 3

46

Taro should do activities in the order C, A, B, A, C, B, A.

D - Knapsack 1

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 個の品物があります。 品物には 1, 2, \ldots, N と番号が振られています。 各 i (1 \leq i \leq N) について、品物 i の重さは w_i で、価値は v_i です。

太郎君は、N 個の品物のうちいくつかを選び、ナップサックに入れて持ち帰ることにしました。 ナップサックの容量は W であり、持ち帰る品物の重さの総和は W 以下でなければなりません。

太郎君が持ち帰る品物の価値の総和の最大値を求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 100
  • 1 \leq W \leq 10^5
  • 1 \leq w_i \leq W
  • 1 \leq v_i \leq 10^9

入力

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

N W
w_1 v_1
w_2 v_2
:
w_N v_N

出力

太郎君が持ち帰る品物の価値の総和の最大値を出力せよ。


入力例 1

3 8
3 30
4 50
5 60

出力例 1

90

品物 1, 3 を選べばよいです。 すると、重さの総和は 3 + 5 = 8 となり、価値の総和は 30 + 60 = 90 となります。


入力例 2

5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

出力例 2

5000000000

答えは 32-bit 整数型に収まらない場合があります。


入力例 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

出力例 3

17

品物 2, 4, 5 を選べばよいです。 すると、重さの総和は 5 + 6 + 3 = 14 となり、価値の総和は 6 + 6 + 5 = 17 となります。

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 100
  • 1 \leq W \leq 10^5
  • 1 \leq w_i \leq W
  • 1 \leq v_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.


Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken. Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.


Sample Input 2

5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 2

5000000000

The answer may not fit into a 32-bit integer type.


Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken. Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.

E - Knapsack 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 個の品物があります。 品物には 1, 2, \ldots, N と番号が振られています。 各 i (1 \leq i \leq N) について、品物 i の重さは w_i で、価値は v_i です。

太郎君は、N 個の品物のうちいくつかを選び、ナップサックに入れて持ち帰ることにしました。 ナップサックの容量は W であり、持ち帰る品物の重さの総和は W 以下でなければなりません。

太郎君が持ち帰る品物の価値の総和の最大値を求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 100
  • 1 \leq W \leq 10^9
  • 1 \leq w_i \leq W
  • 1 \leq v_i \leq 10^3

入力

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

N W
w_1 v_1
w_2 v_2
:
w_N v_N

出力

太郎君が持ち帰る品物の価値の総和の最大値を出力せよ。


入力例 1

3 8
3 30
4 50
5 60

出力例 1

90

品物 1, 3 を選べばよいです。 すると、重さの総和は 3 + 5 = 8 となり、価値の総和は 30 + 60 = 90 となります。


入力例 2

1 1000000000
1000000000 10

出力例 2

10

入力例 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

出力例 3

17

品物 2, 4, 5 を選べばよいです。 すると、重さの総和は 5 + 6 + 3 = 14 となり、価値の総和は 6 + 6 + 5 = 17 となります。

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 100
  • 1 \leq W \leq 10^9
  • 1 \leq w_i \leq W
  • 1 \leq v_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.


Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken. Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.


Sample Input 2

1 1000000000
1000000000 10

Sample Output 2

10

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken. Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.

F - LCS

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

文字列 s および t が与えられます。 s の部分列かつ t の部分列であるような文字列のうち、最長のものをひとつ求めてください。

注釈

文字列 x部分列とは、x から 0 個以上の文字を取り除いた後、残りの文字を元の順序で連結して得られる文字列のことです。

制約

  • s および t は英小文字からなる文字列である。
  • 1 \leq |s|, |t| \leq 3000

入力

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

s
t

出力

s の部分列かつ t の部分列であるような文字列のうち、最長のものをひとつ出力せよ。 答えが複数ある場合、どれを出力してもよい。


入力例 1

axyb
abyxb

出力例 1

axb

答えは axb または ayb です。 どちらを出力しても正解となります。


入力例 2

aa
xayaz

出力例 2

aa

入力例 3

a
z

出力例 3



答えは (空文字列) です。


入力例 4

abracadabra
avadakedavra

出力例 4

aaadara

Score : 100 points

Problem Statement

You are given strings s and t. Find one longest string that is a subsequence of both s and t.

Notes

A subsequence of a string x is the string obtained by removing zero or more characters from x and concatenating the remaining characters without changing the order.

Constraints

  • s and t are strings consisting of lowercase English letters.
  • 1 \leq |s|, |t| \leq 3000

Input

Input is given from Standard Input in the following format:

s
t

Output

Print one longest string that is a subsequence of both s and t. If there are multiple such strings, any of them will be accepted.


Sample Input 1

axyb
abyxb

Sample Output 1

axb

The answer is axb or ayb; either will be accepted.


Sample Input 2

aa
xayaz

Sample Output 2

aa

Sample Input 3

a
z

Sample Output 3



The answer is (an empty string).


Sample Input 4

abracadabra
avadakedavra

Sample Output 4

aaadara
G - Longest Path

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 頂点 M 辺の有向グラフ G があります。 頂点には 1, 2, \ldots, N と番号が振られています。 各 i (1 \leq i \leq M) について、i 番目の有向辺は頂点 x_i から y_i へ張られています。 G有向閉路を含みません

G の有向パスのうち、最長のものの長さを求めてください。 ただし、有向パスの長さとは、有向パスに含まれる辺の本数のことです。

制約

  • 入力はすべて整数である。
  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq x_i, y_i \leq N
  • ペア (x_i, y_i) はすべて相異なる。
  • G有向閉路を含まない

入力

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

N M
x_1 y_1
x_2 y_2
:
x_M y_M

出力

G の有向パスのうち、最長のものの長さを出力せよ。


入力例 1

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

出力例 1

3

次図の赤い有向パスが最長です。


入力例 2

6 3
2 3
4 5
5 6

出力例 2

2

次図の赤い有向パスが最長です。


入力例 3

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

出力例 3

3

例えば、次図の赤い有向パスが最長です。

Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges. The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i. G does not contain directed cycles.

Find the length of the longest directed path in G. Here, the length of a directed path is the number of edges in it.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq x_i, y_i \leq N
  • All pairs (x_i, y_i) are distinct.
  • G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.


Sample Input 1

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

Sample Output 1

3

The red directed path in the following figure is the longest:


Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:


Sample Input 3

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

Sample Output 3

3

The red directed path in the following figure is one of the longest:

H - Grid 1

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

H 行、横 W 列のグリッドがあります。 上から i 行目、左から j 列目のマスを (i, j) で表します。

i, j (1 \leq i \leq H, 1 \leq j \leq W) について、マス (i, j) の情報が文字 a_{i, j} によって与えられます。 a_{i, j}. ならばマス (i, j) は空マスであり、a_{i, j}# ならばマス (i, j) は壁のマスです。 マス (1, 1) および (H, W) は空マスであることが保証されています。

太郎君は、マス (1, 1) から出発し、右または下に隣り合う空マスへの移動を繰り返すことで、マス (H, W) まで辿り着こうとしています。

マス (1, 1) から (H, W) までの太郎君の経路は何通りでしょうか? 答えは非常に大きくなりうるので、10^9 + 7 で割った余りを求めてください。

制約

  • H および W は整数である。
  • 2 \leq H, W \leq 1000
  • a_{i, j}. または # である。
  • マス (1, 1) および (H, W) は空マスである。

入力

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

H W
a_{1, 1}\ldotsa_{1, W}
:
a_{H, 1}\ldotsa_{H, W}

出力

マス (1, 1) から (H, W) までの太郎君の経路は何通りか? 10^9 + 7 で割った余りを出力せよ。


入力例 1

3 4
...#
.#..
....

出力例 1

3

経路は次図の 3 通りです。


入力例 2

5 2
..
#.
..
.#
..

出力例 2

0

経路が存在しない場合もあります。


入力例 3

5 5
..#..
.....
#...#
.....
..#..

出力例 3

24

入力例 4

20 20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

出力例 4

345263555

答えを 10^9 + 7 で割った余りを出力することを忘れずに。

Score : 100 points

Problem Statement

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

For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is ., Square (i, j) is an empty square; if a_{i, j} is #, Square (i, j) is a wall square. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.

Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.

Find the number of Taro's paths from Square (1, 1) to (H, W). As the answer can be extremely large, find the count modulo 10^9 + 7.

Constraints

  • H and W are integers.
  • 2 \leq H, W \leq 1000
  • a_{i, j} is . or #.
  • Squares (1, 1) and (H, W) are empty squares.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}\ldotsa_{1, W}
:
a_{H, 1}\ldotsa_{H, W}

Output

Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.


Sample Input 1

3 4
...#
.#..
....

Sample Output 1

3

There are three paths as follows:


Sample Input 2

5 2
..
#.
..
.#
..

Sample Output 2

0

There may be no paths.


Sample Input 3

5 5
..#..
.....
#...#
.....
..#..

Sample Output 3

24

Sample Input 4

20 20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

Sample Output 4

345263555

Be sure to print the count modulo 10^9 + 7.

I - Coins

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N を正の奇数とします。

N 枚のコインがあります。 コインには 1, 2, \ldots, N と番号が振られています。 各 i (1 \leq i \leq N) について、コイン i を投げると、確率 p_i で表が出て、確率 1 - p_i で裏が出ます。

太郎君は N 枚のコインをすべて投げました。 このとき、表の個数が裏の個数を上回る確率を求めてください。

制約

  • N は奇数である。
  • 1 \leq N \leq 2999
  • p_i は実数であり、小数第 2 位まで与えられる。
  • 0 < p_i < 1

入力

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

N
p_1 p_2 \ldots p_N

出力

表の個数が裏の個数を上回る確率を出力せよ。 絶対誤差が 10^{-9} 以下ならば正解となる。


入力例 1

3
0.30 0.60 0.80

出力例 1

0.612

表の個数が裏の個数を上回るような各ケースの確率を計算すると、次のようになります。

  • (コイン 1, コイン 2, コイン 3) = (表, 表, 表) となる確率は、0.3 × 0.6 × 0.8 = 0.144 である。
  • (コイン 1, コイン 2, コイン 3) = (裏, 表, 表) となる確率は、0.7 × 0.6 × 0.8 = 0.336 である。
  • (コイン 1, コイン 2, コイン 3) = (表, 裏, 表) となる確率は、0.3 × 0.4 × 0.8 = 0.096 である。
  • (コイン 1, コイン 2, コイン 3) = (表, 表, 裏) となる確率は、0.3 × 0.6 × 0.2 = 0.036 である。

よって、表の個数が裏の個数を上回る確率は、0.144 + 0.336 + 0.096 + 0.036 = 0.612 です。


入力例 2

1
0.50

出力例 2

0.5

例えば、0.500, 0.500000001, 0.499999999 などを出力しても正解となります。


入力例 3

5
0.42 0.01 0.42 0.99 0.42

出力例 3

0.3821815872

Score : 100 points

Problem Statement

Let N be a positive odd number.

There are N coins, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), when Coin i is tossed, it comes up heads with probability p_i and tails with probability 1 - p_i.

Taro has tossed all the N coins. Find the probability of having more heads than tails.

Constraints

  • N is an odd number.
  • 1 \leq N \leq 2999
  • p_i is a real number and has two decimal places.
  • 0 < p_i < 1

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 \ldots p_N

Output

Print the probability of having more heads than tails. The output is considered correct when the absolute error is not greater than 10^{-9}.


Sample Input 1

3
0.30 0.60 0.80

Sample Output 1

0.612

The probability of each case where we have more heads than tails is as follows:

  • The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Head) is 0.3 × 0.6 × 0.8 = 0.144;
  • The probability of having (Coin 1, Coin 2, Coin 3) = (Tail, Head, Head) is 0.7 × 0.6 × 0.8 = 0.336;
  • The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Tail, Head) is 0.3 × 0.4 × 0.8 = 0.096;
  • The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Tail) is 0.3 × 0.6 × 0.2 = 0.036.

Thus, the probability of having more heads than tails is 0.144 + 0.336 + 0.096 + 0.036 = 0.612.


Sample Input 2

1
0.50

Sample Output 2

0.5

Outputs such as 0.500, 0.500000001 and 0.499999999 are also considered correct.


Sample Input 3

5
0.42 0.01 0.42 0.99 0.42

Sample Output 3

0.3821815872
J - Sushi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 枚の皿があります。 皿には 1, 2, \ldots, N と番号が振られています。 最初、各 i (1 \leq i \leq N) について、皿 i には a_i (1 \leq a_i \leq 3) 個の寿司が置かれています。

すべての寿司が無くなるまで、太郎君は次の操作を繰り返し行います。

  • 1, 2, \ldots, N の目が等確率で出るサイコロを振り、出目を i とする。 皿 i に寿司がある場合、皿 i の寿司を 1 個食べる。 皿 i に寿司が無い場合、何も行わない。

すべての寿司が無くなるまでの操作回数の期待値を求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 300
  • 1 \leq a_i \leq 3

入力

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

N
a_1 a_2 \ldots a_N

出力

すべての寿司が無くなるまでの操作回数の期待値を出力せよ。 相対誤差が 10^{-9} 以下ならば正解となる。


入力例 1

3
1 1 1

出力例 1

5.5

1 個目の寿司を食べるまでの操作回数の期待値は 1 です。 その後、2 個目の寿司を食べるまでの操作回数の期待値は 1.5 です。 その後、3 個目の寿司を食べるまでの操作回数の期待値は 3 です。 よって、全体の操作回数の期待値は 1 + 1.5 + 3 = 5.5 です。


入力例 2

1
3

出力例 2

3

例えば、3.00, 3.000000003, 2.999999997 などを出力しても正解となります。


入力例 3

2
1 2

出力例 3

4.5

入力例 4

10
1 3 2 3 3 2 3 2 1 3

出力例 4

54.48064457488221

Score : 100 points

Problem Statement

There are N dishes, numbered 1, 2, \ldots, N. Initially, for each i (1 \leq i \leq N), Dish i has a_i (1 \leq a_i \leq 3) pieces of sushi on it.

Taro will perform the following operation repeatedly until all the pieces of sushi are eaten:

  • Roll a die that shows the numbers 1, 2, \ldots, N with equal probabilities, and let i be the outcome. If there are some pieces of sushi on Dish i, eat one of them; if there is none, do nothing.

Find the expected number of times the operation is performed before all the pieces of sushi are eaten.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 300
  • 1 \leq a_i \leq 3

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the expected number of times the operation is performed before all the pieces of sushi are eaten. The output is considered correct when the relative difference is not greater than 10^{-9}.


Sample Input 1

3
1 1 1

Sample Output 1

5.5

The expected number of operations before the first piece of sushi is eaten, is 1. After that, the expected number of operations before the second sushi is eaten, is 1.5. After that, the expected number of operations before the third sushi is eaten, is 3. Thus, the expected total number of operations is 1 + 1.5 + 3 = 5.5.


Sample Input 2

1
3

Sample Output 2

3

Outputs such as 3.00, 3.000000003 and 2.999999997 will also be accepted.


Sample Input 3

2
1 2

Sample Output 3

4.5

Sample Input 4

10
1 3 2 3 3 2 3 2 1 3

Sample Output 4

54.48064457488221
K - Stones

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 個の正整数からなる集合 A = \{ a_1, a_2, \ldots, a_N \} があります。 太郎君と次郎君が次のゲームで勝負します。

最初に、K 個の石からなる山を用意します。 二人は次の操作を交互に行います。 先手は太郎君です。

  • A の元 x をひとつ選び、山からちょうど x 個の石を取り去る。

先に操作を行えなくなった人が負けです。 二人が最適に行動すると仮定したとき、どちらが勝つかを判定してください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 100
  • 1 \leq K \leq 10^5
  • 1 \leq a_1 < a_2 < \cdots < a_N \leq K

入力

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

N K
a_1 a_2 \ldots a_N

出力

先手の太郎君が勝つならば First を、後手の次郎君が勝つならば Second を出力せよ。


入力例 1

2 4
2 3

出力例 1

First

先手が 3 個の石を取り去ると、後手は操作を行なえません。 よって、先手が勝ちます。


入力例 2

2 5
2 3

出力例 2

Second

次のように、先手がどのように操作を行っても後手が勝ちます。

  • 先手が 2 個の石を取り去った場合、後手が 3 個の石を取り去ると、先手は操作を行えない。
  • 先手が 3 個の石を取り去った場合、後手が 2 個の石を取り去ると、先手は操作を行えない。

入力例 3

2 7
2 3

出力例 3

First

先手は 2 個の石を取り去ればよいです。 すると、次のように、後手がどのように操作を行っても先手が勝ちます。

  • 後手が 2 個の石を取り去った場合、先手が 3 個の石を取り去ると、後手は操作を行えない。
  • 後手が 3 個の石を取り去った場合、先手が 2 個の石を取り去ると、後手は操作を行えない。

入力例 4

3 20
1 2 3

出力例 4

Second

入力例 5

3 21
1 2 3

出力例 5

First

入力例 6

1 100000
1

出力例 6

Second

Score : 100 points

Problem Statement

There is a set A = \{ a_1, a_2, \ldots, a_N \} consisting of N positive integers. Taro and Jiro will play the following game against each other.

Initially, we have a pile consisting of K stones. The two players perform the following operation alternately, starting from Taro:

  • Choose an element x in A, and remove exactly x stones from the pile.

A player loses when he becomes unable to play. Assuming that both players play optimally, determine the winner.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 100
  • 1 \leq K \leq 10^5
  • 1 \leq a_1 < a_2 < \cdots < a_N \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

If Taro will win, print First; if Jiro will win, print Second.


Sample Input 1

2 4
2 3

Sample Output 1

First

If Taro removes three stones, Jiro cannot make a move. Thus, Taro wins.


Sample Input 2

2 5
2 3

Sample Output 2

Second

Whatever Taro does in his operation, Jiro wins, as follows:

  • If Taro removes two stones, Jiro can remove three stones to make Taro unable to make a move.
  • If Taro removes three stones, Jiro can remove two stones to make Taro unable to make a move.

Sample Input 3

2 7
2 3

Sample Output 3

First

Taro should remove two stones. Then, whatever Jiro does in his operation, Taro wins, as follows:

  • If Jiro removes two stones, Taro can remove three stones to make Jiro unable to make a move.
  • If Jiro removes three stones, Taro can remove two stones to make Jiro unable to make a move.

Sample Input 4

3 20
1 2 3

Sample Output 4

Second

Sample Input 5

3 21
1 2 3

Sample Output 5

First

Sample Input 6

1 100000
1

Sample Output 6

Second
L - Deque

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

太郎君と次郎君が次のゲームで勝負します。

最初に、数列 a = (a_1, a_2, \ldots, a_N) が与えられます。 a が空になるまで、二人は次の操作を交互に行います。 先手は太郎君です。

  • a の先頭要素または末尾要素を取り除く。 取り除いた要素を x とすると、操作を行った人は x 点を得る。

ゲーム終了時の太郎君の総得点を X、次郎君の総得点を Y とします。 太郎君は X - Y を最大化しようとし、次郎君は X - Y を最小化しようとします。

二人が最適に行動すると仮定したとき、X - Y を求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 3000
  • 1 \leq a_i \leq 10^9

入力

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

N
a_1 a_2 \ldots a_N

出力

二人が最適に行動すると仮定したとき、X - Y を出力せよ。


入力例 1

4
10 80 90 30

出力例 1

10

二人が最適に行動すると、次のように操作が行われます。 操作対象の要素を太字で表しています。

  • 先手: (10, 80, 90, 30) → (10, 80, 90)
  • 後手: (10, 80, 90) → (10, 80)
  • 先手: (10, 80) → (10)
  • 後手: (10) → ()

このとき、X = 30 + 80 = 110, Y = 90 + 10 = 100 となります。


入力例 2

3
10 100 10

出力例 2

-80

二人が最適に行動すると、例えば次のように操作が行われます。

  • 先手: (10, 100, 10) → (100, 10)
  • 後手: (100, 10) → (10)
  • 先手: (10) → ()

このとき、X = 10 + 10 = 20, Y = 100 となります。


入力例 3

1
10

出力例 3

10

入力例 4

10
1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1

出力例 4

4999999995

答えは 32-bit 整数型に収まらない場合があります。


入力例 5

6
4 2 9 7 1 5

出力例 5

2

二人が最適に行動すると、例えば次のように操作が行われます。

  • 先手: (4, 2, 9, 7, 1, 5) → (4, 2, 9, 7, 1)
  • 後手: (4, 2, 9, 7, 1) → (2, 9, 7, 1)
  • 先手: (2, 9, 7, 1) → (2, 9, 7)
  • 後手: (2, 9, 7) → (2, 9)
  • 先手: (2, 9) → (2)
  • 後手: (2) → ()

このとき、X = 5 + 1 + 9 = 15, Y = 4 + 7 + 2 = 13 となります。

Score : 100 points

Problem Statement

Taro and Jiro will play the following game against each other.

Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N). Until a becomes empty, the two players perform the following operation alternately, starting from Taro:

  • Remove the element at the beginning or the end of a. The player earns x points, where x is the removed element.

Let X and Y be Taro's and Jiro's total score at the end of the game, respectively. Taro tries to maximize X - Y, while Jiro tries to minimize X - Y.

Assuming that the two players play optimally, find the resulting value of X - Y.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 3000
  • 1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the resulting value of X - Y, assuming that the two players play optimally.


Sample Input 1

4
10 80 90 30

Sample Output 1

10

The game proceeds as follows when the two players play optimally (the element being removed is written bold):

  • Taro: (10, 80, 90, 30) → (10, 80, 90)
  • Jiro: (10, 80, 90) → (10, 80)
  • Taro: (10, 80) → (10)
  • Jiro: (10) → ()

Here, X = 30 + 80 = 110 and Y = 90 + 10 = 100.


Sample Input 2

3
10 100 10

Sample Output 2

-80

The game proceeds, for example, as follows when the two players play optimally:

  • Taro: (10, 100, 10) → (100, 10)
  • Jiro: (100, 10) → (10)
  • Taro: (10) → ()

Here, X = 10 + 10 = 20 and Y = 100.


Sample Input 3

1
10

Sample Output 3

10

Sample Input 4

10
1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1

Sample Output 4

4999999995

The answer may not fit into a 32-bit integer type.


Sample Input 5

6
4 2 9 7 1 5

Sample Output 5

2

The game proceeds, for example, as follows when the two players play optimally:

  • Taro: (4, 2, 9, 7, 1, 5) → (4, 2, 9, 7, 1)
  • Jiro: (4, 2, 9, 7, 1) → (2, 9, 7, 1)
  • Taro: (2, 9, 7, 1) → (2, 9, 7)
  • Jiro: (2, 9, 7) → (2, 9)
  • Taro: (2, 9) → (2)
  • Jiro: (2) → ()

Here, X = 5 + 1 + 9 = 15 and Y = 4 + 7 + 2 = 13.

M - Candies

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 人の子供たちがいます。 子供たちには 1, 2, \ldots, N と番号が振られています。

子供たちは K 個の飴を分け合うことにしました。 このとき、各 i (1 \leq i \leq N) について、子供 i が受け取る飴の個数は 0 以上 a_i 以下でなければなりません。 また、飴が余ってはいけません。

子供たちが飴を分け合う方法は何通りでしょうか? 10^9 + 7 で割った余りを求めてください。 ただし、2 通りの方法が異なるとは、ある子供が存在し、その子供が受け取る飴の個数が異なることを言います。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 100
  • 0 \leq K \leq 10^5
  • 0 \leq a_i \leq K

入力

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

N K
a_1 a_2 \ldots a_N

出力

子供たちが飴を分け合う方法は何通りか? 10^9 + 7 で割った余りを出力せよ。


入力例 1

3 4
1 2 3

出力例 1

5

子供たちが飴を分け合う方法は、次の 5 通りです。 各数列において、i 番目の要素は子供 i が受け取る飴の個数を表します。

  • (0, 1, 3)
  • (0, 2, 2)
  • (1, 0, 3)
  • (1, 1, 2)
  • (1, 2, 1)

入力例 2

1 10
9

出力例 2

0

子供たちが飴を分け合う方法が存在しない場合もあります。


入力例 3

2 0
0 0

出力例 3

1

子供たちが飴を分け合う方法は、次の 1 通りです。

  • (0, 0)

入力例 4

4 100000
100000 100000 100000 100000

出力例 4

665683269

答えを 10^9 + 7 で割った余りを出力することを忘れずに。

Score : 100 points

Problem Statement

There are N children, numbered 1, 2, \ldots, N.

They have decided to share K candies among themselves. Here, for each i (1 \leq i \leq N), Child i must receive between 0 and a_i candies (inclusive). Also, no candies should be left over.

Find the number of ways for them to share candies, modulo 10^9 + 7. Here, two ways are said to be different when there exists a child who receives a different number of candies.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 100
  • 0 \leq K \leq 10^5
  • 0 \leq a_i \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

Print the number of ways for the children to share candies, modulo 10^9 + 7.


Sample Input 1

3 4
1 2 3

Sample Output 1

5

There are five ways for the children to share candies, as follows:

  • (0, 1, 3)
  • (0, 2, 2)
  • (1, 0, 3)
  • (1, 1, 2)
  • (1, 2, 1)

Here, in each sequence, the i-th element represents the number of candies that Child i receives.


Sample Input 2

1 10
9

Sample Output 2

0

There may be no ways for the children to share candies.


Sample Input 3

2 0
0 0

Sample Output 3

1

There is one way for the children to share candies, as follows:

  • (0, 0)

Sample Input 4

4 100000
100000 100000 100000 100000

Sample Output 4

665683269

Be sure to print the answer modulo 10^9 + 7.

N - Slimes

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 匹のスライムが横一列に並んでいます。 最初、左から i 番目のスライムの大きさは a_i です。

太郎君は、すべてのスライムを合体させて 1 匹のスライムにしようとしています。 スライムが 1 匹になるまで、太郎君は次の操作を繰り返し行います。

  • 左右に隣り合う 2 匹のスライムを選び、それらを合体させて新しい 1 匹のスライムにする。 合体前の 2 匹のスライムの大きさを x および y とすると、合体後のスライムの大きさは x + y となる。 このとき、太郎君は x + y のコストを支払う。 なお、合体の前後でスライムたちの位置関係は変わらない。

太郎君が支払うコストの総和の最小値を求めてください。

制約

  • 入力はすべて整数である。
  • 2 \leq N \leq 400
  • 1 \leq a_i \leq 10^9

入力

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

N
a_1 a_2 \ldots a_N

出力

太郎君が支払うコストの総和の最小値を出力せよ。


入力例 1

4
10 20 30 40

出力例 1

190

次のように操作を行えばよいです。 操作対象のスライムを太字で表しています。

  • (10, 20, 30, 40) → (30, 30, 40)
  • (30, 30, 40) → (60, 40)
  • (60, 40) → (100)

入力例 2

5
10 10 10 10 10

出力例 2

120

例えば、次のように操作を行えばよいです。

  • (10, 10, 10, 10, 10) → (20, 10, 10, 10)
  • (20, 10, 10, 10) → (20, 20, 10)
  • (20, 20, 10) → (20, 30)
  • (20, 30) → (50)

入力例 3

3
1000000000 1000000000 1000000000

出力例 3

5000000000

答えは 32-bit 整数型に収まらない場合があります。


入力例 4

6
7 6 8 6 1 1

出力例 4

68

例えば、次のように操作を行えばよいです。

  • (7, 6, 8, 6, 1, 1) → (7, 6, 8, 6, 2)
  • (7, 6, 8, 6, 2) → (7, 6, 8, 8)
  • (7, 6, 8, 8) → (13, 8, 8)
  • (13, 8, 8) → (13, 16)
  • (13, 16) → (29)

Score : 100 points

Problem Statement

There are N slimes lining up in a row. Initially, the i-th slime from the left has a size of a_i.

Taro is trying to combine all the slimes into a larger slime. He will perform the following operation repeatedly until there is only one slime:

  • Choose two adjacent slimes, and combine them into a new slime. The new slime has a size of x + y, where x and y are the sizes of the slimes before combining them. Here, a cost of x + y is incurred. The positional relationship of the slimes does not change while combining slimes.

Find the minimum possible total cost incurred.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 400
  • 1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the minimum possible total cost incurred.


Sample Input 1

4
10 20 30 40

Sample Output 1

190

Taro should do as follows (slimes being combined are shown in bold):

  • (10, 20, 30, 40) → (30, 30, 40)
  • (30, 30, 40) → (60, 40)
  • (60, 40) → (100)

Sample Input 2

5
10 10 10 10 10

Sample Output 2

120

Taro should do, for example, as follows:

  • (10, 10, 10, 10, 10) → (20, 10, 10, 10)
  • (20, 10, 10, 10) → (20, 20, 10)
  • (20, 20, 10) → (20, 30)
  • (20, 30) → (50)

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.


Sample Input 4

6
7 6 8 6 1 1

Sample Output 4

68

Taro should do, for example, as follows:

  • (7, 6, 8, 6, 1, 1) → (7, 6, 8, 6, 2)
  • (7, 6, 8, 6, 2) → (7, 6, 8, 8)
  • (7, 6, 8, 8) → (13, 8, 8)
  • (13, 8, 8) → (13, 16)
  • (13, 16) → (29)
O - Matching

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 人の男性たちと N 人の女性たちがいます。 男性たちには 1, 2, \ldots, N と番号が振られています。 同様に、女性たちには 1, 2, \ldots, N と番号が振られています。

i, j (1 \leq i, j \leq N) について、男性 i と女性 j の相性の良し悪しが整数 a_{i, j} によって与えられます。 a_{i, j} = 1 ならば男性 i と女性 j は相性が良く、a_{i, j} = 0 ならば相性が悪いです。

太郎君は、相性が良い男女どうしのペアを N 組作ろうとしています。 このとき、各男性および各女性はちょうど 1 つのペアに属さなければなりません。

N 組のペアを作る方法は何通りでしょうか? 10^9 + 7 で割った余りを求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 21
  • a_{i, j}0 または 1 である。

入力

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

N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

出力

N 組のペアを作る方法は何通りか? 10^9 + 7 で割った余りを出力せよ。


入力例 1

3
0 1 1
1 0 1
1 1 1

出力例 1

3

ペアを作る方法は次の 3 通りです。 男性 i と女性 j のペアを (i, j) で表します。

  • (1, 2), (2, 1), (3, 3)
  • (1, 2), (2, 3), (3, 1)
  • (1, 3), (2, 1), (3, 2)

入力例 2

4
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0

出力例 2

1

ペアを作る方法は次の 1 通りです。

  • (1, 2), (2, 4), (3, 1), (4, 3)

入力例 3

1
0

出力例 3

0

入力例 4

21
0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1
1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0
0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1
0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0
1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0
0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1
0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0
0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1
0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1
0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1
0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0
0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1
1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1
0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1
1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1
0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1
0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0
1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0
1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0

出力例 4

102515160

答えを 10^9 + 7 で割った余りを出力することを忘れずに。

Score : 100 points

Problem Statement

There are N men and N women, both numbered 1, 2, \ldots, N.

For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}. If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not.

Taro is trying to make N pairs, each consisting of a man and a woman who are compatible. Here, each man and each woman must belong to exactly one pair.

Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 21
  • a_{i, j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

Output

Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7.


Sample Input 1

3
0 1 1
1 0 1
1 1 1

Sample Output 1

3

There are three ways to make pairs, as follows ((i, j) denotes a pair of Man i and Woman j):

  • (1, 2), (2, 1), (3, 3)
  • (1, 2), (2, 3), (3, 1)
  • (1, 3), (2, 1), (3, 2)

Sample Input 2

4
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0

Sample Output 2

1

There is one way to make pairs, as follows:

  • (1, 2), (2, 4), (3, 1), (4, 3)

Sample Input 3

1
0

Sample Output 3

0

Sample Input 4

21
0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1
1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0
0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1
0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0
1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0
0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1
0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0
0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1
0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1
0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1
0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0
0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1
1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1
0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1
1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1
0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1
0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0
1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0
1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0

Sample Output 4

102515160

Be sure to print the number modulo 10^9 + 7.

P - Independent Set

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 頂点の木があります。 頂点には 1, 2, \ldots, N と番号が振られています。 各 i (1 \leq i \leq N - 1) について、i 番目の辺は頂点 x_iy_i を結んでいます。

太郎君は、各頂点を白または黒で塗ることにしました。 ただし、隣り合う頂点どうしをともに黒で塗ってはいけません。

頂点の色の組合せは何通りでしょうか? 10^9 + 7 で割った余りを求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 10^5
  • 1 \leq x_i, y_i \leq N
  • 与えられるグラフは木である。

入力

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

N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

出力

頂点の色の組合せは何通りか? 10^9 + 7 で割った余りを出力せよ。


入力例 1

3
1 2
2 3

出力例 1

5

頂点の色の組合せは次図の 5 通りです。


入力例 2

4
1 2
1 3
1 4

出力例 2

9

頂点の色の組合せは次図の 9 通りです。


入力例 3

1

出力例 3

2

入力例 4

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

出力例 4

157

Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent vertices both in black.

Find the number of ways in which the vertices can be painted, modulo 10^9 + 7.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 10^5
  • 1 \leq x_i, y_i \leq N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print the number of ways in which the vertices can be painted, modulo 10^9 + 7.


Sample Input 1

3
1 2
2 3

Sample Output 1

5

There are five ways to paint the vertices, as follows:


Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

9

There are nine ways to paint the vertices, as follows:


Sample Input 3

1

Sample Output 3

2

Sample Input 4

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

Sample Output 4

157
Q - Flowers

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 本の花が横一列に並んでいます。 各 i (1 \leq i \leq N) について、左から i 番目の花の高さは h_i で、美しさは a_i です。 ただし、h_1, h_2, \ldots, h_N はすべて相異なります。

太郎君は何本かの花を抜き去ることで、次の条件が成り立つようにしようとしています。

  • 残りの花を左から順に見ると、高さが単調増加になっている。

残りの花の美しさの総和の最大値を求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 2 × 10^5
  • 1 \leq h_i \leq N
  • h_1, h_2, \ldots, h_N はすべて相異なる。
  • 1 \leq a_i \leq 10^9

入力

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

N
h_1 h_2 \ldots h_N
a_1 a_2 \ldots a_N

出力

残りの花の美しさの総和の最大値を出力せよ。


入力例 1

4
3 1 4 2
10 20 30 40

出力例 1

60

左から 2, 4 番目の花を残せばよいです。 すると、高さは左から順に 1, 2 となり、単調増加になっています。 また、美しさの総和は 20 + 40 = 60 となります。


入力例 2

1
1
10

出力例 2

10

最初から条件が成り立っています。


入力例 3

5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

出力例 3

5000000000

答えは 32-bit 整数型に収まらない場合があります。


入力例 4

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

出力例 4

31

左から 2, 3, 6, 8, 9 番目の花を残せばよいです。

Score : 100 points

Problem Statement

There are N flowers arranged in a row. For each i (1 \leq i \leq N), the height and the beauty of the i-th flower from the left is h_i and a_i, respectively. Here, h_1, h_2, \ldots, h_N are all distinct.

Taro is pulling out some flowers so that the following condition is met:

  • The heights of the remaining flowers are monotonically increasing from left to right.

Find the maximum possible sum of the beauties of the remaining flowers.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 2 × 10^5
  • 1 \leq h_i \leq N
  • h_1, h_2, \ldots, h_N are all distinct.
  • 1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N
a_1 a_2 \ldots a_N

Output

Print the maximum possible sum of the beauties of the remaining flowers.


Sample Input 1

4
3 1 4 2
10 20 30 40

Sample Output 1

60

We should keep the second and fourth flowers from the left. Then, the heights would be 1, 2 from left to right, which is monotonically increasing, and the sum of the beauties would be 20 + 40 = 60.


Sample Input 2

1
1
10

Sample Output 2

10

The condition is met already at the beginning.


Sample Input 3

5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.


Sample Input 4

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

Sample Output 4

31

We should keep the second, third, sixth, eighth and ninth flowers from the left.

R - Walk

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 頂点の単純有向グラフ G があります。 頂点には 1, 2, \ldots, N と番号が振られています。

i, j (1 \leq i, j \leq N) について、頂点 i から j への有向辺の有無が整数 a_{i, j} によって与えられます。 a_{i, j} = 1 ならば頂点 i から j への有向辺が存在し、a_{i, j} = 0 ならば存在しません。

G の長さ K の有向パスは何通りでしょうか? 10^9 + 7 で割った余りを求めてください。 ただし、同じ辺を複数回通るような有向パスも数えるものとします。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 50
  • 1 \leq K \leq 10^{18}
  • a_{i, j}0 または 1 である。
  • a_{i, i} = 0

入力

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

N K
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

出力

G の長さ K の有向パスは何通りか? 10^9 + 7 で割った余りを出力せよ。


入力例 1

4 2
0 1 0 0
0 0 1 1
0 0 0 1
1 0 0 0

出力例 1

6

G は次図です。

長さ 2 の有向パスは、次の 6 通りです。

  • 123
  • 124
  • 234
  • 241
  • 341
  • 412

入力例 2

3 3
0 1 0
1 0 1
0 0 0

出力例 2

3

G は次図です。

長さ 3 の有向パスは、次の 3 通りです。

  • 1212
  • 2121
  • 2123

入力例 3

6 2
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0

出力例 3

1

G は次図です。

長さ 2 の有向パスは、次の 1 通りです。

  • 456

入力例 4

1 1
0

出力例 4

0

入力例 5

10 1000000000000000000
0 0 1 1 0 0 0 1 1 0
0 0 0 0 0 1 1 1 0 0
0 1 0 0 0 1 0 1 0 1
1 1 1 0 1 1 0 1 1 0
0 1 1 1 0 1 0 1 1 1
0 0 0 1 0 0 1 0 1 0
0 0 0 1 1 0 0 1 0 1
1 0 0 0 1 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
1 0 1 1 1 0 1 1 1 0

出力例 5

957538352

答えを 10^9 + 7 で割った余りを出力することを忘れずに。

Score : 100 points

Problem Statement

There is a simple directed graph G with N vertices, numbered 1, 2, \ldots, N.

For each i and j (1 \leq i, j \leq N), you are given an integer a_{i, j} that represents whether there is a directed edge from Vertex i to j. If a_{i, j} = 1, there is a directed edge from Vertex i to j; if a_{i, j} = 0, there is not.

Find the number of different directed paths of length K in G, modulo 10^9 + 7. We will also count a path that traverses the same edge multiple times.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 50
  • 1 \leq K \leq 10^{18}
  • a_{i, j} is 0 or 1.
  • a_{i, i} = 0

Input

Input is given from Standard Input in the following format:

N K
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

Output

Print the number of different directed paths of length K in G, modulo 10^9 + 7.


Sample Input 1

4 2
0 1 0 0
0 0 1 1
0 0 0 1
1 0 0 0

Sample Output 1

6

G is drawn in the figure below:

There are six directed paths of length 2:

  • 123
  • 124
  • 234
  • 241
  • 341
  • 412

Sample Input 2

3 3
0 1 0
1 0 1
0 0 0

Sample Output 2

3

G is drawn in the figure below:

There are three directed paths of length 3:

  • 1212
  • 2121
  • 2123

Sample Input 3

6 2
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0

Sample Output 3

1

G is drawn in the figure below:

There is one directed path of length 2:

  • 456

Sample Input 4

1 1
0

Sample Output 4

0

Sample Input 5

10 1000000000000000000
0 0 1 1 0 0 0 1 1 0
0 0 0 0 0 1 1 1 0 0
0 1 0 0 0 1 0 1 0 1
1 1 1 0 1 1 0 1 1 0
0 1 1 1 0 1 0 1 1 1
0 0 0 1 0 0 1 0 1 0
0 0 0 1 1 0 0 1 0 1
1 0 0 0 1 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
1 0 1 1 1 0 1 1 1 0

Sample Output 5

957538352

Be sure to print the count modulo 10^9 + 7.

S - Digit Sum

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

1 以上 K 以下の整数のうち、十進表記における各桁の数字の総和が D の倍数であるようなものは何個でしょうか? 10^9 + 7 で割った余りを求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq K < 10^{10000}
  • 1 \leq D \leq 100

入力

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

K
D

出力

条件を満たす整数は何個か? 10^9 + 7 で割った余りを出力せよ。


入力例 1

30
4

出力例 1

6

4, 8, 13, 17, 22, 266 個です。


入力例 2

1000000009
1

出力例 2

2

答えを 10^9 + 7 で割った余りを出力することを忘れずに。


入力例 3

98765432109876543210
58

出力例 3

635270834

Score : 100 points

Problem Statement

Find the number of integers between 1 and K (inclusive) satisfying the following condition, modulo 10^9 + 7:

  • The sum of the digits in base ten is a multiple of D.

Constraints

  • All values in input are integers.
  • 1 \leq K < 10^{10000}
  • 1 \leq D \leq 100

Input

Input is given from Standard Input in the following format:

K
D

Output

Print the number of integers satisfying the condition, modulo 10^9 + 7.


Sample Input 1

30
4

Sample Output 1

6

Those six integers are: 4, 8, 13, 17, 22 and 26.


Sample Input 2

1000000009
1

Sample Output 2

2

Be sure to print the number modulo 10^9 + 7.


Sample Input 3

98765432109876543210
58

Sample Output 3

635270834
T - Permutation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N を正整数とします。 長さ N - 1 の文字列 s が与えられます。 s<> からなります。

(1, 2, \ldots, N) を並べ替えた順列 (p_1, p_2, \ldots, p_N) であって、次の条件を満たすものは何通りでしょうか? 10^9 + 7 で割った余りを求めてください。

  • i (1 \leq i \leq N - 1) について、si 文字目が < の場合は p_i < p_{i + 1} であり、si 文字目が > の場合は p_i > p_{i + 1} である。

制約

  • N は整数である。
  • 2 \leq N \leq 3000
  • s は長さ N - 1 の文字列である。
  • s<> からなる。

入力

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

N
s

出力

条件を満たす順列は何通りか? 10^9 + 7 で割った余りを出力せよ。


入力例 1

4
<><

出力例 1

5

条件を満たす順列は次の 5 通りです。

  • (1, 3, 2, 4)
  • (1, 4, 2, 3)
  • (2, 3, 1, 4)
  • (2, 4, 1, 3)
  • (3, 4, 1, 2)

入力例 2

5
<<<<

出力例 2

1

条件を満たす順列は次の 1 通りです。

  • (1, 2, 3, 4, 5)

入力例 3

20
>>>><>>><>><>>><<>>

出力例 3

217136290

答えを 10^9 + 7 で割った余りを出力することを忘れずに。

Score : 100 points

Problem Statement

Let N be a positive integer. You are given a string s of length N - 1, consisting of < and >.

Find the number of permutations (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N) that satisfy the following condition, modulo 10^9 + 7:

  • For each i (1 \leq i \leq N - 1), p_i < p_{i + 1} if the i-th character in s is <, and p_i > p_{i + 1} if the i-th character in s is >.

Constraints

  • N is an integer.
  • 2 \leq N \leq 3000
  • s is a string of length N - 1.
  • s consists of < and >.

Input

Input is given from Standard Input in the following format:

N
s

Output

Print the number of permutations that satisfy the condition, modulo 10^9 + 7.


Sample Input 1

4
<><

Sample Output 1

5

There are five permutations that satisfy the condition, as follows:

  • (1, 3, 2, 4)
  • (1, 4, 2, 3)
  • (2, 3, 1, 4)
  • (2, 4, 1, 3)
  • (3, 4, 1, 2)

Sample Input 2

5
<<<<

Sample Output 2

1

There is one permutation that satisfies the condition, as follows:

  • (1, 2, 3, 4, 5)

Sample Input 3

20
>>>><>>><>><>>><<>>

Sample Output 3

217136290

Be sure to print the number modulo 10^9 + 7.

U - Grouping

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 羽のうさぎたちがいます。 うさぎたちには 1, 2, \ldots, N と番号が振られています。

i, j (1 \leq i, j \leq N) について、うさぎ ij の相性が整数 a_{i, j} によって与えられます。 ただし、各 i (1 \leq i \leq N) について a_{i, i} = 0 であり、各 i, j (1 \leq i, j \leq N) について a_{i, j} = a_{j, i} です。

太郎君は、N 羽のうさぎたちをいくつかのグループへ分けようとしています。 このとき、各うさぎはちょうど 1 つのグループに属さなければなりません。 グループ分けの結果、各 i, j (1 \leq i < j \leq N) について、うさぎ ij が同じグループに属するならば、太郎君は a_{i, j} 点を得ます。

太郎君の総得点の最大値を求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 16
  • |a_{i, j}| \leq 10^9
  • a_{i, i} = 0
  • a_{i, j} = a_{j, i}

入力

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

N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

出力

太郎君の総得点の最大値を出力せよ。


入力例 1

3
0 10 20
10 0 -100
20 -100 0

出力例 1

20

\{1, 3\}, \{2\} とグループ分けすればよいです。


入力例 2

2
0 -10
-10 0

出力例 2

0

\{1\}, \{2\} とグループ分けすればよいです。


入力例 3

4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0

出力例 3

4999999999

\{1, 2, 3, 4\} とグループ分けすればよいです。 答えは 32-bit 整数型に収まらない場合があります。


入力例 4

16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0

出力例 4

132

Score : 100 points

Problem Statement

There are N rabbits, numbered 1, 2, \ldots, N.

For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).

Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.

Find Taro's maximum possible total score.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 16
  • |a_{i, j}| \leq 10^9
  • a_{i, i} = 0
  • a_{i, j} = a_{j, i}

Input

Input is given from Standard Input in the following format:

N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

Output

Print Taro's maximum possible total score.


Sample Input 1

3
0 10 20
10 0 -100
20 -100 0

Sample Output 1

20

The rabbits should be divided as \{1, 3\}, \{2\}.


Sample Input 2

2
0 -10
-10 0

Sample Output 2

0

The rabbits should be divided as \{1\}, \{2\}.


Sample Input 3

4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0

Sample Output 3

4999999999

The rabbits should be divided as \{1, 2, 3, 4\}. Note that the answer may not fit into a 32-bit integer type.


Sample Input 4

16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0

Sample Output 4

132
V - Subtree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 頂点の木があります。 頂点には 1, 2, \ldots, N と番号が振られています。 各 i (1 \leq i \leq N - 1) について、i 番目の辺は頂点 x_iy_i を結んでいます。

太郎君は、各頂点を白または黒で塗ることにしました。 このとき、どの黒い頂点からどの黒い頂点へも、黒い頂点のみを辿って到達できるようにします。

正整数 M が与えられます。 各 v (1 \leq v \leq N) について、次の質問に答えてください。

  • 頂点 v が黒であるような頂点の色の組合せは何通りか? M で割った余りを求めよ。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 10^5
  • 2 \leq M \leq 10^9
  • 1 \leq x_i, y_i \leq N
  • 与えられるグラフは木である。

入力

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

N M
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

出力

N 行出力せよ。 v (1 \leq v \leq N) 行目には、次の質問に対する答えを出力せよ。

  • 頂点 v が黒であるような頂点の色の組合せは何通りか? M で割った余りを求めよ。

入力例 1

3 100
1 2
2 3

出力例 1

3
4
3

頂点の色の組合せは次図の 7 通りです。 このうち、頂点 1 が黒であるようなものは 3 通り、頂点 2 が黒であるようなものは 4 通り、頂点 3 が黒であるようなものは 3 通りです。


入力例 2

4 100
1 2
1 3
1 4

出力例 2

8
5
5
5

入力例 3

1 100

出力例 3

1

入力例 4

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

出力例 4

0
0
1
1
1
0
1
0
1
1

答えを M で割った余りを出力することを忘れずに。

Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices.

You are given a positive integer M. For each v (1 \leq v \leq N), answer the following question:

  • Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 10^5
  • 2 \leq M \leq 10^9
  • 1 \leq x_i, y_i \leq N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print N lines. The v-th (1 \leq v \leq N) line should contain the answer to the following question:

  • Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Sample Input 1

3 100
1 2
2 3

Sample Output 1

3
4
3

There are seven ways to paint the vertices, as shown in the figure below. Among them, there are three ways such that Vertex 1 is black, four ways such that Vertex 2 is black and three ways such that Vertex 3 is black.


Sample Input 2

4 100
1 2
1 3
1 4

Sample Output 2

8
5
5
5

Sample Input 3

1 100

Sample Output 3

1

Sample Input 4

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

Sample Output 4

0
0
1
1
1
0
1
0
1
1

Be sure to print the answers modulo M.

W - Intervals

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

長さ N01 からなる文字列を考えます。 この文字列のスコアを次のように計算します。

  • i (1 \leq i \leq M) について、l_i 文字目から r_i 文字目までに 1 がひとつ以上含まれるならば、スコアに a_i を加算する。

文字列のスコアの最大値を求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 2 × 10^5
  • 1 \leq M \leq 2 × 10^5
  • 1 \leq l_i \leq r_i \leq N
  • |a_i| \leq 10^9

入力

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

N M
l_1 r_1 a_1
l_2 r_2 a_2
:
l_M r_M a_M

出力

文字列のスコアの最大値を出力せよ。


入力例 1

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

出力例 1

20

10001 のスコアは a_1 + a_3 = 10 + 10 = 20 となります。


入力例 2

3 4
1 3 100
1 1 -10
2 2 -20
3 3 -30

出力例 2

90

100 のスコアは a_1 + a_2 = 100 + (-10) = 90 となります。


入力例 3

1 1
1 1 -10

出力例 3

0

0 のスコアは 0 となります。


入力例 4

1 5
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000

出力例 4

5000000000

答えは 32-bit 整数型に収まらない場合があります。


入力例 5

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

出力例 5

10

例えば、101000 のスコアは a_2 + a_3 + a_4 + a_5 + a_7 = 10 + (-8) + 5 + 9 + (-6) = 10 となります。

Score : 100 points

Problem Statement

Consider a string of length N consisting of 0 and 1. The score for the string is calculated as follows:

  • For each i (1 \leq i \leq M), a_i is added to the score if the string contains 1 at least once between the l_i-th and r_i-th characters (inclusive).

Find the maximum possible score of a string.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 2 × 10^5
  • 1 \leq M \leq 2 × 10^5
  • 1 \leq l_i \leq r_i \leq N
  • |a_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
l_1 r_1 a_1
l_2 r_2 a_2
:
l_M r_M a_M

Output

Print the maximum possible score of a string.


Sample Input 1

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

Sample Output 1

20

The score for 10001 is a_1 + a_3 = 10 + 10 = 20.


Sample Input 2

3 4
1 3 100
1 1 -10
2 2 -20
3 3 -30

Sample Output 2

90

The score for 100 is a_1 + a_2 = 100 + (-10) = 90.


Sample Input 3

1 1
1 1 -10

Sample Output 3

0

The score for 0 is 0.


Sample Input 4

1 5
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000

Sample Output 4

5000000000

The answer may not fit into a 32-bit integer type.


Sample Input 5

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

Sample Output 5

10

For example, the score for 101000 is a_2 + a_3 + a_4 + a_5 + a_7 = 10 + (-8) + 5 + 9 + (-6) = 10.

X - Tower

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 個のブロックがあります。 ブロックには 1, 2, \ldots, N と番号が振られています。 各 i (1 \leq i \leq N) について、ブロック i の重さは w_i で、丈夫さは s_i で、価値は v_i です。

太郎君は、N 個のブロックのうち何個かを選び、それらを任意の順序で一列に積み重ね、塔を作ることにしました。 このとき、塔は次の条件を満たさなければなりません。

  • 塔に含まれる各ブロック i について、ブロック i より上に積まれたブロックの重さの総和は s_i 以下である。

塔に含まれるブロックの価値の総和の最大値を求めてください。

制約

  • 入力はすべて整数である。
  • 1 \leq N \leq 10^3
  • 1 \leq w_i, s_i \leq 10^4
  • 1 \leq v_i \leq 10^9

入力

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

N
w_1 s_1 v_1
w_2 s_2 v_2
:
w_N s_N v_N

出力

塔に含まれるブロックの価値の総和の最大値を出力せよ。


入力例 1

3
2 2 20
2 1 30
3 1 40

出力例 1

50

上から順にブロック 2, 1 と積み重ねると、この塔は条件を満たします。 塔に含まれるブロックの価値の総和は 30 + 20 = 50 となります。


入力例 2

4
1 2 10
3 1 10
2 4 10
1 6 10

出力例 2

40

上から順にブロック 1, 2, 3, 4 と積み重ねればよいです。


入力例 3

5
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000

出力例 3

5000000000

答えは 32-bit 整数型に収まらない場合があります。


入力例 4

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

出力例 4

22

例えば、上から順にブロック 5, 6, 8, 4 と積み重ねればよいです。

Score : 100 points

Problem Statement

There are N blocks, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), Block i has a weight of w_i, a solidness of s_i and a value of v_i.

Taro has decided to build a tower by choosing some of the N blocks and stacking them vertically in some order. Here, the tower must satisfy the following condition:

  • For each Block i contained in the tower, the sum of the weights of the blocks stacked above it is not greater than s_i.

Find the maximum possible sum of the values of the blocks contained in the tower.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 10^3
  • 1 \leq w_i, s_i \leq 10^4
  • 1 \leq v_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
w_1 s_1 v_1
w_2 s_2 v_2
:
w_N s_N v_N

Output

Print the maximum possible sum of the values of the blocks contained in the tower.


Sample Input 1

3
2 2 20
2 1 30
3 1 40

Sample Output 1

50

If Blocks 2, 1 are stacked in this order from top to bottom, this tower will satisfy the condition, with the total value of 30 + 20 = 50.


Sample Input 2

4
1 2 10
3 1 10
2 4 10
1 6 10

Sample Output 2

40

Blocks 1, 2, 3, 4 should be stacked in this order from top to bottom.


Sample Input 3

5
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.


Sample Input 4

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

Sample Output 4

22

We should, for example, stack Blocks 5, 6, 8, 4 in this order from top to bottom.

Y - Grid 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

H 行、横 W 列のグリッドがあります。 上から i 行目、左から j 列目のマスを (i, j) で表します。

グリッドのうち、N 個のマス (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) は壁のマスであり、それら以外のマスはすべて空マスです。 マス (1, 1) および (H, W) は空マスであることが保証されています。

太郎君は、マス (1, 1) から出発し、右または下に隣り合う空マスへの移動を繰り返すことで、マス (H, W) まで辿り着こうとしています。

マス (1, 1) から (H, W) までの太郎君の経路は何通りでしょうか? 10^9 + 7 で割った余りを求めてください。

制約

  • 入力はすべて整数である。
  • 2 \leq H, W \leq 10^5
  • 1 \leq N \leq 3000
  • 1 \leq r_i \leq H
  • 1 \leq c_i \leq W
  • マス (r_i, c_i) はすべて相異なる。
  • マス (1, 1) および (H, W) は空マスである。

入力

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

H W N
r_1 c_1
r_2 c_2
:
r_N c_N

出力

マス (1, 1) から (H, W) までの太郎君の経路は何通りか? 10^9 + 7 で割った余りを出力せよ。


入力例 1

3 4 2
2 2
1 4

出力例 1

3

経路は次図の 3 通りです。


入力例 2

5 2 2
2 1
4 2

出力例 2

0

経路が存在しない場合もあります。


入力例 3

5 5 4
3 1
3 5
1 3
5 3

出力例 3

24

入力例 4

100000 100000 1
50000 50000

出力例 4

123445622

答えを 10^9 + 7 で割った余りを出力することを忘れずに。

Score : 100 points

Problem Statement

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

In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares.

Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square.

Find the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.

Constraints

  • All values in input are integers.
  • 2 \leq H, W \leq 10^5
  • 1 \leq N \leq 3000
  • 1 \leq r_i \leq H
  • 1 \leq c_i \leq W
  • Squares (r_i, c_i) are all distinct.
  • Squares (1, 1) and (H, W) are empty squares.

Input

Input is given from Standard Input in the following format:

H W N
r_1 c_1
r_2 c_2
:
r_N c_N

Output

Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7.


Sample Input 1

3 4 2
2 2
1 4

Sample Output 1

3

There are three paths as follows:


Sample Input 2

5 2 2
2 1
4 2

Sample Output 2

0

There may be no paths.


Sample Input 3

5 5 4
3 1
3 5
1 3
5 3

Sample Output 3

24

Sample Input 4

100000 100000 1
50000 50000

Sample Output 4

123445622

Be sure to print the count modulo 10^9 + 7.

Z - Frog 3

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 個の足場があります。 足場には 1, 2, \ldots, N と番号が振られています。 各 i (1 \leq i \leq N) について、足場 i の高さは h_i です。 ここで、h_1 < h_2 < \cdots < h_N です。

最初、足場 1 にカエルがいます。 カエルは次の行動を何回か繰り返し、足場 N まで辿り着こうとしています。

  • 足場 i にいるとき、足場 i + 1, i + 2, \ldots, N のどれかへジャンプする。 このとき、ジャンプ先の足場を j とすると、コスト (h_j - h_i)^2 + C を支払う。

カエルが足場 N に辿り着くまでに支払うコストの総和の最小値を求めてください。

制約

  • 入力はすべて整数である。
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq C \leq 10^{12}
  • 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6

入力

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

N C
h_1 h_2 \ldots h_N

出力

カエルが支払うコストの総和の最小値を出力せよ。


入力例 1

5 6
1 2 3 4 5

出力例 1

20

足場 135 と移動すると、コストの総和は ((3 - 1)^2 + 6) + ((5 - 3)^2 + 6) = 20 となります。


入力例 2

2 1000000000000
500000 1000000

出力例 2

1250000000000

答えは 32-bit 整数型に収まらない場合があります。


入力例 3

8 5
1 3 4 5 10 11 12 13

出力例 3

62

足場 12458 と移動すると、コストの総和は ((3 - 1)^2 + 5) + ((5 - 3)^2 + 5) + ((10 - 5)^2 + 5) + ((13 - 10)^2 + 5) = 62 となります。

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.

There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:

  • If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq C \leq 10^{12}
  • 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6

Input

Input is given from Standard Input in the following format:

N C
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.


Sample Input 1

5 6
1 2 3 4 5

Sample Output 1

20

If we follow the path 135, the total cost incurred would be ((3 - 1)^2 + 6) + ((5 - 3)^2 + 6) = 20.


Sample Input 2

2 1000000000000
500000 1000000

Sample Output 2

1250000000000

The answer may not fit into a 32-bit integer type.


Sample Input 3

8 5
1 3 4 5 10 11 12 13

Sample Output 3

62

If we follow the path 12458, the total cost incurred would be ((3 - 1)^2 + 5) + ((5 - 3)^2 + 5) + ((10 - 5)^2 + 5) + ((13 - 10)^2 + 5) = 62.