Bored Board Judges

View as PDF

Submit solution


Points: 100
Time limit: 2.0s
PyPy 3 10.0s
Python 3 10.0s
Memory limit: 1G

Author:
Problem type

Amir is preparing his set for the Street Rules Olympic Skateboarding. The MAPS team has prepared a rectangular course, featuring many different interesting elements to make tricks on.

As part of his set, Amir will make two runs through the grid - one up the grid, at which point he turns around at the half-pipe, and then a second down the grid.

Amir can start and finish his sets at any position on the lowest/highest rows of the grid. On Amir's run, he must always move towards the other end of the grid, and while doing so, he may also move 1 unit left/right.

Amir has spent some time studying the grid, and knows if he performs a trick on this cell, how many points he will score from the judges. Let's call this the "trick score". However, if both of his runs feature a trick on the same grid square, he knows that he will receive a reduced score the second time round. Let's call this the "dupe score".

Can you help Amir determine the maximum score he can achieve on the grid specified?

Input

Input will begin with a line containing 2 integers, n (2 \leq n \leq 5000) and m (1 \leq m \leq 50), the dimensions of the grid (height and width, respectively).

n lines then follow, each containing m pairs of space separated integers (So 2m integers), representing a single row of the grid.

The first number in each pair is the trick score for the related grid cell t_{i, j}, and the second number in each pair is the dupe score for the related grid cell d_{i, j} (0 \leq d_{i, j} \leq t_{i, j} \leq 10^9).

The first line of the grid is the top-row (midpoint, adjacent to the half-pipe), and the last line of the grid is the bottom-row (starting/ending point)

Output

Output a single integer, representing the maximum possible score achievable for this grid.

Example 1

Input
5 4
6 4 10 5 7 7 3 1
6 6 6 6 6 6 6 6
1 0 2 1 13 6 10 10
11 10 10 9 1 1 1 1
5 2 4 4 6 2 7 1
Output
78
Explanation

One possible answer to the sample is demonstrated below:


Comments

There are no comments at the moment.