実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
AtCoder 村には N 本の橋があり、i 本目( i は 1 以上 N 以下の整数)の橋の高さは H_i です。
ここで、AtCoder 村にある N 本の橋のうち、どの相異なる 2 本の橋も高さが異なります。
AtCoder 村で最も高い橋は何本目の橋か出力してください。
制約
- 1\leq N \leq 100
- 1\leq H_i \leq 10^9
- H_i はすべて異なる
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H_1 H_2 \ldots H_N
出力
AtCoder 村で最も高い橋は何本目の橋かを、整数で出力せよ。
入力例 1
3 50 80 70
出力例 1
2
AtCoder 村には 3 本の橋があります。
1,2,3 本目の橋の高さはそれぞれ, 50,80,70 であり、
最も高い橋は 2 本目の橋です。
よって、2 を出力します。
入力例 2
1 1000000000
出力例 2
1
AtCoder 村に橋が 1 本しか存在しないため、2 本目以降の橋は存在せず、最も高い橋は 1 本目の橋となります。
入力例 3
10 22 75 26 45 72 81 47 29 97 2
出力例 3
9
AtCoder 村には 10 本の橋があり、それらのうち最も高い橋は 9 番目の橋(高さは 97 )です。
Score : 100 points
Problem Statement
There are N bridges in AtCoder Village. The height of the bridge numbered i is H_i (i is an integer between 1 and N).
Every two different bridges in the village have different heights.
Print the number representing the highest bridge in the village.
Constraints
- 1\leq N \leq 100
- 1\leq H_i \leq 10^9
- All H_i are different.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N H_1 H_2 \ldots H_N
Output
Print the integer representing the highest bridge in AtCoder village.
Sample Input 1
3 50 80 70
Sample Output 1
2
The village has three bridges.
The first, second, and third bridges have heights of 50, 80, and 70, respectively,
so the second bridge is the highest.
Thus, 2 should be printed.
Sample Input 2
1 1000000000
Sample Output 2
1
The village has only one bridge, so the first bridge is the highest.
Sample Input 3
10 22 75 26 45 72 81 47 29 97 2
Sample Output 3
9
The village has ten bridges, and the ninth bridge (with a height of 97) is the highest.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
非負整数 x に対し定義される関数 f(x) は以下の条件を満たします。
- f(0) = 1
- 任意の正整数 k に対し f(k) = k \times f(k-1)
このとき、 f(N) を求めてください。
制約
- N は 0 \le N \le 10 を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
2
出力例 1
2
f(2) = 2 \times f(1) = 2 \times 1 \times f(0) = 2 \times 1 \times 1 = 2 です。
入力例 2
3
出力例 2
6
f(3) = 3 \times f(2) = 3 \times 2 = 6 です。
入力例 3
0
出力例 3
1
入力例 4
10
出力例 4
3628800
Score : 100 points
Problem Statement
A function f(x) defined for non-negative integer x satisfies the following conditions:
- f(0) = 1;
- f(k) = k \times f(k-1) for all positive integers k.
Find f(N).
Constraints
- N is an integer such that 0 \le N \le 10.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
2
Sample Output 1
2
We have f(2) = 2 \times f(1) = 2 \times 1 \times f(0) = 2 \times 1 \times 1 = 2.
Sample Input 2
3
Sample Output 2
6
We have f(3) = 3 \times f(2) = 3 \times 2 = 6.
Sample Input 3
0
Sample Output 3
1
Sample Input 4
10
Sample Output 4
3628800
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
H 行 W 列のグリッドがあり、はじめすべてのマスが白で塗られています。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。
このグリッドはトーラス状であるとみなします。すなわち、各 1 \leq i \leq H に対して (i, W) の右に (i, 1) があり、各 1 \leq j \leq W に対して (H, j) の下に (1, j) があるとします。
高橋君が (1, 1) にいて上を向いています。高橋君が以下の操作を N 回繰り返した後のグリッドの各マスがどの色で塗られているか出力してください。
- 現在いるマスが白で塗られている場合は、現在いるマスを黒に塗り替え、時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。そうでない場合は、現在いるマスを白に塗り替え、反時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。
制約
- 1 \leq H, W \leq 100
- 1 \leq N \leq 1000
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W N
出力
H 行出力せよ。i 行目には長さ W の文字列であって、(i, j) が白で塗られている場合は j 文字目が .、黒で塗られている場合は j 文字目が # であるものを出力せよ。
入力例 1
3 4 5
出力例 1
.#.. ##.. ....
グリッドの各マスは操作によって以下のように変化します。
.... #... ##.. ##.. ##.. .#.. .... → .... → .... → .#.. → ##.. → ##.. .... .... .... .... .... ....
入力例 2
2 2 1000
出力例 2
.. ..
入力例 3
10 10 10
出力例 3
##........ ##........ .......... .......... .......... .......... .......... .......... .......... #........#
Score: 250 points
Problem Statement
There is a grid with H rows and W columns; initially, all cells are painted white. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.
This grid is considered to be toroidal. That is, (i, 1) is to the right of (i, W) for each 1 \leq i \leq H, and (1, j) is below (H, j) for each 1 \leq j \leq W.
Takahashi is at (1, 1) and facing upwards. Print the color of each cell in the grid after Takahashi repeats the following operation N times.
- If the current cell is painted white, repaint it black, rotate 90^\circ clockwise, and move forward one cell in the direction he is facing. Otherwise, repaint the current cell white, rotate 90^\circ counterclockwise, and move forward one cell in the direction he is facing.
Constraints
- 1 \leq H, W \leq 100
- 1 \leq N \leq 1000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W N
Output
Print H lines. The i-th line should contain a string of length W where the j-th character is . if the cell (i, j) is painted white, and # if it is painted black.
Sample Input 1
3 4 5
Sample Output 1
.#.. ##.. ....
The cells of the grid change as follows due to the operations:
.... #... ##.. ##.. ##.. .#.. .... → .... → .... → .#.. → ##.. → ##.. .... .... .... .... .... ....
Sample Input 2
2 2 1000
Sample Output 2
.. ..
Sample Input 3
10 10 10
Sample Output 3
##........ ##........ .......... .......... .......... .......... .......... .......... .......... #........#
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
AtCoder 国の暦では、一年は 1,2,\dots,M 番目の月の M か月からなり、そのうち i 番目の月は 1,2,\dots,D_i 番目の日の D_i 日からなります。
さらに、 AtCoder 国の一年の日数は奇数、即ち D_1+D_2+\dots+D_M は奇数です。
一年の真ん中の日は何番目の月の何番目の日か求めてください。
言い換えると、 1 番目の月の 1 番目の日を 1 日目としたときの (D_1+D_2+\dots+D_M+1)/2 日目が何番目の月の何番目の日かを求めてください。
制約
- 入力は全て整数
- 1 \le M \le 100
- 1 \le D_i \le 100
- D_1 + D_2 + \dots + D_M は奇数
入力
入力は以下の形式で標準入力から与えられる。
M D_1 D_2 \dots D_M
出力
答えが a 番目の月の b 番目の日であるとき、以下の形式で出力せよ。
a b
入力例 1
12 31 28 31 30 31 30 31 31 30 31 30 31
出力例 1
7 2
この入力では、 1 年は 31+28+31+30+31+30+31+31+30+31+30+31=365 日からなります。
真ん中の日は (365+1)/2 = 183 日目であり、これを求めることを考えます。
- 1,2,3,4,5,6 番目の月に含まれる日数の合計は 181 日です。
- 7 番目の月の 1 番目の日は 182 日目です。
- 7 番目の月の 2 番目の日は 183 日目です。
以上から、答えが 7 番目の月の 2 番目の日であることが分かります。
入力例 2
1 1
出力例 2
1 1
入力例 3
6 3 1 4 1 5 9
出力例 3
5 3
Score : 200 points
Problem Statement
In the calendar of AtCoderLand, a year consists of M months: month 1, month 2, \dots, month M. The i-th month consists of D_i days: day 1, day 2, \dots, day D_i.
Furthermore, the number of days in a year is odd, that is, D_1+D_2+\dots+D_M is odd.
Find what day of what month is the middle day of the year.
In other words, let day 1 of month 1 be the first day, and find a and b such that the ((D_1+D_2+\dots+D_M+1)/2)-th day is day b of month a.
Constraints
- All input values are integers.
- 1 \le M \le 100
- 1 \le D_i \le 100
- D_1 + D_2 + \dots + D_M is odd.
Input
The input is given from Standard Input in the following format:
M D_1 D_2 \dots D_M
Output
Let the answer be day b of month a, and print it in the following format:
a b
Sample Input 1
12 31 28 31 30 31 30 31 31 30 31 30 31
Sample Output 1
7 2
In this input, a year consists of 31+28+31+30+31+30+31+31+30+31+30+31=365 days.
Let us find the middle day, which is the ((365+1)/2 = 183)-th day.
- Months 1,2,3,4,5,6 contain a total of 181 days.
- Day 1 of month 7 is the 182-th day.
- Day 2 of month 7 is the 183-th day.
Thus, the answer is day 2 of month 7.
Sample Input 2
1 1
Sample Output 2
1 1
Sample Input 3
6 3 1 4 1 5 9
Sample Output 3
5 3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字、,、" からなる長さ N の文字列 S が与えられます。S に含まれる " の個数は偶数であることが保証されています。
S に含まれる " の個数を 2K 個とすると、各 i=1,2,\ldots,K について 2i-1 番目の " から 2i 番目の " までの文字のことを 括られた文字 と呼びます。
あなたの仕事は、 S に含まれる , のうち、括られた文字 でないもの を . で置き換えて得られる文字列を答えることです。
制約
- N は 1 以上 2\times 10^5 以下の整数
- S は英小文字、
,、"からなる長さ N の文字列 - S に含まれる
"の個数は偶数
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
8 "a,b"c,d
出力例 1
"a,b"c.d
S のうち "a,b" が括られた文字であり、c,d は括られた文字ではありません。
S に含まれる , のうち、括られた文字でないのは S の左から 7 番目の文字なので、7 番目の文字を . で置き換えたものが答えとなります。
入力例 2
5 ,,,,,
出力例 2
.....
入力例 3
20 a,"t,"c,"o,"d,"e,"r,
出力例 3
a."t,"c."o,"d."e,"r.
Score : 300 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters, ,, and ". It is guaranteed that S contains an even number of ".
Let 2K be the number of " in S. For each i=1,2,\ldots,K, the characters from the (2i-1)-th " through the (2i)-th " are said to be enclosed.
Your task is to replace each , in S that is not an enclosed character with . and print the resulting string.
Constraints
- N is an integer between 1 and 2\times 10^5, inclusive.
- S is a string of length N consisting of lowercase English letters,
,, and". - S contains an even number of
".
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer.
Sample Input 1
8 "a,b"c,d
Sample Output 1
"a,b"c.d
In S, "a,b" are enclosed characters, and c,d are not.
The , in S that is not an enclosed character is the seventh character from the left in S, so replace that character with . to get the answer.
Sample Input 2
5 ,,,,,
Sample Output 2
.....
Sample Input 3
20 a,"t,"c,"o,"d,"e,"r,
Sample Output 3
a."t,"c."o,"d."e,"r.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
6 個の整数 h_1, h_2, h_3, w_1, w_2, w_3 が与えられます。
縦横 3 \times 3 のマス目に、以下の条件をすべて満たすように各マスに正の整数を 1 つずつ書きこむことを考えます。
- i=1,2,3 について、上から i 行目に書きこんだ数の和が h_i になる。
- j=1,2,3 について、左から j 列目に書きこんだ数の和が w_j になる。
例えば (h_1, h_2, h_3) = (5, 13, 10), (w_1, w_2, w_3) = (6, 13, 9) のとき、以下の 3 通りの書きこみ方はすべて条件を満たしています。(条件を満たす書きこみ方は他にもあります)

