A - Last Letter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

英小文字からなる長さ N の文字列 S が与えられます。S の末尾の文字を出力してください。

制約

  • N は整数
  • 1 ≤ N ≤ 1000
  • S は英小文字からなる長さ N の文字列

入力

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

N
S

出力

S の末尾の文字を出力せよ。


入力例 1

5
abcde

出力例 1

e

S = {}abcde です。S の末尾の文字は e なので、e を出力します。


入力例 2

1
a

出力例 2

a

Score : 100 points

Problem Statement

Given a string S of length N consisting of lowercase English alphabets, print the last character of S.

Constraints

  • N is an integer.
  • 1 ≤ N ≤ 1000
  • S is a string of length N consisting of lowercase English alphabets.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the last character of S.


Sample Input 1

5
abcde

Sample Output 1

e

The last character of S = {}abcde is e, so e should be printed.


Sample Input 2

1
a

Sample Output 2

a
B - π

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

正整数 D が与えられます。

直径が D の円の面積を求めて下さい。

制約

  • 1\le D\le 100
  • 入力される値は整数

入力

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

D

出力

答えを出力せよ。

真の解との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。


入力例 1

2

出力例 1

3.141592653589793

直径が 2 の円の面積は 3.141592653589793\ldots です。


入力例 2

7

出力例 2

38.48451000647496

入力例 3

98

出力例 3

7542.9639612690935

Score : 100 points

Problem Statement

You are given a positive integer D.

Find the area of a circle with diameter D.

Constraints

  • 1\le D\le 100
  • The input value is an integer.

Input

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

D

Output

Output the answer.

Your answer is considered correct if the absolute or relative error from the true answer is at most 10^{-6}.


Sample Input 1

2

Sample Output 1

3.141592653589793

The area of a circle with diameter 2 is 3.141592653589793\ldots.


Sample Input 2

7

Sample Output 2

38.48451000647496

Sample Input 3

98

Sample Output 3

7542.9639612690935
C - Takahashi's Secret

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君には N 人の友達がいます。N 人の友達はそれぞれ、友達 1 、友達 2 、\ldots 、友達 N というあだ名で呼ばれています。

ある日、高橋君はある恥ずかしい秘密を、友達の一人である友達 X に知られてしまいました。
i = 1, 2, \ldots, N について、友達 i が高橋君の秘密を知ったとき、友達 A_i がまだ高橋君の秘密を知らなければ、友達 i は高橋君の秘密を友達 A_i にも教えてしまいます。

高橋君の秘密は最終的に何人の友達に知られることになるでしょうか?

制約

  • 2 \leq N \leq 10^5
  • 1 \leq X \leq N
  • 1 \leq A_i \leq N
  • A_i \neq i
  • 入力はすべて整数

入力

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

N X
A_1 A_2 \cdots A_N

出力

答えを出力せよ。


入力例 1

4 2
3 1 1 2

出力例 1

3

高橋君の秘密は以下の流れで友達 1 、友達 2 、友達 3 の 3 人に知れ渡ります。

  • ある日、高橋君は秘密を友達 2 に知られてしまいました。
  • 秘密を知った友達 2 は、その秘密を友達 1 に教えます。
  • 秘密を知った友達 1 は、その秘密を友達 3 に教えます。

高橋君の秘密は最終的に 3 人の友達に知られることになるため、3 を出力します。


入力例 2

20 12
7 11 10 1 7 20 14 2 17 3 2 5 19 20 8 14 18 2 10 10

出力例 2

7

Score : 200 points

Problem Statement

Takahashi has N friends. They have nicknames: Friend 1, Friend 2, \ldots, Friend N.

One day, Takahashi accidentally let one of his friends, Friend X, learn his shameful secret.
For each i = 1, 2, \ldots, N, when Friend i learns the secret, he/she will share it with Friend A_i, if Friend A_i has not already learned it.

How many of Takahashi's friends will learn the secret in the end?

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq X \leq N
  • 1 \leq A_i \leq N
  • A_i \neq i
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

4 2
3 1 1 2

Sample Output 1

3

Takahashi's secret will be learned by Friend 1, Friend 2, and Friend 3, as follows.

  • One day, Takahashi let Friend 2 learn the secret.
  • Friend 2 shares it with Friend 1.
  • Friend 1 shares it with Friend 3.

