LA 3530

LA 3530 - Martian Mining

 * [Problem Link]

Summary
Given is a $$R\times C$$ matrix, each cell contains some amounts of two types of materials. In each cell you can build a conveyor belt going upwards or a conveyor belt going to the left (or nothing, but not both conveyor belts at the same time). The conveyor belts of the first type transport the first type of materials, and vice versa.

The task is to maximize the total amount of materials the conveyor belts transport to the boundary of the matrix.

Explanation
Clearly there is an optimal solution with a conveyor belt in each cell and with no useless conveyor belts. The only thing we have to do is to determine the optimal boundary between the two types of conveyor belts. This can be done in $$O(RC)$$ using dynamic programming.

Input
4 4 0 0 10 9 1 3 10 0 4 2 1 3 1 1 20 0 10 0 0 0 1 1 1 30 0 0 5 5 5 10 10 10 0 0

Output
98