Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
1 文字目が数字、2 文字目が文字 x、3 文字目が数字であるような 3 文字の文字列 S が与えられます。
S の 2 つの数の積を求めてください。
制約
- S は 1 文字目が 1 以上 9 以下の整数、2 文字目が文字
x、3 文字目が 1 以上 9 以下の整数であるような 3 文字の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
3x8
出力例 1
24
3 \times 8 = 24 より、24 を出力します。
入力例 2
9x9
出力例 2
81
9 \times 9 = 81 より、81 を出力します。
Score : 100 points
Problem Statement
You are given a 3-character string S, where the first character is a digit, the second character is the character x, and the third character is a digit.
Find the product of the two numbers in S.
Constraints
- S is a 3-character string where the first character is an integer between 1 and 9, inclusive, the second character is the character
x, and the third character is an integer between 1 and 9, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer as an integer.
Sample Input 1
3x8
Sample Output 1
24
From 3 \times 8 = 24, print 24.
Sample Input 2
9x9
Sample Output 2
81
From 9 \times 9 = 81, print 81.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
地上 x メートルの高さから見える水平線は \sqrt{x(12800000+x)} メートル先にあるとするとき、 地上 H メートルの高さから見える水平線が何メートル先にあるか求めてください。
制約
- 1 \leq H \leq 10^5
- H は整数である
入力
入力は以下の形式で標準入力から与えられる。
H
出力
答えを出力せよ。
なお、想定解との絶対誤差または相対誤差が 10^{-6} 以下であれば、正解として扱われる。
入力例 1
333
出力例 1
65287.907678222
\sqrt{333(12800000+333)} = 65287.9076782\ldots です。65287.91 などの出力でも正解となります。
入力例 2
634
出力例 2
90086.635834623
\sqrt{634(12800000+634)} = 90086.6358346\ldots です。
Score : 100 points
Problem Statement
Assuming that the horizon seen from a place x meters above the ground is \sqrt{x(12800000+x)} meters away, find how many meters away the horizon seen from a place H meters above the ground is.
Constraints
- 1 \leq H \leq 10^5
- H is an integer.
Input
Input is given from Standard Input in the following format:
H
Output
Print the answer.
Your answer will be considered correct when the absolute or relative error from the judge's answer is at most 10^{-6}.
Sample Input 1
333
Sample Output 1
65287.907678222
We have \sqrt{333(12800000+333)} = 65287.9076782\ldots. Outputs such as 65287.91 would also be accepted.
Sample Input 2
634
Sample Output 2
90086.635834623
We have \sqrt{634(12800000+634)} = 90086.6358346\ldots.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
アイドルグループ Bit♡Beat では、新曲のカップリングとしてメンバーの中から 2 人のユニット(選抜)を組むことになりました。
グループには N 人のメンバーがおり、それぞれのメンバーにスキル値が定まっています。 i 人目のメンバー (1\le i\le N) のスキル値は A_i です。
プロデューサーであるあなたは、スキル値の合計が ちょうど S になるような 2 人のメンバーの選び方を探しています。
スキル値の合計がちょうど S になるような 2 人のメンバーの選び方が何通りあるか求めてください。
制約
- 2\le N\le 5\times 10^5
- 1\le S \le 10^9
- 1\le A_i\le 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N S A_1 A_2 \ldots A_N
出力
スキル値の合計がちょうど S になるような 2 人のメンバーの選び方が何通りあるか出力せよ。
入力例 1
5 11 5 6 9 2 2
出力例 1
3
1 人目と 2 人目がユニットを組むと、スキル値の合計は 5+6=11 となります。
このほかにも、 3 人目と 4 人目、3 人目と 5 人目がユニットを組んでも条件を満たします。
条件を満たす 2 人のメンバーの選び方は 3 通りであるため、 3 を出力してください。
入力例 2
10 1000000000 1 1 1 1 1 1 1 1 1 1
出力例 2
0
条件を満たす 2 人のメンバーの選び方が存在しない場合もあります。
入力例 3
6 14 7 7 7 7 7 7
出力例 3
15
入力例 4
15 8 3 4 10 9 1 1 1 7 8 9 9 3 8 9 7
出力例 4
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
実数 X が小数点以下第 3 位まで与えられます。
実数 X を以下の条件を満たすように出力してください。
- 小数点以下の部分について、末尾に
0を付けない - 末尾に過剰な小数点を付けない
制約
- 0 \le X < 100
- X は小数点以下第 3 位まで与えられる
入力
入力は以下の形式で標準入力から与えられる。
X
出力
答えを出力せよ。
入力例 1
1.012
出力例 1
1.012
1.012 はそのまま出力しても構いません。
入力例 2
12.340
出力例 2
12.34
12.340 を末尾に 0 を付けずに出力すると 12.34 となります。
入力例 3
99.900
出力例 3
99.9
99.900 を末尾に 0 を付けずに出力すると 99.9 となります。
入力例 4
0.000
出力例 4
0
0.000 を末尾に 0 や過剰な小数点を付けずに出力すると 0 となります。
Score : 150 points
Problem Statement
A real number X is given to the third decimal place.
Print the real number X under the following conditions.
- The decimal part must not have trailing
0s. - There must not be an unnecessary trailing decimal point.
Constraints
- 0 \le X < 100
- X is given to the third decimal place.
Input
The input is given from Standard Input in the following format:
X
Output
Output the answer.
Sample Input 1
1.012
Sample Output 1
1.012
1.012 can be printed as it is.
Sample Input 2
12.340
Sample Output 2
12.34
Printing 12.340 without the trailing 0 results in 12.34.
Sample Input 3
99.900
Sample Output 3
99.9
Printing 99.900 without the trailing 0s results in 99.9.
Sample Input 4
0.000
Sample Output 4
0
Printing 0.000 without trailing 0s or an unnecessary decimal point results in 0.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
AtCoder 王国では、これから N 日間のお祭りが開催されます。そのうち、A_1 日目、A_2 日目、\dots、A_M 日目の M 日では花火が上がります。ここで、お祭りの最終日には花火が上がることが保証されます。(つまり、A_M=N が保証されます。)
i=1,2,\dots,N に対して、以下の問題を解いてください。
- i 日目以降で初めて花火が上がるのは、i 日目から数えて何日後か?ただし、i 日目に花火が上がる場合 0 日後とする。
制約
- 1 \le M \le N \le 2 \times 10^5
- 1 \le A_1 < A_2 < \dots < A_M = N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_M
出力
N 行出力せよ。
i(1 \le i \le N) 行目には、i 日目以降で初めて花火が上がるのは、i 日目から数えて何日後かを整数として出力せよ。
入力例 1
3 2 2 3
出力例 1
1 0 0
AtCoder 王国ではお祭りを 3 日間開催し、2,3 日目に花火が上がります。
- 1 日目以降で初めて花火が上がるのは 2 日目なので、1 日目から数えて 1 日後です。
- 2 日目以降で初めて花火が上がるのは 2 日目なので、2 日目から数えて 0 日後です。
- 3 日目以降で初めて花火が上がるのは 3 日目なので、3 日目から数えて 0 日後です。
入力例 2
8 5 1 3 4 7 8
出力例 2
0 1 0 0 2 1 0 0
Score : 250 points
Problem Statement
The AtCoder Kingdom holds a festival for N days. On M of these days, namely on the A_1-th, A_2-th, \dots, A_M-th days, fireworks will be launched. It is guaranteed that fireworks will be launched on the last day of the festival. (In other words, A_M=N is guaranteed.)
For each i=1,2,\dots,N, solve the following problem.
- How many days later from the i-th day will fireworks be launched for the first time on or after the i-th day? If fireworks are launched on the i-th day, it is considered to be 0 days later.
Constraints
- 1 \le M \le N \le 2 \times 10^5
- 1 \le A_1 < A_2 < \dots < A_M = N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_M
Output
Print N lines.
The i-th line (1 \le i \le N) should contain an integer representing the number of days from the i-th day until fireworks are launched for the first time on or after the i-th day.
Sample Input 1
3 2 2 3
Sample Output 1
1 0 0
The kingdom holds a festival for 3 days, and fireworks are launched on the 2-nd and 3-rd days.
- From the 1-st day, the first time fireworks are launched is the 2-nd day of the festival, which is 1 day later.
- From the 2-nd day, the first time fireworks are launched is the 2-nd day of the festival, which is 0 days later.
- From the 3-rd day, the first time fireworks are launched is the 3-rd day of the festival, which is 0 days later.
Sample Input 2
8 5 1 3 4 7 8
Sample Output 2
0 1 0 0 2 1 0 0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は風船で空を飛ぶことにしました。高橋君は時刻 0 (単位は秒) の時点で高度 H にいて、これから 10^9 秒間飛行します。
高橋君は自身が飛んでいる高度を 1 秒あたり 1 まで変化させることが出来ます。ただし、高度を 0 以下にすることはできません。
- 言い換えると、高橋君の時刻 t における高度を F(t) とした時、F(t) は以下の条件を全て満たします。
- F(0) = H
- 0 \leq t \lt u \leq 10^9 において -1 \leq \dfrac{F(u) - F(t)}{u - t} \leq 1
- 0 \leq t \leq 10^9 において F(t) \gt 0
N 個の高度に関する目標があります。i 個目の目標は時刻 t_i 時点での高度を l_i 以上 u_i 以下にすることです。
高橋君は目標を全て達成するような飛行が可能ですか?
T 個のテストケースが与えられるので、それぞれに対して答えを求めてください。
制約
- 1 \leq T \leq 10^5
- 1 \leq N \leq 10^5
- 1 \leq H \leq 10^9
- 1 \leq t_1 \lt t_2 \lt \dots \lt t_N \leq 10^9
- 1 \leq l_i \leq u_i \leq 10^9
- 全てのテストケースにおける N の総和は 10^5 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_i は i 番目のテストケースを意味する。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N H t_1 l_1 u_1 t_2 l_2 u_2 \vdots t_N l_N u_N
出力
T 行出力せよ。i 行目には、i 番目のテストケースにおいて目標を全て達成するような飛行が可能である場合 Yes を、そうでない場合 No を出力せよ。
入力例 1
3 2 5 3 1 4 8 9 11 2 6 1 1 4 3 5 8 10 36 27 37 38 30 34 54 38 20 77 45 1 36 49 38 51 52 31 58 65 43 60 71 14 42 73 36 38 85 14 29
出力例 1
Yes No Yes
1 番目のテストケースについて、高橋君は以下の方法で飛行することで全ての目標を達成することが出来ます。
- 時刻 0 の時点で高橋君は高度 5 にいる。
- 時刻 0 から時刻 2 にかけて高橋君は秒速 0.5 で高度を下げる。
- 時刻 2 の時点で高橋君は高度 5 - 0.5 \times 2 = 4 にいる。
- 時刻 2 から時刻 3 の間、高橋君は同じ高度にとどまる。
- 時刻 3 の時点で高橋君は高度 4 にいる。高橋君は 1 番目の目標を満たす。
- 時刻 3 から時刻 8 にかけて高橋君は秒速 1 で高度を上げる。
- 時刻 8 の時点で高橋君は高度 4 + 1 \times 5 = 9 にいる。高橋君は 2 番目の目標を満たす。
2 番目のテストケースについて、高橋君はどのように飛行しても目標を全て達成することは出来ません。
Score : 300 points
Problem Statement
Takahashi has decided to fly in the sky with balloons. Takahashi is at altitude H at time 0 (the unit is seconds), and will fly for 10^9 seconds from now.
Takahashi can change his flying altitude by up to 1 per second. However, he cannot make his altitude 0 or less.
- In other words, if F(t) denotes Takahashi's altitude at time t, then F(t) satisfies all of the following conditions:
- F(0) = H
- -1 \leq \dfrac{F(u) - F(t)}{u - t} \leq 1 for 0 \leq t \lt u \leq 10^9.
- F(t) \gt 0 for 0 \leq t \leq 10^9.
There are N goals regarding altitude. The i-th goal is to make the altitude at time t_i at least l_i and at most u_i.
Is it possible for Takahashi to fly in a way that achieves all goals?
You are given T test cases; solve each of them.
Constraints
- 1 \leq T \leq 10^5
- 1 \leq N \leq 10^5
- 1 \leq H \leq 10^9
- 1 \leq t_1 \lt t_2 \lt \dots \lt t_N \leq 10^9
- 1 \leq l_i \leq u_i \leq 10^9
- The sum of N over all test cases is at most 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case.
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N H t_1 l_1 u_1 t_2 l_2 u_2 \vdots t_N l_N u_N
Output
Output T lines. The i-th line should contain Yes if it is possible to fly in a way that achieves all goals in the i-th test case, and No otherwise.
Sample Input 1
3 2 5 3 1 4 8 9 11 2 6 1 1 4 3 5 8 10 36 27 37 38 30 34 54 38 20 77 45 1 36 49 38 51 52 31 58 65 43 60 71 14 42 73 36 38 85 14 29
Sample Output 1
Yes No Yes
For the first test case, Takahashi can achieve all goals by flying as follows:
- At time 0, he is at altitude 5.
- From time 0 to time 2, he descends at a rate of 0.5 per second.
- At time 2, he is at altitude 5 - 0.5 \times 2 = 4.
- From time 2 to time 3, he stays at the same altitude.
- At time 3, he is at altitude 4. He satisfies the first goal.
- From time 3 to time 8, he ascends at a rate of 1 per second.
- At time 8, he is at altitude 4 + 1 \times 5 = 9. He satisfies the second goal.
For the second test case, he cannot achieve all goals no matter how he flies.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
AtCoder 国は無限に広がる直交座標の上にあります。
AtCoder 国には N 個の街があり、 1,2,\dots,N と番号が付けられています。街 i は地点 (x_i, y_i) にあり、2 つの異なる番号の街が同じ座標にあることはありません。
AtCoder 国には転移魔法(以下、魔法と表記)があります。魔法は整数の組 (a,b) によって識別されていて、地点 (x,y) にいるときに魔法 (a,b) を使うと (x+a,y+b) にワープすることができます。
すぬけ君は、任意の整数の組 (a,b) を選んで魔法 (a,b) を覚えることができる大魔術師です。また、すぬけ君は何種類でも魔法を覚えることができます。
魔法を使って街と街の間を移動したくなったすぬけ君は、全ての相異なる街の組 (i,j) について次の行動を取れるようにいくつかの魔法を覚えることにしました。
- 覚えた魔法のうち 1 種類の魔法のみ を選ぶ。その後、選んだ魔法 のみ を繰り返し使って街 i から 街 j に移動する。
すぬけ君が上の条件を満たすように魔法を覚えるとき、少なくとも何種類の魔法を覚えればよいですか?
制約
- 2 \leq N \leq 500
- 0 \leq x_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq y_i \leq 10^9 (1 \leq i \leq N)
- i \neq j ならば (x_i, y_i) \neq (x_j, y_j) である。
入力
入力は以下の形式で標準入力から与えられる。
N x_1 y_1 x_2 y_2 \vdots x_N y_N
出力
すぬけ君が覚える必要がある魔法の個数の最小値を出力せよ。
入力例 1
3 1 2 3 6 7 4
出力例 1
6
AtCoder 国の街の位置を図示したのが下の図です。(わかりやすさのために四隅の座標を表示しています。)