In the end, three of his friends learn the secret, so we print 3.


Sample Input 2

20 12
7 11 10 1 7 20 14 2 17 3 2 5 19 20 8 14 18 2 10 10

Sample Output 2

7
D - Looped Rope

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

縦 H マス、横 W マスのマス目があります。 上から i 行目 (1\le i\le H) 、左から j 列目 (1\le j\le W) のマスをマス (i,j) と呼ぶことにします。

それぞれのマスは白もしくは黒のどちらか 1 色で塗られています。 マスに塗られている色は H 個の文字列 S _ 1,S _ 2,\ldots,S _ H で表され、S _ i\ (1\le i\le H) の j 文字目 (1\le j\le W) が . のとき、マス (i,j) は白で塗られており、S _ i\ (1\le i\le H) の j 文字目 (1\le j\le W) が # のとき、マス (i,j) は黒で塗られています。

マス目が以下の条件を満たすか判定してください。

  • どの黒で塗られたマスについても、上下左右で隣り合うマスのうち黒く塗られているものは 2 つもしくは 4 つである。

ただし、マス (i,j)\ (1\le i\le H,1\le j\le W) とマス (k,l)\ (1\le k\le H,1\le l\le W) は、|i-k|+|j-l|=1 であるとき、かつそのときに限り上下左右で隣り合っているとします。

制約

  • 1\le H\le 20
  • 1\le W\le 20
  • H,W は整数
  • S _ i は . および # からなる長さ W の文字列 (1\le i\le H)

入力

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

H W
S _ 1
S _ 2
\vdots
S _ H

出力

与えられたマス目が条件を満たしているとき Yes を、条件を満たしていないとき No を出力せよ。


入力例 1

8 7
.######
##....#
#.###.#
#.#.#.#
#.#.#.#
#.#####
#...#..
#####..

出力例 1

Yes

例えば、マス (6,3) は黒で塗られており、隣り合っているマス (5,3),(6,2),(6,4),(7,3) のうち、マス (5,3) およびマス (6,4) の 2 マスが黒で塗られているため、条件を満たしています。

他の黒で塗られたどのマスについても条件を満たしているため、Yes を出力してください。


入力例 2

1 2
##

出力例 2

No

マス (1,1) は黒で塗られていますが、隣り合っているマスは 1 つしかないため、条件を満たしていません。

よって、No を出力して下さい。


入力例 3

4 3
...
...
...
...

出力例 3

Yes

黒で塗られているマスがないため、条件を満たしています。

よって、Yes を出力してください。


入力例 4

15 18
##.###..##.##..##.
##.#.##.##.##.####
...##.#.......####
###.###....###.##.
#.##.......#.#....
#..#.##.##.#.#....
#.########.####.##
#.##.##.#....##.##
#......##.........
##.##..#..##..####
.#.#####..#####..#
.#..#...##.#.....#
.#..#.####.#.....#
.##.#.#.#..##..###
..###.###...####..

出力例 4

Yes

Score : 200 points

Problem Statement

There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row (1\le i\le H) from the top and the j-th column (1\le j\le W) from the left.

Each cell is painted with one color, white or black. The color painted on the cells is represented by H strings S _ 1,S _ 2,\ldots,S _ H. When the j-th character (1\le j\le W) of S _ i\ (1\le i\le H) is ., cell (i,j) is painted white, and when the j-th character (1\le j\le W) of S _ i\ (1\le i\le H) is #, cell (i,j) is painted black.

Determine whether the grid satisfies the following condition:

  • For every black cell, the number of horizontally or vertically adjacent cells that are painted black is 2 or 4.

Here, cells (i,j)\ (1\le i\le H,1\le j\le W) and (k,l)\ (1\le k\le H,1\le l\le W) are horizontally or vertically adjacent if and only if |i-k|+|j-l|=1.

Constraints

  • 1\le H\le 20
  • 1\le W\le 20
  • H and W are integers.
  • S _ i is a string of length W consisting of . and # (1\le i\le H).

Input

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

H W
S _ 1
S _ 2
\vdots
S _ H

Output

