A - Seismic magnitude scales

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

地震のマグニチュードは、その地震のエネルギーの大きさを対数で表した値です。マグニチュードが 1 増える度にエネルギーは約 32 倍になることが知られています。
ここではマグニチュードが 1 増える度に地震のエネルギーがちょうど 32 倍になるとします。このとき、マグニチュード A の地震のエネルギーの大きさはマグニチュード B の地震のエネルギーの大きさの何倍ですか?

制約

  • 3\leq B\leq A\leq 9
  • A , B は整数

入力

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

A B

出力

答えを整数で出力せよ。


入力例 1

6 4

出力例 1

1024

64 より 2 だけ大きいので、 マグニチュード 6 の地震はマグニチュード 4 の地震と比べて 32\times 32=1024 倍のエネルギーを持っています。


入力例 2

5 5

出力例 2

1

マグニチュードが同じなのでエネルギーの大きさも同じです。

Score : 100 points

Problem Statement

The magnitude of an earthquake is a logarithmic scale of the energy released by the earthquake. It is known that each time the magnitude increases by 1, the amount of energy gets multiplied by approximately 32.
Here, we assume that the amount of energy gets multiplied by exactly 32 each time the magnitude increases by 1. In this case, how many times is the amount of energy of a magnitude A earthquake as much as that of a magnitude B earthquake?

Constraints

  • 3\leq B\leq A\leq 9
  • A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer as an integer.


Sample Input 1

6 4

Sample Output 1

1024

6 is 2 greater than 4, so a magnitude 6 earthquake has 32\times 32=1024 times as much energy as a magnitude 4 earthquake has.


Sample Input 2

5 5

Sample Output 2

1

Earthquakes with the same magnitude have the same amount of energy.

B - Water Station

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

全長 100\;\mathrm{km} のマラソンコースがあります。 マラソンコースにはスタートから 5\;\mathrm{km} おきに給水所が設置されており、スタート地点・ゴール地点とあわせて 21 箇所の給水所があります。

高橋くんは、このマラソンコースの N\;\mathrm{km} 地点にいます。 高橋くんに最も近い給水所は何 \mathrm{km} 地点の給水所か求めてください。

この問題の制約のもとで、最も近い給水所が 1 つに定まることが証明できます。

制約

  • 0\leq N\leq100
  • N は整数

入力

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

N

出力

高橋くんがいる地点に最も近い給水所が何 \mathrm{km} 地点にあるかを 1 行で出力せよ。


入力例 1

53

出力例 1

55

高橋くんは、マラソンコースの 53\;\mathrm{km} 地点にいます。 55\;\mathrm{km} 地点の給水所までの距離は 2\;\mathrm{km} で、これより近い給水所はありません。 よって、55 を出力してください。


入力例 2

21

出力例 2

20

高橋くんはマラソンコースを戻ることもできます。


入力例 3

100

出力例 3

100

給水所はスタートやゴールにもあります。 また、高橋くんはすでに給水所にいることもあります。

Score : 100 points

Problem Statement

There is an ultramarathon course totaling 100\;\mathrm{km}. Water stations are set up every 5\;\mathrm{km} along the course, including the start and goal, for a total of 21.

Takahashi is at the N\;\mathrm{km} point of this course. Find the position of the nearest water station to him.

Under the constraints of this problem, it can be proven that the nearest water station is uniquely determined.

Constraints

  • 0\leq N\leq100
  • N is an integer.

Input

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

N

Output

Print the distance between the start and the water station nearest to Takahashi, in kilometers, in a single line.


Sample Input 1

53

Sample Output 1

55

Takahashi is at the 53\;\mathrm{km} point of the course. The water station at the 55\;\mathrm{km} point is 2\;\mathrm{km} away, and there is no closer water station. Therefore, you should print 55.


Sample Input 2

21

Sample Output 2

20

Takahashi could also go back the way.


Sample Input 3

100

Sample Output 3

100

There are also water stations at the start and goal. Additionally, Takahashi may already be at a water station.

C - Glass and Mug

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

AtCoder 社はグラスマグカップを販売しています。

高橋君は容量が G ml のグラスと、容量が M ml のマグカップを 1 つずつ持っています。
ここで、G,MG<M をみたします。

