atcoder#ABC228F. [ABC228F] Stamp Game
[ABC228F] Stamp Game
Score : points
Problem Statement
There is a grid with horizontal rows and vertical columns. Let denote the square at the -th row from the top and -th column from the left. For each pair of integers such that and , contains a positive integer . Additionally, every square is painted white.
Takahashi has a rectangular black stamp that can cover rows and columns, and Aoki has a rectangular white stamp that can cover rows and columns. Using these stamps and the grid, they will play a game against each other.
First, Takahashi presses his black stamp to paint a rectangular region with rows and columns black. That is, he chooses a pair of integers such that and , and paints black every square such that and .
Second, Aoki presses his white stamp to paint a rectangular region with rows and columns white. That is, he chooses a pair of integers such that and , and paints white every square such that and . If some of these squares are already painted black by Takahashi, they will also become white.
The game's score is defined as the sum of the integers written on the squares painted black after Takahashi and Aoki press their stamps as described above. Takahashi follows the optimal strategy to make the score as large as possible, while Aoki follows the optimal strategy to make the score as small as possible. What will the game's score be?
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the game's score when Takahashi follows the optimal strategy to make the score as large as possible and Aoki follows the optimal strategy to make the score as small as possible.
3 4 2 3 3 1
3 1 4 1
5 9 2 6
5 3 5 8
19
The game will go as follows.
- Initially, all squares are painted white.
- First, Takahashi presses his black stamp to paint the following six squares black: .
- Second, Aoki presses his white stamp to paint the following three squares white: .
- Eventually, the following four squares are painted black: , for a score of .
3 4 2 3 3 4
3 1 4 1
5 9 2 6
5 3 5 8
0
After Aoki presses his stamp, all squares will be white, for a score of .
10 10 3 7 2 3
9 7 19 7 10 4 13 9 4 8
10 15 16 3 18 19 17 12 13 2
12 18 4 9 13 13 6 13 5 2
16 12 2 14 18 17 14 7 8 12
12 13 17 12 14 15 19 7 13 15
5 2 16 10 4 6 1 2 7 8
10 14 14 10 9 13 11 4 9 19
16 12 3 19 19 6 2 19 14 20
15 3 19 19 2 10 1 4 3 15
13 20 5 6 19 1 7 17 10 19
180