さて、条件を満たす書きこみ方は全部で何通り存在しますか?
制約
- 3 \leq h_1, h_2, h_3, w_1, w_2, w_3 \leq 30
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
h_1 h_2 h_3 w_1 w_2 w_3
出力
条件を満たす書きこみ方が何通りあるかを出力せよ。
入力例 1
3 4 6 3 3 7
出力例 1
1
条件を満たす数の書きこみ方は次の 1 通りのみです。よって 1 を出力します。

入力例 2
3 4 5 6 7 8
出力例 2
0
条件を満たす書きこみ方が存在しないこともあります。
入力例 3
5 13 10 6 13 9
出力例 3
120
入力例 4
20 25 30 22 29 24
出力例 4
30613
Score : 300 points
Problem Statement
You are given six integers: h_1, h_2, h_3, w_1, w_2, and w_3.
Consider writing a positive integer on each square of a 3 \times 3 grid so that all of the following conditions are satisfied:
- For i=1,2,3, the sum of numbers written in the i-th row from the top is h_i.
- For j=1,2,3, the sum of numbers written in the j-th column from the left is w_i.
For example, if (h_1, h_2, h_3) = (5, 13, 10) and (w_1, w_2, w_3) = (6, 13, 9), then all of the following three ways satisfy the conditions. (There are other ways to satisfy the conditions.)