最初、グラスとマグカップはいずれも空です。
以下の操作を K 回繰り返した後で、グラスとマグカップに水がそれぞれ何 ml ずつ入っているか求めてください。

  • グラスが水で満たされているとき、すなわちグラスにちょうど G ml 入っているとき、グラスの水をすべて捨てる。
  • そうでなく、マグカップが空であるとき、マグカップを水で満たす。
  • 上のいずれでもないとき、マグカップが空になるかグラスが水で満たされるまで、マグカップからグラスに水を移す。

制約

  • 1\leq K\leq 100
  • 1\leq G<M\leq 1000
  • G,M,K は整数

入力

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

K G M

出力

操作を K 回行った後で、グラスとマグカップに水がそれぞれ何 ml ずつ入っているか、この順に空白区切りで出力せよ。


入力例 1

5 300 500

出力例 1

200 500

操作は次の順で行われます。最初、グラスとマグカップはいずれも空です。

  • マグカップを水で満たす。グラスには 0 ml, マグカップには 500 ml 入った状態になる。
  • グラスが満たされるまでマグカップからグラスに水を移す。グラスには 300 ml, マグカップには 200 ml 入った状態になる。
  • グラスの水をすべて捨てる。グラスには 0 ml, マグカップには 200 ml 入った状態になる。
  • マグカップが空になるまでマグカップからグラスに水を移す。グラスには 200 ml, マグカップには 0 ml 入った状態になる。
  • マグカップを水で満たす。グラスには 200 ml, マグカップには 500 ml 入った状態になる。

よって、5 回の操作の後でグラスには 200 ml, マグカップには 500 ml 入った状態になります。 ゆえに、200, 500 を空白区切りでこの順に出力します。


入力例 2

5 100 200

出力例 2

0 0

Score : 200 points

Problem Statement

AtCoder Inc. sells glasses and mugs.

Takahashi has a glass with a capacity of G milliliters and a mug with a capacity of M milliliters.
Here, G<M.

Initially, both the glass and the mug are empty.
After performing the following operation K times, determine how many milliliters of water are in the glass and the mug, respectively.

  • When the glass is filled with water, that is, the glass contains exactly G milliliters of water, discard all the water from the glass.
  • Otherwise, if the mug is empty, fill the mug with water.
  • Otherwise, transfer water from the mug to the glass until the mug is empty or the glass is filled with water.

Constraints

  • 1\leq K\leq 100
  • 1\leq G<M\leq 1000
  • G, M, and K are integers.

Input

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

K G M

Output

Print the amounts, in milliliters, of water in the glass and the mug, in this order, separated by a space, after performing the operation K times.


Sample Input 1

5 300 500

Sample Output 1

200 500

The operation will be performed as follows. Initially, both the glass and the mug are empty.

  • Fill the mug with water. The glass has 0 milliliters, and the mug has 500 milliliters of water.
  • Transfer water from the mug to the glass until the glass is filled. The glass has 300 milliliters, and the mug has 200 milliliters of water.
  • Discard all the water from the glass. The glass has 0 milliliters, and the mug has 200 milliliters of water.
  • Transfer water from the mug to the glass until the mug is empty. The glass has 200 milliliters, and the mug has 0 milliliters of water.
  • Fill the mug with water. The glass has 200 milliliters, and the mug has 500 milliliters of water.

Thus, after five operations, the glass has 200 milliliters, and the mug has 500 milliliters of water. Hence, print 200 and 500 in this order, separated by a space.


Sample Input 2

5 100 200

Sample Output 2

0 0
D - Unvarnished Report

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

キーエンスでは良いことも悪いこともありのままに報告するという文化があります。
そこで、報告内容が元の文章のありのままであるかを確認したいです。

英小文字のみからなる文字列 S, T が与えられます。
ST が等しいならば 0 を、そうでないならば異なっている文字のうち先頭のものが何文字目かを出力してください。
ただし、S,T の一方にのみ i 文字目が存在するときも、i 文字目は異なっているとみなすものとします。

より厳密には、ST が等しくないならば次の条件のうちいずれかをみたす最小の整数 i を出力してください。

  • 1\leq i\leq |S| かつ 1\leq i\leq |T| かつ S_i\neq T_i
  • |S|< i\leq |T|
  • |T|< i\leq |S|