Output Yes if the given grid satisfies the condition, and No if it does not satisfy the condition.


Sample Input 1

8 7
.######
##....#
#.###.#
#.#.#.#
#.#.#.#
#.#####
#...#..
#####..

Sample Output 1

Yes

For example, cell (6,3) is painted black, and among the adjacent cells (5,3),(6,2),(6,4),(7,3), cells (5,3) and (6,4) are painted black, which is 2 cells, so it satisfies the condition.

Every other black cell also satisfies the condition, so output Yes.


Sample Input 2

1 2
##

Sample Output 2

No

Cell (1,1) is painted black, but it has only one adjacent cell, so it does not satisfy the condition.

Therefore, output No.


Sample Input 3

4 3
...
...
...
...

Sample Output 3

Yes

There are no black cells, so the condition is satisfied.

Therefore, output Yes.


Sample Input 4

15 18
##.###..##.##..##.
##.#.##.##.##.####
...##.#.......####
###.###....###.##.
#.##.......#.#....
#..#.##.##.#.#....
#.########.####.##
#.##.##.#....##.##
#......##.........
##.##..#..##..####
.#.#####..#####..#
.#..#...##.#.....#
.#..#.####.#.....#
.##.#.#.#..##..###
..###.###...####..

Sample Output 4

Yes
E - Sugoroku Destination

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

マス 1, マス 2,\ldots, マス N の N 個のマスが 1 列に並んでいます。 マス i には整数 A _ i (i\le A_ i\le N) が書かれています。

s=1,2,\ldots,N のそれぞれについて、以下の問題を解いてください。

  • はじめ、マス s に駒を置く。「駒が置かれているマスに書かれている整数を x として、駒をマス x に移動させる」という操作を 10 ^ {100} 回行った後、駒が置かれているマスの番号を出力する。

制約

  • 1\le N\le5\times10 ^ 5
  • i\le A _ i\le N\ (1\le i\le N)
  • 入力はすべて整数

入力

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

N
A _ 1 A _ 2 \ldots A _ N

出力

s=1,2,\ldots,N に対する答えを、この順に空白を区切りとして一行に出力せよ。


入力例 1

7
2 4 7 5 5 6 7

出力例 1

5 5 7 5 5 6 7

s=1 のとき、駒は以下の図のように移動します。

駒がマス 5 に置かれているとき、操作が行われても駒は移動しないため、s=1 のときの答えは 5 となります。


入力例 2

5
1 2 3 4 5

出力例 2

1 2 3 4 5

駒が一度も移動しないこともあります。


入力例 3

15
11 3 10 7 15 10 10 11 11 13 11 12 14 14 15

出力例 3

11 14 14 14 15 14 14 11 11 14 11 12 14 14 15

Score : 300 points

Problem Statement

There are N cells, cell 1, cell 2,\ldots, cell N, arranged in a line. Cell i has an integer A_i\ (i \le A_i \le N) written on it.

For each of s=1,2,\ldots,N, solve the following problem.

  • Initially, place a piece on cell s. After performing the operation "let x be the integer written on the cell where the piece is placed, and then move the piece to cell x" 10^{100} times, output the number of the cell where the piece is placed.

Constraints

  • 1 \le N \le 5\times10^5
  • i \le A_i \le N\ (1 \le i \le N)
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Output the answers for s=1,2,\ldots,N in this order on a single line, separated by spaces.


Sample Input 1

7
2 4 7 5 5 6 7

Sample Output 1

5 5 7 5 5 6 7

For s=1, the piece moves as shown in the following figure.

When the piece is placed on cell 5, the operation does not move the piece, so the answer for s=1 is 5.


Sample Input 2

5
1 2 3 4 5

Sample Output 2

1 2 3 4 5

It is possible that the piece never moves.


Sample Input 3

15
11 3 10 7 15 10 10 11 11 13 11 12 14 14 15

Sample Output 3

11 14 14 14 15 14 14 11 11 14 11 12 14 14 15
F - Sneaking Glances

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

数直線上の座標 0.5 に高橋君がいます。

高橋君はこれから N 回の移動を行います。
i 回目の移動では、「正の方向」「負の方向」のいずれかを選び、その方向に L_i 進みます。

