Gecko
InterviewTime limit2sMemory limit512 MB
Pick a top-row tile and move straight down, down-left, or down-right each row to maximize the mosquitoes eaten.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
During the rainy season one of the walls in the house is covered with mosquitoes. The wall is tiled with square tiles: rows of tiles from top to bottom and columns from left to right. Between and mosquitoes rest on each tile.
A gecko wants to eat as many mosquitoes as it can under the following rules. It starts by choosing any tile in the top row and eats the mosquitoes on that tile. Then it moves to a tile in the next row down, eats the mosquitoes there, and keeps going the same way until it reaches the floor. When it moves down one row, it may only go straight down, diagonally down to the left, or diagonally down to the right (see Figure 1).

Figure 1 When it moves down one row, the gecko may only go straight down, diagonally down to the left, or diagonally down to the right.
You are given , , and the number of mosquitoes on every tile. Write a program that computes the largest number of mosquitoes the gecko can eat on one trip from the top of the wall to the bottom.
Input
The first line contains two integers. The first integer () is the number of tile rows on the wall, and the second integer () is the number of tile columns.
Then lines follow. The -th line gives the number of mosquitoes on each tile of the -th row from the top. Each line holds integers separated by a single space, and each integer () is the number of mosquitoes on that tile.
Output
Print a single integer, the largest number of mosquitoes the gecko can eat on one trip from the top of the wall to the bottom.