すぬけ君は次の 6 種類の魔法を覚えれば、すべての (i,j) (i \neq j) の組に対して街 i から 1 種類の魔法を 1 回使うことで街 j に着くことができるので条件を満たします。
- (2, 4)
- (-2, -4)
- (4, -2)
- (-4, 2)
- (-6, -2)
- (6, 2)
次の 6 種類の魔法も条件を満たします。このときすぬけ君は、すべての (i,j) (i \neq j) の組に対して街 i から 1 種類の魔法を 2 回使うことで街 j に着くことができます。
- (1, 2)
- (-1, -2)
- (2, -1)
- (-2, 1)
- (-3, -1)
- (3, 1)
条件を満たす魔法の組み合わせのうち 6 種類より少ないものは存在しないので、 6 を出力します。
入力例 2
3 1 2 2 2 4 2
出力例 2
2
次の 2 種類の魔法を覚えるのが最適です。
- (1, 0)
- (-1, 0)
入力例 3
4 0 0 0 1000000000 1000000000 0 1000000000 1000000000
出力例 3
8
Score : 400 points
Problem Statement
The Republic of AtCoder lies on a Cartesian coordinate plane.
It has N towns, numbered 1,2,\dots,N. Town i is at (x_i, y_i), and no two different towns are at the same coordinates.
There are teleportation spells in the nation. A spell is identified by a pair of integers (a,b), and casting a spell (a, b) at coordinates (x, y) teleports you to (x+a, y+b).
Snuke is a great magician who can learn the spell (a, b) for any pair of integers (a, b) of his choice. The number of spells he can learn is also unlimited.
To be able to travel between the towns using spells, he has decided to learn some number of spells so that it is possible to do the following for every pair of different towns (i, j).
- Choose just one spell among the spells learned. Then, repeatedly use just the chosen spell to get from Town i to Town j.
At least how many spells does Snuke need to learn to achieve the objective above?
Constraints
- 2 \leq N \leq 500
- 0 \leq x_i \leq 10^9 (1 \leq i \leq N)
- 0 \leq y_i \leq 10^9 (1 \leq i \leq N)
- (x_i, y_i) \neq (x_j, y_j) if i \neq j.
Input
Input is given from Standard Input in the following format:
N x_1 y_1 x_2 y_2 \vdots x_N y_N
Output
Print the minimum number of spells Snuke needs to learn.
Sample Input 1
3 1 2 3 6 7 4
Sample Output 1
6
The figure below illustrates the positions of the towns (along with the coordinates of the four corners).