高橋君は座標 0 を最大で何回通り過ぎることが出来るでしょうか?
なお、この問題の制約上、座標 0 で完了する移動が生じることはありません。

制約

  • 1 \le N \le 20
  • 1 \le L_i \le 10^9
  • 入力はすべて整数

入力

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

N
L_1 L_2 \dots L_N

出力

答えを出力せよ。


入力例 1

5
2 5 2 2 1

出力例 1

4

例えば以下のように移動の方向を選択することで座標 0 を 4 回通り過ぎることができ、これが最大です。

  • 1 回目の移動で負の方向を選び、 2 進む。高橋君は座標 0.5 から -1.5 へと移動し、座標 0 を通り過ぎる。
  • 2 回目の移動で正の方向を選び、 5 進む。高橋君は座標 -1.5 から 3.5 へと移動し、座標 0 を通り過ぎる。
  • 3 回目の移動で負の方向を選び、 2 進む。高橋君は座標 3.5 から 1.5 へと移動する。
  • 4 回目の移動で負の方向を選び、 2 進む。高橋君は座標 1.5 から -0.5 へと移動し、座標 0 を通り過ぎる。
  • 5 回目の移動で正の方向を選び、 1 進む。高橋君は座標 -0.5 から 0.5 へと移動し、座標 0 を通り過ぎる。

入力例 2

5
100 1 2 3 4

出力例 2

1

入力例 3

20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

出力例 3

20

Score : 300 points

Problem Statement

Takahashi is at coordinate 0.5 on a number line.

He will make N moves.
In the i-th move, he chooses either the positive direction or the negative direction, and moves L_i in that direction.

What is the maximum number of times he can pass through coordinate 0?
Under the constraints of this problem, no move will end at coordinate 0.

Constraints

  • 1 \le N \le 20
  • 1 \le L_i \le 10^9
  • All input values are integers.

Input

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

N
L_1 L_2 \dots L_N

Output

Output the answer.


Sample Input 1

5
2 5 2 2 1

Sample Output 1

4

For example, by choosing the directions of movement as follows, he can pass through coordinate 0 four times, which is the maximum.

  • In the first move, choose the negative direction and move 2. He moves from coordinate 0.5 to -1.5, passing through coordinate 0.
  • In the second move, choose the positive direction and move 5. He moves from coordinate -1.5 to 3.5, passing through coordinate 0.
  • In the third move, choose the negative direction and move 2. He moves from coordinate 3.5 to 1.5.
  • In the fourth move, choose the negative direction and move 2. He moves from coordinate 1.5 to -0.5, passing through coordinate 0.
  • In the fifth move, choose the positive direction and move 1. He moves from coordinate -0.5 to 0.5, passing through coordinate 0.

Sample Input 2

5
100 1 2 3 4

Sample Output 2

1

Sample Input 3

20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 3

20
G - Querying Multiset

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は何も書かれていないたくさんのボールと 1 つの袋を持っています。 最初、袋は空で、高橋君は Q 回の操作を行います。 それぞれの操作は以下の 3 種類のうちのいずれかです。

  • 操作 1 : まだ何も書かれていないボール 1 つに整数 X_i を書き込み、袋に入れる。
  • 操作 2 : 袋に入っているすべてのボールについて、そこに書かれている数を、それに X_i を加えたものに書き換える。
  • 操作 3 : 袋に入っているボールのうち書かれている数が最小のもの(複数ある場合はそのうちの 1 つ)を取り出し、そこに書かれている数を記録する。その後、そのボールを捨てる。

1\leq i\leq Q について i 回目の操作の種類 P_i および操作 1 , 2 における X_i の値が与えられるので、操作 3 において記録された数を順に出力してください。

制約

  • 1 \leq Q \leq 2\times 10^5
  • 1 \leq P_i \leq 3
  • 1 \leq X_i \leq 10^9
  • 入力は全て整数である。
  • P_i=3 であるような i が 1 つ以上存在する。
  • P_i=3 であるとき、 i 回目の操作の直前の時点で、袋には 1 つ以上のボールが入っている。

入力

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

Q
query_1
query_2
:
query_Q

2 行目から Q+1 行目の各 query_i は次のいずれかの形で与えられる。

1 X_i
2 X_i
3

