All problems

Problem 449

Cherry Pickup with Two Robots

Hard · 30 pts · Matrix, Dynamic Programming

Problem

An R x C grid holds cherries (grid[i][j] cherries in each cell). Two robots start in the top row, one at the left corner (0, 0) and one at the right corner (0, C-1). In each step both robots move down one row, each to the column on its left, the same column or the column on its right, staying in the grid. A robot collects all cherries in every cell it passes; if both robots are in the same cell, the cherries there are collected only once. Find the maximum number of cherries collected once both reach the bottom row. A dynamic program over (row, column of robot 1, column of robot 2) solves it.

Input

First line: R and C. Next R lines: C integers each.

Output

Print the maximum number of cherries.

Constraints

2 <= R, C <= 70; 0 <= grid[i][j] <= 100

Example input

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

Example output

24

Explanation

In the grid 3 1 1 / 2 5 1 / 1 5 5 / 2 1 1, robot 1 collects 3 + 2 + 5 + 2 = 12 down the left side, and robot 2 collects 1 + 5 + 5 + 1 = 12 on the right. The total is 24.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Falling Path Sum Next: Dungeon Game