If Snuke learns the six spells below, he can get from Town i to Town j by using one of the spells once for every pair (i,j) (i \neq j), achieving his objective.
- (2, 4)
- (-2, -4)
- (4, -2)
- (-4, 2)
- (-6, -2)
- (6, 2)
Another option is to learn the six spells below. In this case, he can get from Town i to Town j by using one of the spells twice for every pair (i,j) (i \neq j), achieving his objective.
- (1, 2)
- (-1, -2)
- (2, -1)
- (-2, 1)
- (-3, -1)
- (3, 1)
There is no combination of spells that consists of less than six spells and achieve the objective, so we should print 6.
Sample Input 2
3 1 2 2 2 4 2
Sample Output 2
2
The optimal choice is to learn the two spells below:
- (1, 0)
- (-1, 0)
Sample Input 3
4 0 0 0 1000000000 1000000000 0 1000000000 1000000000
Sample Output 3
8
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
英小文字からなる文字列が N 個与えられます。i \, (i = 1, 2, \dots, N) 番目のものを S_i と表します。
二つの文字列 x, y に対し、以下の条件を全て満たす最大の整数 n を \mathrm{LCP}(x, y) と表します。
- x, y の長さはいずれも n 以上
- 1 以上 n 以下の全ての整数 i に対し、x の i 文字目と y の i 文字目が等しい
全ての i = 1, 2, \dots, N に対し、以下の値を求めてください。
- \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j)
制約
- 2 \leq N \leq 5 \times 10^5
- N は整数
- S_i は英小文字からなる長さ 1 以上の文字列 (i = 1, 2, \dots, N)
- S_i の長さの総和は 5 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
N 行出力せよ。i \, (i = 1, 2, \dots, N) 行目には、\displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j) を出力せよ。
入力例 1
3 abc abb aac
出力例 1
2 2 1
\mathrm{LCP}(S_1, S_2) = 2, \mathrm{LCP}(S_1, S_3) = 1, \mathrm{LCP}(S_2, S_3) = 1 です。
入力例 2
11 abracadabra bracadabra racadabra acadabra cadabra adabra dabra abra bra ra a
出力例 2
4 3 2 1 0 1 0 4 3 2 1
Score : 500 points
Problem Statement
You are given N strings consisting of lowercase English letters. Let S_i be the i-th (i = 1, 2, \dots, N) of them.
For two strings x and y, \mathrm{LCP}(x, y) is defined to be the maximum integer n that satisfies all of the following conditions:
- The lengths of x and y are both at least n.
- For all integers i between 1 and n, inclusive, the i-th character of x and that of y are equal.
Find the following value for all i = 1, 2, \dots, N:
- \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j)
Constraints
- 2 \leq N \leq 5 \times 10^5
- N is an integer.
- S_i is a string of length at least 1 consisting of lowercase English letters (i = 1, 2, \dots, N).
- The sum of lengths of S_i is at most 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print N lines. The i-th (i = 1, 2, \dots, N) line should contain \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j).
Sample Input 1
3 abc abb aac
Sample Output 1
2 2 1
\mathrm{LCP}(S_1, S_2) = 2, \mathrm{LCP}(S_1, S_3) = 1, and \mathrm{LCP}(S_2, S_3) = 1.
Sample Input 2
11 abracadabra bracadabra racadabra acadabra cadabra adabra dabra abra bra ra a
Sample Output 2
4 3 2 1 0 1 0 4 3 2 1
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
縦 N 行横 N 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と表します。
高橋君は最初マス (1,1) におり、所持金は 0 です。
高橋君はマス (i,j) にいるとき、1 回の行動で以下のいずれかを行うことができます。
- 同じマスにとどまり、所持金を P_{i,j} 増やす。
- 所持金から R_{i,j} 払ってマス (i,j+1) に移動する。
- 所持金から D_{i,j} 払ってマス (i+1,j) に移動する。
所持金が負になる移動、グリッドの外に出る移動はできません。
高橋君が最適に行動したとき、何回の行動でマス (N,N) にたどり着くことができますか。
制約
- 2 \leq N \leq 80
- 1 \leq P_{i,j} \leq 10^9
- 1 \leq R_{i,j},D_{i,j} \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N
P_{1,1} \ldots P_{1,N}
\vdots
P_{N,1} \ldots P_{N,N}
R_{1,1} \ldots R_{1,N-1}
\vdots
R_{N,1} \ldots R_{N,N-1}
D_{1,1} \ldots D_{1,N}
\vdots
D_{N-1,1} \ldots D_{N-1,N}
出力
答えを出力せよ。
入力例 1
3 1 2 3 3 1 2 2 1 1 1 2 4 3 4 2 1 5 7 5 3 3
出力例 1
8