ただし、|S|,|T| でそれぞれ S,T の長さを、S_i,T_i でそれぞれ S,Ti 文字目を表します。

制約

  • S,T は英小文字のみからなる長さ 1 以上 100 以下の文字列

入力

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

S
T

出力

ST が等しいならば 0 を、そうでないならば異なっている文字のうち先頭のものが何文字目かを出力せよ。


入力例 1

abcde
abedc

出力例 1

3

S=abcde, T=abedc です。
ST1,2 文字目は等しく、3 文字目が異なるため、 3 を出力します。


入力例 2

abcde
abcdefg

出力例 2

6

S=abcde, T=abcdefg です。
ST5 文字目まで等しく、T にのみ 6 文字目が存在するため、6 を出力します。


入力例 3

keyence
keyence

出力例 3

0

ST は等しいため、 0 を出力します。

Score : 200 points

Problem Statement

KEYENCE has a culture of reporting things as they are, whether good or bad.
So we want to check whether the reported content is exactly the same as the original text.

You are given two strings S and T, consisting of lowercase English letters.
If S and T are equal, print 0; otherwise, print the position of the first character where they differ.
Here, if the i-th character exists in only one of S and T, consider that the i-th characters are different.

More precisely, if S and T are not equal, print the smallest integer i satisfying one of the following conditions:

  • 1\leq i\leq |S|, 1\leq i\leq |T|, and S_i\neq T_i.
  • |S| < i \leq |T|.
  • |T| < i \leq |S|.

Here, |S| and |T| denote the lengths of S and T, respectively, and S_i and T_i denote the i-th characters of S and T, respectively.

Constraints

  • S and T are strings of length between 1 and 100, inclusive, consisting of lowercase English letters.

Input

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

S
T

Output

If S and T are equal, print 0; otherwise, print the position of the first character where they differ.


Sample Input 1

abcde
abedc

Sample Output 1

3

We have S= abcde and T= abedc.
S and T have the same first and second characters, but differ at the third character, so print 3.


Sample Input 2

abcde
abcdefg

Sample Output 2

6

We have S= abcde and T= abcdefg.
S and T are equal up to the fifth character, but only T has a sixth character, so print 6.


Sample Input 3

keyence
keyence

Sample Output 3

0

S and T are equal, so print 0.

E - Takahashi Gets Lost

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

HW 列のグリッドがあります。

グリッドの各マスはのどちらかであり、 その情報は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H で与えられます。 上から i 行目、左から j 列目のマスを (i, j) で表すと、 S_ij 文字目が . のときマス (i, j) は陸であり、# のときマス (i, j) は海です。

ここで、グリッドの外周のマス(すなわち、i = 1i = Hj = 1j = W のうち少なくとも 1 個以上を満たすマス (i, j) )については、すべて海であることが制約として保証されます。

高橋君が乗った宇宙船が、グリッド上のあるマスに不時着してしまいました。 その後、高橋君は LRUD のみからなる長さ N の文字列 T で表される手順に沿って、グリッド上を N 回移動しました。 i = 1, 2, \ldots, N について、Ti 文字目は i 回目の移動の内容を下記の通り表します。

  • L のとき、左に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j-1) である。
  • R のとき、右に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i, j+1) である。
  • U のとき、上に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i-1, j) である。
  • D のとき、下に 1 マス移動したことを表す。すなわち、移動前のマスを (i, j) とするとき、移動後のマスは (i+1, j) である。

高橋君の移動経路上のマス(不時着したマスおよび現在いるマスを含む)はいずれも海でないことがわかっています。 高橋君が現在いるマスとしてあり得るものの個数を出力してください。

制約

  • H, W, N は整数
  • 3 \leq H, W \leq 500
  • 1 \leq N \leq 500
  • TLRUD のみからなる長さ N の文字列
  • S_i.# のみからなる長さ W の文字列
  • 高橋君が現在いるマスとしてあり得るものが少なくとも 1 個存在する。
  • グリッドの外周のマスはすべて海である。

入力

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

H W N
T
S_1
S_2
\vdots
S_H

出力

答えを出力せよ。


入力例 1

6 7 5
LULDR
#######
#...#.#
##...##
#.#...#
#...#.#
#######

出力例 1

2