まず、1\leq P_i\leq 3 が与えられる。これは操作の種類を表す。 P_i=1 または P_i=2 ならば、その後に空白区切りで X_i が与えられる。

出力

Q 回の操作のうち操作の種類が P_i=3 であるような各操作について、記録された数を改行区切りで出力せよ。


入力例 1

5
1 3
1 5
3
2 2
3

出力例 1

3
7

高橋君は次のように操作を行います。

  • 3 の書かれたボールを袋に入れる。
  • 5 の書かれたボールを袋に入れる。
  • 今、袋には 3 の書かれたボールと 5 の書かれたボールが入っているため、このうち小さい 3 の書かれたボールを取り出し、 3 を記録した後に捨てる。
  • 今、袋には 5 の書かれたボールのみが入っているため、この数を 5+2=7 に書き換える。
  • 今、袋には 7 の書かれたボールのみが入っているため、このボールを取り出し、 7 を記録した後に捨てる。

よって、記録された順に 3 , 7 を出力します。


入力例 2

6
1 1000000000
2 1000000000
2 1000000000
2 1000000000
2 1000000000
3

出力例 2

5000000000

答えが 32 bit整数に収まらないことがある事に注意してください。

Score : 400 points

Problem Statement

Takahashi has many balls, on which nothing is written, and one bag. Initially, the bag is empty. Takahashi will do Q operations, each of which is of one of the following three types.

  • Type 1: Write an integer X_i on a blank ball and put it in the bag.
  • Type 2: For each ball in the bag, replace the integer written on it with that integer plus X_i.
  • Type 3: Pick up the ball with the smallest integer in the bag (if there are multiple such balls, pick up one of them). Record the integer written on this ball and throw it away.

For each 1\leq i\leq Q, you are given the type P_i of the i-th operation and the value of X_i if the operation is of Type 1 or 2. Print the integers recorded in the operations of Type 3 in order.

Constraints

  • 1 \leq Q \leq 2\times 10^5
  • 1 \leq P_i \leq 3
  • 1 \leq X_i \leq 10^9
  • All values in input are integers.
  • There is one or more i such that P_i=3.
  • If P_i=3, the bag contains at least one ball just before the i-th operation.

Input

Input is given from Standard Input in the following format:

Q
query_1
query_2
:
query_Q

Each query_i in the 2-nd through (Q+1)-th lines is in the following format:

1 X_i
2 X_i
3

The first number in each line is 1\leq P_i\leq 3, representing the type of the operation. If P_i=1 or P_i=2, it is followed by a space, and then by X_i.

Output

For each operation with P_i=3 among the Q operations, print the recorded integer in its own line.


Sample Input 1

5
1 3
1 5
3
2 2
3

Sample Output 1

3
7

Takahashi will do the following operations.

  • Write 3 on a ball and put it in the bag.
  • Write 5 on a ball and put it in the bag.
  • The bag now contains a ball with 3 and another with 5. Pick up the ball with the smaller of them, or 3. Record 3 and throw it away.
  • The bag now contains just a ball with 5. Replace this integer with 5+2=7.
  • The bag now contains just a ball with 7. Pick up this ball, record 7, and throw it away.

Therefore, we should print 3 and 7, in the order they are recorded.


Sample Input 2

6
1 1000000000
2 1000000000
2 1000000000
2 1000000000
2 1000000000
3

Sample Output 2

5000000000

Note that the outputs may not fit into a 32-bit integer.

H - Transitivity

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の単純有向グラフが与えられます。辺 i は頂点 u_i から頂点 v_i への有向辺です。

また、あなたは次の操作を 0 回以上何度でも行えます。

  • 相異なる頂点 x,y であって頂点 x から頂点 y への有向辺が存在しないようなものを選ぶ。そして、頂点 x から頂点 y への有向辺を追加する。

このグラフが次の条件を満たす状態にするために最小で何回操作を行う必要があるかを求めてください。

  • 相異なる頂点 a,b,c すべてについて、頂点 a から頂点 b への有向辺と頂点 b から頂点 c への有向辺がともに存在するならば頂点 a から頂点 c への有向辺も存在する。

