Woburn Challenge 2015-16 Round 4 - Senior Division
Bond has gotten his hands on a mysterious slip of paper. It's divided into a grid of square cells, with rows and columns. The -th cell in the -th row has the integer printed in it.
Bond also has access to a curious machine which can print rectangular pieces of cardboard on demand. He can use it to print as many rectangles as he'd like, each of them having any integral dimensions of his choice. Each rectangle produced by this machine will have a single integer printed on it, regardless of its size. Bond may choose to place these cardboard rectangles on top of the slip of paper such that they cover some of its cells, as long as they're aligned with the grid, they fit within the grid, and none of them overlap with one another.
He has reason to believe that the purpose of this slip of paper is actually to encode a single, secret integer, which is the maximum possible sum of visible integers after zero or more cardboard rectangles have been placed on it. This sum includes the integers written on the rectangles used, of course, and doesn't include any integers in cells which are underneath a rectangle. Can you help him crack the code by determining the maximum possible sum that can be achieved?
Subtasks
In test cases worth of the points, .
In test cases worth another of the points, .
Input Specification
The first line of input consists of two space-separated integers and
.
The second line consists of integers, .
The third line consists of integers, .
Output Specification
Output a single integer, the maximum sum of visible integers that Bond can achieve by placing cardboard rectangles optimally.
Sample Input 1
4 -5
20 -10 -1 5
-2 3 1 -6
Sample Output 1
17
Explanation
Bond should place a rectangle in the middle of the first row (covering and ), and a rectangle in the bottom-right corner (covering ). The visible numbers will then be , , , , , , and , which sum to .
Sample Input 2
2 -1
-10 -20
-30 -40
Sample Output 2
-1
Sample Input 3
2 1
-10 -20
-30 -40
Sample Output 3
4
Comments