PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000430. Vượt qua hẻm núi (Canyon Crossing)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Vượt qua hẻm núi (Canyon Crossing)

Canyon Crossing

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

A new route through a canyon is under construction when a rising river floods the low-lying terrain. To reduce the impact of flooding, you want to maximize the lowest terrain height along the route. You have purchased KK rope bridges to bypass low cells.

The map is a rectangular grid with RR rows and CC columns. Cell (i,j)(i,j) has height Hi,jH_{i,j}. Rows are numbered from 11 to RR from north to south, and columns from 11 to CC.

A route starts at any cell in row RR and finishes at any cell in row 11. Each move must go to a cell sharing an edge with the current cell; diagonal moves are not allowed.

Each bridge allows exactly one cell to be crossed without including that cell's height in the minimum height of the route. Bridges may be used at the first cell, the last cell, or consecutive cells. At most KK bridges may be used.

The value of a route is the minimum height of its cells not bypassed by bridges. Since K<RK<R and every route visits at least RR cells, at least one cell contributes to this value.

Find the greatest possible route value.

Input

  • The first line contains three integers R,C,KR,C,K: the number of rows, the number of columns, and the available number of bridges.
  • The next RR lines each contain CC integers. The jj-th integer of the ii-th such line is Hi,jH_{i,j}, the corresponding cell height.

Output

Print one integer: the maximum value of a valid route.

Subtasks

  • Subtask 1 (20 points): 1≤R,C≤301\le R,C\le30; 0≤K≤R−10\le K\le R-1; 0≤Hi,j≤1000\le H_{i,j}\le100 for every 1≤i≤R1\le i\le R, 1≤j≤C1\le j\le C.
  • Subtask 2 (30 points): 1≤R,C≤10001\le R,C\le1000; K=0K=0; 0≤Hi,j≤1090\le H_{i,j}\le10^9 for every 1≤i≤R1\le i\le R, 1≤j≤C1\le j\le C.
  • Subtask 3 (50 points): 1≤R,C≤10001\le R,C\le1000; 0≤K≤R−10\le K\le R-1; 0≤Hi,j≤1090\le H_{i,j}\le10^9 for every 1≤i≤R1\le i\le R, 1≤j≤C1\le j\le C.

Examples

Example 1

Input

5 3 1
1 1 3
3 3 3
0 0 0
2 2 1
1 2 1

Output

2

Explanation

Take (5,2)→(4,2)→(3,2)→(2,2)→(2,3)→(1,3)(5,2)\to(4,2)\to(3,2)\to(2,2)\to(2,3)\to(1,3) and bridge (3,2)(3,2). The remaining heights are 2,2,3,3,32,2,3,3,3, so the route value is 22. A value of 33 would require a bridge in each of rows 55 and 33, since all their cells are below 33. One bridge is insufficient.

Route and bridged cells

Example 2

Input

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

Output

2

Explanation

Go from (5,3)(5,3) to (4,3)(4,3), then (4,2),(3,2),(3,1),(2,1),(1,1)(4,2),(3,2),(3,1),(2,1),(1,1). Bridge (4,2),(3,2),(3,1)(4,2),(3,2),(3,1). The four remaining cells all have height 22. No cell is higher than 22, so no larger value is possible.

Route and bridged cells

Example 3

Input

3 2 2
1 1
4 4
1 2

Output

4

Explanation

Follow column 22 from (3,2)(3,2) through (2,2)(2,2) to (1,2)(1,2). Bridge the first and last cells, of heights 22 and 11. The only remaining height is 44, which is also the maximum grid height. Thus the answer is 44.


Adapted from Canyon Crossing, © 2019 The BAPC 2019 jury. Licensed under CC BY-SA 4.0. Statements adapted and sample illustrations redrawn.