Source: APMO-2012-P2
Category: Operations and Strategies

Problem: In a $2012\\times2012$ grid, each cell is filled with a real number from the interval $[0,1]$. The grid is divided into two smaller rectangular grids by drawing a straight line parallel to the edges. If for every possible division, at least one of the smaller grids has the sum of all its numbers not greater than $1$, find the maximum possible sum of all numbers in the entire grid.

Answer: 5

Reference solution:
We first construct a grid where the sum of all numbers is $5$. Let the top-left $3\times3$ cells of the grid be filled such that the cells $(1,2), (2,1), (2,2), (2,3),$ and $(3,2)$ contain $1$, and the remaining four cells in this $3\times 3$ block contain $0$. All other cells in the $2012\times 2012$ grid are filled with $0$. It is easy to verify that the requirement is satisfied: for any horizontal or vertical division, the sum of the numbers in one of the resulting parts is at most $1$ (in fact, exactly $1$ or $0$). The sum of all numbers in this grid is $5$.

Next, we prove that for every grid satisfying the conditions, the sum of all numbers does not exceed $5$. Assume the total sum of the grid is greater than $1$ (otherwise the bound of $5$ trivially holds). Consider the division after the first row. By the problem condition, either the first row has sum $\le 1$, or the remaining $2011$ rows have sum $\le 1$. In the latter case, since all entries are non-negative, the last row must also have sum $\le 1$. Thus, the sum of the numbers in the first row or the last row does not exceed $1$; by symmetry, we may assume it is the former.

Since the sum of the first row is at most $1$ and the total sum is greater than $1$, there exists a largest positive integer $i$ such that the sum of the numbers in the first $i-1$ rows does not exceed $1$. By the maximality of $i$, the sum of the numbers in the first $i$ rows is greater than $1$. Therefore, by the problem's condition applied to the cut after the $i$-th row, the sum of the numbers in the last $2012-i$ rows does not exceed $1$.

Similarly, by considering vertical divisions, there exists a positive integer $j$ such that the sum of the numbers in the first $j-1$ columns does not exceed $1$, and the sum of the numbers in the last $2012-j$ columns does not exceed $1$.

Notice that every cell in the grid is covered by at least one of the following five regions:
- The first $i-1$ rows
- The last $2012-i$ rows
- The first $j-1$ columns
- The last $2012-j$ columns
- The single intersecting cell at row $i$, column $j$

To see this, observe that any cell not in the first $i-1$ rows and not in the last $2012-i$ rows must lie in row $i$. If it is in row $i$, and it is not in the first $j-1$ columns and not in the last $2012-j$ columns, it must be exactly the cell at row $i$, column $j$.

Since all numbers in the grid are non-negative, the total sum of all numbers is bounded by the sum of the totals of these five regions. The sum of each of the first four regions does not exceed $1$, and the value in the cell at row $i$, column $j$ is in $[0,1]$, so it also does not exceed $1$. Therefore, the total sum of all numbers in the grid does not exceed $1+1+1+1+1=5$.

