cswork / OAOpen practice workspace

MicrosoftOA #38

Maximum Sum of Two Non-Attacking Rooks

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyIn-place Arrays

Place exactly two rooks on a rectangular board; they must be in different rows and different columns. Find the maximum sum of the scores of their cells. The original site's sample input is not rectangular, so this site uses independent examples consistent with the statement.

Input

This site's ranges: the first line contains N M (2..500), followed by N lines each with M integer scores (−10⁹..10⁹).

Output

Output a single integer, the answer.

Example 1

Input:

2 2
1 4
2 3

Output:

6

Example 2

Input:

2 2
-5 -1
-2 -3

Output:

-3

Example 3

Input:

2 2
9 1
8 2

Output:

11

Example notes

Example 1: take the top-right 4 and the bottom-left 2, for a sum of 6.

Example 2: two rooks must be placed, so take −1 and −2, for a maximum sum of −3.

Example 3: 9 and 8 are in the same column and cannot be paired, so take 9 and 2, for a sum of 11.

Editorial

Loading solution…