Source: RMO-2017-P22
Category: Extremal Problems

Problem: On a $200\\times200$ chessboard, some squares contain a red or blue piece, while the remaining squares are empty. Two pieces are said to \"see\" each other if they are in the same row or the same column. Suppose each piece sees exactly five pieces of the opposite color (and may also see some pieces of the same color). Find the maximum possible total number of pieces on the chessboard.

Answer: 3800

Reference solution:
The maximum possible total number of pieces on the chessboard is 3800.

There exists a valid placement of pieces that satisfies the requirements, yielding a total of $10\times190\times2=3800$ pieces.

Note that when a row contains pieces of only one colour, the row can have at most $200$ pieces. When a row contains pieces of two colours, each colour appears at most $5$ times, so the row can have at most $10$ pieces. If there are $191$ rows that contain two colours, then the total number of pieces does not exceed $9\times200+191\times10=3710<3800$. Suppose at most $190$ rows contain two colours; similarly, we may assume at most $190$ columns contain two colours. 

Consider the number $S$ of pairs of pieces of different colours that lie in the same row or the same column. On the one hand, if a row contains only one colour, it contributes no heterochromatic pairs; if a row contains two colours, the number of heterochromatic pairs in that row is at most $5\times5=25$. The same holds for columns. Hence, $S\le 25\times190\times2$. On the other hand, each piece forms heterochromatic pairs with exactly $5$ other pieces, so the total number of pieces is at most $\frac{2S}{5}\le \frac{2 \times (25 \times 190 \times 2)}{5} = 3800$.

Grading guidelines:
(Partial) 1. Observed that any row (or column) containing both colours has at most 10 pieces (at most 5 of each colour), since each piece must see exactly five opposite-colour pieces in its row/column. 2. Introduced the double-counting parameter S = number of opposite-colour pairs in a common row or column, and correctly deduced that the total number of pieces equals 2S/5. 3. Obtained a nontrivial global upper bound by combining a bound on heterochromatic pairs per mixed row/column (e.g. ≤25) with a bound on the number of mixed rows/columns (e.g. ≤200), even if the final numeric maximum is not reached. (Almost) 1. Proved the upper bound 3800 via the S double count, but made a small slip in justifying that one may assume at most 190 mixed rows and at most 190 mixed columns (or handled only rows but not columns), while the rest of the argument is correct. 2. Gave a construction intended to achieve 3800 pieces (with 190 mixed rows and 190 mixed columns, 5 of each colour in each mixed line), but did not fully verify the condition “each piece sees exactly five opposite-colour pieces” for all pieces (e.g. missed checking edge rows/columns or symmetry cases). 3. Solution is almost complete, but made minor mistakes which are not negligible.

Construction task:
Provide an explicit construction of the chessboard that achieves this maximum possible total number of pieces.

Construction requirements:
- Output a single Python expression that evaluates to a 200x200 list of lists of integers representing the chessboard.
- Use 1 for a red piece, 2 for a blue piece, and 0 for an empty square.
- The configuration must satisfy all problem constraints: every piece (1 or 2) must share its row and column with exactly 5 pieces in total of the opposite color.
- The total number of pieces (1s and 2s) must be maximized.
- The output must be a pure Python expression (e.g., using list comprehensions and conditional expressions). Do not include any explanations, comments, variable assignments, or markdown formatting (such as ```python). Put ONLY the raw Python expression inside the construction block.

Reference construction:
[[1 if (r < 5 and c >= 10) or (c < 5 and r >= 10) else 2 if (5 <= r < 10 and c >= 10) or (5 <= c < 10 and r >= 10) else 0 for c in range(200)] for r in range(200)]

Verifier code:
import sys

def verify():
    payload = sys.stdin.read().strip()
    if not payload:
        print("Empty input.")
        return
    
    try:
        # The instruction allows pure Python expressions including list comprehensions,
        # so we use eval() as specified in the user intent.
        board = eval(payload)
    except Exception as e:
        print(f"Failed to evaluate python expression: {e}")
        return
        
    if type(board) is not list or len(board) != 200:
        print("Board must be a list of exactly 200 elements.")
        return
        
    for r, row in enumerate(board):
        if type(row) is not list or len(row) != 200:
            print(f"Row {r} is not a list of exactly 200 elements.")
            return
        for c, val in enumerate(row):
            if type(val) is not int or val not in {0, 1, 2}:
                print(f"Invalid value {repr(val)} at ({r}, {c}). Expected 0, 1, or 2.")
                return
                
    total_pieces = 0
    row_counts = {1: [0]*200, 2: [0]*200}
    col_counts = {1: [0]*200, 2: [0]*200}
    
    # Count pieces and precompute the number of 1s and 2s in each row and column
    for r in range(200):
        for c in range(200):
            val = board[r][c]
            if val in {1, 2}:
                total_pieces += 1
                row_counts[val][r] += 1
                col_counts[val][c] += 1
                
    if total_pieces != 3800:
        print(f"Total pieces is {total_pieces}, expected exactly 3800.")
        return
                
    # Verify that each piece sees exactly 5 pieces of the opposite color
    for r in range(200):
        for c in range(200):
            val = board[r][c]
            if val in {1, 2}:
                opp = 3 - val
                sees = row_counts[opp][r] + col_counts[opp][c]
                if sees != 5:
                    print(f"Piece at ({r}, {c}) of color {val} sees {sees} opposite color pieces in its row and column combined, expected exactly 5.")
                    return
                    
    print("True")

if __name__ == "__main__":
    verify()