Grading guidelines:
(Partial) 1. Gave a concrete configuration with total sum 5 (e.g. the cross of five 1's in a 3×3 block) and checked that every horizontal/vertical cut leaves a part with sum ≤1. 2. From considering the cut after the first row (or first column), correctly deduced that at least one of the two boundary rows (or boundary columns) has sum ≤1 (using nonnegativity). 3. Defined the maximal i with sum of first i−1 rows ≤1 (or the analogous j for columns), and concluded from the condition that the complementary block of last 2012−i rows (resp. last 2012−j columns) has sum ≤1. (Almost) 1. Obtained the correct 5-region cover idea (first i−1 rows, last 2012−i rows, first j−1 columns, last 2012−j columns, and the single cell (i,j)) and bounded each of the four strips by 1, but did not fully justify that every cell is covered (or mishandled the boundary indices i=1 or i=2012, etc.). 2. Proved the upper bound ≤5 via the above row/column maximality argument but left one localized gap (e.g. did not justify existence of i,j from the assumption total sum >1, or forgot to bound the central cell by ≤1). 3. Solution is almost complete, but made minor mistakes which are not negligible.

Construction task:
Provide an explicit construction for the $2012 \times 2012$ grid that achieves the maximum possible sum of all numbers.

Construction requirements:
- Output a single Python expression that evaluates to a list of lists of numbers representing the $2012 \times 2012$ grid.
- Each number must be in the interval $[0, 1]$.
- For every possible division of the grid into two smaller rectangular grids by a straight line parallel to the edges, at least one of the smaller grids must have the sum of all its numbers not greater than $1$.
- The sum of all numbers in the grid must be exactly the maximum possible sum.
- The output must be a single, valid Python expression (e.g., using list comprehensions) that evaluates directly to the target data structure.
- Do not include any explanations, comments, print statements, assignments, or imports.

Output formatting requirements:
- Include exactly one construction block and put ONLY the Python expression between the tags:
<construct>
...
</construct>
- Do not use markdown code blocks (like ```python). Do not put any extra text inside <construct>...</construct>.

Reference construction:
[[1 if (i == 1 and j <= 2) or (j == 1 and i <= 2) else 0 for j in range(2012)] for i in range(2012)]

Verifier code:
import sys
import math

def verify():
    payload = sys.stdin.read().strip()
    if not payload:
        print("Empty input.")
        return
        
    # Allow safe builtins to evaluate list comprehensions
    allowed_builtins = {
        'range': range,
        'len': len,
        'min': min,
        'max': max,
        'sum': sum,
        'int': int,
        'float': float,
        'bool': bool,
        'list': list,
        'tuple': tuple,
        'zip': zip,
        'abs': abs,
        'round': round,
        'enumerate': enumerate,
        'map': map,
        'filter': filter,
        'any': any,
        'all': all,
    }
    
    try:
        grid = eval(payload, {"__builtins__": allowed_builtins}, {})
    except Exception as e:
        print(f"Failed to evaluate payload: {e}")
        return
        
    if not isinstance(grid, list):
        print("Grid must be a list.")
        return
    if len(grid) != 2012:
        print(f"Grid must have exactly 2012 rows, got {len(grid)}.")
        return
        
    for i in range(2012):
        row = grid[i]
        if not isinstance(row, list):
            print(f"Row {i} is not a list.")
            return
        if len(row) != 2012:
            print(f"Row {i} must have exactly 2012 elements, got {len(row)}.")
            return
        for j in range(2012):
            val = row[j]
            if not isinstance(val, (int, float)):
                print(f"Element at ({i}, {j}) is not a number: {type(val).__name__}")
                return
            if not (0 <= val <= 1):
                print(f"Element at ({i}, {j}) is out of bounds [0, 1]: {val}")
                return

    # Precalculate row sums and column sums using math.fsum for numerical stability
    row_sums = [math.fsum(row) for row in grid]
    col_sums = [math.fsum(grid[i][j] for i in range(2012)) for j in range(2012)]
    total_sum = math.fsum(row_sums)

    if not math.isclose(total_sum, 5.0, rel_tol=0.0, abs_tol=1e-9):
        print(f"Total sum must be exactly 5, got {total_sum}.")
        return
        
    # Verify horizontal division constraint
    for k in range(1, 2012):
        top_sum = math.fsum(row_sums[:k])
        bottom_sum = math.fsum(row_sums[k:])
        if top_sum > 1 + 1e-9 and bottom_sum > 1 + 1e-9:
            print(f"Horizontal division at row {k} fails constraint: top_sum={top_sum}, bottom_sum={bottom_sum}")
            return
            
    # Verify vertical division constraint
    for k in range(1, 2012):
        left_sum = math.fsum(col_sums[:k])
        right_sum = math.fsum(col_sums[k:])
        if left_sum > 1 + 1e-9 and right_sum > 1 + 1e-9:
            print(f"Vertical division at column {k} fails constraint: left_sum={left_sum}, right_sum={right_sum}")
            return
            
    print("True")

if __name__ == "__main__":
    verify()