#Q154. 「一本通 5.1 练习 3」矩阵取数游戏
「一本通 5.1 练习 3」矩阵取数游戏
Description
Original Source: NOIP 2007
Shuai Shuai often plays a matrix number game with his classmates: Given an matrix where each element is a non-negative integer, the game rules are as follows:
- Each time numbers are taken, one element must be taken from each row, totaling elements. All elements are taken after rounds.
- The elements taken each time must be either the first or the last element of their respective rows.
- Each round of taking numbers has a score, which is the sum of the scores from each row. The score for taking a number from a row is calculated as the taken element value , where represents the -th round of taking numbers, starting from 1.
- At the end of the game, the total score is the sum of the scores from all rounds.
Shuai Shuai asks you to write a program that, for any given matrix, can compute the maximum possible score after taking all the numbers.
Input Format
The input consists of lines. The first line contains two space-separated positive integers and . The next lines each contain space-separated integers.
Output Format
Output a single integer, which is the maximum score obtainable from the input matrix after taking all the numbers.
Sample 1
First round: Take the first element from the first row and the last element from the second row. The score for this round is . Second round: Take the first element from both rows. The score for this round is . Third round: The score for this round is . The total score is .
2 3
1 2 3
3 4 2
82
Sample 2
1 4
4 5 0 5
122
Sample 3
2 10
96 56 54 46 86 12 23 88 80 43
16 95 18 29 30 53 88 83 64 67
316994
Data Range and Hint
For of the data, , and the answer does not exceed ;
For of the data, , and .