How many ways are there to write numbers to satisfy the conditions?
Constraints
- 3 \leq h_1, h_2, h_3, w_1, w_2, w_3 \leq 30
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
h_1 h_2 h_3 w_1 w_2 w_3
Output
Print the number of ways to write numbers to satisfy the conditions.
Sample Input 1
3 4 6 3 3 7
Sample Output 1
1
The following is the only way to satisfy the conditions. Thus, 1 should be printed.

Sample Input 2
3 4 5 6 7 8
Sample Output 2
0
There may not be a way to satisfy the conditions.
Sample Input 3
5 13 10 6 13 9
Sample Output 3
120
Sample Input 4
20 25 30 22 29 24
Sample Output 4
30613
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
一辺の長さが 1 のマスからなる H 行 W 列のマス目と、N 枚のタイルがあります。
i (1\leq i\leq N) 枚目のタイルは A_i\times B_i の長方形です。
以下の条件をすべてみたすようにタイルをマス目に置くことができるか判定してください。
- 全てのマスがちょうど 1 枚のタイルで覆われている。
- 使用されないタイルがあっても良い。
- 使用するタイルは 回転したり裏返したりして置かれていても良い。ただし、各タイルはマスの線に合わせてマス目からはみ出ることがないように置かれていなければならない。
制約
- 1\leq N\leq 7
- 1 \leq H,W \leq 10
- 1\leq A_i,B_i\leq 10
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N H W A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
問題文中の条件をみたすようにタイルをマス目に置くことができるならば Yes を、そうでないならば No を出力せよ。
入力例 1
5 5 5 1 1 3 3 4 4 2 3 2 5
出力例 1
Yes
2,4,5 枚目のタイルを使用して次のように置くと、マス目の各マスをちょうど 1 枚のタイルで覆うことができます。