下記の 2 つの場合がありえるため、高橋君が現在いるマスとしてあり得るものは (3, 4)(4, 5)2 個です。

  • マス (3, 5) に不時着し、(3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4) と移動した場合
  • マス (4, 6) に不時着し、(4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5) と移動した場合

入力例 2

13 16 9
ULURDLURD
################
##..##.#..####.#
###.#..#.....#.#
#..##..#####.###
#...#..#......##
###.##.#..#....#
##.#####....##.#
###.###.#.#.#..#
######.....##..#
#...#.#.######.#
##..###..#..#.##
#...#.#.#...#..#
################

出力例 2

6

Score: 250 points

Problem Statement

There is a grid with H rows and W columns.

Each cell of the grid is land or sea, which is represented by H strings S_1, S_2, \ldots, S_H of length W. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left, and (i, j) is land if the j-th character of S_i is ., and (i, j) is sea if the character is #.

The constraints guarantee that all cells on the perimeter of the grid (that is, the cells (i, j) that satisfy at least one of i = 1, i = H, j = 1, j = W) are sea.

Takahashi's spaceship has crash-landed on a cell in the grid. Afterward, he moved N times on the grid following the instructions represented by a string T of length N consisting of L, R, U, and D. For i = 1, 2, \ldots, N, the i-th character of T describes the i-th move as follows:

  • L indicates a move of one cell to the left. That is, if he is at (i, j) before the move, he will be at (i, j-1) after the move.
  • R indicates a move of one cell to the right. That is, if he is at (i, j) before the move, he will be at (i, j+1) after the move.
  • U indicates a move of one cell up. That is, if he is at (i, j) before the move, he will be at (i-1, j) after the move.
  • D indicates a move of one cell down. That is, if he is at (i, j) before the move, he will be at (i+1, j) after the move.

It is known that all cells along his path (including the cell where he crash-landed and the cell he is currently on) are not sea. Print the number of cells that could be his current position.

Constraints

  • H, W, and N are integers.
  • 3 \leq H, W \leq 500
  • 1 \leq N \leq 500
  • T is a string of length N consisting of L, R, U, and D.
  • S_i is a string of length W consisting of . and #.
  • There is at least one cell that could be Takahashi's current position.
  • All cells on the perimeter of the grid are sea.

Input

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

H W N
T
S_1
S_2
\vdots
S_H

Output

Print the answer.


Sample Input 1

6 7 5
LULDR
#######
#...#.#
##...##
#.#...#
#...#.#
#######

Sample Output 1

2

The following two cases are possible, so there are two cells that could be Takahashi's current position: (3, 4) and (4, 5).

  • He crash-landed on cell (3, 5) and moved (3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4).
  • He crash-landed on cell (4, 6) and moved (4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5).

Sample Input 2

13 16 9
ULURDLURD
################
##..##.#..####.#
###.#..#.....#.#
#..##..#####.###
#...#..#......##
###.##.#..#....#
##.#####....##.#
###.###.#.#.#..#
######.....##..#
#...#.#.######.#
##..###..#..#.##
#...#.#.#...#..#
################

Sample Output 2

6
F - Route Map

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoder 鉄道のとある路線には N 個の駅が存在し、始点から終点に向かって i \, (1 \leq i \leq N) 番目の駅の名前は S_i です。

普通列車は全ての駅に止まりますが、急行列車は全ての駅に止まるとは限りません。具体的には、急行列車は M \, (M \leq N) 個の駅にのみ止まり、j \, (1 \leq j \leq M) 番目に止まる駅の名前は T_j です。
ただし、T_1 = S_1 かつ T_M = S_N、すなわち急行列車は始点と終点の両方に止まることが保証されます。

N 個の駅それぞれについて、その駅に急行列車が止まるかどうか判定してください。

制約

  • 2 \leq M \leq N \leq 10^5
  • N, M は整数
  • S_i \, (1 \leq i \leq N) は英小文字のみからなる 1 文字以上 10 文字以下の文字列
  • S_i \neq S_j \, (i \neq j)
  • T_1 = S_1 かつ T_M = S_N
  • (T_1, \dots, T_M)(S_1, \dots, S_N) から 0 個以上の文字列を選んで取り除き、残った文字列を元の順序で並べることで得られる

入力

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