以下のようにして 8 回の行動でマス (3,3) にたどり着くことができます。
- マス (1,1) にとどまり、所持金を 1 増やす。所持金は 1 になる。
- 所持金から 1 払ってマス (2,1) に移動する。所持金は 0 になる。
- マス (2,1) にとどまり、所持金を 3 増やす。所持金は 3 になる。
- マス (2,1) にとどまり、所持金を 3 増やす。所持金は 6 になる。
- マス (2,1) にとどまり、所持金を 3 増やす。所持金は 9 になる。
- 所持金から 4 払ってマス (2,2) に移動する。所持金は 5 になる。
- 所持金から 3 払ってマス (3,2) に移動する。所持金は 2 になる。
- 所持金から 2 払ってマス (3,3) に移動する。所持金は 0 になる。
入力例 2
3 1 1 1 1 1 1 1 1 1 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 2
4000000004
Score: 550 points
Problem Statement
There is a grid with N rows and N columns. Let (i,j) denote the square at the i-th row from the top and j-th column from the left.
Takahashi is initially at square (1,1) with zero money.
When Takahashi is at square (i,j), he can perform one of the following in one action:
- Stay at the same square and increase his money by P_{i,j}.
- Pay R_{i,j} from his money and move to square (i,j+1).
- Pay D_{i,j} from his money and move to square (i+1,j).
He cannot make a move that would make his money negative or take him outside the grid.
If Takahashi acts optimally, how many actions does he need to reach square (N,N)?
Constraints
- 2 \leq N \leq 80
- 1 \leq P_{i,j} \leq 10^9
- 1 \leq R_{i,j},D_{i,j} \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
P_{1,1} \ldots P_{1,N}
\vdots
P_{N,1} \ldots P_{N,N}
R_{1,1} \ldots R_{1,N-1}
\vdots
R_{N,1} \ldots R_{N,N-1}
D_{1,1} \ldots D_{1,N}
\vdots
D_{N-1,1} \ldots D_{N-1,N}
Output
Print the answer.
Sample Input 1
3 1 2 3 3 1 2 2 1 1 1 2 4 3 4 2 1 5 7 5 3 3
Sample Output 1
8

It is possible to reach square (3,3) in eight actions as follows:
- Stay at square (1,1) and increase money by 1. His money is now 1.
- Pay 1 money and move to square (2,1). His money is now 0.
- Stay at square (2,1) and increase money by 3. His money is now 3.
- Stay at square (2,1) and increase money by 3. His money is now 6.
- Stay at square (2,1) and increase money by 3. His money is now 9.
- Pay 4 money and move to square (2,2). His money is now 5.
- Pay 3 money and move to square (3,2). His money is now 2.
- Pay 2 money and move to square (3,3). His money is now 0.
Sample Input 2
3 1 1 1 1 1 1 1 1 1 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 2
4000000004