#K35167. Tile Placement on Grid

    ID: 25472 Type: Default 1000ms 256MiB

Tile Placement on Grid

Tile Placement on Grid

Given an integer ( N ) representing the grid dimension and an integer ( L ) representing the limiting sum, along with ( N^2 ) tiles each having an integer value, determine whether it is possible to place all the tiles on an ( N \times N ) grid so that the sum of the numbers in every row and every column does not exceed ( L ).

The task is to check if there exists any arrangement (i.e. permutation) of the tiles that satisfies the condition for both rows and columns.

Formally, if the grid is denoted by ( a_{i,j} ) for ( 1 \leq i,j \leq N ), then for every row ( i ) and every column ( j ), the following must hold:
[ \sum_{j=1}^{N} a_{i,j} \leq L \quad \text{and} \quad \sum_{i=1}^{N} a_{i,j} \leq L ]

inputFormat

The first line contains two integers ( N ) and ( L ), where ( N ) (1 ( \leq N \leq 3 )) is the dimension of the grid and ( L ) is the maximum allowed sum for any row or column. The second line contains ( N^2 ) integers representing the values of the tiles.

outputFormat

Output a single line with either "YES" if there exists an arrangement such that every row and every column has a sum less than or equal to ( L ), or "NO" otherwise.## sample

2 10
1 2 3 4
YES