N M
S_1 \ldots S_N
T_1 \ldots T_M

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、始点から終点に向かって i 番目の駅に急行列車が止まるなら Yes、そうでないなら No と出力せよ。


入力例 1

5 3
tokyo kanda akiba okachi ueno
tokyo akiba ueno

出力例 1

Yes
No
Yes
No
Yes

入力例 2

7 7
a t c o d e r
a t c o d e r

出力例 2

Yes
Yes
Yes
Yes
Yes
Yes
Yes

急行列車が全ての駅に止まることもあります。

Score : 300 points

Problem Statement

There are N stations on a certain line operated by AtCoder Railway. The i-th station (1 \leq i \leq N) from the starting station is named S_i.

Local trains stop at all stations, while express trains may not. Specifically, express trains stop at only M \, (M \leq N) stations, and the j-th stop (1 \leq j \leq M) is the station named T_j.
Here, it is guaranteed that T_1 = S_1 and T_M = S_N, that is, express trains stop at both starting and terminal stations.

For each of the N stations, determine whether express trains stop at that station.

Constrains

  • 2 \leq M \leq N \leq 10^5
  • N and M are integers.
  • S_i (1 \leq i \leq N) is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
  • S_i \neq S_j \, (i \neq j)
  • T_1 = S_1 and T_M = S_N.
  • (T_1, \dots, T_M) is obtained by removing zero or more strings from (S_1, \dots, S_N) and lining up the remaining strings without changing the order.

Input

Input is given from Standard Input in the following format:

N M
S_1 \ldots S_N
T_1 \ldots T_M

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain Yes if express trains stop at the i-th station from the starting station, and No otherwise.


Sample Input 1

5 3
tokyo kanda akiba okachi ueno
tokyo akiba ueno

Sample Output 1

Yes
No
Yes
No
Yes

Sample Input 2

7 7
a t c o d e r
a t c o d e r

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes
Yes

Express trains may stop at all stations.

G - On AtCoder Conference

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

1 周の長さが M である池があり、その周上に 1 つの小屋と N 人の人が立っています。
実数 x (0\leq x <M) について、小屋から時計回りに x だけ進んだところを地点 x と定義します。
i 番目の人は、 地点 A_i にいます。 複数の人が同じ場所に立っていることもあります。

また、N 以下の整数 C が与えられます。 i=0,1,\ldots,M-1 について、X_i を次のように定義します。

  1. 高橋君は地点 (i+0.5) からスタートして、時計回りに動き始める。
  2. 高橋君は出会った人数の合計が C 未満である限り(時計回りに)動き続け、C 以上になったら止まる。ここで、「地点 y にいる人と出会う」とは、高橋君が地点 y に到達することを指す。
  3. 高橋君が止まるまでに出会った人数を X_i とする。ここで、高橋君が止まった地点に複数の人がいる場合、そこにいた人はすべて出会った人として数えられ、特に X_iC より大きくなる可能性がある。

i=0,1,\ldots,M-1 にわたる X_i の総和、すなわち \displaystyle\sum_{i=0}^{M-1} X_i を求めてください。

制約

  • 1\leq N\leq 5\times 10^5
  • 1\leq M\leq 10^{12}
  • 0\leq A_i\leq M-1
  • 1\leq C\leq N
  • 入力はすべて整数

入力

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

N M C
A_1 A_2 \ldots A_N

出力

i=0,1,\ldots,M-1 にわたる X_i の総和を一行に出力せよ。


入力例 1

5 3 2
1 2 1 0 1

出力例 1

9

i=0 のとき、高橋君は地点 0.5 からスタートして時計回りに動きます。その後、次のようになります。

  • 地点 11,3,5 番目の 3 人と出会い、今まで出会った人数の合計は 3 となる。これは C=2 以上であるため、高橋くんはそこで止まる。よって、X_0=3 である。

i=1 のとき、高橋君は地点 1.5 からスタートして時計回りに動きます。その後、次のようになります。

  • 地点 22 番目の人と出会う。今まで出会った人数の合計は 1 であるため、動き続ける。
  • 地点 04 番目の人と出会い、今まで出会った人数の合計は 2 となる。これは C=2 以上であるため、高橋くんはそこで止まる。よって、X_1=2 である。

