cswork / OAOpen practice workspace

VisaOA #14

Arrange Squares by Missing Values

Estimated EasyJudge-ready

Topics (AI-labelled)SimulationIn-place Arrays

You are given a 4×4n grid divided into n side-by-side 4×4 squares. Each square contains every value in 1..16 except one, and the missing cell is marked -1. Find the missing value by subtracting the sum of the other 15 cells from 136, then arrange the whole squares from left to right in ascending order of missing value. If missing values are equal, this site adds that the squares keep their original left-to-right order (stable).

Input

The first line contains n (1≤n≤10000, site-added), followed by 4 lines of 4n numbers each. Each 4×4 square must contain exactly -1 and the other 15 distinct values from 1..16.

Output

Output the rearranged 4-row grid.

Example 1

Input:

2
-1 1 2 3 1 2 3 4
4 5 6 7 5 6 8 9
8 9 10 11 10 11 12 13
13 14 15 16 14 15 16 -1

Output:

1 2 3 4 12 1 2 3
5 6 8 9 4 5 6 7
10 11 12 13 8 9 10 11
14 15 16 7 13 14 15 16

Example 2

Input:

1
1 2 3 5
6 -1 7 8
9 10 11 12
13 14 15 16

Output:

1 2 3 5
6 4 7 8
9 10 11 12
13 14 15 16

Example 3

Input:

3
1 2 -1 3 1 2 3 4 1 2 4 5
4 5 6 8 5 6 8 9 6 7 8 9
9 10 11 12 -1 10 11 12 10 11 12 13
13 14 15 16 13 14 15 16 14 15 -1 16

Output:

1 2 4 5 1 2 7 3 1 2 3 4
6 7 8 9 4 5 6 8 5 6 8 9
10 11 12 13 9 10 11 12 7 10 11 12
14 15 3 16 13 14 15 16 13 14 15 16

Editorial

Loading solution…