Find two bishops on an board, one on a black square and one on a white square, such that the sum of values on both bishops' diagonals is maximized.
Gargari and Bishops
You are given an table of integers. Consider placing a bishop on any cell of the board.
A bishop attacks every cell on the two diagonals passing through its position. For a bishop placed at cell , define its score as the sum of all values on both diagonals that pass through , counting the cell itself only once.
Choose two cells:
- one on a black square,
- one on a white square,
so that the sum of their scores is as large as possible.
Return the maximum possible total score and the coordinates of the two chosen cells.
The standard chessboard coloring is used: a cell is black if is even, otherwise white.
Input Format
Input
- The first line contains an integer .
- Each of the next lines contains integers describing the board.
Output
Print the maximum total score, followed by the coordinates of the chosen black cell and white cell.
If multiple answers exist, any valid one may be returned.
Output Format
Output
Print four integers for the best black cell and four integers for the best white cell, or otherwise the maximal total value and coordinates in the required order depending on the implementation format used by the platform.
For practice purposes, the key goal is to compute:
- the best black-square bishop position,
- the best white-square bishop position,
- the sum of their diagonal scores.
Constraints
- The board contains integer values.
- A correct solution should run in or similar time.
Example 1
Input
2 1 2 3 4
Output
8 1 2 2 1
Explanation
Cell has diagonal sum and cell has diagonal sum if diagonals are counted over the board; since each cell belongs to one black and one white square, the best valid pair is one black and one white cell. This tiny example illustrates the idea of evaluating both colors separately.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.