i=2 のとき、高橋君は地点 2.5 からスタートして時計回りに動きます。その後、次のようになります。

  • 地点 04 番目の人と会う。今まで出会った人数の合計は 1 であるため、動き続ける。
  • 地点 11,3,5 番目の 3 人と会い、今まで出会った人数の合計は 4 となる。これは C=2 以上であるため、高橋くんはそこで止まる。よって、X_2=4 である。

よって、答えは X_0+X_1+X_2=3+2+4=9 となります。


入力例 2

1 1000000000000 1
1

出力例 2

1000000000000

高橋君はスタートする位置によらず、池の周りに立っている唯一の人である、地点 1 にいる人に出会った時に止まります。
よって、i によらず X_i=1 であり、答えは 10^{12} となります。

Score : 425 points

Problem Statement

There is a pond with a circumference of M, and on its shore stand one hut and N people.
For a real number x (0\leq x <M), define point x as the location that is x distance clockwise from the hut.
The i-th person is at point A_i. Multiple people may be standing at the same location.

Additionally, an integer C not greater than N is given. For i=0,1,\ldots,M-1, define X_i as follows:

  1. Takahashi starts at point (i+0.5) and begins moving clockwise.
  2. Takahashi continues moving (clockwise) as long as the total number of people he has met is less than C, and stops when it becomes C or more. Here, "meeting a person at point y" means that Takahashi reaches point y.
  3. Let X_i be the number of people Takahashi met before stopping. Here, if there are multiple people at the point where Takahashi stopped, all the people there are counted as people he met, and particularly, X_i may be greater than C.

Find the sum of X_i over i=0,1,\ldots,M-1, that is, \displaystyle\sum_{i=0}^{M-1} X_i.

Constraints

  • 1\leq N\leq 5\times 10^5
  • 1\leq M\leq 10^{12}
  • 0\leq A_i\leq M-1
  • 1\leq C\leq N
  • All input values are integers.

Input

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

N M C
A_1 A_2 \ldots A_N

Output

Print the sum of X_i over i=0,1,\ldots,M-1 on a single line.


Sample Input 1

5 3 2
1 2 1 0 1

Sample Output 1

9

When i=0, Takahashi starts at point 0.5 and moves clockwise. Then, the following happens:

  • At point 1, he meets the 1st, 3rd, and 5th people, a total of three people, and the total number of people met so far is 3. This is not less than C=2, so Takahashi stops there. Therefore, X_0=3.

When i=1, Takahashi starts at point 1.5 and moves clockwise. Then, the following happens:

  • At point 2, he meets the 2nd person. The total number of people met so far is 1, so he continues moving.
  • At point 0, he meets the 4th person, and the total number of people met so far is 2. This is not less than C=2, so Takahashi stops there. Therefore, X_1=2.

When i=2, Takahashi starts at point 2.5 and moves clockwise. Then, the following happens:

  • At point 0, he meets the 4th person. The total number of people met so far is 1, so he continues moving.
  • At point 1, he meets the 1st, 3rd, and 5th people, a total of three people, and the total number of people met so far is 4. This is not less than C=2, so Takahashi stops there. Therefore, X_2=4.

Therefore, the answer is X_0+X_1+X_2=3+2+4=9.


Sample Input 2

1 1000000000000 1
1

Sample Output 2

1000000000000

Regardless of the starting position, Takahashi stops when he meets the only person standing around the pond, who is at point 1.
Therefore, X_i=1 regardless of i, and the answer is 10^{12}.

H - Hungry Takahashi

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

HW 列のグリッドがあります。 上から i 行目、左から j 列目のマスのことを (i,j) と表記します。 各マスには何枚かのコインが置かれており、マス (i,j) に置かれているコインは A_{i,j} 枚です。

高橋君ははじめマス (1,1) におり、x 枚のコインを持っています。 これから H+W-1 日間にかけていくつかの出来事が起こります。 k\ (1\leq k\leq H+W-1) 日目には以下のことが順に起こります:

  1. 高橋君が、そのときいるマスに置かれたコインを全て回収する。
  2. お腹の空いた高橋君が、手持ちのコインを P_k 枚消費してご飯を買う。ただし、所持しているコインが P_k 枚未満の場合ご飯を買うことはできず、空腹のあまり倒れてしまう。
  3. k < H+W-1 ならば、高橋君が、そのときいるマスの 1 つ右または 1 つ下のいずれかのマスを選んでそこに移動する。ただし、グリッドの外に出てしまうような移動はできない。k=H+W-1 ならば、何もしない。

