Nami's Hidden Treasure

0

0 votes
Medium
Problem

Nothing will keep me from this treasuer.

                                                    -Nami

As we all know about the Navigator of the Straw hat Pirates Nami, not doubt she looks good, but she also is very greedy.

The Straw Hat pirates reach an island, the island has the hidden treasure beneath its soil. The island can be considered to be an NxM grid, with rows and columns. Each cell of the grid (i, j) contains two type of treasures one is Gold and other is Platinum, both of them are liked equally by Nami.

Now, digging every cell of grid is a difficult task to be done by hand, so Nami asked Usopp to design something which can help. Usopp designed a system of conveyer belts. The setting of system is like this way: on the West side of the island(On the left), there are Gold processors and on the North side of island (on the top) there are platinum processors. And on each cell they place a conveyer belt (another of Usopp's invention) which will take the treasure from that part and will deliver, the conveyer can be in left or up direction only. The conveyer belt can't be intersected with other type.

A sample is shown under:

As shown the above is a conveyer, the arrows tell where the mineral from the soil goes, to the left or to up, the conveyer belt don't intersect, as shown there is no case conveyer going left will suddenly go up, i.e. if (i, j) points left then it is not possible (i-1, j) will point up (obviously i != 0) and also similar case with the other direction. The conveyer going left only take the gold with it and conveyer going up only collect platinum with it.

Now given the amount of gold and platinum in each cell of the island, we need to get the maximum amount of the total mineral collected. i.e. just maximies gold + platinum by taking care of above conditions.

Nami is just a Navigator, she is not a coder or mathematician, so here you gets the honour to help her, help her and get the marks of this question.

Input

First line contains 2 integers N and M, denotes the number of rows and columns respectively.

Next lines, each line having M integers, Gij these integers say amount of gold in the (i, j) cell of the soil.

In similar way next lines, each line having M integers, Pij which say the amount of Platinum in (i, j) cell of the soil.

Output

Output just one integer which is maximum gold + platinum which can be collected by the use of conveyers.

Contstraints

1 <= N <= 500

1 <= M <=500

1 <=  Gij <= 106

1 <= Pij <= 106

 

Disclamer: It was my first question of this type of questions. :-)

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The conveyer belts can be arranged in the following manner:

U U U

U U U

L L L

By this way no conveyer belt intersects (or overlaps), and 1+1+1+9+9+9+10+3+1 = 46.

Editor Image

?