制約

  • 3 \leq N \leq 2000
  • 0 \leq M \leq 2000
  • 1 \leq u_i ,v_i \leq N
  • u_i \neq v_i
  • i \neq j ならば (u_i,v_i) \neq (u_j,v_j)
  • 入力はすべて整数

入力

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

N M
u_1 v_1
\vdots
u_M v_M

出力

答えを出力せよ。


入力例 1

4 3
2 4
3 1
4 3

出力例 1

3

初め、一例として頂点 2,4,3 について、頂点 2 から頂点 4 への有向辺と頂点 4 から頂点 3 への有向辺がともに存在するにもかかわらず、頂点 2 から頂点 3 への有向辺は存在せず、条件を満たさない状態です。

そこで、以下の 3 本の有向辺を追加すると条件を満たす状態になります。

  • 頂点 2 から頂点 3 への有向辺
  • 頂点 2 から頂点 1 への有向辺
  • 頂点 4 から頂点 1 への有向辺

一方、3 本未満の追加で条件を満たす状態には出来ないため、答えは 3 です。


入力例 2

292 0

出力例 2

0

入力例 3

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

出力例 3

12

Score : 500 points

Problem Statement

You are given a simple directed graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i is a directed edge from vertex u_i to vertex v_i.

You may perform the following operation zero or more times.

  • Choose distinct vertices x and y such that there is no directed edge from vertex x to vertex y, and add a directed edge from vertex x to vertex y.

Find the minimum number of times you need to perform the operation to make the graph satisfy the following condition.

  • For every triple of distinct vertices a, b, and c, if there are directed edges from vertex a to vertex b and from vertex b to vertex c, there is also a directed edge from vertex a to vertex c.

Constraints

  • 3 \leq N \leq 2000
  • 0 \leq M \leq 2000
  • 1 \leq u_i ,v_i \leq N
  • u_i \neq v_i
  • (u_i,v_i) \neq (u_j,v_j) if i \neq j.
  • All values in the input are integers.

Input

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

N M
u_1 v_1
\vdots
u_M v_M

Output

Print the answer.


Sample Input 1

4 3
2 4
3 1
4 3

Sample Output 1

3

Initially, the condition is not satisfied because, for instance, for vertices 2, 4, and 3, there are directed edges from vertex 2 to vertex 4 and from vertex 4 to vertex 3, but not from vertex 2 to vertex 3.

You can make the graph satisfy the condition by adding the following three directed edges:

  • one from vertex 2 to vertex 3,
  • one from vertex 2 to vertex 1, and
  • one from vertex 4 to vertex 1.

On the other hand, the condition cannot be satisfied by adding two or fewer edges, so the answer is 3.


Sample Input 2

292 0

Sample Output 2

0

Sample Input 3

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

Sample Output 3

12
I - Monochromatic Path

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

各マスが白または黒で塗られた H 行 W 列のグリッドがあります。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i, j) について、 グリッドの上から i 行目、左から j 行目のマス(以下、単にマス (i, j) と呼ぶ)の色は A_{i, j} で表されます。 A_{i, j} = 0 のときマス (i, j) は白色、A_{i, j} = 1 のときマス (i, j) は黒色です。

「下記の 2 つの操作のどちらかを行うこと」を好きなだけ( 0 回でも良い)繰り返すことができます。

  • 1 \leq i \leq H を満たす整数を選び、R_i 円払った後、グリッドの上から i 行目にあるそれぞれのマスの色を反転させる(白色のマスは黒色に、黒色のマスは白色に塗り替える)。
  • 1 \leq j \leq W を満たす整数を選び、C_j 円払った後、グリッドの左から j 列目にあるそれぞれのマスの色を反転させる。

下記の条件を満たすようにするためにかかる合計費用の最小値を出力して下さい。

  • マス (1, 1) から「現在いるマスの右隣もしくは下隣のマスに移動する」 ことを繰り返してマス (H, W) まで移動する経路であって、通るマス(マス (1, 1) とマス (H, W) も含む)の色がすべて同じであるようなものが存在する。

本問題の制約下において、有限回の操作によって上記の条件を満たすようにすることが可能であることが証明できます。