高橋君が一度も空腹で倒れることなくこれからの H+W-1 日間を終えられるような方法が存在するとき、 高橋君がはじめに持っているコインの枚数 x としてありうる最小の非負整数を求めてください。

制約

  • H,W\geq 1
  • H\times W \leq 2\times 10^5
  • 1\leq A_{i,j}\leq 10^9
  • 1\leq P_k\leq 10^9
  • 入力は全て整数

入力

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

H W
A_{1,1} A_{1,2} \dots A_{1,W}
A_{2,1} A_{2,2} \dots A_{2,W}
\vdots
A_{H,1} A_{H,2} \dots A_{H,W}
P_1 P_2 \dots P_{H+W-1}

出力

答えを出力せよ。


入力例 1

2 2
3 2
4 1
1 3 6

出力例 1

2

x=2 のとき、高橋君は次のように行動することで、一度も空腹で倒れずに済みます。

  • 最初、マス (1,1) におり、2 枚のコインを所持している。
  • 1 日目:
    1. マス (1,1) に置かれたコイン 3 枚を回収し、手持ちのコインが 5 枚になる。
    2. コインを 1 枚消費してご飯を買い、手持ちのコインが 4 枚になる。
    3. 1 つ下にあるマス (2,1) に移動する。
  • 2 日目:
    1. マス (2,1) に置かれたコイン 4 枚を回収し、手持ちのコインが 8 枚になる。
    2. コインを 3 枚消費してご飯を買い、手持ちのコインが 5 枚になる。
    3. 1 つ右にあるマス (2,2) に移動する。
  • 3 日目:
    1. マス (2,2) に置かれたコイン 1 枚を回収し、手持ちのコインが 6 枚になる。
    2. コインを 6 枚消費してご飯を買い、手持ちのコインが 0 枚になる。

x1 以下のとき、高橋君はどのように行動してもいずれかのタイミングで空腹で倒れてしまいます。 よって答えは 2 です。


入力例 2

1 1
5
3

出力例 2

0

最初に 1 枚もコインを持っていなかったとしても、高橋君は空腹で倒れることはありません。


入力例 3

4 7
35 29 36 88 58 15 25
99 7 49 61 67 4 57
96 92 3 49 49 36 90
72 89 40 44 24 53 45
55 43 23 71 77 6 94 19 27 21

出力例 3

20

Score : 450 points

Problem Statement

There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left. Some coins are placed on each cell, and there are A_{i,j} coins on cell (i,j).

Takahashi is initially at cell (1,1) and has x coins. Over the next H+W-1 days, several events will occur. On the k-th day (1\leq k\leq H+W-1), the following things happen in order:

  1. Takahashi collects all the coins placed on the cell where he is currently located.
  2. Hungry Takahashi consumes P_k coins from his hand to buy food. However, if he has fewer than P_k coins, he cannot buy food and collapses from hunger.
  3. If k < H+W-1, Takahashi moves either one cell right or one cell down. He cannot leave the grid. If k=H+W-1, he does nothing.

When there exists a way for Takahashi to finish the next H+W-1 days without ever collapsing from hunger, find the minimum non-negative integer x that can be the number of coins Takahashi initially has.

Constraints

  • H,W\geq 1
  • H\times W \leq 2\times 10^5
  • 1\leq A_{i,j}\leq 10^9
  • 1\leq P_k\leq 10^9
  • All input values are integers.

Input

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

H W
A_{1,1} A_{1,2} \dots A_{1,W}
A_{2,1} A_{2,2} \dots A_{2,W}
\vdots
A_{H,1} A_{H,2} \dots A_{H,W}
P_1 P_2 \dots P_{H+W-1}

Output

Output the answer.


Sample Input 1

2 2
3 2
4 1
1 3 6

Sample Output 1

2