よって、Yes を出力します。
入力例 2
1 1 2 2 3
出力例 2
No
マス目からはみ出さないようにタイルを置くことはできません。
よって、No を出力します。
入力例 3
1 2 2 1 1
出力例 3
No
全てのマスを覆うようにタイルを置くことができません。
よって、No を出力します。
入力例 4
5 3 3 1 1 2 2 2 2 2 2 2 2
出力例 4
No
全てのマスはちょうど 1 枚のタイルで覆われている必要があることに注意してください。
Score: 450 points
Problem Statement
There is a grid of H rows and W columns, each cell having a side length of 1, and we have N tiles.
The i-th tile (1\leq i\leq N) is a rectangle of size A_i\times B_i.
Determine whether it is possible to place the tiles on the grid so that all of the following conditions are satisfied:
- Every cell is covered by exactly one tile.
- It is fine to have unused tiles.
- The tiles may be rotated or flipped when placed. However, each tile must be aligned with the edges of the cells without extending outside the grid.
Constraints
- 1\leq N\leq 7
- 1 \leq H,W \leq 10
- 1\leq A_i,B_i\leq 10
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H W A_1 B_1 A_2 B_2 \ldots A_N B_N
Output
If it is possible to place the tiles on the grid so that all of the conditions in the problem statement are satisfied, print Yes; otherwise, print No.
Sample Input 1
5 5 5 1 1 3 3 4 4 2 3 2 5
Sample Output 1
Yes
Placing the 2-nd, 4-th, and 5-th tiles as shown below covers every cell of the grid by exactly one tile.

