#P4363. [九省联考 2018] 一双木棋 chess
[九省联考 2018] 一双木棋 chess
Description
Feifei and Niuniu play on an by board. Feifei plays black and goes first, Niuniu plays white and goes second.
At the start, the board is empty. They alternately place pieces on cells until the board is completely filled.
The placement rule is: a cell can be chosen if and only if the cell is empty and all cells to its left in the same row and all cells above it in the same column already contain pieces.
Each cell of the board has two nonnegative integers written on it. For the cell in row (from top to bottom) and column (from left to right), the two integers are denoted by and .
After the game ends, Feifei’s score is the sum of over all cells with a black piece, and Niuniu’s score is the sum of over all cells with a white piece.
Both players want to maximize the result of their own score minus the opponent’s score. They would like to know, on the given board, if both sides play optimally and know the other will also play optimally, what is the final result.
Input Format
The first line contains two integers, the numbers of rows and columns of the board.
Lines through each contain integers; on line , the -th integer is .
Lines through each contain integers; on line , the -th integer is .
Output Format
Output a single integer, the value of Feifei’s score minus Niuniu’s score.
2 3
2 7 3
9 1 2
3 7 2
2 3 1
2
Hint
Sample 1 Explanation

The board is as shown. Under optimal play, the game proceeds as follows:
- Feifei plays at row , column (this is the only legal move on the first turn).
- Niuniu plays at row , column .
- Feifei plays at row , column .
- Niuniu plays at row , column .
- Feifei plays at row , column .
- Niuniu plays at row , column (this is the only legal move on this turn).
- The board is filled, and the game ends.
The final position is:

Feifei’s score is , and Niuniu’s score is .
Constraints
The information for each test point is shown in the table below.

- For test points with odd indices, it is guaranteed that .
- For all test points, it is guaranteed that and .
Translated by ChatGPT 5
京公网安备 11011102002149号