Subtask 1 Let be the number of empty cells. Enumerate all direction assignments. Mark rows containing a horizontal box and columns containing a vertical box, then check every student. This takes time.
Full solution If every row has an empty cell, direct every box horizontally. Every student receives Pepero from a box in their row. Likewise, if every column has an empty cell, directing every box vertically works.
If neither condition holds, there is a row with no empty cells and a column with no empty cells. The student at their intersection cannot receive Pepero from either direction. Thus the assignment is impossible. This proves both the decision rule and the construction.
The time complexity, including input and output, is . Storing the grid uses space.
Solution written by GPT6.1