When x=2, Takahashi can act as follows to avoid collapsing from hunger:

  • Initially, he is at cell (1,1) and has 2 coins.
  • Day 1:
    1. He collects 3 coins placed on cell (1,1), so he has 5 coins.
    2. He consumes 1 coin to buy food, so he has 4 coins.
    3. He moves to cell (2,1) which is 1 below.
  • Day 2:
    1. He collects 4 coins placed on cell (2,1), so he has 8 coins.
    2. He consumes 3 coins to buy food, so he has 5 coins.
    3. He moves to cell (2,2) which is 1 to the right.
  • Day 3:
    1. He collects 1 coin placed on cell (2,2), so he has 6 coins.
    2. He consumes 6 coins to buy food, so he has 0 coins.

When x is 1 or less, Takahashi will collapse from hunger at some point no matter how he acts. Therefore, the answer is 2.


Sample Input 2

1 1
5
3

Sample Output 2

0

Even if Takahashi initially has no coins, he will not collapse from hunger.


Sample Input 3

4 7
35 29 36 88 58 15 25
99 7 49 61 67 4 57
96 92 3 49 49 36 90
72 89 40 44 24 53 45
55 43 23 71 77 6 94 19 27 21

Sample Output 3

20
I - Skate

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

HW 列のグリッド型のスケート場があります。上から i 行目、左から j 行目のマスを (i,j) で表します。

スケート場には N 個の障害物があり、i 個目の障害物は (X_i,Y_i) に置かれています。

高橋君は 1 回の移動において、上下左右いずれかの方向を選んで、障害物に当たるまで進み続けます。
障害物に当たるとその 1 つ手前のマスで停止します。 なお、スケート場の周りは崖になっており、障害物に当たらないような移動は禁止とします。

高橋君ははじめ (s_x,s_y) にいて、何回か移動することで (g_x,g_y) で停止したいと考えています。

(g_x,g_y) へ辿り着くには最小で何回の移動が必要ですか?辿り着けないときはその旨を伝えてください。

制約

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • i\neq j ならば、(X_i,Y_i)\neq (X_j,Y_j)
  • 入力は全て整数である

入力

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

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

(g_x,g_y) へ辿り着くには最小で何回の移動が必要か出力せよ。
ただし、辿り着けないならば -1 と出力せよ。


入力例 1

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

出力例 1

4

図は、(s_x,s_y)S で、(g_x,g_y)G で表しています。
(3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6) と移動すると、4 回の移動で (g_x,g_y) に辿り着くことができます。


入力例 2

4 6 2
3 2
3 5
4 5
2 5

出力例 2

-1

(g_x,g_y) で停止する必要があります。
通過しただけでは (g_x,g_y) へ辿り着いたとみなされないことに注意してください。


入力例 3

1 10 1
1 5
1 1
1 7

出力例 3

-1


左を選んで進むと、高橋君は (g_x,g_y) を通過したのちに崖に落ちてしまいます。
スケート場の周りは崖になっており、障害物に当たらないような移動は禁止されていることに注意してください。

Score : 500 points

Problem Statement

There is a skating rink represented by 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 j-th column from the left.

The skating rink has N obstacles. The i-th obstacle is placed at (X_i,Y_i).

In a single move, Takahashi chooses one of the four directions, up, down, left, or right, and keeps moving until he hits an obstacle.
When he hits an obstacle, he stops at the square right before the obstacle. Since the skating rink is surrounded by cliffs, it is prohibited to start a move in which he will never hit an obstacle.

Takahashi is initially at (s_x,s_y). He wants to make some number of moves to stop at (g_x,g_y).

Find the minimum number of moves required to end up at (g_x, g_y). If it is not possible, report the fact.

Constraints

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • If i\neq j, then (X_i,Y_i)\neq (X_j,Y_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the minimum number of moves required to end up at (g_x,g_y).
If it is impossible to end up there, print -1.


Sample Input 1

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

Sample Output 1

4

In the figure, (s_x,s_y) is denoted by S and (g_x,g_y) is denoted by G.
By moving as (3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6), he can end up at (g_x,g_y) with 4 moves.


Sample Input 2

4 6 2
3 2
3 5
4 5
2 5

Sample Output 2

-1

He must stop at (g_x,g_y).
Note that just passing through (g_x,g_y) is not considered to be ending up at the goal.


Sample Input 3

1 10 1
1 5
1 1
1 7

Sample Output 3

-1


If he chooses to move to the left, Takahashi will fall down the cliff after passing through (g_x,g_y).
Note that it is prohibited to start a move in which he will never hit an obstacle, as the skating rink is surrounded by cliffs.