Skip to content

Latest commit

 

History

History
49 lines (32 loc) · 1.22 KB

README.md

File metadata and controls

49 lines (32 loc) · 1.22 KB

Happy Walk

You are walking in a city, which is a rectangle of $n \times m$ blocks. Initially, you are in the upper left corner, and your destination is the lower right one. As you are in a hurry, you move only right and down.

Visiting a city block affects your happiness level. A block in the $i$-th row and the $j$-th column has a happiness value of $a_{ij}$. Every time you visit a block, your happiness level changes by the corresponding value. Initially, your happiness level is 0.

Besides, making more than $k$ consecutive moves in the same direction is extremely boring and can drastically reduce your happiness. Your task is to find a valid path (such that contains no more than $k$ consecutive moves in the same direction) to the destination that maximizes your final happiness level.

Input Format

The first line contains three integers $n$, $m$, and $k$. Each of the next $n$ lines contains $m$ integers $a_{ij}$.

Constraints

$1 \le n, m, k \le 1000$,

$|a_{ij}| \le 10^9$,

it is guaranteed that at least one valid path exists.

Output Format

Print your maximum happiness level at the destination.

Sample Input 1

5 5 2
1 4 2 5 3
4 1 3 2 4
15 2 3 4 5
1 3 4 6 -5
-2 4 5 1 3

Sample Output 1

39