Hence, print Yes.
Sample Input 2
1 1 2 2 3
Sample Output 2
No
It is impossible to place the tile without letting it extend outside the grid.
Hence, print No.
Sample Input 3
1 2 2 1 1
Sample Output 3
No
It is impossible to cover all cells with the tile.
Hence, print No.
Sample Input 4
5 3 3 1 1 2 2 2 2 2 2 2 2
Sample Output 4
No
Note that each cell must be covered by exactly one tile.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
縦 H 行, 横 W 列のグリッドがあります。グリッドの上から i 行目, 左から j 列目のマスを (i, j) と呼びます。
グリッドの各マスは穴の空いたマスとそうでないマスのどちらかです。穴が空いたマスは (a_1, b_1), (a_2, b_2), \dots, (a_N, b_N) のちょうど N マスです。
正整数の組 (i, j, n) が次の条件を満たすとき、(i, j) を左上隅, (i + n - 1, j + n - 1) を右下隅とする正方形領域を 穴のない正方形 と呼びます。
- i + n - 1 \leq H
- j + n - 1 \leq W
- 0 \leq k \leq n - 1, 0 \leq l \leq n - 1 を満たす全ての非負整数の組 (k, l) に対して、(i + k, j + l) は穴が空いていないマスである。
グリッド内に穴のない正方形は何個ありますか?
制約
- 1 \leq H, W \leq 3000
- 0 \leq N \leq \min(H \times W, 10^5)
- 1 \leq a_i \leq H
- 1 \leq b_i \leq W
- (a_i, b_i) は互いに異なる
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
H W N a_1 b_1 a_2 b_2 \vdots a_N b_N
出力
穴のない正方形の個数を出力せよ。
入力例 1
2 3 1 2 3
出力例 1
6
穴のない正方形は全部で 6 個あります。
それらを列挙すると次の通りです。このうちはじめの 5 個は n = 1 の場合であり、領域の左上隅のマスと右下隅のマスが一致します。
- (1, 1) を左上隅かつ右下隅とする正方形領域
- (1, 2) を左上隅かつ右下隅とする正方形領域
- (1, 3) を左上隅かつ右下隅とする正方形領域
- (2, 1) を左上隅かつ右下隅とする正方形領域
- (2, 2) を左上隅かつ右下隅とする正方形領域
- (1, 1) を左上隅, (2, 2) を右下隅とする正方形領域
入力例 2
3 2 6 1 1 1 2 2 1 2 2 3 1 3 2
出力例 2
0
穴のない正方形が存在しない場合もあります。
入力例 3
1 1 0
出力例 3
1
穴のない正方形がグリッド全体と一致する場合もあります。
入力例 4
3000 3000 0
出力例 4
9004500500
Score : 475 points
Problem Statement
There is a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left of the grid.
Each square of the grid is holed or not. There are exactly N holed squares: (a_1, b_1), (a_2, b_2), \dots, (a_N, b_N).
When the triple of positive integers (i, j, n) satisfies the following condition, the square region whose top-left corner is (i, j) and whose bottom-right corner is (i + n - 1, j + n - 1) is called a holeless square.
- i + n - 1 \leq H.
- j + n - 1 \leq W.
- For every pair of non-negative integers (k, l) such that 0 \leq k \leq n - 1, 0 \leq l \leq n - 1, square (i + k, j + l) is not holed.
How many holeless squares are in the grid?
Constraints
- 1 \leq H, W \leq 3000
- 0 \leq N \leq \min(H \times W, 10^5)
- 1 \leq a_i \leq H
- 1 \leq b_i \leq W
- All (a_i, b_i) are pairwise different.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W N a_1 b_1 a_2 b_2 \vdots a_N b_N
Output
Print the number of holeless squares.
Sample Input 1
2 3 1 2 3
Sample Output 1
6
There are six holeless squares, listed below. For the first five, n = 1, and the top-left and bottom-right corners are the same square.
- The square region whose top-left and bottom-right corners are (1, 1).
- The square region whose top-left and bottom-right corners are (1, 2).
- The square region whose top-left and bottom-right corners are (1, 3).
- The square region whose top-left and bottom-right corners are (2, 1).
- The square region whose top-left and bottom-right corners are (2, 2).
- The square region whose top-left corner is (1, 1) and whose bottom-right corner is (2, 2).
Sample Input 2
3 2 6 1 1 1 2 2 1 2 2 3 1 3 2
Sample Output 2
0
There may be no holeless square.
Sample Input 3
1 1 0
Sample Output 3
1
The whole grid may be a holeless square.
Sample Input 4
3000 3000 0
Sample Output 4
9004500500
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
縦 H 行、横 W 列のマス目があります。上から i 行目、左から j 列目のマスを (i,j) と書くことにします。(i,j) には整数 A_{i,j} が書かれています。
高橋君は (1,1) を出発し、(H,W) にたどり着くまで、1 つ右あるいは 1 つ下のマスへ移動することを繰り返します。ただし、マス目の外に出ることはできません。
この時、移動のコストを以下のように定義します。
通った H+W-1 個のマスに書かれた整数のうち大きい方 K 個の和
コストとしてありうる最小値を求めてください。
制約
- 1 \leq H,W \leq 30
- 1 \leq K < H+W
- 1 \leq A_{i,j} \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
H W K
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
1 3 2 3 4 5
出力例 1
9
移動の方法は一通りのみであり、通ったマスに書かれた整数は大きい方から順に 5、4、3 となるので、9(=5+4) を出力します。
入力例 2
2 2 1 3 2 4 3
出力例 2
3
(1,1)、(1,2)、(2,2) の順に通った時コストが最小となります。
入力例 3
3 5 3 4 7 8 6 4 6 7 3 10 2 3 8 1 10 4
出力例 3
21
Score : 500 points
Problem Statement
We have 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. (i, j) contains an integer A_{i,j}.
Takahashi will depart (1, 1) and repeatedly move one square right or down until reaching (H, W). It is not allowed to exit the grid.
The cost of this travel is defined as:
the sum of the K greatest integers among the integers written on the H+W-1 squares traversed.
Find the minimum possible cost.
Constraints
- 1 \leq H,W \leq 30
- 1 \leq K < H+W
- 1 \leq A_{i,j} \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
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
1 3 2 3 4 5
Sample Output 1
9
There is only one way to travel, where the traversed squares contain the integers 5, 4, 3 from largest to smallest, so we print 9(=5+4).
Sample Input 2
2 2 1 3 2 4 3
Sample Output 2
3
The minimum cost is achieved by traversing (1,1), (1,2), (2,2) in this order.
Sample Input 3
3 5 3 4 7 8 6 4 6 7 3 10 2 3 8 1 10 4
Sample Output 3
21