制約

  • 2 \leq H, W \leq 2000
  • 1 \leq R_i \leq 10^9
  • 1 \leq C_j \leq 10^9
  • A_{i, j} \in \lbrace 0, 1\rbrace
  • 入力はすべて整数

入力

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

H W
R_1 R_2 \ldots R_H
C_1 C_2 \ldots C_W
A_{1, 1}A_{1, 2}\ldots A_{1, W}
A_{2, 1}A_{2, 2}\ldots A_{2, W}
\vdots
A_{H, 1}A_{H, 2}\ldots A_{H, W}

出力

答えを出力せよ。


入力例 1

3 4
4 3 5
2 6 7 4
0100
1011
1010

出力例 1

9

白色のマスを 0 、黒色のマスを 1 で表します。 初期状態のグリッドに対し、R_2 = 3 円払って上から 2 行目にあるそれぞれのマスを反転させると、

0100
0100
1010

となります。さらに、C_2 = 6 円払って左から 2 列目にあるそれぞれのマスを反転させると、

0000
0000
1110

となり、マス (1, 1) からマス (3, 4) への移動経路であって通るマスの色がすべて同じであるようなものが存在する状態になります(例えば (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (3, 4) という経路)。 かかった合計費用は 3+6 = 9 円であり、このときが考えられる中で最小です。


入力例 2

15 20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78
39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18
01011100110000001111
10101111100010011000
11011000011010001010
00010100011111010100
11111001101010001011
01111001100101011100
10010000001110101110
01001011100100101000
11001000100101011000
01110000111011100101
00111110111110011111
10101111111011101101
11000011000111111001
00011101011110001101
01010000000001000000

出力例 2

125

Score : 500 points

Problem Statement

We have a grid with H rows and W columns. Each square is painted either white or black. For each integer pair (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, the color of the square at the i-th row from the top and j-th column from the left (we simply denote this square by Square (i, j)) is represented by A_{i, j}. Square (i, j) is white if A_{i, j} = 0, and black if A_{i, j} = 1.

You may perform the following operations any number of (possibly 0) times in any order:

  • Choose an integer i such that 1 \leq i \leq H, pay R_i yen (the currency in Japan), and invert the color of each square in the i-th row from the top in the grid. (White squares are painted black, and black squares are painted white.)
  • Choose an integer j such that 1 \leq j \leq W, pay C_j yen, and invert the color of each square in the j-th column from the left in the grid.

Print the minimum total cost to satisfy the following condition:

  • There exists a path from Square (1, 1) to Square (H, W) that can be obtained by repeatedly moving down or to the right, such that all squares in the path (including Square (1, 1) and Square (H, W)) have the same color.

We can prove that it is always possible to satisfy the condition in a finite number of operations under the Constraints of this problem.

Constraints

  • 2 \leq H, W \leq 2000
  • 1 \leq R_i \leq 10^9
  • 1 \leq C_j \leq 10^9
  • A_{i, j} \in \lbrace 0, 1\rbrace
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
R_1 R_2 \ldots R_H
C_1 C_2 \ldots C_W
A_{1, 1}A_{1, 2}\ldots A_{1, W}
A_{2, 1}A_{2, 2}\ldots A_{2, W}
\vdots
A_{H, 1}A_{H, 2}\ldots A_{H, W}

Output

Print the answer.


Sample Input 1

3 4
4 3 5
2 6 7 4
0100
1011
1010

Sample Output 1

9

We denote a white square by 0 and a black square by 1. On the initial grid, you can pay R_2 = 3 yen to invert the color of each square in the 2-nd row from the top to make the grid:

0100
0100
1010

Then, you can pay C_2 = 6 yen to invert the color of each square in the 2-nd row from the left to make the grid:

0000
0000
1110

Now, there exists a path from Square (1, 1) to Square (3, 4) such that all squares in the path have the same color (such as the path (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (3, 4)). The total cost paid is 3+6 = 9 yen, which is the minimum possible.


Sample Input 2

15 20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78
39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18
01011100110000001111
10101111100010011000
11011000011010001010
00010100011111010100
11111001101010001011
01111001100101011100
10010000001110101110
01001011100100101000
11001000100101011000
01110000111011100101
00111110111110011111
10101111111011101101
11000011000111111001
00011101011110001101
01010000000001000000

Sample Output 2

125