C. Palindromic Matrix

problem_index: C

time_limit: 2 seconds

memory_limit: 256 megabytes

solutions: ["import sys\nn = int(input())\nA = list(map(int, input().split()))\nfrom collections import Counter\ncounter = Counter(A)\nANS = [[0] * n for i in range(n)]\n\ndef write(ANS):\n\tfor i in range(n):\n\t\tprint(' '.join([str(a) for a in ANS[i]]))\nif n % 2 == 0:\n\tfor v in counter.values():\n\t\tif v % 4 != 0:\n\t\t\tprint('NO')\n\t\t\tsys.exit()\n\ti = j = 0\n\tfor k in counter.keys():\n\t\twhile counter[k] > 0:\n\t\t\tANS[i][j] = ANS[n - i - 1][j] = ANS[i][n - j - 1] = ANS[n - i - 1][n - j - 1] = k\n\t\t\tcounter[k] -= 4\n\t\t\tif j == n // 2 - 1:\n\t\t\t\ti += 1\n\t\t\t\tj = 0\n\t\t\telse:\n\t\t\t\tj += 1\n\tprint('YES')\n\twrite(ANS)\nelse:\n\tc1 = 0\n\tc2 = 0\n\tfor v in counter.values():\n\t\tif v % 2 == 1:\n\t\t\tc1 += 1\n\t\t\tv -= 1\n\t\tif v % 4 == 2:\n\t\t\tc2 += 1\n\tif c1 > 1 or c2 > n - 1:\n\t\tprint('NO')\n\t\tsys.exit()\n\ti2 = 0\n\tj2 = 0\n\tfor k in counter.keys():\n\t\tif counter[k] > 0:\n\t\t\tif counter[k] % 2 == 1:\n\t\t\t\tANS[n // 2][n // 2] = k\n\t\t\t\tcounter[k] -= 1\n\t\t\tif counter[k] % 4 == 2 and i2 != n // 2:\n\t\t\t\tANS[i2][n // 2] = ANS[n - i2 - 1][n // 2] = k\n\t\t\t\ti2 += 1\n\t\t\t\tcounter[k] -= 2\n\t\t\tif counter[k] % 4 == 2 and i2 == n // 2:\n\t\t\t\tANS[n // 2][j2] = ANS[n // 2][n - 1 - j2] = k\n\t\t\t\tj2 += 1\n\t\t\t\tcounter[k] -= 2\n\ti = j = 0\n\tfor k in counter.keys():\n\t\tif counter[k] > 0:\n\t\t\twhile counter[k] > 0:\n\t\t\t\tif j == n // 2:\n\t\t\t\t\tif ANS[i][j] == 0:\n\t\t\t\t\t\tANS[i][j] = ANS[n - i - 1][j] = ANS[j][i] = ANS[j][n - i - 1] = k\n\t\t\t\t\t\tcounter[k] -= 4\n\t\t\t\t\telse:\n\t\t\t\t\t\ti += 1\n\t\t\t\t\t\tj = 0\n\t\t\t\tif i != n // 2 and j != n // 2:\n\t\t\t\t\tANS[i][j] = ANS[n - i - 1][j] = ANS[i][n - j - 1] = ANS[n - i - 1][n - j - 1] = k\n\t\t\t\t\tcounter[k] -= 4\n\t\t\t\tif i == n // 2 and ANS[i][j] == 0:\n\t\t\t\t\tANS[i][j] = ANS[i][n - j - 1] = k\n\t\t\t\t\tcounter[k] -= 2\n\t\t\t\tif j == n // 2:\n\t\t\t\t\ti += 1\n\t\t\t\t\tj = 0\n\t\t\t\telse:\n\t\t\t\t\tj += 1\n\tprint('YES')\n\twrite(ANS)\n", "from collections import Counter\nn = int(input())\nA = [int(i) for i in input().split()]\nC = Counter(A)\nB = [[] for i in range(5)]\nQ = [[0] * 5 for i in range(21)]\nQ[1][1] = 1\nfor i in range(3, 21, 2):\n\tQ[i][1] = 1\n\tQ[i][2] = Q[i - 2][2] + 2\n\tQ[i][4] = Q[i - 2][4] + (i - 2)\nfor i in range(2, 21, 2):\n\tQ[i][4] = (i // 2) ** 2\nP = Q[n]\nif n % 2 == 0:\n\tfor (k, v) in C.items():\n\t\tif v % 4 != 0:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\tB[4] += [k] * (v // 4)\n\tprint('YES')\n\tm = n // 2\n\tD = dict()\n\tfor i in range(n):\n\t\ta = i if i < m else n - i - 1\n\t\tans = []\n\t\tfor j in range(n):\n\t\t\tb = j if j < m else n - j - 1\n\t\t\tif not (a, b) in D:\n\t\t\t\tD[a, b] = B[4].pop()\n\t\t\tans.append(D[a, b])\n\t\tprint(*ans)\nelse:\n\tfor (k, v) in C.items():\n\t\tif v % 2 == 1:\n\t\t\tC[k] -= 1\n\t\t\tP[1] -= 1\n\t\t\tB[1].append(k)\n\tfor (k, v) in C.items():\n\t\tif P[4] == 0:\n\t\t\tbreak\n\t\tt = min(P[4], v // 4)\n\t\tC[k] -= 4 * t\n\t\tP[4] -= t\n\t\tB[4] += [k] * t\n\tfor (k, v) in C.items():\n\t\tt = v // 2\n\t\tB[2] += [k] * t\n\t\tP[2] -= t\n\t\tC[k] -= 2 * t\n\tif P[1] != 0 or P[4] != 0 or P[2] != 0:\n\t\tprint('NO')\n\t\texit()\n\tprint('YES')\n\tm = n // 2\n\tD = dict()\n\tfor i in range(n):\n\t\tans = []\n\t\tfor j in range(n):\n\t\t\tif i == m and j == m:\n\t\t\t\tans.append(B[1][0])\n\t\t\telif i == m:\n\t\t\t\tk = j if j < m else n - j - 1\n\t\t\t\tk += Q[n][2] // 2\n\t\t\t\tans.append(B[2][k])\n\t\t\telif j == m:\n\t\t\t\tk = i if i < m else n - i - 1\n\t\t\t\tans.append(B[2][k + m])\n\t\t\telse:\n\t\t\t\t(a, b) = (abs(i - m), abs(j - m))\n\t\t\t\tif not (a, b) in D:\n\t\t\t\t\tD[a, b] = B[4].pop()\n\t\t\t\tans.append(D[a, b])\n\t\tprint(*ans)\n", "n = int(input())\na = list(map(int, input().split()))\ncnt = [0] * 1001\nfor val in a:\n\tcnt[val] += 1\n\ndef solve():\n\tif n % 2 == 0:\n\t\tfor i in range(1001):\n\t\t\tif cnt[i] % 4 != 0:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\tpos = 0\n\t\tm = [[0] * n for _ in range(n)]\n\t\tfor row in range(n // 2):\n\t\t\tfor row_or_col in range(n // 2):\n\t\t\t\twhile cnt[pos] == 0:\n\t\t\t\t\tpos += 1\n\t\t\t\tm[row][row_or_col] = pos\n\t\t\t\tm[row][n - 1 - row_or_col] = pos\n\t\t\t\tm[n - 1 - row][row_or_col] = pos\n\t\t\t\tm[n - 1 - row][n - 1 - row_or_col] = pos\n\t\t\t\tcnt[pos] -= 4\n\t\tprint('YES')\n\t\tfor row in range(n):\n\t\t\tprint(' '.join(map(str, m[row])))\n\telse:\n\t\tpos4 = 0\n\t\tpos2 = 0\n\n\t\tdef next4(pos4):\n\t\t\twhile pos4 < len(cnt) and (cnt[pos4] == 0 or cnt[pos4] < 4):\n\t\t\t\tpos4 += 1\n\t\t\treturn pos4\n\n\t\tdef next2(pos2):\n\t\t\twhile pos2 < len(cnt) and (cnt[pos2] == 0 or cnt[pos2] < 2):\n\t\t\t\tpos2 += 1\n\t\t\treturn pos2\n\n\t\tdef next1():\n\t\t\tfor pos in range(len(cnt)):\n\t\t\t\tif cnt[pos] > 0:\n\t\t\t\t\treturn pos\n\t\t\treturn len(cnt)\n\t\tm = [[0] * n for _ in range(n)]\n\t\tfor row in range(n // 2):\n\t\t\tfor row_or_col in range(n // 2):\n\t\t\t\tpos4 = next4(pos4)\n\t\t\t\tif pos4 >= len(cnt):\n\t\t\t\t\tprint('NO')\n\t\t\t\t\treturn\n\t\t\t\tcnt[pos4] -= 4\n\t\t\t\tm[row][row_or_col] = pos4\n\t\t\t\tm[row][n - 1 - row_or_col] = pos4\n\t\t\t\tm[n - 1 - row][row_or_col] = pos4\n\t\t\t\tm[n - 1 - row][n - 1 - row_or_col] = pos4\n\t\tfor row_or_col in range(n // 2):\n\t\t\tif row_or_col != n // 2:\n\t\t\t\tpos2 = next2(pos2)\n\t\t\t\tif pos2 >= len(cnt):\n\t\t\t\t\tprint('NO')\n\t\t\t\t\treturn\n\t\t\t\tcnt[pos2] -= 2\n\t\t\t\tm[n // 2][row_or_col] = m[n // 2][n - 1 - row_or_col] = pos2\n\t\t\t\tpos2 = next2(pos2)\n\t\t\t\tif pos2 >= len(cnt):\n\t\t\t\t\tprint('NO')\n\t\t\t\t\treturn\n\t\t\t\tcnt[pos2] -= 2\n\t\t\t\tm[row_or_col][n // 2] = m[n - 1 - row_or_col][n // 2] = pos2\n\t\tpos1 = next1()\n\t\tif pos1 >= len(cnt):\n\t\t\tprint('NO')\n\t\t\treturn\n\t\tm[n // 2][n // 2] = pos1\n\t\tprint('YES')\n\t\tfor row in range(n):\n\t\t\tprint(' '.join(map(str, m[row])))\nsolve()\n", "n = int(input())\ns = [int(x) for x in input().split()]\nL = sorted(s)\nflag = 0\nif n % 2 != 0:\n\tl4 = []\n\tl2 = []\n\tl1 = []\n\tG = []\n\tp = 1\n\tfor i in range(1, len(L)):\n\t\tif L[i] != L[i - 1]:\n\t\t\tG.append([L[i - 1], p])\n\t\t\tp = 1\n\t\telse:\n\t\t\tp = p + 1\n\tG.append([L[-1], p])\n\tfor i in range(0, len(G)):\n\t\tif G[i][1] >= 4:\n\t\t\tl4 = l4 + [G[i][0]] * (4 * (G[i][1] // 4))\n\t\t\tG[i][1] = G[i][1] % 4\n\t\t\tl2 = l2 + [G[i][0]] * (2 * (G[i][1] // 2))\n\t\t\tG[i][1] = G[i][1] % 2\n\t\t\tl1 = l1 + [G[i][0]] * G[i][1]\n\t\telif G[i][1] >= 2:\n\t\t\tl2 = l2 + [G[i][0]] * (2 * (G[i][1] // 2))\n\t\t\tG[i][1] = G[i][1] % 2\n\t\t\tl1 = l1 + [G[i][0]] * G[i][1]\n\t\telse:\n\t\t\tl1 = l1 + [G[i][0]] * G[i][1]\n\tn1 = 4 * ((n - 1) // 2) ** 2\n\tn2 = 2 * (n - 1)\n\tif len(l4) > n1:\n\t\tl2 = l2 + l4[n1:]\n\t\tl4 = l4[:n1]\n\tif len(l2) > n2:\n\t\tl1 = l1 + l2[n2:]\n\t\tl2 = l2[:n2]\n\tif len(l4) == n1 and len(l2) == n2 and (len(l1) == 1):\n\t\tflag = 1\n\tif flag != 0:\n\t\tL1 = []\n\t\tk = (n - 1) // 2\n\t\tpt1 = 0\n\t\tpt2 = 0\n\t\tfor i in range(0, k):\n\t\t\tfor j in range(0, k):\n\t\t\t\tfor m in range(4):\n\t\t\t\t\tL1.append(l4[pt1])\n\t\t\t\t\tpt1 = pt1 + 1\n\t\t\tfor j in range(2):\n\t\t\t\tL1.append(l2[pt2])\n\t\t\t\tpt2 = pt2 + 1\n\t\tfor i in range(n - 1):\n\t\t\tL1.append(l2[pt2])\n\t\t\tpt2 = pt2 + 1\n\t\tL1 = L1 + l1\n\t\tL = L1\nelse:\n\tI = 0\n\tfor i in range(0, len(L), 4):\n\t\tW = L[i:4 * (I + 1)]\n\t\tI = I + 1\n\t\tif len(list(set(W))) == 1:\n\t\t\tflag = 2\n\t\telse:\n\t\t\tflag = 0\n\t\t\tbreak\nif flag == 0:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tdp = []\n\tfor i in range(0, n):\n\t\th = []\n\t\tfor j in range(0, n):\n\t\t\th.append(0)\n\t\tdp.append(h)\n\tptr1 = [0, 0]\n\tptr3 = [n - 1, n - 1]\n\tptr2 = [n - 1, 0]\n\tptr4 = [0, n - 1]\n\tif n % 2 != 0:\n\t\tc = 1\n\t\tr = -1\n\t\tfor i in range(0, len(L), 2):\n\t\t\tr = r + 1\n\t\t\tif ptr1 == ptr2 and ptr3 == ptr4 and (ptr1 != ptr3):\n\t\t\t\tdp[ptr1[0]][ptr1[1]] = L[i]\n\t\t\t\tdp[ptr3[0]][ptr3[1]] = L[i + 1]\n\t\t\t\tptr1 = [0, c]\n\t\t\t\tptr3 = [n - 1, n - c - 1]\n\t\t\t\tptr2 = [n - 1, c]\n\t\t\t\tptr4 = [0, n - c - 1]\n\t\t\t\tc = c + 1\n\t\t\t\tr = 0\n\t\t\telif ptr1 == ptr4 and ptr2 == ptr3 and (ptr1 != ptr2):\n\t\t\t\tdp[ptr1[0]][ptr1[1]] = L[i]\n\t\t\t\tdp[ptr2[0]][ptr2[1]] = L[i + 1]\n\t\t\t\tptr1 = [ptr1[0] + 1, ptr1[1]]\n\t\t\t\tptr2 = [ptr2[0] - 1, ptr2[1]]\n\t\t\t\tptr3 = [ptr3[0] - 1, ptr3[1]]\n\t\t\t\tptr4 = [ptr4[0] + 1, ptr4[1]]\n\t\t\telif ptr1 == ptr3 and ptr2 == ptr4 and (ptr1 == ptr4):\n\t\t\t\tdp[ptr1[0]][ptr1[1]] = L[i]\n\t\t\telif r % 2 == 0:\n\t\t\t\tdp[ptr1[0]][ptr1[1]] = L[i]\n\t\t\t\tdp[ptr2[0]][ptr2[1]] = L[i + 1]\n\t\t\t\tptr1 = [ptr1[0] + 1, ptr1[1]]\n\t\t\t\tptr2 = [ptr2[0] - 1, ptr2[1]]\n\t\t\telse:\n\t\t\t\tdp[ptr3[0]][ptr3[1]] = L[i]\n\t\t\t\tdp[ptr4[0]][ptr4[1]] = L[i + 1]\n\t\t\t\tptr3 = [ptr3[0] - 1, ptr3[1]]\n\t\t\t\tptr4 = [ptr4[0] + 1, ptr4[1]]\n\telse:\n\t\tc = 1\n\t\tfor i in range(0, len(L), 4):\n\t\t\tdp[ptr1[0]][ptr1[1]] = L[i]\n\t\t\tdp[ptr2[0]][ptr2[1]] = L[i + 1]\n\t\t\tdp[ptr3[0]][ptr3[1]] = L[i + 2]\n\t\t\tdp[ptr4[0]][ptr4[1]] = L[i + 3]\n\t\t\tptr1 = [ptr1[0] + 1, ptr1[1]]\n\t\t\tptr2 = [ptr2[0] - 1, ptr2[1]]\n\t\t\tptr3 = [ptr3[0] - 1, ptr3[1]]\n\t\t\tptr4 = [ptr4[0] + 1, ptr4[1]]\n\t\t\tif ptr1[0] > ptr2[0]:\n\t\t\t\tptr1 = [0, c]\n\t\t\t\tptr3 = [n - 1, n - c - 1]\n\t\t\t\tptr2 = [n - 1, c]\n\t\t\t\tptr4 = [0, n - c - 1]\n\t\t\t\tc = c + 1\n\tfor i in range(0, n):\n\t\tfor j in range(0, n):\n\t\t\tprint(dp[i][j], end=' ')\n\t\tprint(' ')\n", "n = int(input())\nA = list(map(int, input().split()))\ncount = [0] * 1001\nfor a in A:\n\tcount[a] += 1\nif n % 2 == 1:\n\tcan = True\n\tone = []\n\ttwo = []\n\tfore = []\n\tfor i in range(1001):\n\t\tc = count[i]\n\t\tif c % 4 == 0:\n\t\t\tfore += [i] * (c // 4)\n\t\telif c % 4 == 1:\n\t\t\tone += [i]\n\t\t\tfore += [i] * (c // 4)\n\t\telif c % 4 == 2:\n\t\t\ttwo += [i]\n\t\t\tfore += [i] * (c // 4)\n\t\telse:\n\t\t\tone += [i]\n\t\t\ttwo += [i]\n\t\t\tfore += [i] * (c // 4)\n\tif len(one) != 1:\n\t\tcan = False\n\tif len(two) > n - 1:\n\t\tcan = False\n\tif can:\n\t\tanswer = [[0] * n for i in range(n)]\n\t\tprint('YES')\n\t\tcurrent = 0\n\t\tfor i in range((n + 1) // 2):\n\t\t\tfor j in range((n + 1) // 2):\n\t\t\t\tif i == (n - 1) // 2:\n\t\t\t\t\tif j == (n - 1) // 2:\n\t\t\t\t\t\tanswer[i][j] = one[-1]\n\t\t\t\t\telse:\n\t\t\t\t\t\tif len(two) == 0:\n\t\t\t\t\t\t\ttwo += [fore[-1]] * 2\n\t\t\t\t\t\t\tfore = fore[:-1]\n\t\t\t\t\t\tanswer[i][j] = two[-1]\n\t\t\t\t\t\tanswer[i][n - j - 1] = two[-1]\n\t\t\t\t\t\ttwo = two[:-1]\n\t\t\t\telif j == (n - 1) // 2:\n\t\t\t\t\tif len(two) == 0:\n\t\t\t\t\t\ttwo += [fore[-1]] * 2\n\t\t\t\t\t\tfore = fore[:-1]\n\t\t\t\t\tanswer[i][j] = two[-1]\n\t\t\t\t\tanswer[n - i - 1][j] = two[-1]\n\t\t\t\t\ttwo = two[:-1]\n\t\t\t\telse:\n\t\t\t\t\tanswer[i][j] = fore[-1]\n\t\t\t\t\tanswer[n - i - 1][j] = fore[-1]\n\t\t\t\t\tanswer[i][n - j - 1] = fore[-1]\n\t\t\t\t\tanswer[n - i - 1][n - j - 1] = fore[-1]\n\t\t\t\t\tfore = fore[:-1]\n\t\tfor line in answer:\n\t\t\tprint(' '.join(list(map(str, line))))\n\telse:\n\t\tprint('NO')\nelse:\n\tcan = True\n\tfore = []\n\tfor i in range(1001):\n\t\tc = count[i]\n\t\tif c % 4 == 0:\n\t\t\tfore += [i] * (c // 4)\n\t\telse:\n\t\t\tcan = False\n\tif can:\n\t\tanswer = [[0] * n for i in range(n)]\n\t\tprint('YES')\n\t\tcurrent = 0\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tanswer[i][j] = fore[-1]\n\t\t\t\tanswer[n - i - 1][j] = fore[-1]\n\t\t\t\tanswer[i][n - j - 1] = fore[-1]\n\t\t\t\tanswer[n - i - 1][n - j - 1] = fore[-1]\n\t\t\t\tfore = fore[:-1]\n\t\tfor line in answer:\n\t\t\tprint(' '.join(list(map(str, line))))\n\telse:\n\t\tprint('NO')\n", "import sys\ndl = 0\nsrodek = 0\nsr = 0\nn = int(input())\ntab = [int(i) for i in input().split()]\nnumof = [0] * 1001\nfor i in tab:\n\tnumof[i] += 1\nczworki = []\nfor i in range(1001):\n\tif numof[i] > 0:\n\t\tczworki.append(i)\nmatrix = [[0 for x in range(n)] for y in range(n)]\npom = 1\nzm = 0\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tzm = 0\n\t\tif len(czworki) > 0:\n\t\t\tpom = 1\n\t\t\twhile pom:\n\t\t\t\tif len(czworki) == 0:\n\t\t\t\t\tbreak\n\t\t\t\telse:\n\t\t\t\t\tdl = len(czworki) - 1\n\t\t\t\t\tif numof[czworki[dl]] >= 4:\n\t\t\t\t\t\tmatrix[i][j] = czworki[dl]\n\t\t\t\t\t\tmatrix[i][n - 1 - j] = czworki[dl]\n\t\t\t\t\t\tmatrix[n - 1 - i][j] = czworki[dl]\n\t\t\t\t\t\tmatrix[n - 1 - i][n - 1 - j] = czworki[dl]\n\t\t\t\t\t\tnumof[czworki[dl]] -= 4\n\t\t\t\t\t\tpom = 0\n\t\t\t\t\t\tzm = 1\n\t\t\t\t\tif numof[czworki[dl]] < 4:\n\t\t\t\t\t\tczworki.pop()\n\t\tif len(czworki) == 0 and zm == 0:\n\t\t\tprint('NO')\n\t\t\tsys.exit(0)\nif n % 2 == 1:\n\tk = n // 2\n\tduzo = []\n\tfor i in range(1001):\n\t\tif numof[i] > 3:\n\t\t\tduzo.append(i)\n\tt = len(duzo)\n\tchuj = -1\n\tfor i in range(n):\n\t\tzm = 0\n\t\tif len(duzo) > 0:\n\t\t\tpom = 1\n\t\t\twhile pom:\n\t\t\t\tif len(duzo) == 0:\n\t\t\t\t\tbreak\n\t\t\t\telse:\n\t\t\t\t\tdl = len(duzo) - 1\n\t\t\t\t\tif numof[duzo[dl]] >= 4:\n\t\t\t\t\t\tmatrix[i][k] = duzo[dl]\n\t\t\t\t\t\tmatrix[k][i] = duzo[dl]\n\t\t\t\t\t\tmatrix[n - 1 - i][k] = duzo[dl]\n\t\t\t\t\t\tmatrix[k][n - 1 - i] = duzo[dl]\n\t\t\t\t\t\tnumof[duzo[dl]] -= 4\n\t\t\t\t\t\tpom = 0\n\t\t\t\t\t\tzm = 1\n\t\t\t\t\tif numof[duzo[dl]] < 4:\n\t\t\t\t\t\tduzo.pop()\n\t\tif len(duzo) == 0 and chuj == -1:\n\t\t\tchuj = i\n\tif t == 0:\n\t\tchuj = -1\n\tdupa = 0\n\tfor i in range(1001):\n\t\tif numof[i] % 2 == 1:\n\t\t\tnumof[i] -= 1\n\t\t\tif dupa == 0:\n\t\t\t\tdupa = 1\n\t\t\t\tsrodek = i\n\tziomki = []\n\tfor i in range(1001):\n\t\tif numof[i] == 2:\n\t\t\tziomki.append(i)\n\tif len(ziomki) < n - 1 - 2 * (chuj + 1):\n\t\tprint('NO')\n\t\tsys.exit(0)\n\telse:\n\t\tfor i in range(chuj + 1, n // 2):\n\t\t\tmatrix[k][i] = ziomki[len(ziomki) - 1]\n\t\t\tmatrix[k][n - 1 - i] = ziomki[len(ziomki) - 1]\n\t\t\tziomki.pop()\n\t\t\tmatrix[i][k] = ziomki[len(ziomki) - 1]\n\t\t\tmatrix[n - 1 - i][k] = ziomki[len(ziomki) - 1]\n\t\t\tziomki.pop()\n\t\tmatrix[n // 2][n // 2] = srodek\nprint('YES', end='\\n')\nfor i in range(n):\n\tfor j in range(n):\n\t\tif j < n - 1:\n\t\t\tprint(matrix[i][j], end=' ')\n\t\telse:\n\t\t\tprint(matrix[i][j], end='\\n')\n", "n = int(input())\nar = list(map(int, input().split()))\ndic = {}\nfor i in range(n * n):\n\tif ar[i] in dic:\n\t\tdic[ar[i]] += 1\n\telse:\n\t\tdic[ar[i]] = 1\nans = []\nfor i in range(n):\n\ttem = []\n\tfor j in range(n):\n\t\ttem.append(0)\n\tans.append(tem)\nflag = True\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tif max(list(dic.values())) < 4:\n\t\t\tflag = False\n\t\telse:\n\t\t\tfor k in dic:\n\t\t\t\tif dic[k] >= 4:\n\t\t\t\t\tdic[k] -= 4\n\t\t\t\t\tans[i][j] = k\n\t\t\t\t\tans[n - i - 1][j] = k\n\t\t\t\t\tans[i][n - j - 1] = k\n\t\t\t\t\tans[n - i - 1][n - j - 1] = k\n\t\t\t\t\tbreak\nif n % 2 != 0:\n\tfor j in range(n // 2):\n\t\tif max(list(dic.values())) < 2:\n\t\t\tflag = False\n\t\telse:\n\t\t\tfor k in dic:\n\t\t\t\tif dic[k] >= 2:\n\t\t\t\t\tdic[k] -= 2\n\t\t\t\t\tans[j][n // 2] = k\n\t\t\t\t\tans[n - j - 1][n // 2] = k\n\t\t\t\t\tbreak\n\tfor j in range(n // 2):\n\t\tif max(list(dic.values())) < 2:\n\t\t\tflag = False\n\t\telse:\n\t\t\tfor k in dic:\n\t\t\t\tif dic[k] >= 2:\n\t\t\t\t\tdic[k] -= 2\n\t\t\t\t\tans[n // 2][j] = k\n\t\t\t\t\tans[n // 2][n - j - 1] = k\n\t\t\t\t\tbreak\n\tfor k in dic:\n\t\tif dic[k] != 0:\n\t\t\tans[n // 2][n // 2] = k\nif flag:\n\tprint('YES')\n\tfor i in ans:\n\t\tprint(*i)\nelse:\n\tprint('NO')\n", "n = int(input())\na = list(map(int, input().split()))\nfrom collections import defaultdict as dc\nd = dc(int)\nfor i in a:\n\td[i] += 1\nch = 0\nfor i in d:\n\tif d[i] % 2 != 0:\n\t\tch += 1\n\tif ch > 1:\n\t\tprint('NO')\n\t\texit()\nl = len(d)\nch = 0\na1 = (n // 2) ** 2\nif n % 2 == 0:\n\tif l > a1:\n\t\tch = 1\nelif l > a1 + (n ** 2 - a1 * 4 - 1) // 2 + 1:\n\tch = 1\nif ch == 1:\n\tprint('NO')\n\texit()\nans = [[0 for j in range(n)] for i in range(n)]\nsa = [[i, d[i]] for i in d]\nsa.sort(reverse=True, key=lambda x: x[1])\nch1 = 0\ntry:\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\twhile sa[ch1][1] < 4:\n\t\t\t\tch1 += 1\n\t\t\tans[i][j] = sa[ch1][0]\n\t\t\tans[n - i - 1][j] = sa[ch1][0]\n\t\t\tans[i][n - j - 1] = sa[ch1][0]\n\t\t\tans[n - i - 1][n - j - 1] = sa[ch1][0]\n\t\t\tsa[ch1][1] -= 4\n\tif n % 2 != 0:\n\t\tss = []\n\t\tfor (i, j) in sa:\n\t\t\tif j != 0:\n\t\t\t\tss.append([i, j])\n\t\tss.sort(reverse=True, key=lambda x: x[1])\n\t\tch1 = 0\n\t\tfor i in range(n // 2):\n\t\t\twhile ss[ch1][1] < 2:\n\t\t\t\tch1 += 1\n\t\t\tans[n // 2][i] = ss[ch1][0]\n\t\t\tans[n // 2][n - i - 1] = ss[ch1][0]\n\t\t\tss[ch1][1] -= 2\n\t\tfor i in range(n // 2):\n\t\t\twhile ss[ch1][1] < 2:\n\t\t\t\tch1 += 1\n\t\t\tans[i][n // 2] = ss[ch1][0]\n\t\t\tans[n - i - 1][n // 2] = ss[ch1][0]\n\t\t\tss[ch1][1] -= 2\n\t\tfor (i, j) in ss:\n\t\t\tif j != 0:\n\t\t\t\tans[n // 2][n // 2] = i\n\t\t\t\tbreak\nexcept:\n\tprint('NO')\n\texit()\nprint('YES')\nfor i in ans:\n\tprint(*i)\n", "from sys import stdin, stdout\nfrom collections import *\nfrom math import gcd, floor, ceil\nst = lambda : list(stdin.readline().strip())\nli = lambda : list(map(int, stdin.readline().split()))\nmp = lambda : map(int, stdin.readline().split())\ninp = lambda : int(stdin.readline())\npr = lambda n: stdout.write(str(n) + '\\n')\nmod = 1000000007\nINF = float('inf')\n\ndef solve():\n\tn = inp()\n\tl = li()\n\td = Counter(l)\n\tele = -1\n\todd = 0\n\t(v, o) = ([], [])\n\tfor i in d:\n\t\tif d[i] & 1:\n\t\t\tif n % 2 == 0:\n\t\t\t\tpr('NO')\n\t\t\t\treturn\n\t\t\telse:\n\t\t\t\todd += 1\n\t\t\t\tele = i\n\t\t\t\tif odd > 1:\n\t\t\t\t\tpr('NO')\n\t\t\t\t\treturn\n\t\tif d[i] % 2 == 0:\n\t\t\tv.append(i)\n\t\t\tif d[i] % 4 == 2:\n\t\t\t\to.append(i)\n\tif (d[ele] - 1) % 2 == 0:\n\t\tv.append(ele)\n\t\tif (d[ele] - 1) % 4 == 2:\n\t\t\to.append(ele)\n\ti = 0\n\tv.sort(key=lambda x: d[x], reverse=True)\n\tans = [[0 for i in range(n)] for j in range(n)]\n\tif n & 1:\n\t\tans[n // 2][n // 2] = ele\n\t\td[ele] -= 1\n\t\t(j, k) = (n // 2, 0)\n\t\ti = 0\n\t\twhile k < n // 2 and i < len(o):\n\t\t\tans[j][k] = o[i]\n\t\t\tans[j][n - 1 - k] = o[i]\n\t\t\tk += 1\n\t\t\td[o[i]] -= 2\n\t\t\ti += 1\n\t\t(j, k) = (0, n // 2)\n\t\twhile j < n // 2 and i < len(o):\n\t\t\tans[j][k] = o[i]\n\t\t\tans[n - 1 - j][k] = o[i]\n\t\t\td[o[i]] -= 2\n\t\t\ti += 1\n\t\t\tj += 1\n\t\ti = 0\n\t\t(j, k) = (n // 2, 0)\n\t\twhile i < len(v):\n\t\t\twhile k < n // 2 and d[v[i]] > 0:\n\t\t\t\tif ans[j][k] == 0:\n\t\t\t\t\tans[j][k] = v[i]\n\t\t\t\t\tans[j][n - 1 - k] = v[i]\n\t\t\t\t\td[v[i]] -= 2\n\t\t\t\tk += 1\n\t\t\ti += 1\n\t\ti = 0\n\t\t(j, k) = (0, n // 2)\n\t\twhile i < len(v):\n\t\t\twhile j < n // 2 and d[v[i]] > 0:\n\t\t\t\tif ans[j][k] == 0:\n\t\t\t\t\tans[j][k] = v[i]\n\t\t\t\t\tans[n - 1 - j][k] = v[i]\n\t\t\t\t\td[v[i]] -= 2\n\t\t\t\tj += 1\n\t\t\ti += 1\n\tx = [[d[i], i] for i in d if d[i]]\n\t(p, q) = (0, 0)\n\ti = 0\n\twhile i < len(x):\n\t\t(a, b) = x[i]\n\t\tif a % 4:\n\t\t\tpr('NO')\n\t\t\treturn\n\t\tif p < n // 2:\n\t\t\twhile a > 0:\n\t\t\t\tans[p][q] = b\n\t\t\t\tans[p][n - 1 - q] = b\n\t\t\t\tans[n - 1 - p][q] = b\n\t\t\t\tans[n - 1 - p][n - 1 - q] = b\n\t\t\t\ta -= 4\n\t\t\t\tif a > 0 and a % 4 == 0:\n\t\t\t\t\tp += 1\n\t\t\t\t\tif p == n // 2:\n\t\t\t\t\t\tp = 0\n\t\t\t\t\t\tq += 1\n\t\t\tp += 1\n\t\t\tif p == n // 2:\n\t\t\t\tp = 0\n\t\t\t\tq += 1\n\t\ti += 1\n\tpr('YES')\n\tfor i in ans:\n\t\tprint(*i)\nsolve()\n", "import ctypes\n\nclass Array:\n\n\tdef __init__(self, size):\n\t\tassert size > 0\n\t\tself.size = size\n\t\tpy_array = ctypes.py_object * size\n\t\tself.items = py_array()\n\n\tdef __len__(self):\n\t\treturn self.size\n\n\tdef __getitem__(self, index):\n\t\tassert 0 <= index < len(self)\n\t\treturn self.items[index]\n\n\tdef __setitem__(self, index, value):\n\t\tassert 0 <= index < len(self)\n\t\tself.items[index] = value\nn = int(input())\na = list(map(int, input().split()))\nd_count = {}\nmatrix = Array(n)\ns = len(a)\nfor i in range(n):\n\tmatrix[i] = Array(n)\nfor i in range(n ** 2):\n\tif a[i] in d_count:\n\t\td_count[a[i]] += 1\n\telse:\n\t\td_count[a[i]] = 1\nif n % 2:\n\t(i, j) = (0, 0)\n\tcount = 0\n\tfor key in d_count:\n\t\twhile d_count[key] >= 4 and i <= n // 2 - 1:\n\t\t\tmatrix[i][j] = matrix[i][n - 1 - j] = matrix[n - 1 - i][j] = matrix[n - 1 - i][n - 1 - j] = key\n\t\t\td_count[key] -= 4\n\t\t\tcount += 4\n\t\t\tj += 1\n\t\t\tif j > n // 2 - 1:\n\t\t\t\tj = 0\n\t\t\t\ti += 1\n\t\tif i > n // 2 - 1:\n\t\t\tbreak\n\ti = n // 2\n\tj = 0\n\tfor key in d_count:\n\t\twhile d_count[key] >= 2 and j < n // 2:\n\t\t\tmatrix[i][j] = matrix[i][n - 1 - j] = key\n\t\t\td_count[key] -= 2\n\t\t\tcount += 2\n\t\t\tj += 1\n\t\tif j == n // 2:\n\t\t\tbreak\n\ti = 0\n\tj = n // 2\n\tfor key in d_count:\n\t\twhile d_count[key] >= 2 and i < n // 2:\n\t\t\tmatrix[i][j] = matrix[n - 1 - i][j] = key\n\t\t\td_count[key] -= 2\n\t\t\tcount += 2\n\t\t\ti += 1\n\t\tif i == n // 2:\n\t\t\tbreak\n\ti = j = n // 2\n\tfor key in d_count:\n\t\tif d_count[key] == 1:\n\t\t\td_count[key] -= 1\n\t\t\tcount += 1\n\t\t\tmatrix[i][j] = key\n\t\t\tbreak\n\tif count == n * n:\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tprint(matrix[i][j], end=' ')\n\t\t\tprint('')\n\telse:\n\t\tprint('NO')\nelse:\n\tcount = 0\n\t(i, j) = (0, 0)\n\tfor key in d_count:\n\t\twhile d_count[key] and count < n * n:\n\t\t\tmatrix[i][j] = matrix[i][n - 1 - j] = matrix[n - 1 - i][j] = matrix[n - 1 - i][n - 1 - j] = key\n\t\t\td_count[key] -= 4\n\t\t\tcount += 4\n\t\t\tj += 1\n\t\t\tif j > (n - 1) // 2:\n\t\t\t\tj = 0\n\t\t\t\ti += 1\n\tpossible = True\n\tfor key in d_count:\n\t\tif d_count[key]:\n\t\t\tpossible = False\n\t\t\tbreak\n\tif possible:\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tprint(matrix[i][j], end=' ')\n\t\t\tprint('')\n\telse:\n\t\tprint('NO')\n", "def mi():\n\treturn map(int, input().split())\nn = int(input())\na = list(mi())\ncurInd = 0\nfrom collections import Counter as C\ntemp = C(a)\ndb4 = []\ndb2 = []\ndb1 = []\nfor i in temp:\n\twhile temp[i] >= 4:\n\t\tdb4.append(i)\n\t\ttemp[i] -= 4\n\twhile temp[i] >= 2:\n\t\tdb2.append(i)\n\t\ttemp[i] -= 2\n\twhile temp[i] >= 1:\n\t\tdb1.append(i)\n\t\ttemp[i] -= 1\no = [[10000000000.0] * n for i in range(n)]\nldb4 = len(db4)\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tif curInd == ldb4:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\t\to[i][j] = o[-1 - i][-1 - j] = o[i][-1 - j] = o[-1 - i][j] = db4[curInd]\n\t\tcurInd += 1\nci1 = ci2 = 0\nldb2 = len(db2)\nldb1 = len(db1)\nif n % 2:\n\tflag = 0\n\tf1 = 0\n\tfor i in range(n // 2):\n\t\tif flag == 1:\n\t\t\tflag = 0\n\t\t\tcontinue\n\t\tif curInd != ldb4:\n\t\t\tif i == n // 2 - 1:\n\t\t\t\to[i][n // 2] = o[-1 - i][n // 2] = o[n // 2][0] = o[n // 2][-1] = db4[curInd]\n\t\t\t\tf1 = 1\n\t\t\telse:\n\t\t\t\to[i][n // 2] = o[i + 1][n // 2] = o[-1 - i][n // 2] = o[-i - 1 - 1][n // 2] = db4[curInd]\n\t\t\tcurInd += 1\n\t\t\tflag = 1\n\t\telif ci2 != ldb2:\n\t\t\to[i][n // 2] = o[-1 - i][n // 2] = db2[ci2]\n\t\t\tci2 += 1\n\t\telif ci1 != ldb1:\n\t\t\to[i][n // 2] = db1[ci1]\n\t\t\tci1 += 1\n\tflag = 0\n\tfor i in range(n // 2):\n\t\tif f1 and i == 0:\n\t\t\tcontinue\n\t\tif flag == 1:\n\t\t\tflag = 0\n\t\t\tcontinue\n\t\tif curInd != ldb4:\n\t\t\to[n // 2][i] = o[n // 2][i + 1] = o[n // 2][-1 - i] = o[n // 2][-1 - i - 1] = db4[curInd]\n\t\t\tcurInd += 1\n\t\t\tflag = 1\n\t\telif ci2 != ldb2:\n\t\t\to[n // 2][i] = o[n // 2][-i - 1] = db2[ci2]\n\t\t\tci2 += 1\n\t\telif ci1 != ldb1:\n\t\t\to[n // 2][i] = db1[ci1]\n\t\t\tci1 += 1\n\tif ldb1 != ci1:\n\t\to[n // 2][n // 2] = db1[ci1]\n\t\tci1 += 1\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tt = [o[i][j], o[-1 - i][-1 - j], o[i][-1 - j], o[-1 - i][j]]\n\t\tif min(t) != max(t) or max(t) == 10000000000.0:\n\t\t\tprint('NO')\n\t\t\texit(0)\nif n % 2:\n\tfor i in range(n):\n\t\tt = [o[i][n // 2], o[-1 - i][n // 2]]\n\t\tif min(t) != max(t) or max(t) == 10000000000.0:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\t\tt = [o[n // 2][i], o[n // 2][-1 - i]]\n\t\tif min(t) != max(t) or max(t) == 10000000000.0:\n\t\t\tprint('NO')\n\t\t\texit(0)\nprint('YES')\nfor i in range(n):\n\tfor j in range(n):\n\t\tprint(o[i][j], end=' ')\n\tprint()\n", "from collections import *\nfrom math import *\nn = int(input())\na = list(map(int, input().split()))\n(ct4, ct2, ct1) = ([], [], [])\nd = dict()\nfor i in a:\n\tif i not in d:\n\t\td[i] = 0\n\td[i] += 1\n\tif d[i] == 4:\n\t\td[i] = 0\n\t\tct4.append(i)\nfor (k, v) in d.items():\n\tif v == 0:\n\t\tcontinue\n\tif v >= 2:\n\t\tct2.append(k)\n\t\tv -= 2\n\tif v == 1:\n\t\tct1.append(k)\nif n & 1 and len(ct1) != 1:\n\tprint('NO')\n\texit(0)\nans = [[0 for j in range(n)] for i in range(n)]\nfor i in range((n + 1) // 2):\n\tfor j in range((n + 1) // 2):\n\t\tif j == n - j - 1 and i == n - i - 1:\n\t\t\tct = 1\n\t\telif i == n - i - 1 or j == n - j - 1:\n\t\t\tct = 2\n\t\telse:\n\t\t\tct = 4\n\t\tif ct == 4:\n\t\t\tif len(ct4) == 0:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\t\t\ty = ct4[-1]\n\t\t\tct4.pop()\n\t\telif ct == 2:\n\t\t\tif len(ct2) == 0:\n\t\t\t\tif len(ct4) == 0:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\texit(0)\n\t\t\t\ty = ct4[-1]\n\t\t\t\tct4.pop()\n\t\t\t\tct2.append(y)\n\t\t\telse:\n\t\t\t\ty = ct2[-1]\n\t\t\t\tct2.pop()\n\t\telse:\n\t\t\ty = ct1[-1]\n\t\t\tct1.pop()\n\t\t(ans[i][j], ans[i][n - j - 1], ans[n - i - 1][j], ans[n - i - 1][n - j - 1]) = (y, y, y, y)\nprint('YES')\nfor i in ans:\n\tprint(*i)\n", "n = int(input())\na = [int(x) for x in input().split()]\na.sort()\nd = {}\nMatrix = [[0 for x in range(n)] for y in range(n)]\nfor x in a:\n\tif x in d:\n\t\td[x] += 1\n\telse:\n\t\td[x] = 1\nsum = 0\nfor x in d:\n\tif d[x] % 2 == 1 and n & 1 and (Matrix[n // 2][n // 2] == 0):\n\t\td[x] -= 1\n\t\tMatrix[n // 2][n // 2] = x\n\tif d[x] % 4 == 2:\n\t\tsum += 1\n\tif d[x] % 2 == 1:\n\t\tprint('NO')\n\t\tbreak\nelse:\n\tif sum & 1:\n\t\tprint('NO')\n\telif sum > 0 and n % 2 == 0:\n\t\tprint('NO')\n\telif sum > n - 1:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tf = 1\n\t\tfor x in d:\n\t\t\tif d[x] % 4 == 2:\n\t\t\t\tif f == 1:\n\t\t\t\t\tfor i in range(n):\n\t\t\t\t\t\tif Matrix[i][n // 2] == 0:\n\t\t\t\t\t\t\tMatrix[i][n // 2] = Matrix[n - i - 1][n // 2] = x\n\t\t\t\t\t\t\td[x] -= 2\n\t\t\t\t\t\t\tfla = True\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\tf = 0\n\t\t\t\telse:\n\t\t\t\t\tfor i in range(n):\n\t\t\t\t\t\tif Matrix[n // 2][i] == 0:\n\t\t\t\t\t\t\tMatrix[n // 2][i] = Matrix[n // 2][n - i - 1] = x\n\t\t\t\t\t\t\td[x] -= 2\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\tf = 1\n\t\tfor x in d:\n\t\t\twhile d[x] > 0:\n\t\t\t\tfla = False\n\t\t\t\tfor i in range(n):\n\t\t\t\t\tfor j in range(n):\n\t\t\t\t\t\tif Matrix[i][j] == 0:\n\t\t\t\t\t\t\tMatrix[i][j] = Matrix[i][n - j - 1] = Matrix[n - i - 1][j] = Matrix[n - i - 1][n - j - 1] = x\n\t\t\t\t\t\t\tif n & 1 and j == n // 2:\n\t\t\t\t\t\t\t\tMatrix[n - j - 1][i] = Matrix[n - j - 1][n - i - 1] = x\n\t\t\t\t\t\t\tfla = True\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\tif fla:\n\t\t\t\t\t\tbreak\n\t\t\t\td[x] -= 4\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tprint(Matrix[i][j], end=' ')\n\t\t\tprint('')\n", "from collections import Counter\nn = int(input())\na = list(map(int, input().split()))\nctr = Counter(a)\n\ndef mp(r, size):\n\tfor i in range(size):\n\t\tfor j in range(size):\n\t\t\tprint(r[i][j], end='')\n\t\t\tprint(' ' if j < size - 1 else '', end='')\n\t\tprint('')\nif n % 2 == 0:\n\tb = True\n\tfor i in range(1, max(a) + 1):\n\t\tif ctr[i] % 4 != 0:\n\t\t\tb = False\n\t\t\tbreak\n\tprint('yes' if b else 'no')\n\tif b:\n\t\tt = [[0 for i in range(n)] for j in range(n)]\n\t\tx = y = 0\n\t\tfor i in range(1, max(a) + 1):\n\t\t\twhile ctr[i] > 0:\n\t\t\t\tt[x][y] = t[n - x - 1][y] = t[x][n - y - 1] = t[n - x - 1][n - y - 1] = i\n\t\t\t\ty += 0 if x < n // 2 - 1 else 1\n\t\t\t\tx = x + 1 if x < n // 2 - 1 else 0\n\t\t\t\tctr[i] -= 4\n\t\tmp(t, n)\nelse:\n\todd = two = four = temp = 0\n\tb = True\n\tfor i in range(1, max(a) + 1):\n\t\tif ctr[i] % 2 == 1:\n\t\t\todd += 1\n\t\t\ttemp = i\n\t\telif ctr[i] % 4 == 0 and ctr[i] != 0:\n\t\t\tfour += 1\n\t\telif ctr[i] % 4 == 2 and ctr[i] != 0:\n\t\t\ttwo += 1\n\tif odd != 1:\n\t\tprint('no')\n\t\tb = False\n\telse:\n\t\tif (ctr[temp] - 1) % 4 == 0:\n\t\t\tfour += 1\n\t\telse:\n\t\t\ttwo += 1\n\t\tprint('yes' if two <= n - 1 or n == 1 else 'no')\n\t\tb = True if two <= n - 1 or n == 1 else False\n\tif b:\n\t\tt = [[0 for i in range(n)] for j in range(n)]\n\t\tt[n // 2][n // 2] = temp\n\t\tctr[temp] -= 1\n\t\tx = y = 0\n\t\ttt = 0\n\t\tfor i in range(1, max(a) + 1):\n\t\t\twhile ctr[i] > 0 and (ctr[i] % 4 == 0 or ctr[i] > 4) and (y < n // 2):\n\t\t\t\tt[x][y] = t[n - x - 1][y] = t[x][n - y - 1] = t[n - x - 1][n - y - 1] = i\n\t\t\t\ty += 0 if x < n // 2 - 1 else 1\n\t\t\t\tx = x + 1 if x < n // 2 - 1 else 0\n\t\t\t\tctr[i] -= 4\n\t\t\twhile ctr[i] > 0:\n\t\t\t\tif tt < n // 2:\n\t\t\t\t\tt[tt][n // 2] = t[n - tt - 1][n // 2] = i\n\t\t\t\t\tctr[i] -= 2\n\t\t\t\t\ttt += 1\n\t\t\t\telse:\n\t\t\t\t\tt[n // 2][tt - n // 2] = t[n // 2][n - tt + n // 2 - 1] = i\n\t\t\t\t\tctr[i] -= 2\n\t\t\t\t\ttt += 1\n\t\tmp(t, n)\n", "n = int(input())\nl = list(map(int, input().split()))\nd = dict()\nfor j in range(n * n):\n\tif l[j] not in d:\n\t\td.update({l[j]: 1})\n\telse:\n\t\td[l[j]] += 1\nfour = 0\nt = 0\nf = 0\nfor j in d:\n\tif d[j] % 2 == 1:\n\t\tif n % 2 == 1 and t == 0:\n\t\t\tt = 1\n\t\telse:\n\t\t\tf = 1\n\t\t\tbreak\n\tif d[j] >= 4:\n\t\tfour += d[j] // 4\nif f == 1 or four < (n // 2) ** 2:\n\tprint('NO')\nelse:\n\tprint('YES')\n\ta = [[0 for j in range(n)] for i in range(n)]\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tfor k in d:\n\t\t\t\tif d[k] >= 4:\n\t\t\t\t\ta[i][j] = k\n\t\t\t\t\ta[n - 1 - i][j] = k\n\t\t\t\t\ta[i][n - 1 - j] = k\n\t\t\t\t\ta[n - 1 - i][n - 1 - j] = k\n\t\t\t\t\td[k] -= 4\n\t\t\t\t\tif d[k] == 0:\n\t\t\t\t\t\tdel d[k]\n\t\t\t\t\tbreak\n\tif n % 2 == 1:\n\t\tfor i in range(n // 2):\n\t\t\tfor k in d:\n\t\t\t\tif d[k] >= 2:\n\t\t\t\t\ta[i][n // 2] = k\n\t\t\t\t\ta[n - 1 - i][n // 2] = k\n\t\t\t\t\td[k] -= 2\n\t\t\t\t\tif d[k] == 0:\n\t\t\t\t\t\tdel d[k]\n\t\t\t\t\tbreak\n\t\tfor i in range(n // 2):\n\t\t\tfor k in d:\n\t\t\t\tif d[k] >= 2:\n\t\t\t\t\ta[n // 2][i] = k\n\t\t\t\t\ta[n // 2][n - 1 - i] = k\n\t\t\t\t\td[k] -= 2\n\t\t\t\t\tif d[k] == 0:\n\t\t\t\t\t\tdel d[k]\n\t\t\t\t\tbreak\n\t\tfor k in d:\n\t\t\tif d[k] == 1:\n\t\t\t\ta[n // 2][n // 2] = k\n\tfor i in range(n):\n\t\tprint(*a[i], sep=' ')\n", "n = int(input())\nA = list(map(int, input().split()))\n\ndef Find(x, par):\n\tif par[x] < 0:\n\t\treturn x\n\telse:\n\t\tpar[x] = Find(par[x], par)\n\t\treturn par[x]\n\ndef Unite(x, y, par, rank):\n\tx = Find(x, par)\n\ty = Find(y, par)\n\tif x != y:\n\t\tif rank[x] < rank[y]:\n\t\t\tpar[y] += par[x]\n\t\t\tpar[x] = y\n\t\telse:\n\t\t\tpar[x] += par[y]\n\t\t\tpar[y] = x\n\t\t\tif rank[x] == rank[y]:\n\t\t\t\trank[x] += 1\n\ndef Same(x, y, par):\n\treturn Find(x, par) == Find(y, par)\n\ndef Size(x, par):\n\treturn -par[Find(x, par)]\npar = [-1] * n ** 2\nrank = [0] * n ** 2\nfor i in range(n):\n\tfor j in range(n):\n\t\tidx1 = i * n + j\n\t\tidx2 = i * n + (n - 1 - j)\n\t\tidx3 = (n - 1 - i) * n + j\n\t\tUnite(idx1, idx2, par, rank)\n\t\tUnite(idx1, idx3, par, rank)\nS = []\nfrom collections import defaultdict\nd = defaultdict(lambda : [])\nfor i in range(n ** 2):\n\tif par[i] < 0:\n\t\tS.append((-par[i], i))\n\t\td[i].append(i)\n\telse:\n\t\td[Find(i, par)].append(i)\nS.sort(reverse=True)\nC = [0] * (1000 + 1)\nfor i in range(n ** 2):\n\tC[A[i]] += 1\nC = [(-c, i) for (i, c) in enumerate(C)]\nimport heapq\nheapq.heapify(C)\nans = [[0] * n for _ in range(n)]\nfor (s, i) in S:\n\t(c, j) = heapq.heappop(C)\n\tc *= -1\n\tif c >= s:\n\t\tfor idx in d[i]:\n\t\t\t(row, col) = divmod(idx, n)\n\t\t\tans[row][col] = j\n\t\tc -= s\n\t\theapq.heappush(C, (-c, j))\n\telse:\n\t\tprint('NO')\n\t\texit()\nelse:\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*ans[i])\n", "n = int(input())\na = list(map(int, input().split()))\nh = {}\nfor i in range(n ** 2):\n\tif a[i] in h:\n\t\th[a[i]].append(i)\n\telse:\n\t\th[a[i]] = [i]\nf = 0\nc1 = c2 = 0\nif n % 2 == 0:\n\tfor i in h:\n\t\tif len(h[i]) % 4 != 0:\n\t\t\tf = 1\n\t\t\tbreak\nelse:\n\tfor i in h:\n\t\tif len(h[i]) % 4 != 0:\n\t\t\tif len(h[i]) % 4 == 1:\n\t\t\t\tif c1 == 0:\n\t\t\t\t\tc1 += 1\n\t\t\t\telse:\n\t\t\t\t\tf = 1\n\t\t\t\t\tbreak\n\t\t\telif len(h[i]) % 4 == 2:\n\t\t\t\tif c2 <= n - 1:\n\t\t\t\t\tc2 += 1\n\t\t\t\telse:\n\t\t\t\t\tf = 1\n\t\t\t\t\tbreak\nif f == 1 or c1 > 1 or c2 > n - 1:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tif n % 2 == 0:\n\t\tmat = [[0 for i in range(n)] for i in range(n)]\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tmat[i][j] = a[0]\n\t\t\t\tmat[i][n - j - 1] = a[0]\n\t\t\t\tmat[n - i - 1][n - j - 1] = a[0]\n\t\t\t\tmat[n - i - 1][j] = a[0]\n\t\t\t\tr = a[0]\n\t\t\t\ta.remove(r)\n\t\t\t\ta.remove(r)\n\t\t\t\ta.remove(r)\n\t\t\t\ta.remove(r)\n\t\tfor i in range(n):\n\t\t\tprint(*mat[i])\n\telse:\n\t\tmat = [[0 for i in range(n)] for i in range(n)]\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tfor k in h:\n\t\t\t\t\tif len(h[k]) % 4 == 0 and len(h[k]) != 0 or (len(h[k]) % 4 == 2 and len(h[k]) != 2) or (len(h[k]) % 4 == 1 and len(h[k]) != 1) or (len(h[k]) % 4 == 3 and len(h[k]) != 3):\n\t\t\t\t\t\tci = k\n\t\t\t\tc = a.index(ci)\n\t\t\t\tmat[i][j] = a[c]\n\t\t\t\tmat[i][n - j - 1] = a[c]\n\t\t\t\tmat[n - i - 1][n - j - 1] = a[c]\n\t\t\t\tmat[n - i - 1][j] = a[c]\n\t\t\t\tr = a[c]\n\t\t\t\ta.remove(r)\n\t\t\t\ta.remove(r)\n\t\t\t\ta.remove(r)\n\t\t\t\ta.remove(r)\n\t\t\t\tdel h[ci][0]\n\t\t\t\tdel h[ci][0]\n\t\t\t\tdel h[ci][0]\n\t\t\t\tdel h[ci][0]\n\t\tfor i in h:\n\t\t\tif len(h[i]) % 4 % 2 == 1:\n\t\t\t\tmid = i\n\t\tmat[n // 2][n // 2] = mid\n\t\tdel h[mid][0]\n\t\ta.remove(mid)\n\t\tfor i in range(n // 2):\n\t\t\tmat[i][n // 2] = a[0]\n\t\t\tmat[n - i - 1][n // 2] = a[0]\n\t\t\tr = a[0]\n\t\t\ta.remove(r)\n\t\t\ta.remove(r)\n\t\tfor i in range(n // 2):\n\t\t\tmat[n // 2][i] = a[0]\n\t\t\tmat[n // 2][n - i - 1] = a[0]\n\t\t\tr = a[0]\n\t\t\ta.remove(r)\n\t\t\ta.remove(r)\n\t\tfor i in range(n):\n\t\t\tprint(*mat[i])\n", "n = int(input())\na = list(map(int, input().split()))\no = [0] * 1001\nfor i in range(n * n):\n\to[a[i]] += 1\nif n % 2 == 0:\n\tfor i in range(1, 1001):\n\t\tif o[i] % 4 != 0:\n\t\t\tprint('NO')\n\t\t\texit()\n\tans = [[0] * n for i in range(n)]\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tfor k in range(1001):\n\t\t\t\tif o[k] != 0:\n\t\t\t\t\tans[i][j] = k\n\t\t\t\t\tans[n - i - 1][j] = k\n\t\t\t\t\tans[i][n - j - 1] = k\n\t\t\t\t\tans[n - i - 1][n - j - 1] = k\n\t\t\t\t\to[k] -= 4\n\t\t\t\t\tbreak\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(ans[i][j], end=' ')\n\t\tprint()\nelse:\n\tf1 = 0\n\tf2 = 1\n\tfor i in range(1, 1001):\n\t\tif o[i] % 4 != 0:\n\t\t\tif o[i] % 4 == 2:\n\t\t\t\tf1 += 1\n\t\t\telif o[i] % 4 == 1:\n\t\t\t\tif f2 == 1:\n\t\t\t\t\tf2 = 0\n\t\t\t\telse:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\texit()\n\t\t\telif o[i] % 4 == 3:\n\t\t\t\tif f2 == 1:\n\t\t\t\t\tf2 = 0\n\t\t\t\t\tf1 += 1\n\t\t\t\telse:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\texit()\n\t\t\telse:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\tif f1 >= n:\n\t\tprint('NO')\n\t\texit()\n\tans = [[0] * n for i in range(n)]\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tfor k in range(1001):\n\t\t\t\tif o[k] > 3:\n\t\t\t\t\tans[i][j] = k\n\t\t\t\t\tans[n - i - 1][j] = k\n\t\t\t\t\tans[i][n - j - 1] = k\n\t\t\t\t\tans[n - i - 1][n - j - 1] = k\n\t\t\t\t\to[k] -= 4\n\t\t\t\t\tbreak\n\tfor i in range(n // 2 + 1):\n\t\tfor j in range(n // 2 + 1):\n\t\t\tif i == n // 2 and j == n // 2:\n\t\t\t\tfor k in range(1001):\n\t\t\t\t\tif o[k] % 2 == 1:\n\t\t\t\t\t\tans[i][j] = k\n\t\t\t\t\t\to[k] -= 1\n\t\t\t\t\t\tbreak\n\t\t\tif i == n // 2:\n\t\t\t\tfor k in range(1001):\n\t\t\t\t\tif o[k] > 1:\n\t\t\t\t\t\tans[i][j] = k\n\t\t\t\t\t\tans[i][n - j - 1] = k\n\t\t\t\t\t\to[k] -= 2\n\t\t\t\t\t\tbreak\n\t\t\tif j == n // 2:\n\t\t\t\tfor k in range(1, 1001):\n\t\t\t\t\tif o[k] > 1:\n\t\t\t\t\t\tans[i][j] = k\n\t\t\t\t\t\tans[n - i - 1][j] = k\n\t\t\t\t\t\to[k] -= 2\n\t\t\t\t\t\tbreak\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(ans[i][j], end=' ')\n\t\tprint()\n", "import collections\n\ndef print_matrix(m):\n\tl = len(m)\n\tfor i in range(l):\n\t\tfor j in range(l):\n\t\t\tprint(m[i][j], end=' ')\n\t\tprint()\nn = int(input())\na = input().split()\na = [int(i) for i in a]\nc = collections.Counter(a)\nmatrix = [[0 for _ in range(n)] for _ in range(n)]\nif n % 2 == 0:\n\tlength = n // 2\n\tindex = 0\n\tfor (k, v) in c.items():\n\t\tif v % 4 != 0:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\t\twhile v > 0:\n\t\t\tx = index % length\n\t\t\ty = index // length\n\t\t\tmatrix[x][y] = k\n\t\t\tmatrix[n - x - 1][y] = k\n\t\t\tmatrix[x][n - y - 1] = k\n\t\t\tmatrix[n - x - 1][n - y - 1] = k\n\t\t\tv -= 4\n\t\t\tindex += 1\n\tprint('YES')\n\tprint_matrix(matrix)\nelse:\n\tcenter = False\n\tlength = (n - 1) // 2\n\tarea_index = 0\n\tver_index = 0\n\thon_index = 0\n\tfor (k, v) in c.items():\n\t\twhile v >= 4 and area_index < length * length:\n\t\t\tx = area_index % length\n\t\t\ty = area_index // length\n\t\t\tmatrix[x][y] = k\n\t\t\tmatrix[n - x - 1][y] = k\n\t\t\tmatrix[x][n - y - 1] = k\n\t\t\tmatrix[n - x - 1][n - y - 1] = k\n\t\t\tv -= 4\n\t\t\tarea_index += 1\n\t\twhile v >= 4 and area_index == length * length:\n\t\t\tif ver_index < length:\n\t\t\t\tmatrix[length][ver_index] = k\n\t\t\t\tmatrix[length][n - 1 - ver_index] = k\n\t\t\t\tver_index += 1\n\t\t\telif hon_index < length:\n\t\t\t\tmatrix[hon_index][length] = k\n\t\t\t\tmatrix[n - 1 - hon_index][length] = k\n\t\t\t\thon_index += 1\n\t\t\telse:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\t\t\tv -= 2\n\t\tif v == 1 or v == 3:\n\t\t\tif center:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\t\t\telse:\n\t\t\t\tcenter = True\n\t\t\t\tmatrix[length][length] = k\n\t\t\t\tv -= 1\n\t\tif v == 2:\n\t\t\tif ver_index < length:\n\t\t\t\tmatrix[length][ver_index] = k\n\t\t\t\tmatrix[length][n - 1 - ver_index] = k\n\t\t\t\tver_index += 1\n\t\t\telif hon_index < length:\n\t\t\t\tmatrix[hon_index][length] = k\n\t\t\t\tmatrix[n - 1 - hon_index][length] = k\n\t\t\t\thon_index += 1\n\t\t\telse:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\tprint('YES')\n\tprint_matrix(matrix)\n", "import math\nimport os\nimport random\nimport re\nimport sys\n\ndef printMatrix(m):\n\tfor i in range(len(m)):\n\t\tprint(' '.join(m[i]))\n\ndef solve(n, digits):\n\tif n == 1:\n\t\tprint('YES')\n\t\tprint(digits[0])\n\t\treturn\n\tif n == 2:\n\t\tfirst = digits[0]\n\t\tfor digit in digits:\n\t\t\tif digit != first:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\tprint('YES')\n\t\tprintMatrix([[first, first], [first, first]])\n\t\treturn\n\tsums = {}\n\tfor digit in digits:\n\t\tif digit not in sums:\n\t\t\tsums[digit] = 0\n\t\tsums[digit] += 1\n\tm = []\n\tfor i in range(n):\n\t\tm.append([])\n\t\tfor j in range(n):\n\t\t\tm[i].append('0')\n\thalfn = n // 2\n\tfor i in range(halfn):\n\t\tfor j in range(halfn):\n\t\t\tfound = False\n\t\t\tfor digit in sums:\n\t\t\t\tif sums[digit] >= 4:\n\t\t\t\t\tfound = True\n\t\t\t\t\tm[i][j] = digit\n\t\t\t\t\tm[n - 1 - i][j] = digit\n\t\t\t\t\tm[n - 1 - i][n - 1 - j] = digit\n\t\t\t\t\tm[i][n - 1 - j] = digit\n\t\t\t\t\tif sums[digit] == 4:\n\t\t\t\t\t\tdel sums[digit]\n\t\t\t\t\telse:\n\t\t\t\t\t\tsums[digit] -= 4\n\t\t\t\t\tbreak\n\t\t\tif not found:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\tif halfn * 2 != n:\n\t\tfor i in range(halfn):\n\t\t\tfound = False\n\t\t\tfor digit in sums:\n\t\t\t\tif sums[digit] >= 2:\n\t\t\t\t\tfound = True\n\t\t\t\t\tm[halfn][i] = digit\n\t\t\t\t\tm[halfn][n - 1 - i] = digit\n\t\t\t\t\tif sums[digit] == 2:\n\t\t\t\t\t\tdel sums[digit]\n\t\t\t\t\telse:\n\t\t\t\t\t\tsums[digit] -= 2\n\t\t\t\t\tbreak\n\t\t\tif not found:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\tfor i in range(halfn):\n\t\t\tfound = False\n\t\t\tfor digit in sums:\n\t\t\t\tif sums[digit] >= 2:\n\t\t\t\t\tfound = True\n\t\t\t\t\tm[i][halfn] = digit\n\t\t\t\t\tm[n - 1 - i][halfn] = digit\n\t\t\t\t\tif sums[digit] == 2:\n\t\t\t\t\t\tdel sums[digit]\n\t\t\t\t\telse:\n\t\t\t\t\t\tsums[digit] -= 2\n\t\t\t\t\tbreak\n\t\t\tif not found:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\tfound = False\n\t\tfor digit in sums:\n\t\t\tif sums[digit] >= 1:\n\t\t\t\tfound = True\n\t\t\t\tm[halfn][halfn] = digit\n\t\t\t\tbreak\n\t\tif not found:\n\t\t\tprint('NO')\n\t\t\treturn\n\tprint('YES')\n\tprintMatrix(m)\nn = int(input())\ndigits = list(input().strip().split())\nsolve(n, digits)\n", "from collections import defaultdict\nn = int(input())\narr = list(map(int, input().split()))\ncounts = defaultdict(int)\nfor a in arr:\n\tcounts[a] += 1\nif n == 1:\n\tprint('YES')\n\tprint(arr[0])\n\texit(0)\nmatrix = [[None] * n for _ in range(n)]\nif n % 2 == 0:\n\tvals = []\n\tfor (val, cnt) in counts.items():\n\t\tif cnt % 4 != 0:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\t\tvals.append((val, cnt))\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\t(val, cnt) = vals.pop(0)\n\t\t\tmatrix[i][j] = val\n\t\t\tmatrix[i][n - j - 1] = val\n\t\t\tmatrix[n - i - 1][j] = val\n\t\t\tmatrix[n - i - 1][n - j - 1] = val\n\t\t\tif cnt - 4 != 0:\n\t\t\t\tvals.append((val, cnt - 4))\n\tprint('YES')\n\tfor row in matrix:\n\t\tprint(*row)\nelse:\n\tvals = []\n\tavail = True\n\tc = 0\n\tfor (val, cnt) in counts.items():\n\t\tif cnt % 4 == 1 and avail:\n\t\t\tavail = False\n\t\t\tmatrix[n // 2][n // 2] = val\n\t\t\tcnt -= 1\n\t\t\tif cnt != 0:\n\t\t\t\tvals.append((val, cnt))\n\t\telif cnt % 4 == 3 and avail:\n\t\t\tavail = False\n\t\t\tmatrix[n // 2][n // 2] = val\n\t\t\tif c < n // 2:\n\t\t\t\tmatrix[n // 2][c] = val\n\t\t\t\tmatrix[n // 2][n - c - 1] = val\n\t\t\t\tc += 1\n\t\t\telif c < n - 1:\n\t\t\t\ti = c % (n // 2)\n\t\t\t\tmatrix[i][n // 2] = val\n\t\t\t\tmatrix[n - i - 1][n // 2] = val\n\t\t\t\tc += 1\n\t\t\telse:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\t\t\tif cnt - 3 > 0:\n\t\t\t\tvals.append((val, cnt - 3))\n\t\telif cnt % 4 == 0:\n\t\t\tvals.append((val, cnt))\n\t\telif cnt % 2 == 0:\n\t\t\tif c < n // 2:\n\t\t\t\tmatrix[n // 2][c] = val\n\t\t\t\tmatrix[n // 2][n - c - 1] = val\n\t\t\t\tc += 1\n\t\t\telif c < n - 1:\n\t\t\t\ti = c % (n // 2)\n\t\t\t\tmatrix[i][n // 2] = val\n\t\t\t\tmatrix[n - i - 1][n // 2] = val\n\t\t\t\tc += 1\n\t\t\telse:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\t\t\tif cnt - 2 > 0:\n\t\t\t\tvals.append((val, cnt - 2))\n\t\telse:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\twhile c < n - 1:\n\t\t(val, cnt) = vals.pop(0)\n\t\tif c < n // 2:\n\t\t\tmatrix[n // 2][c] = val\n\t\t\tmatrix[n // 2][n - c - 1] = val\n\t\t\tc += 1\n\t\telse:\n\t\t\ti = c % (n // 2)\n\t\t\tmatrix[i][n // 2] = val\n\t\t\tmatrix[n - i - 1][n // 2] = val\n\t\t\tc += 1\n\t\tif cnt - 2 > 0:\n\t\t\tvals.insert(0, (val, cnt - 2))\n\tif vals[0][1] % 4 != 0:\n\t\tprint('NO')\n\t\texit(0)\n\tif avail:\n\t\tprint('NO')\n\t\texit(0)\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\t(val, cnt) = vals.pop(0)\n\t\t\tmatrix[i][j] = val\n\t\t\tmatrix[i][n - j - 1] = val\n\t\t\tmatrix[n - i - 1][j] = val\n\t\t\tmatrix[n - i - 1][n - j - 1] = val\n\t\t\tif cnt - 4 != 0:\n\t\t\t\tvals.append((val, cnt - 4))\n\tprint('YES')\n\tfor row in matrix:\n\t\tprint(*row)\n", "n = int(input())\na = list(map(int, input().split()))\nd = {}\nfor i in a:\n\td[i] = d.get(i, 0) + 1\nif n % 2 == 0:\n\tans = []\n\tfor i in d:\n\t\tif d[i] % 4 != 0:\n\t\t\tprint('NO')\n\t\t\tbreak\n\t\tans = ans + [i] * (d[i] // 4)\n\telse:\n\t\tprint('YES')\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(i * (n // 2), (i + 1) * (n // 2)):\n\t\t\t\tprint(ans[j], end=' ')\n\t\t\tfor j in range((i + 1) * (n // 2) - 1, i * (n // 2) - 1, -1):\n\t\t\t\tprint(ans[j], end=' ')\n\t\t\tprint()\n\t\tfor i in range(n // 2 - 1, -1, -1):\n\t\t\tfor j in range(i * (n // 2), (i + 1) * (n // 2)):\n\t\t\t\tprint(ans[j], end=' ')\n\t\t\tfor j in range((i + 1) * (n // 2) - 1, i * (n // 2) - 1, -1):\n\t\t\t\tprint(ans[j], end=' ')\n\t\t\tprint()\nelse:\n\tans1 = []\n\tans2 = []\n\tans4 = []\n\tfor i in d:\n\t\tans4 = ans4 + [i] * (d[i] // 4)\n\t\tans2 = ans2 + [i] * (d[i] % 4 // 2)\n\t\tans1 = ans1 + [i] * (d[i] % 2)\n\tif len(ans1) != 1 or len(ans4) < (n // 2) ** 2:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tfor i in range(len(ans4) - (n // 2) ** 2):\n\t\t\ta = ans4.pop()\n\t\t\tans2 = ans2 + [a] * 2\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tprint(ans4[i * (n // 2) + j], end=' ')\n\t\t\tprint(ans2[i], end=' ')\n\t\t\tfor j in range(n // 2):\n\t\t\t\tprint(ans4[i * (n // 2) + n // 2 - 1 - j], end=' ')\n\t\t\tprint()\n\t\tfor j in range(n // 2):\n\t\t\tprint(ans2[j + n // 2], end=' ')\n\t\tprint(ans1[0], end=' ')\n\t\tfor j in range(n // 2):\n\t\t\tprint(ans2[n // 2 - 1 - j + n // 2], end=' ')\n\t\tprint()\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tprint(ans4[(n // 2 - 1 - i) * (n // 2) + j], end=' ')\n\t\t\tprint(ans2[n // 2 - 1 - i], end=' ')\n\t\t\tfor j in range(n // 2):\n\t\t\t\tprint(ans4[(n // 2 - 1 - i) * (n // 2) + n // 2 - 1 - j], end=' ')\n\t\t\tprint()\n", "n = int(input())\na = list(map(int, input().split()))\nd = {}\nfor i in a:\n\tif d.get(i):\n\t\td[i] += 1\n\telse:\n\t\td[i] = 1\nb = [[0 for i in range(n)] for j in range(n)]\na.sort()\nind = 0\np = 0\nif n % 2 == 0:\n\tfor i in range(n // 2):\n\t\tt = 0\n\t\tfor j in range(n // 2):\n\t\t\tif d[a[ind]] > 3:\n\t\t\t\tb[i][j] = a[ind]\n\t\t\t\tb[-1 - i][j] = a[ind]\n\t\t\t\tb[i][-1 - j] = a[ind]\n\t\t\t\tb[-1 - i][-1 - j] = a[ind]\n\t\t\t\td[a[ind]] -= 4\n\t\t\t\tind += 4\n\t\t\telse:\n\t\t\t\tp = 1\n\t\t\t\tt = 1\n\t\t\t\tbreak\n\t\tif t:\n\t\t\tbreak\nelse:\n\ti = 0\n\tl = len(a)\n\twhile i < n // 2:\n\t\tj = 0\n\t\tt = 0\n\t\twhile j < n // 2:\n\t\t\tif ind >= l:\n\t\t\t\tp = 1\n\t\t\t\tt = 1\n\t\t\t\tbreak\n\t\t\tif d[a[ind]] > 3:\n\t\t\t\tb[i][j] = a[ind]\n\t\t\t\tb[-1 - i][j] = a[ind]\n\t\t\t\tb[i][-1 - j] = a[ind]\n\t\t\t\tb[-1 - i][-1 - j] = a[ind]\n\t\t\t\td[a[ind]] -= 4\n\t\t\t\tind += 4\n\t\t\t\tj += 1\n\t\t\telse:\n\t\t\t\tind += d[a[ind]]\n\t\tif t:\n\t\t\tbreak\n\t\ti += 1\n\tj = 0\n\tif p == 0:\n\t\tfor i in d:\n\t\t\twhile d[i] > 1 and j < n // 2:\n\t\t\t\tb[j][n // 2] = i\n\t\t\t\tb[-1 - j][n // 2] = i\n\t\t\t\td[i] -= 2\n\t\t\t\tj += 1\n\tj = 0\n\tif p == 0:\n\t\tfor i in d:\n\t\t\twhile d[i] > 1 and j < n // 2:\n\t\t\t\tb[n // 2][j] = i\n\t\t\t\tb[n // 2][-1 - j] = i\n\t\t\t\td[i] -= 2\n\t\t\t\tj += 1\n\tx = 0\n\tif p == 0:\n\t\tfor i in d:\n\t\t\tif d[i] > 0 and x == 0:\n\t\t\t\tb[n // 2][n // 2] = i\n\t\t\t\tx = 1\n\t\t\telif d[i] > 0 and x == 1:\n\t\t\t\tp = 1\n\t\t\t\tbreak\nif p:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(b[i][j], end=' ')\n\t\tprint()\n", "import sys\nn = int(sys.stdin.readline())\na = [int(tmp) for tmp in sys.stdin.readline().split()]\ndf = dict()\nfor ea in a:\n\tif ea in df:\n\t\tdf[ea] += 1\n\telse:\n\t\tdf[ea] = 1\nm = []\nfor i in range(n):\n\tm.append([0] * n)\nyes = True\nodd = n % 2\nndiv2 = n // 2\np4 = ndiv2 ** 2\np2h = odd * ndiv2\np2v = odd * ndiv2\np1 = int(odd)\n(f4x, f4y) = (0, 0)\n(f2h, f2v) = (0, 0)\nf1 = 0\nfor ea in df:\n\tif not yes:\n\t\tbreak\n\tcc = df[ea]\n\thi = 1\n\twhile yes and cc > 0 and hi:\n\t\tif cc % 4 == 0 and p4 > 0:\n\t\t\twhile cc > 0 and p4 > 0:\n\t\t\t\tm[f4y][f4x] = ea\n\t\t\t\tcc -= 4\n\t\t\t\tp4 -= 1\n\t\t\t\tf4x += 1\n\t\t\t\tif f4x >= ndiv2:\n\t\t\t\t\tf4x = 0\n\t\t\t\t\tf4y += 1\n\t\telif cc % 2 == 0:\n\t\t\tif p2h > 0:\n\t\t\t\tm[ndiv2][f2h] = ea\n\t\t\t\tcc -= 2\n\t\t\t\tp2h -= 1\n\t\t\t\tf2h += 1\n\t\t\telif p2v > 0:\n\t\t\t\tm[f2v][ndiv2] = ea\n\t\t\t\tcc -= 2\n\t\t\t\tp2v -= 1\n\t\t\t\tf2v += 1\n\t\t\telse:\n\t\t\t\tyes = False\n\t\t\t\tbreak\n\t\telif p1 > 0:\n\t\t\tm[ndiv2][ndiv2] = ea\n\t\t\tcc -= 1\n\t\t\tp1 -= 1\n\t\t\tf1 += 1\n\t\telse:\n\t\t\tyes = False\n\t\t\tbreak\nif yes:\n\tprint('YES')\n\tif n > 1:\n\t\tfor i in range(ndiv2 + odd):\n\t\t\tm[i][ndiv2 + odd:] = m[i][ndiv2 - 1::-1] + []\n\t\tfor i in range(ndiv2):\n\t\t\tm[n - 1 - i] = m[i]\n\tfor em in m:\n\t\tprint(*em)\nelse:\n\tprint('NO')\n", "def gen_matrix(matrix, count, n):\n\tc = 0\n\tif n % 2 == 0:\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\twhile count[c] == 0:\n\t\t\t\t\tc += 1\n\t\t\t\tmatrix[i][j] = matrix[n - 1 - i][j] = matrix[i][n - 1 - j] = matrix[n - 1 - i][n - 1 - j] = c + 1\n\t\t\t\tcount[c] -= 4\n\telse:\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\twhile count[c] < 4:\n\t\t\t\t\tc += 1\n\t\t\t\tmatrix[i][j] = matrix[n - 1 - i][j] = matrix[i][n - 1 - j] = matrix[n - 1 - i][n - 1 - j] = c + 1\n\t\t\t\tcount[c] -= 4\n\t\tc = 0\n\t\tfor i in range(n // 2):\n\t\t\twhile count[c] < 2:\n\t\t\t\tc += 1\n\t\t\tmatrix[i][n // 2] = matrix[n - 1 - i][n // 2] = c + 1\n\t\t\tcount[c] -= 2\n\t\tfor j in range(n // 2):\n\t\t\twhile count[c] < 2:\n\t\t\t\tc += 1\n\t\t\tmatrix[n // 2][j] = matrix[n // 2][n - 1 - j] = c + 1\n\t\t\tcount[c] -= 2\n\t\tc = 0\n\t\twhile count[c] < 1:\n\t\t\tc += 1\n\t\tmatrix[n // 2][n // 2] = c + 1\nn = int(input())\ncount = [0] * 1000\nmatrix = [[0] * n for _ in range(n)]\nfor a in map(int, input().split()):\n\tcount[a - 1] += 1\neven = n % 2\npair = (n - 1) * (n % 2)\nfor c in count:\n\tif c % 4:\n\t\teven -= c % 2\n\t\tpair -= 1 if c % 4 > 1 else 0\nif even >= 0 and pair >= 0:\n\tprint('YES')\n\tgen_matrix(matrix, count, n)\n\tfor i in range(n):\n\t\tfor j in range(n - 1):\n\t\t\tprint(matrix[i][j], end=' ')\n\t\tprint(matrix[i][n - 1])\nelse:\n\tprint('NO')\n", "n = int(input())\nl = list(map(int, input().split()))\nd = {}\nfor i in l:\n\tif i in d:\n\t\td[i] += 1\n\telse:\n\t\td[i] = 1\ncount = 0\nfor i in d:\n\tif d[i] % 2 == 1:\n\t\tcount += 1\nflag = True\nif n % 2 == 0:\n\tfor i in d:\n\t\tif d[i] % 4 != 0:\n\t\t\tflag = False\n\t\t\tprint('NO')\n\t\t\tbreak\nif flag:\n\tflag = True\n\tmat = [[0 for i in range(n)] for i in range(n)]\n\tm = n // 2\n\tleft = {}\n\ttemp = {}\n\tfor i in d:\n\t\tif d[i] < 4:\n\t\t\tleft[i] = d[i]\n\t\telse:\n\t\t\ttemp[i] = d[i]\n\td = temp\n\tfor j in range(m):\n\t\tfor i in range(m):\n\t\t\tif len(d) == 0:\n\t\t\t\tflag = False\n\t\t\t\tbreak\n\t\t\tele = list(d.keys())[0]\n\t\t\tmat[i][j] = ele\n\t\t\tmat[i][n - 1 - j] = ele\n\t\t\tmat[n - 1 - i][j] = ele\n\t\t\tmat[n - 1 - i][n - 1 - j] = ele\n\t\t\td[ele] -= 4\n\t\t\tif d[ele] == 0:\n\t\t\t\tdel d[ele]\n\t\t\telif d[ele] < 4:\n\t\t\t\tleft[ele] = d[ele]\n\t\t\t\tdel d[ele]\n\t\tif not flag:\n\t\t\tbreak\n\tif not flag:\n\t\tprint('NO')\n\telse:\n\t\tflag = True\n\t\tfor i in d:\n\t\t\tleft[i] = d[i]\n\t\tif n % 2 == 1:\n\t\t\tcount = 0\n\t\t\tmid_ele = 0\n\t\t\tfor i in left:\n\t\t\t\tif left[i] % 2 == 1:\n\t\t\t\t\tcount += 1\n\t\t\t\t\tmid_ele = i\n\t\t\t\t\tleft[i] -= 1\n\t\t\tif count != 1:\n\t\t\t\tprint('NO')\n\t\t\t\tflag = False\n\t\t\telse:\n\t\t\t\tif left[mid_ele] == 0:\n\t\t\t\t\tdel left[mid_ele]\n\t\t\t\tfor i in range(m):\n\t\t\t\t\tele = list(left.keys())[0]\n\t\t\t\t\tmat[n // 2][i] = ele\n\t\t\t\t\tmat[n // 2][n - 1 - i] = ele\n\t\t\t\t\tleft[ele] -= 2\n\t\t\t\t\tif left[ele] == 0:\n\t\t\t\t\t\tdel left[ele]\n\t\t\t\tfor i in range(m):\n\t\t\t\t\tif not left:\n\t\t\t\t\t\tflag = False\n\t\t\t\t\t\tprint('NO')\n\t\t\t\t\t\tbreak\n\t\t\t\t\tele = list(left.keys())[0]\n\t\t\t\t\tmat[i][n // 2] = ele\n\t\t\t\t\tmat[n - 1 - i][n // 2] = ele\n\t\t\t\t\tleft[ele] -= 2\n\t\t\t\t\tif left[ele] == 0:\n\t\t\t\t\t\tdel left[ele]\n\t\t\t\tmat[n // 2][n // 2] = mid_ele\n\t\tif flag:\n\t\t\tprint('YES')\n\t\t\tfor i in range(n):\n\t\t\t\tprint(*mat[i])\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\n\ndef RL():\n\treturn map(int, sys.stdin.readline().rstrip().split())\n\ndef RLL():\n\treturn list(RL())\n\ndef N():\n\treturn int(input())\n\ndef comb(n, m):\n\treturn factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0\n\ndef perm(n, m):\n\treturn factorial(n) // factorial(n - m) if n >= m else 0\n\ndef mdis(x1, y1, x2, y2):\n\treturn abs(x1 - x2) + abs(y1 - y2)\n\ndef toord(c):\n\treturn ord(c) - ord('a')\n\ndef lcm(a, b):\n\treturn a * b // gcd(a, b)\nmod = 998244353\nINF = float('inf')\nfrom math import factorial, sqrt, ceil, floor, gcd\nfrom collections import Counter, defaultdict, deque\nfrom heapq import heapify, heappop, heappush\n\ndef main():\n\tn = N()\n\tarr = RLL()\n\tct = Counter(arr)\n\tres = [[0] * n for _ in range(n)]\n\tindex = 0\n\tle = n // 2\n\n\tdef put(value):\n\t\tnonlocal index\n\t\t(px, py) = (index // le, index % le)\n\t\tres[px][py] = value\n\t\tres[px][n - py - 1] = value\n\t\tres[n - px - 1][py] = value\n\t\tres[n - px - 1][n - py - 1] = value\n\t\tindex += 1\n\tcenter = 0\n\tsd_index_v = 0\n\tsd_index_h = 0\n\tfor (k, v) in ct.items():\n\t\tfor _ in range(v // 4):\n\t\t\tif index == le ** 2:\n\t\t\t\ttag = 2\n\t\t\t\twhile tag > 0:\n\t\t\t\t\tif sd_index_v < le:\n\t\t\t\t\t\tres[sd_index_v][le] = k\n\t\t\t\t\t\tres[n - 1 - sd_index_v][le] = k\n\t\t\t\t\t\tsd_index_v += 1\n\t\t\t\t\telse:\n\t\t\t\t\t\tres[le][sd_index_h] = k\n\t\t\t\t\t\tres[le][n - 1 - sd_index_h] = k\n\t\t\t\t\t\tsd_index_h += 1\n\t\t\t\t\ttag -= 1\n\t\t\telse:\n\t\t\t\tput(k)\n\t\tif v % 2 == 1:\n\t\t\tif n % 2 == 0:\n\t\t\t\tprint('NO')\n\t\t\t\tbreak\n\t\t\tcenter += 1\n\t\t\tres[le][le] = k\n\t\tif v % 4 >= 2:\n\t\t\tif n % 2 == 0:\n\t\t\t\tprint('NO')\n\t\t\t\tbreak\n\t\t\tif sd_index_v < le:\n\t\t\t\tres[sd_index_v][le] = k\n\t\t\t\tres[n - 1 - sd_index_v][le] = k\n\t\t\t\tsd_index_v += 1\n\t\t\telse:\n\t\t\t\tres[le][sd_index_h] = k\n\t\t\t\tres[le][n - 1 - sd_index_h] = k\n\t\t\t\tsd_index_h += 1\n\t\tif center > 1 or sd_index_h > le:\n\t\t\tprint('NO')\n\t\t\tbreak\n\telse:\n\t\tprint('YES')\n\t\tfor i in res:\n\t\t\tprint(*i)\nmain()\n", "from collections import Counter\nimport math\nimport sys\nn = int(input())\nstock = Counter(map(int, input().split()))\nsubn = math.floor(n / 2)\nuse = [[0 for col in range(subn)] for row in range(subn)]\nif n == 1:\n\tprint('YES')\n\tprint(stock.most_common(1)[0][0])\n\texit()\nfor row in range(subn):\n\tfor col in range(subn):\n\t\tmc = stock.most_common(1)[0]\n\t\tif mc[1] < 4:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\tstock[mc[0]] -= 4\n\t\tuse[row][col] = mc[0]\nif n % 2:\n\tfor row in range(subn):\n\t\tmc = stock.most_common(1)[0]\n\t\tif mc[1] < 2:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\tstock[mc[0]] -= 2\n\t\tuse[row].append(mc[0])\n\tuse.append([])\n\tfor col in range(subn):\n\t\tmc = stock.most_common(1)[0]\n\t\tif mc[1] < 2:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\tstock[mc[0]] -= 2\n\t\tuse[subn].append(mc[0])\n\tmc = stock.most_common(1)[0]\n\tif mc[1] < 1:\n\t\tprint('NO')\n\t\texit()\n\tuse[subn].append(mc[0])\n\tsubn += 1\nprint('YES')\nfor row in range(subn):\n\tfor col in range(subn):\n\t\tprint(use[row][col], end=' ')\n\tfor col in reversed(range(1, subn - n % 2)):\n\t\tprint(use[row][col], end=' ')\n\tprint(use[row][0])\nfor row in reversed(range(1, subn - n % 2)):\n\tfor col in range(subn):\n\t\tprint(use[row][col], end=' ')\n\tfor col in reversed(range(1, subn - n % 2)):\n\t\tprint(use[row][col], end=' ')\n\tprint(use[row][0])\nfor col in range(subn):\n\tprint(use[0][col], end=' ')\nfor col in reversed(range(1, subn - n % 2)):\n\tprint(use[0][col], end=' ')\nprint(use[0][0])\n", "import random\nstorona = int(input())\nmas = list(map(int, input().split(' ')))\n\ndef get_slovar(mas):\n\tsl = {}\n\tfor i in mas:\n\t\tif i not in sl:\n\t\t\tsl[i] = 1\n\t\telse:\n\t\t\tsl[i] += 1\n\treturn sl\n\ndef chet(sl, storona):\n\tsl_strok = {}\n\tfor i in sl.values():\n\t\tif i % 4 != 0:\n\t\t\treturn False\n\tfor i in range(1, storona + 1):\n\t\tsl_strok[i] = []\n\tfor i in range(1, storona // 2 + 1):\n\t\tfor g in range(1, storona // 2 + 1):\n\t\t\ta = random.sample(sl.keys(), 1)[0]\n\t\t\tif sl[a] == 4:\n\t\t\t\tdel sl[a]\n\t\t\telse:\n\t\t\t\tsl[a] = sl[a] - 4\n\t\t\tsl_strok[i] = [a] + sl_strok[i] + [a]\n\t\t\tsl_strok[storona - i + 1] = [a] + sl_strok[storona - i + 1] + [a]\n\treturn sl_strok\n\ndef nechet(mas, storona):\n\ttemp1 = 0\n\tkrest = []\n\tsl_strok = {}\n\tsl = get_slovar(mas)\n\tsl_dvoek = {}\n\tvert = []\n\tgoris = []\n\tfor (key, value) in sl.items():\n\t\tif value % 2 != 0:\n\t\t\tsl[key] -= 1\n\t\t\tcentral = key\n\t\t\tbreak\n\tfor (key, value) in sl.items():\n\t\tif value % 2 != 0:\n\t\t\treturn False\n\t\tif value % 4 != 0:\n\t\t\tsl[key] -= 2\n\t\t\tsl_dvoek[key] = 2\n\tostatok = (storona - 1) * 2 - sum(sl_dvoek.values())\n\tif ostatok < 0:\n\t\treturn False\n\tif ostatok % 4 != 0:\n\t\treturn False\n\twhile ostatok > 0:\n\t\ta = random.sample(sl.keys(), 1)[0]\n\t\tif sl[a] >= 4:\n\t\t\tsl[a] -= 4\n\t\t\tif a not in sl_dvoek:\n\t\t\t\tsl_dvoek[a] = 4\n\t\t\telse:\n\t\t\t\tsl_dvoek[a] += 4\n\t\t\tostatok -= 4\n\tsl = {key: value for (key, value) in sl.items() if value != 0}\n\tfor i in range(storona - 1):\n\t\ta = random.sample(sl_dvoek.keys(), 1)[0]\n\t\tif sl_dvoek[a] == 2:\n\t\t\tdel sl_dvoek[a]\n\t\telse:\n\t\t\tsl_dvoek[a] = sl_dvoek[a] - 2\n\t\tkrest = [a] + krest + [a]\n\tsl_strok = chet(sl, storona - 1)\n\tgoris = krest[:len(krest) // 4] + [central] + krest[-len(krest) // 4:]\n\tvert = krest[len(krest) // 4:-len(krest) // 4]\n\tvert = vert[:len(vert) // 2] + [central] + vert[len(vert) // 2:]\n\tsl_strok[storona / 2] = goris\n\tfor i in sorted(sl_strok.keys()):\n\t\tif i != storona / 2:\n\t\t\tsl_strok[i] = sl_strok[i][:len(sl_strok[i]) // 2] + [vert[temp1]] + sl_strok[i][len(sl_strok[i]) // 2:]\n\t\ttemp1 += 1\n\treturn sl_strok\nif storona % 2 == 0:\n\ta = chet(get_slovar(mas), storona)\nelse:\n\ta = nechet(mas, storona)\nif not a:\n\tprint('NO')\n\texit()\nelse:\n\tprint('YES')\nfor i in sorted(a.keys()):\n\tprint(*a[i])\n", "import sys\nn = int(input())\nl = list(map(int, input().split()))\nfreq = [0] * 1001\nd = {}\nfor i in range(n * n):\n\ttry:\n\t\td[l[i]] += 1\n\texcept:\n\t\td[l[i]] = 1\nans = [[0] * n for i in range(n)]\nif n % 2:\n\tfor i in d:\n\t\tif d[i] & 1:\n\t\t\tans[n // 2][n // 2] = i\n\t\t\td[i] -= 1\n\t\t\tbreak\nfor x in d:\n\tjana_hai = x\n\tkitna = d[x]\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tif ans[i][j] != 0:\n\t\t\t\tcontinue\n\t\t\tif kitna >= 4:\n\t\t\t\tans[i][j] = ans[n - 1 - i][j] = ans[i][n - 1 - j] = ans[n - 1 - i][n - 1 - j] = jana_hai\n\t\t\t\tkitna -= 4\n\tif n % 2:\n\t\tfor i in range(n // 2):\n\t\t\tif ans[i][n // 2] != 0:\n\t\t\t\tcontinue\n\t\t\tif kitna >= 2:\n\t\t\t\tans[i][n // 2] = ans[n - 1 - i][n // 2] = jana_hai\n\t\t\t\tkitna -= 2\n\t\tfor i in range(n // 2):\n\t\t\tif ans[n // 2][i] != 0:\n\t\t\t\tcontinue\n\t\t\tif kitna >= 2:\n\t\t\t\tans[n // 2][i] = ans[n // 2][n - 1 - i] = jana_hai\n\t\t\t\tkitna -= 2\n\tif kitna > 0:\n\t\tprint('NO')\n\t\tsys.exit(0)\nprint('YES')\nfor i in range(n):\n\tprint(*ans[i])\n", "n = int(input())\na = list(map(int, input().split()))\nb = [[0 for i in range(n)] for j in range(n)]\no0 = []\no1 = []\no2 = []\no3 = 0\nz = set(a)\nfor e in z:\n\tr = a.count(e)\n\tif r % 2 == 1:\n\t\to1 += [e]\n\t\tr -= 1\n\tif r % 4 == 2:\n\t\to2 += [e]\n\t\tr -= 2\n\tif r % 4 == 0:\n\t\tfor i in range(r // 4):\n\t\t\to0 += [e]\n\telse:\n\t\to3 = 1\nc = o0\nc1 = o0[(n // 2) ** 2:] * 2 + o2\nif o3 != 0:\n\tprint('NO')\nelif n % 2 == 0:\n\tif len(o1) != 0 or len(o2) != 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tfor j in range(n // 2):\n\t\t\tfor i in range(n // 2):\n\t\t\t\tb[j][i] = c[j * (n // 2) + i]\n\t\t\t\tb[n - 1 - j][i] = c[j * (n // 2) + i]\n\t\t\t\tb[j][n - 1 - i] = c[j * (n // 2) + i]\n\t\t\t\tb[n - 1 - j][n - 1 - i] = c[j * (n // 2) + i]\n\t\tfor j in range(n):\n\t\t\tfor i in range(n):\n\t\t\t\tprint(b[j][i], end=' ')\n\t\t\tprint()\nelif len(o1) > 1 or len(o0) * 4 < n * n - 2 * n + 1 or len(o1) + len(o2) * 2 + len(o0) * 4 != n * n:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tfor j in range(n // 2):\n\t\tfor i in range(n // 2):\n\t\t\tb[j][i] = c[j * (n // 2) + i]\n\t\t\tb[n - 1 - j][i] = c[j * (n // 2) + i]\n\t\t\tb[j][n - 1 - i] = c[j * (n // 2) + i]\n\t\t\tb[n - 1 - j][n - 1 - i] = c[j * (n // 2) + i]\n\tfor j in range(n // 2):\n\t\tb[j][n // 2] = c1[j]\n\t\tb[n - 1 - j][n // 2] = c1[j]\n\tfor j in range(n // 2):\n\t\tb[n // 2][j] = c1[j + n // 2]\n\t\tb[n // 2][n - 1 - j] = c1[j + n // 2]\n\tb[n // 2][n // 2] = o1[0]\n\tfor j in range(n):\n\t\tfor i in range(n):\n\t\t\tprint(b[j][i], end=' ')\n\t\tprint()\n", "from collections import defaultdict\nimport sys\ninput = sys.stdin.readline\nn = int(input())\na = list(map(int, input().split()))\ncnt = defaultdict(lambda : 0)\nfor i in a:\n\tcnt[i] += 1\nb = [[-1] * n for _ in range(n)]\nx = [[] for _ in range(5)]\nfor i in range((n + 1) // 2):\n\tfor j in range((n + 1) // 2):\n\t\ts = set()\n\t\ts.add((i, j))\n\t\ts.add((n - i - 1, j))\n\t\ts.add((i, n - j - 1))\n\t\ts.add((n - i - 1, n - j - 1))\n\t\ts = list(s)\n\t\tx[len(s)].append(s)\nfor i in [4, 2, 1]:\n\tfor j in cnt:\n\t\twhile cnt[j] >= i and x[i]:\n\t\t\ty = x[i].pop()\n\t\t\tcnt[j] -= i\n\t\t\tfor (k, l) in y:\n\t\t\t\tb[k][l] = j\nans = 'YES' if not max((cnt[i] for i in cnt)) else 'NO'\nprint(ans)\nif ans == 'YES':\n\tfor b0 in b:\n\t\tprint(*b0)\n", "n = int(input())\na = list(map(int, input().split()))\nd = {i: 0 for i in set(a)}\nfor i in a:\n\td[i] += 1\nk = 0\nii = -1\nr = []\nfor i in range(n):\n\tr.append([0] * n)\nif n % 2 == 0:\n\tm = n // 2\n\tcur = 0\n\tfor i in d:\n\t\tif d[i] % 4:\n\t\t\tk += 1000000\n\t\t\tbreak\n\t\twhile d[i]:\n\t\t\tr[cur % m][cur // m] = i\n\t\t\tr[n - cur % m - 1][cur // m] = i\n\t\t\tr[cur % m][n - cur // m - 1] = i\n\t\t\tr[n - cur % m - 1][n - cur // m - 1] = i\n\t\t\tcur += 1\n\t\t\td[i] -= 4\nelse:\n\tq = w = 0\n\tm = n // 2\n\tcur = 0\n\ts = 0\n\tfor i in d:\n\t\tif d[i] % 2:\n\t\t\tq += 1\n\t\t\tif q > 1:\n\t\t\t\tk += 100000\n\t\t\t\tbreak\n\t\t\tr[m][m] = i\n\t\t\td[i] -= 1\n\t\tif d[i] % 4:\n\t\t\tw += 2\n\t\t\td[i] -= 2\n\t\t\tif w > 4 * m:\n\t\t\t\tk += 100000\n\t\t\t\tbreak\n\t\t\tif s < m:\n\t\t\t\tr[m][s] = i\n\t\t\t\tr[m][n - s - 1] = i\n\t\t\telse:\n\t\t\t\tr[s - m][m] = i\n\t\t\t\tr[n - s + m - 1][m] = i\n\t\t\ts += 1\n\t\twhile d[i]:\n\t\t\tif cur >= m * m:\n\t\t\t\tif s < m:\n\t\t\t\t\tr[m][s] = i\n\t\t\t\t\tr[m][n - s - 1] = i\n\t\t\t\telse:\n\t\t\t\t\tr[s - m][m] = i\n\t\t\t\t\tr[n - s + m - 1][m] = i\n\t\t\t\ts += 1\n\t\t\t\td[i] -= 2\n\t\t\telse:\n\t\t\t\tr[cur % m][cur // m] = i\n\t\t\t\tr[n - cur % m - 1][cur // m] = i\n\t\t\t\tr[cur % m][n - cur // m - 1] = i\n\t\t\t\tr[n - cur % m - 1][n - cur // m - 1] = i\n\t\t\t\tcur += 1\n\t\t\t\td[i] -= 4\nif k > 100:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tfor i in r:\n\t\tprint(*i)\n", "import collections\nn = int(input())\na = list(map(int, input().split()))\ntry:\n\tmatrix = [[1]]\n\tfor i in range(1, 12):\n\t\tmatrix.append([4 for j in range(len(matrix[-1]))])\n\t\tmatrix.append(sorted(matrix[-2] + [2, 2] + [4 for j in range(i * 2 - 1)], reverse=True))\n\tnowmatrix = matrix[n - 1]\n\tnowmatrix = collections.Counter(nowmatrix)\n\tnowmatrix = nowmatrix.most_common()\n\tmatrix = []\n\tfor i in range(len(nowmatrix)):\n\t\tmatrix.append(list(nowmatrix[i]))\n\tmatrix.sort(reverse=True)\n\ta = collections.Counter(a)\n\ta = a.most_common()\n\talist = []\n\tfor i in range(len(a)):\n\t\talist.append(list(a[i]))\n\tans = []\n\tfor i in range(len(alist)):\n\t\tcount = alist[i][1] // 4\n\t\talist[i][1] -= 4 * count\n\t\tans += [alist[i][0] for j in range(count)]\n\tif n % 2 == 1:\n\t\tif len(ans) >= matrix[0][1]:\n\t\t\tans += ans[matrix[0][1]:]\n\t\t\tfor i in range(len(alist)):\n\t\t\t\tcount = alist[i][1] // 2\n\t\t\t\talist[i][1] -= 2 * count\n\t\t\t\tans += [alist[i][0] for j in range(count)]\n\t\t\tif len(ans) == matrix[0][1] + matrix[1][1]:\n\t\t\t\tfor i in range(len(alist)):\n\t\t\t\t\tcount = alist[i][1]\n\t\t\t\t\talist[i][1] -= count\n\t\t\t\t\tans += [alist[i][0] for j in range(count)]\n\t\t\telse:\n\t\t\t\tans = -1\n\t\telse:\n\t\t\tans = -1\n\telif len(ans) != matrix[0][1]:\n\t\tans = -1\n\tif n == 1:\n\t\tprint('YES')\n\t\tprint(alist[0][0])\n\telif ans == -1:\n\t\tprint('NO')\n\telse:\n\t\tansmatrix = [[0 for i in range(n)] for j in range(n)]\n\t\tcount = 0\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tansmatrix[i][j] = ans[count]\n\t\t\t\tansmatrix[n - 1 - i][n - 1 - j] = ans[count]\n\t\t\t\tansmatrix[n - 1 - i][j] = ans[count]\n\t\t\t\tansmatrix[i][n - 1 - j] = ans[count]\n\t\t\t\tcount += 1\n\t\tif n % 2 == 1:\n\t\t\tfor i in range(n // 2):\n\t\t\t\tansmatrix[n // 2][i] = ans[count]\n\t\t\t\tansmatrix[n // 2][n - 1 - i] = ans[count]\n\t\t\t\tcount += 1\n\t\t\tfor i in range(n // 2):\n\t\t\t\tansmatrix[i][n // 2] = ans[count]\n\t\t\t\tansmatrix[n - 1 - i][n // 2] = ans[count]\n\t\t\t\tcount += 1\n\t\t\tansmatrix[n // 2][n // 2] = ans[count]\n\t\tprint('YES')\n\t\tfor i in range(len(ansmatrix)):\n\t\t\tprint(' '.join(map(str, ansmatrix[i])))\nexcept:\n\tprint('NO')\n", "n = int(input())\narr = list(map(int, input().split()))\nif n == 1:\n\tprint('YES')\n\tprint(arr[0])\nelif n % 2 == 0:\n\tarr1 = list(set(arr))\n\tarr2 = []\n\tif len(arr1) > n * n // 4:\n\t\tprint('NO')\n\telse:\n\t\tfor a in arr1:\n\t\t\tb = arr.count(a)\n\t\t\tif b % 4 != 0:\n\t\t\t\tprint('NO')\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tarr2.append(b)\n\t\tif len(arr1) == len(arr2):\n\t\t\ti = 0\n\t\t\tans = [[] for z in range(n // 2)]\n\t\t\tj = 0\n\t\t\twhile j < n // 2:\n\t\t\t\tk = 0\n\t\t\t\twhile k < n // 2:\n\t\t\t\t\tif arr2[i] == 0:\n\t\t\t\t\t\ti += 1\n\t\t\t\t\tans[k].append(str(arr1[i]))\n\t\t\t\t\tarr2[i] -= 4\n\t\t\t\t\tk += 1\n\t\t\t\tj += 1\n\t\t\tg = 0\n\t\t\twhile g < n // 2:\n\t\t\t\tans[g] += ans[g][::-1]\n\t\t\t\tg += 1\n\t\t\tans += ans[::-1]\n\t\t\tprint('YES')\n\t\t\tfor a in ans:\n\t\t\t\tprint(' '.join(a))\nelse:\n\tarr1 = list(set(arr))\n\tarr2 = []\n\tarr3 = []\n\tfor a in arr1:\n\t\tb = arr.count(a)\n\t\tarr2.append(b % 4)\n\t\tarr3.append(b - b % 4)\n\tif sum(arr3) < (n - 1) * (n - 1):\n\t\tprint('NO')\n\telse:\n\t\ti = 0\n\t\tans = [[] for z in range(n // 2)]\n\t\tj = 0\n\t\twhile j < n // 2:\n\t\t\tk = 0\n\t\t\twhile k < n // 2:\n\t\t\t\twhile arr3[i] == 0:\n\t\t\t\t\ti += 1\n\t\t\t\tans[k].append(str(arr1[i]))\n\t\t\t\tarr3[i] -= 4\n\t\t\t\tk += 1\n\t\t\tj += 1\n\t\ti = 0\n\t\th = 0\n\t\twhile i < len(arr1):\n\t\t\tarr2[i] = arr2[i] + arr3[i]\n\t\t\tif arr2[i] % 2 == 1:\n\t\t\t\th += 1\n\t\t\t\tp = i\n\t\t\t\tarr2[i] -= 1\n\t\t\ti += 1\n\t\tif h > 1:\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tprint('YES')\n\t\t\ti = 0\n\t\t\tj = 0\n\t\t\twhile i < n // 2:\n\t\t\t\twhile arr2[j] == 0:\n\t\t\t\t\tj += 1\n\t\t\t\tans[i] = ans[i] + [str(arr1[j])] + ans[i][::-1]\n\t\t\t\tarr2[j] -= 2\n\t\t\t\ti += 1\n\t\t\ti = 0\n\t\t\tv = []\n\t\t\twhile i < n // 2:\n\t\t\t\twhile arr2[j] == 0:\n\t\t\t\t\tj += 1\n\t\t\t\tv.append(str(arr1[j]))\n\t\t\t\tarr2[j] -= 2\n\t\t\t\ti += 1\n\t\t\tfor el in ans:\n\t\t\t\tprint(' '.join(el))\n\t\t\tprint(' '.join(v) + ' ' + str(arr1[p]) + ' ' + ' '.join(v[::-1]))\n\t\t\tfor el in ans[::-1]:\n\t\t\t\tprint(' '.join(el))\n", "def print_out():\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(matrix[i][j], end=' ')\n\t\tprint()\nn = int(input())\nmatrix = [[0] * n for i in range(n)]\nls = list(map(int, input().split()))\nnumbers = [0] * 1020\nfor x in ls:\n\tnumbers[x] += 1\nif n % 2 == 0:\n\tu = n // 2\n\ttmp = 0\n\tptr = 0\n\table = True\n\tfor i in range(1020):\n\t\tif numbers[i] % 4 > 0:\n\t\t\tprint('NO')\n\t\t\tquit()\n\tprint('YES')\n\twhile tmp < u * u:\n\t\tif numbers[ptr] == 0:\n\t\t\tptr += 1\n\t\t\tcontinue\n\t\telse:\n\t\t\tnumbers[ptr] -= 4\n\t\t\tmatrix[tmp // u][tmp % u] = ptr\n\t\t\ttmp += 1\n\t\t\tcontinue\n\tfor i in range(u):\n\t\tfor j in range(u):\n\t\t\tmatrix[n - i - 1][n - j - 1] = matrix[i][j]\n\t\t\tmatrix[i][n - j - 1] = matrix[i][j]\n\t\t\tmatrix[n - i - 1][j] = matrix[i][j]\n\tprint_out()\nelse:\n\tu = n // 2\n\ttmp = 0\n\tptr = 0\n\twhile tmp < u * u:\n\t\tif ptr > 1000:\n\t\t\tprint('NO')\n\t\t\tquit()\n\t\tif numbers[ptr] < 4:\n\t\t\tptr += 1\n\t\t\tcontinue\n\t\telse:\n\t\t\tnumbers[ptr] -= 4\n\t\t\tmatrix[tmp // u][tmp % u] = ptr\n\t\t\ttmp += 1\n\t\t\tcontinue\n\tfor i in range(u):\n\t\tfor j in range(u):\n\t\t\tmatrix[n - i - 1][n - j - 1] = matrix[i][j]\n\t\t\tmatrix[i][n - j - 1] = matrix[i][j]\n\t\t\tmatrix[n - i - 1][j] = matrix[i][j]\n\table = 0\n\tonly = 0\n\tfor i in range(1020):\n\t\tif numbers[i] % 2 > 0:\n\t\t\table += 1\n\t\t\tonly = i\n\tif able == 1:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n\t\tquit()\n\tvar = u\n\tnumbers[only] -= 1\n\tmatrix[var][var] = only\n\ttmp = 0\n\tptr = 0\n\twhile tmp < u:\n\t\tif ptr > 1000:\n\t\t\tprint('NO')\n\t\t\tquit()\n\t\telif numbers[ptr] < 2:\n\t\t\tptr += 1\n\t\t\tcontinue\n\t\telse:\n\t\t\tnumbers[ptr] -= 2\n\t\t\tmatrix[tmp][var] = ptr\n\t\t\tmatrix[n - tmp - 1][var] = ptr\n\t\t\ttmp += 1\n\t\t\tcontinue\n\ttmp = 0\n\tptr = 0\n\twhile tmp < u:\n\t\tif ptr > 1000:\n\t\t\tprint('NO')\n\t\t\tquit()\n\t\telif numbers[ptr] < 2:\n\t\t\tptr += 1\n\t\t\tcontinue\n\t\telse:\n\t\t\tnumbers[ptr] -= 2\n\t\t\tmatrix[var][tmp] = ptr\n\t\t\tmatrix[var][n - tmp - 1] = ptr\n\t\t\ttmp += 1\n\t\t\tcontinue\n\tprint_out()\n", "n = int(input())\na = list(map(int, input().split()))\nd = {}\nres = [[0] * n for i in range(n)]\nfor i in a:\n\td[i] = d.get(i, 0) + 1\n\ndef put(a, i, j, k, d):\n\td[k] -= 4\n\ta[i][j] = k\n\ta[n - i - 1][j] = k\n\ta[i][n - j - 1] = k\n\ta[n - i - 1][n - j - 1] = k\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tfor k in d:\n\t\t\tif d[k] >= 4:\n\t\t\t\tput(res, i, j, k, d)\n\t\t\t\tbreak\nif n % 2 == 1:\n\tfor i in range(n // 2):\n\t\tfor k in d:\n\t\t\tif d[k] >= 2:\n\t\t\t\td[k] -= 2\n\t\t\t\tres[i][n // 2] = k\n\t\t\t\tres[n - i - 1][n // 2] = k\n\t\t\t\tbreak\n\t\tfor k in d:\n\t\t\tif d[k] >= 2:\n\t\t\t\td[k] -= 2\n\t\t\t\tres[n // 2][i] = k\n\t\t\t\tres[n // 2][n - i - 1] = k\n\t\t\t\tbreak\nif n % 2 == 1:\n\tfor k in d:\n\t\tif d[k] >= 1:\n\t\t\td[k] -= 1\n\t\t\tres[n // 2][n // 2] = k\n\t\t\tbreak\nif any((v for (k, v) in d.items())):\n\tprint('NO')\n\texit()\nprint('YES')\nfor i in res:\n\tprint(' '.join(map(str, i)))\n", "n = int(input())\nd = {}\nfor x in map(int, input().split()):\n\td[x] = d.get(x, 0) + 1\n(x, x_1, y, flag) = (0, 0, 0, 1)\na = [[0 for i in range(n)] for i in range(n)]\nfor z in d:\n\tif n % 2 == 0:\n\t\twhile d[z] - 4 >= 0:\n\t\t\t(a[x][y], a[x][n - y - 1], a[n - x - 1][y], a[n - x - 1][n - y - 1]) = (z, z, z, z)\n\t\t\td[z] -= 4\n\t\t\tx += 1\n\t\t\t(x, y) = (x % (n // 2), y + x // (n // 2))\n\telse:\n\t\twhile d[z] - 4 >= 0 and x < n // 2 and (y < n // 2):\n\t\t\t(a[x][y], a[x][n - y - 1], a[n - x - 1][y], a[n - x - 1][n - y - 1]) = (z, z, z, z)\n\t\t\td[z] -= 4\n\t\t\tx += 1\n\t\t\t(x, y) = (x % (n // 2), y + x // (n // 2))\n\t\twhile d[z] >= 2 and flag < 3:\n\t\t\tif flag == 1:\n\t\t\t\t(a[x_1][n // 2], a[n - x_1 - 1][n // 2]) = (z, z)\n\t\t\t\td[z] -= 2\n\t\t\t\tx_1 += 1\n\t\t\t\tif x_1 == n // 2:\n\t\t\t\t\t(flag, x_1) = (2, 0)\n\t\t\telif flag == 2:\n\t\t\t\t(a[n // 2][x_1], a[n // 2][n - x_1 - 1]) = (z, z)\n\t\t\t\td[z] -= 2\n\t\t\t\tx_1 += 1\n\t\t\t\tif x_1 == n // 2:\n\t\t\t\t\t(flag, x_1) = (3, 0)\n\t\tif d[z] == 1 and a[n // 2][n // 2] == 0:\n\t\t\ta[n // 2][n // 2] = z\n\t\t\td[z] -= 1\n\tif d[z] > 0:\n\t\tprint('NO')\n\t\texit()\nprint('YES')\nfor x in a:\n\tprint(*x)\n", "n = int(input())\na = list(map(int, input().split()))\nif n == 1:\n\tprint('YES')\n\tprint(a[0])\n\texit()\ncnt = [0] * 1001\nfor _ in a:\n\tcnt[_] += 1\n(odd, x) = (0, -1)\nfor (i, _) in enumerate(cnt):\n\tif _ % 2:\n\t\todd += 1\n\t\tx = i\nif odd > 1 or (n % 2 == 0 and odd):\n\tprint('NO')\n\texit()\nans = [[0] * n for _ in range(n)]\nif n % 2:\n\tans[n // 2][n // 2] = x\n\tcnt[x] -= 1\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tfor (v, _) in enumerate(cnt):\n\t\t\tif _ >= 4:\n\t\t\t\tans[i][j] = ans[n - 1 - i][j] = ans[i][n - 1 - j] = ans[n - 1 - i][n - 1 - j] = v\n\t\t\t\tcnt[v] -= 4\n\t\t\t\tbreak\nif n % 2:\n\tfor i in range(n // 2):\n\t\tfor (v, _) in enumerate(cnt):\n\t\t\tif _ >= 2:\n\t\t\t\tans[i][n // 2] = ans[n - 1 - i][n // 2] = v\n\t\t\t\tcnt[v] -= 2\n\t\t\t\tbreak\n\tfor j in range(n // 2):\n\t\tfor (v, _) in enumerate(cnt):\n\t\t\tif _ >= 2:\n\t\t\t\tans[n // 2][j] = ans[n // 2][n - 1 - j] = v\n\t\t\t\tcnt[v] -= 2\n\t\t\t\tbreak\ny = 0\nfor i in range(n):\n\tfor j in range(n):\n\t\tif ans[i][j] == 0:\n\t\t\ty += 1\nif y:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(ans[i][j], end=' ')\n\t\tprint()\n", "n = int(input())\na = list(map(int, input().split()))\ns = {}\nfor q in a:\n\ts[q] = s.get(q, 0) + 1\nif n % 2 == 0:\n\tif all((q % 4 == 0 for q in s.values())):\n\t\tprint('YES')\n\t\tkeys1 = []\n\t\tfor q in s:\n\t\t\tkeys1.extend([q] * (s[q] // 4))\n\t\tans1 = []\n\t\tfor q in range(len(keys1)):\n\t\t\tif q % (n // 2) == 0:\n\t\t\t\tans1.append([keys1[q]])\n\t\t\telse:\n\t\t\t\tans1[-1].append(keys1[q])\n\t\tfor q in range(n // 2):\n\t\t\tans1[q].extend(ans1[q][::-1])\n\t\tfor q in range(n // 2 - 1, -1, -1):\n\t\t\tans1.append([])\n\t\t\tfor q1 in range(n):\n\t\t\t\tans1[-1].append(ans1[q][q1])\n\t\tfor q in range(n):\n\t\t\tprint(*ans1[q])\n\telse:\n\t\tprint('NO')\nelse:\n\t(center, center2) = (set(), set())\n\tfor q in range(len(a)):\n\t\tif s[a[q]] % 2 == 1:\n\t\t\tcenter.add(a[q])\n\t\tif s[a[q]] % 4 > 1:\n\t\t\tcenter2.add(a[q])\n\t(center, center2) = (list(center), list(center2))\n\tif len(center) != 1 or len(center2) > n - 1:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tkeys = []\n\t\tfor q in s:\n\t\t\tkeys.extend([q] * (s[q] // 4))\n\t\tans = []\n\t\tfor q in range(n // 2 * (n // 2)):\n\t\t\tif q % (n // 2) == 0:\n\t\t\t\tans.append([keys[q]])\n\t\t\telse:\n\t\t\t\tans[-1].append(keys[q])\n\t\t(q2, q3) = (0, n // 2 * (n // 2) * 2)\n\t\tfor q in range(n // 2):\n\t\t\tif q2 < len(center2):\n\t\t\t\tans[q].append(center2[q2])\n\t\t\t\tq2 += 1\n\t\t\telse:\n\t\t\t\tans[q].append(keys[q3 // 2])\n\t\t\t\tq3 += 1\n\t\t\tans[q].extend(ans[q][:-1][::-1])\n\t\tans.append([])\n\t\tfor _ in range(n // 2):\n\t\t\tif q2 < len(center2):\n\t\t\t\tans[n // 2].append(center2[q2])\n\t\t\t\tq2 += 1\n\t\t\telse:\n\t\t\t\tans[n // 2].append(keys[q3 // 2])\n\t\t\t\tq3 += 1\n\t\tans[n // 2].append(center[0])\n\t\tans[n // 2].extend(ans[n // 2][:n // 2][::-1])\n\t\tfor q in range(n // 2 - 1, -1, -1):\n\t\t\tans.append([])\n\t\t\tfor q1 in range(n):\n\t\t\t\tans[-1].append(ans[q][q1])\n\t\tfor q in range(n):\n\t\t\tprint(*ans[q])\n", "from sys import stdin, stdout\n\ndef getKey(item):\n\treturn item[1]\n\ndef inp():\n\treturn stdin.readline()\n\ndef minp():\n\treturn map(int, stdin.readline().rstrip().split())\n\ndef linp():\n\treturn list(minp())\n\ndef main():\n\tn = int(inp())\n\tl = linp()\n\tll = list(set(l))\n\tcnt = 0\n\ta = [[0 for x in range(n)] for y in range(n)]\n\tif n == 1:\n\t\tprint('YES')\n\t\tprint(*l)\n\t\texit()\n\tif n % 2 == 0:\n\t\tif len(ll) <= int(n / 2) * int(n / 2):\n\t\t\tnum = []\n\t\t\tfor i in ll:\n\t\t\t\tc = l.count(i)\n\t\t\t\tif c % 4 == 0:\n\t\t\t\t\tx = int(c / 4)\n\t\t\t\t\tcnt += x\n\t\t\t\t\tfor j in range(x):\n\t\t\t\t\t\tnum.append(i)\n\t\t\t\telse:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\texit()\n\t\t\tif cnt == int(n / 2) * int(n / 2):\n\t\t\t\tprint('YES')\n\t\t\t\tpos = 0\n\t\t\t\tfor j in range(int(n / 2)):\n\t\t\t\t\tfor k in range(int(n / 2)):\n\t\t\t\t\t\ta[j][k] = num[pos]\n\t\t\t\t\t\ta[j][n - k - 1] = num[pos]\n\t\t\t\t\t\ta[n - j - 1][k] = num[pos]\n\t\t\t\t\t\ta[n - j - 1][n - k - 1] = num[pos]\n\t\t\t\t\t\tpos += 1\n\t\t\t\tfor j in range(n):\n\t\t\t\t\tprint(*a[j])\n\t\t\telse:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\telse:\n\t\t\tprint('NO')\n\t\t\texit()\n\telse:\n\t\tnum = []\n\t\tex = []\n\t\tf = 0\n\t\td = []\n\t\tf = 0\n\t\tfor i in ll:\n\t\t\td.append([i, l.count(i)])\n\t\td = sorted(d, key=getKey, reverse=True)\n\t\tfor i in range(len(ll)):\n\t\t\tc = int(d[i][1] / 4)\n\t\t\td[i][1] -= c * 4\n\t\t\tcnt += c\n\t\t\tfor j in range(c):\n\t\t\t\tnum.append(d[i][0])\n\t\t\tif cnt >= int(n / 2) * int(n / 2):\n\t\t\t\td[i][1] += (cnt - int(n / 2) * int(n / 2)) * 4\n\t\t\t\tf = 1\n\t\t\t\tbreak\n\t\tif f == 0:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\tpos = 0\n\t\tfor j in range(int(n / 2)):\n\t\t\tfor k in range(int(n / 2)):\n\t\t\t\ta[j][k] = num[pos]\n\t\t\t\ta[j][n - k - 1] = num[pos]\n\t\t\t\ta[n - j - 1][k] = num[pos]\n\t\t\t\ta[n - j - 1][n - k - 1] = num[pos]\n\t\t\t\tpos += 1\n\t\tf = 0\n\t\tpos = 0\n\t\tp = 0\n\t\twhile p < int(n / 2):\n\t\t\tif d[pos][1] % 2 == 0:\n\t\t\t\tc = int(d[pos][1] / 2)\n\t\t\t\tfor j in range(c):\n\t\t\t\t\ta[int(n / 2)][p] = d[pos][0]\n\t\t\t\t\ta[int(n / 2)][n - p - 1] = d[pos][0]\n\t\t\t\t\td[pos][1] -= 2\n\t\t\t\t\tp += 1\n\t\t\t\t\tif p >= int(n / 2):\n\t\t\t\t\t\tbreak\n\t\t\t\tpos += 1\n\t\t\telif f == 0:\n\t\t\t\tf = 1\n\t\t\t\tc = int(d[pos][1] / 2)\n\t\t\t\tfor j in range(c):\n\t\t\t\t\ta[int(n / 2)][p] = d[pos][0]\n\t\t\t\t\ta[int(n / 2)][n - p - 1] = d[pos][0]\n\t\t\t\t\td[pos][1] -= 2\n\t\t\t\t\tp += 1\n\t\t\t\t\tif p >= int(n / 2):\n\t\t\t\t\t\tbreak\n\t\t\t\ta[int(n / 2)][int(n / 2)] = d[pos][0]\n\t\t\t\td[pos][1] -= 1\n\t\t\telse:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\tp = 0\n\t\tpos = 0\n\t\twhile p < int(n / 2):\n\t\t\tif d[pos][1] % 2 == 0:\n\t\t\t\tc = int(d[pos][1] / 2)\n\t\t\t\tfor j in range(c):\n\t\t\t\t\ta[p][int(n / 2)] = d[pos][0]\n\t\t\t\t\ta[n - p - 1][int(n / 2)] = d[pos][0]\n\t\t\t\t\td[pos][1] -= 2\n\t\t\t\t\tp += 1\n\t\t\t\t\tif p >= int(n / 2):\n\t\t\t\t\t\tbreak\n\t\t\t\tpos += 1\n\t\t\telse:\n\t\t\t\td[pos][1]\n\t\t\t\tif f == 0:\n\t\t\t\t\tf = 1\n\t\t\t\t\tc = int(d[pos][1] / 2)\n\t\t\t\t\tfor j in range(c):\n\t\t\t\t\t\ta[p][int(n / 2)] = d[pos][0]\n\t\t\t\t\t\ta[n - p - 1][int(n / 2)] = d[pos][0]\n\t\t\t\t\t\tp += 1\n\t\t\t\t\t\td[pos][1] -= 2\n\t\t\t\t\t\tif p >= int(n / 2):\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\ta[int(n / 2)][int(n / 2)] = d[pos][0]\n\t\t\t\t\td[pos][1] -= 1\n\t\t\t\telse:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\texit()\n\t\tif a[int(n / 2)][int(n / 2)] == 0:\n\t\t\ta[int(n / 2)][int(n / 2)] = d[-1][0]\n\t\tprint('YES')\n\t\tfor j in range(n):\n\t\t\tprint(*a[j])\nmain()\n", "n = int(input())\narr = list(map(int, input().split()))\ntemparr = [0] * 1001\nfor i in range(len(arr)):\n\ttemparr[arr[i]] += 1\nflag = 0\ncount = 0\narr2 = []\narr3 = []\nfor i in range(1, 1001):\n\tif temparr[i] % 2 != 0:\n\t\tcount += 1\n\telif temparr[i] % 4 == 0:\n\t\tarr2.append(i)\n\telif temparr[i] % 2 == 0:\n\t\tarr3.append(i)\nansarr = []\nfor i in range(n):\n\tansarr.append([0] * n)\nif n % 2 == 0 and count != 0:\n\tprint('NO')\nelif n % 2 != 0 and count > 1:\n\tprint('NO')\nelif n % 2 == 0:\n\tif len(arr3) != 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tindex = 1\n\t\tfor j in range(n // 2):\n\t\t\tfor i in range(n // 2):\n\t\t\t\twhile temparr[index] < 4:\n\t\t\t\t\tindex += 1\n\t\t\t\tansarr[i][j] = index\n\t\t\t\tansarr[i][n - j - 1] = index\n\t\t\t\tansarr[n - i - 1][j] = index\n\t\t\t\tansarr[n - i - 1][n - j - 1] = index\n\t\t\t\ttemparr[index] -= 4\n\t\tfor i in range(n):\n\t\t\tprint(*ansarr[i])\nelse:\n\tfor j in range(n // 2):\n\t\tfor i in range(n // 2):\n\t\t\tflag = 0\n\t\t\tfor k in range(1, 1001):\n\t\t\t\tif temparr[k] >= 4:\n\t\t\t\t\tansarr[i][j] = k\n\t\t\t\t\tansarr[i][n - j - 1] = k\n\t\t\t\t\tansarr[n - i - 1][j] = k\n\t\t\t\t\tansarr[n - i - 1][n - j - 1] = k\n\t\t\t\t\ttemparr[k] -= 4\n\t\t\t\t\tflag = 1\n\t\t\t\t\tbreak\n\t\t\tif flag == 0:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\tfor j in range(n // 2):\n\t\tflag = 0\n\t\tfor k in range(1, 1001):\n\t\t\tif temparr[k] >= 2:\n\t\t\t\tansarr[n // 2][j] = k\n\t\t\t\tansarr[n // 2][n - j - 1] = k\n\t\t\t\ttemparr[k] -= 2\n\t\t\t\tflag = 1\n\t\t\t\tbreak\n\t\tif flag == 0:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\tfor i in range(n // 2):\n\t\tflag = 0\n\t\tfor k in range(1, 1001):\n\t\t\tif temparr[k] >= 2:\n\t\t\t\tansarr[i][n // 2] = k\n\t\t\t\tansarr[n - i - 1][n // 2] = k\n\t\t\t\ttemparr[k] -= 2\n\t\t\t\tflag = 1\n\t\t\t\tbreak\n\t\tif flag == 0:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\tfor k in range(1, 1001):\n\t\tif temparr[k] == 1:\n\t\t\tansarr[n // 2][n // 2] = k\n\t\t\tflag = 1\n\t\t\tbreak\n\tif flag == 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tprint(*ansarr[i])\n", "from collections import *\n(n, a) = (int(input()), list(map(int, input().split())))\n(ans, c) = ([[0 for i in range(n)] for j in range(n)], list(map(list, Counter(a).most_common())))\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tif c[0][1] >= 4:\n\t\t\tans[i][j] = ans[i][n - j - 1] = ans[n - i - 1][j] = ans[n - i - 1][n - j - 1] = c[0][0]\n\t\t\tc[0][1] -= 4\n\t\telse:\n\t\t\texit(print('no'))\n\t\tc.sort(reverse=True, key=lambda x: x[1])\nif n & 1:\n\tfor i in range(n // 2):\n\t\tif c[0][1] >= 2:\n\t\t\tans[n // 2][i] = ans[n // 2][n - i - 1] = c[0][0]\n\t\t\tc[0][1] -= 2\n\t\telse:\n\t\t\texit(print('no'))\n\t\tc.sort(reverse=True, key=lambda x: x[1])\n\t\tif c[0][1] >= 2:\n\t\t\tans[i][n // 2] = ans[n - i - 1][n // 2] = c[0][0]\n\t\t\tc[0][1] -= 2\n\t\telse:\n\t\t\texit(print('no'))\n\t\tc.sort(reverse=True, key=lambda x: x[1])\n\tans[n // 2][n // 2] = c[0][0]\nprint('YES')\nfor i in range(n):\n\tprint(*ans[i])\n", "num = int(input().strip())\nnum_dict = dict()\nnums = [int(i) for i in input().strip().split()]\nfor i in nums:\n\tif i not in num_dict:\n\t\tnum_dict[i] = 1\n\telse:\n\t\tnum_dict[i] += 1\nmatrix = [[0 for i in range(num)] for j in range(num)]\nmid = num // 2\nif num % 2 != 0:\n\tfor key in num_dict:\n\t\tif num_dict[key] != 0 and num_dict[key] % 2 != 0:\n\t\t\tmatrix[mid][mid] = key\n\t\t\tnum_dict[key] -= 1\n\t\t\tbreak\nfor row in range(num // 2):\n\tfor col in range(num // 2):\n\t\tif matrix[row][col] == 0:\n\t\t\tfor key in num_dict:\n\t\t\t\tif num_dict[key] >= 4 and num_dict[key] % 2 == 0:\n\t\t\t\t\tmatrix[row][col] = key\n\t\t\t\t\tmatrix[row][num - col - 1] = key\n\t\t\t\t\tmatrix[num - row - 1][col] = key\n\t\t\t\t\tmatrix[num - row - 1][num - col - 1] = key\n\t\t\t\t\tnum_dict[key] -= 4\n\t\t\t\t\tbreak\nif num % 2 != 0:\n\tfor col in range(num):\n\t\tif col != mid and matrix[mid][col] == 0:\n\t\t\tfor key in num_dict:\n\t\t\t\tif num_dict[key] != 0 and num_dict[key] % 2 == 0:\n\t\t\t\t\tmatrix[mid][col] = key\n\t\t\t\t\tmatrix[mid][num - col - 1] = key\n\t\t\t\t\tnum_dict[key] -= 2\n\t\t\t\t\tbreak\n\tfor row in range(num):\n\t\tif row != mid and matrix[row][mid] == 0:\n\t\t\tfor key in num_dict:\n\t\t\t\tif num_dict[key] != 0 and num_dict[key] % 2 == 0:\n\t\t\t\t\tmatrix[row][mid] = key\n\t\t\t\t\tmatrix[num - row - 1][mid] = key\n\t\t\t\t\tnum_dict[key] -= 2\n\t\t\t\t\tbreak\nyes = True\nfor i in matrix:\n\tfor j in i:\n\t\tif j == 0:\n\t\t\tyes = False\n\t\t\tbreak\nif yes:\n\tprint('Yes')\n\tfor row in matrix:\n\t\tprint(' '.join((str(i) for i in row)))\nelse:\n\tprint('No')\n", "import os\nfrom io import BytesIO, StringIO\nfrom collections import defaultdict\n\ndef input_as_list():\n\treturn list(map(int, input().split()))\n\ndef array_of(f, *dim):\n\treturn [array_of(f, *dim[1:]) for _ in range(dim[0])] if dim else f()\n\ndef main():\n\tn = int(input())\n\ta = input_as_list()\n\tout = array_of(int, n, n)\n\n\tdef put(x, i, j):\n\t\tout[i][j] = x\n\t\tout[n - i - 1][j] = x\n\t\tout[i][n - j - 1] = x\n\t\tout[n - i - 1][n - j - 1] = x\n\td = defaultdict(int)\n\tfor x in a:\n\t\td[x] += 1\n\tif n % 2 == 0:\n\t\tfor v in d.values():\n\t\t\tif v % 4 != 0:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\tix = 0\n\t\tfor k in d.keys():\n\t\t\tfor _ in range(d[k] // 4):\n\t\t\t\t(i, j) = divmod(ix, n // 2)\n\t\t\t\tput(k, i, j)\n\t\t\t\tix += 1\n\telse:\n\t\tones = []\n\t\ttwos = []\n\t\tfours = []\n\t\tfor (k, v) in d.items():\n\t\t\tif v % 2 != 0:\n\t\t\t\tif ones:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\treturn\n\t\t\t\telse:\n\t\t\t\t\tones.append(k)\n\t\t\t\t\td[k] -= 1\n\t\tfor (k, v) in d.items():\n\t\t\tif v % 4 != 0:\n\t\t\t\tif len(twos) >= n:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\treturn\n\t\t\t\telse:\n\t\t\t\t\ttwos.append(k)\n\t\t\t\t\td[k] -= 2\n\t\tfor (k, v) in d.items():\n\t\t\tif v % 4 != 0:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\t\telse:\n\t\t\t\tfor _ in range(v // 4):\n\t\t\t\t\tfours.append(k)\n\t\tif not ones:\n\t\t\tprint('NO')\n\t\t\treturn\n\t\twhile len(twos) < n - 1:\n\t\t\tk = fours.pop()\n\t\t\ttwos.append(k)\n\t\t\ttwos.append(k)\n\t\tix = 0\n\t\tfor k in fours:\n\t\t\t(i, j) = divmod(ix, n // 2)\n\t\t\tput(k, i, j)\n\t\t\tix += 1\n\t\tfor i in range(n // 2):\n\t\t\tk = twos.pop()\n\t\t\tput(k, i, n // 2)\n\t\tfor i in range(n // 2):\n\t\t\tk = twos.pop()\n\t\t\tput(k, n // 2, i)\n\t\tput(ones[0], n // 2, n // 2)\n\tout_str = ['YES\\n']\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tout_str.append(str(out[i][j]) + ' ')\n\t\tout_str.append('\\n')\n\tprint(''.join(out_str))\nmain()\n", "n = int(input())\nList = list(map(int, input().split()))\nd = {}\nfor i in List:\n\tif i in d:\n\t\td[i] += 1\n\telse:\n\t\td[i] = 1\nList1 = []\ni = 0\nfor i in range(n):\n\tList2 = []\n\tfor j in range(n):\n\t\tList2.append(i)\n\tList1.append(List2)\ncn = n\ncount = 0\ncount1 = 0\nn //= 2\nif n == 0:\n\tprint('YES')\n\ts = ''.join(map(str, List))\n\tprint(s)\nfor i in range(n):\n\tfor j in range(n):\n\t\tfor k in d:\n\t\t\tif d[k] >= 4:\n\t\t\t\td[k] -= 4\n\t\t\t\tList1[i][j] = k\n\t\t\t\tList1[cn - 1 - i][j] = k\n\t\t\t\tList1[i][cn - 1 - j] = k\n\t\t\t\tList1[cn - 1 - i][cn - 1 - j] = k\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\tif count == 0:\n\t\t\tprint('NO')\n\t\t\tcount1 = 0\n\t\t\tbreak\n\t\telse:\n\t\t\tcount = 0\n\t\t\tcount1 += 1\n\tif count1 == 0:\n\t\tbreak\nif count1 != 0:\n\tif cn % 2 == 0:\n\t\tprint('YES')\n\t\tfor i in range(len(List1)):\n\t\t\ts = ' '.join(map(str, List1[i]))\n\t\t\tprint(s)\n\telse:\n\t\tj = n\n\t\tfor i in range(n):\n\t\t\tcount = 0\n\t\t\tfor k in d:\n\t\t\t\tif d[k] >= 2:\n\t\t\t\t\td[k] -= 2\n\t\t\t\t\tList1[i][j] = k\n\t\t\t\t\tList1[cn - 1 - i][j] = k\n\t\t\t\t\tcount += 1\n\t\t\t\t\tbreak\n\t\t\tif count == 0:\n\t\t\t\tprint('NO')\n\t\t\t\tcount1 = 0\n\t\t\t\tbreak\n\t\tif count1 != 0:\n\t\t\ti = n\n\t\t\tfor j in range(n):\n\t\t\t\tcount = 0\n\t\t\t\tfor k in d:\n\t\t\t\t\tif d[k] >= 2:\n\t\t\t\t\t\td[k] -= 2\n\t\t\t\t\t\tList1[i][j] = k\n\t\t\t\t\t\tList1[i][cn - 1 - j] = k\n\t\t\t\t\t\tcount += 1\n\t\t\t\t\t\tbreak\n\t\t\t\tif count == 0:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\tcount1 = 0\n\t\t\t\t\tbreak\n\t\tif count1 != 0:\n\t\t\ti = n\n\t\t\tj = n\n\t\t\tfor k in d:\n\t\t\t\tif d[k] > 0:\n\t\t\t\t\tList1[i][j] = k\n\t\t\t\t\tbreak\n\t\t\tprint('YES')\n\t\t\tfor i in range(len(List1)):\n\t\t\t\ts = ' '.join(map(str, List1[i]))\n\t\t\t\tprint(s)\n", "def draw(arr, size, i, j, value):\n\tarr[i][j] = value\n\tarr[i][size - 1 - j] = value\n\tarr[size - 1 - i][j] = value\n\tarr[size - 1 - i][size - 1 - j] = value\n\ndef drawRow(arr, size, index, value):\n\tarr[index][size // 2] = value\n\tarr[size - 1 - index][size // 2] = value\n\ndef drawCol(arr, size, index, value):\n\tarr[size // 2][index] = value\n\tarr[size // 2][size - 1 - index] = value\n\ndef printt(arr, size):\n\tfor i in range(size):\n\t\tprint(*arr[i])\nsize = int(input())\nitems = list(map(int, input().split()))\nlog = {}\nquads = []\ntwos = []\none = -1\nx = False\nfor i in items:\n\tif i in log:\n\t\tlog[i] += 1\n\t\tif log[i] % 4 == 0:\n\t\t\tquads.append(i)\n\telse:\n\t\tlog[i] = 1\nfor (key, value) in log.items():\n\tif value % 4 == 1:\n\t\tone = key\n\tif value % 4 == 3:\n\t\tif one != -1:\n\t\t\tx = True\n\t\telse:\n\t\t\ttwos.append(key)\n\t\t\tone = key\n\telif value % 4 == 2:\n\t\ttwos.append(key)\nif size % 2 == 0:\n\tif (size / 2) ** 2 == len(quads):\n\t\tprint('YES')\n\t\tdrawing = [[0] * size for i in range(size)]\n\t\tfor i in range(size // 2):\n\t\t\tfor j in range(size // 2):\n\t\t\t\tvalue = quads.pop()\n\t\t\t\tdraw(drawing, size, i, j, value)\n\t\tprintt(drawing, size)\n\telse:\n\t\tprint('NO')\nelif len(quads) < (size // 2) ** 2 or one == -1 or x:\n\tprint('NO')\nelse:\n\ttemp4 = len(quads) - (size // 2) ** 2\n\ttemp2 = len(twos) + temp4 * 2\n\tif temp2 == size - 1:\n\t\tprint('YES')\n\t\tdrawing = [[0] * size for i in range(size)]\n\t\tfor i in range(size // 2):\n\t\t\tfor j in range(size // 2):\n\t\t\t\tvalue = quads.pop()\n\t\t\t\tdraw(drawing, size, i, j, value)\n\t\twhile len(quads) > 0:\n\t\t\tvalue = quads.pop()\n\t\t\ttwos.append(value)\n\t\t\ttwos.append(value)\n\t\tfor i in range(size // 2):\n\t\t\tvalue = twos.pop()\n\t\t\tdrawRow(drawing, size, i, value)\n\t\t\tvalue = twos.pop()\n\t\t\tdrawCol(drawing, size, i, value)\n\t\tdrawing[size // 2][size // 2] = one\n\t\tprintt(drawing, size)\n\telse:\n\t\tprint('NO')\n", "n = int(input())\ncnt = [0] * 1007\na = [[0] * 20 for i in range(20)]\nder = list(map(int, input().split()))\nfor i in der:\n\tcnt[i] += 1\ncel = []\nfor i in range((n + 1) // 2):\n\tfor j in range((n + 1) // 2):\n\t\tif i != n - i - 1 and j != n - j - 1:\n\t\t\tcel.append((4, (i, j)))\n\t\telif (i != n - i - 1) ^ (j != n - j - 1):\n\t\t\tcel.append((2, (i, j)))\n\t\telse:\n\t\t\tcel.append((1, (i, j)))\nfor cur in [4, 2, 1]:\n\tlst = 1\n\tfor it in cel:\n\t\tif it[0] != cur:\n\t\t\tcontinue\n\t\ti = it[1][0]\n\t\tj = it[1][1]\n\t\twhile lst < 1007 and cnt[lst] < cur:\n\t\t\tlst += 1\n\t\tif lst == 1007:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\t\ta[i][j] = a[n - i - 1][j] = a[i][n - j - 1] = a[n - i - 1][n - j - 1] = lst\n\t\tcnt[lst] -= cur\nprint('YES')\nfor i in range(n):\n\tprint(*a[i][:n])\n", "from collections import Counter\nn = int(input())\nm = list(map(int, input().split()))\nr = [[0 for _ in range(n)] for _ in range(n)]\nif n % 2 == 0:\n\tc = Counter(m)\n\tans = []\n\tfor x in c:\n\t\tif c[x] % 4 != 0:\n\t\t\tprint('NO')\n\t\t\tbreak\n\t\telse:\n\t\t\tans.extend([x] * (c[x] // 4))\n\telse:\n\t\tx = n // 2 * (n // 2)\n\t\ti = 0\n\t\tj = 0\n\t\tfor x in ans:\n\t\t\tr[i][j] = x\n\t\t\tr[i][-j - 1] = x\n\t\t\tr[-i - 1][j] = x\n\t\t\tr[-i - 1][-j - 1] = x\n\t\t\tj += 1\n\t\t\tif j == n // 2:\n\t\t\t\tj = 0\n\t\t\t\ti += 1\n\t\tprint('YES')\n\t\tfor x in r:\n\t\t\tprint(*x)\nelse:\n\tc = Counter(m)\n\tcc = 0\n\tindex = 0\n\tfor x in c:\n\t\tif c[x] % 2 != 0:\n\t\t\tcc += 1\n\t\t\tindex = x\n\tif cc != 1:\n\t\tprint('NO')\n\telse:\n\t\tr[n // 2][n // 2] = index\n\t\tc[index] -= 1\n\t\tif c[index] == 0:\n\t\t\tdel c[index]\n\t\tcount = n // 2 + n // 2\n\t\tused = []\n\t\tans = []\n\t\ts = sum([c[x] for x in c]) - count * 2\n\t\tif s % 4 != 0:\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tans = []\n\t\t\tused = []\n\t\t\tfor x in c:\n\t\t\t\tif c[x] % 4 != 0:\n\t\t\t\t\tcount -= 1\n\t\t\t\t\tans.append(x)\n\t\t\t\t\tc[x] -= 2\n\t\t\t\t\tif c[x] == 0:\n\t\t\t\t\t\tused.append(x)\n\t\t\t\tif count == 0:\n\t\t\t\t\tbreak\n\t\t\tfor x in used:\n\t\t\t\tdel c[x]\n\t\t\tused = []\n\t\t\tif count > 0:\n\t\t\t\tfor x in c:\n\t\t\t\t\tif c[x] <= count * 2:\n\t\t\t\t\t\tused.append(x)\n\t\t\t\t\t\tans.extend([x] * (c[x] // 2))\n\t\t\t\t\t\tcount -= c[x] // 2\n\t\t\t\t\telse:\n\t\t\t\t\t\tc[x] -= count * 2\n\t\t\t\t\t\tans.extend([x] * count)\n\t\t\t\t\t\tcount = 0\n\t\t\t\t\tif count == 0:\n\t\t\t\t\t\tbreak\n\t\t\tfor x in used:\n\t\t\t\tdel c[x]\n\t\t\tf = False\n\t\t\ti = 0\n\t\t\tfor x in ans:\n\t\t\t\tif not f:\n\t\t\t\t\tr[n // 2][i] = x\n\t\t\t\t\tr[n // 2][-i - 1] = x\n\t\t\t\t\tf = True\n\t\t\t\telse:\n\t\t\t\t\tr[i][n // 2] = x\n\t\t\t\t\tr[-i - 1][n // 2] = x\n\t\t\t\t\tf = False\n\t\t\t\t\ti += 1\n\t\t\tans = []\n\t\t\tfor x in c:\n\t\t\t\tif c[x] % 4 != 0:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\tbreak\n\t\t\t\telse:\n\t\t\t\t\tans.extend([x] * (c[x] // 4))\n\t\t\telse:\n\t\t\t\tx = n // 2 * (n // 2)\n\t\t\t\ti = 0\n\t\t\t\tj = 0\n\t\t\t\tfor x in ans:\n\t\t\t\t\tr[i][j] = x\n\t\t\t\t\tr[i][-j - 1] = x\n\t\t\t\t\tr[-i - 1][j] = x\n\t\t\t\t\tr[-i - 1][-j - 1] = x\n\t\t\t\t\tj += 1\n\t\t\t\t\tif j == n // 2:\n\t\t\t\t\t\tj = 0\n\t\t\t\t\t\ti += 1\n\t\t\t\tprint('YES')\n\t\t\t\tfor x in r:\n\t\t\t\t\tprint(*x)\n", "from collections import defaultdict, deque\nimport sys\n\ndef input():\n\treturn sys.stdin.readline().rstrip()\n\ndef getcnt(LIST):\n\td = defaultdict(int)\n\tfor v in LIST:\n\t\td[v] += 1\n\treturn d\n\ndef odd_only_1(dic):\n\tcnt = 0\n\tres_value = 0\n\tfor (key, value) in dic.items():\n\t\tif value % 2 != 0:\n\t\t\tcnt += 1\n\t\t\tres_value = key\n\tif cnt == 1:\n\t\treturn res_value\n\treturn 0\n\ndef slv():\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\td = getcnt(a)\n\tif n % 2 == 0:\n\t\tif all((value % 4 == 0 for value in d.values())):\n\t\t\tstack = deque([])\n\t\t\tfor (key, value) in d.items():\n\t\t\t\tfor _ in range(value):\n\t\t\t\t\tstack.append(key)\n\t\t\tprint('YES')\n\t\t\tans = [[-1] * n for i in range(n)]\n\t\t\tfor i in range(n // 2):\n\t\t\t\tfor j in range(n // 2):\n\t\t\t\t\tnums = stack[0]\n\t\t\t\t\tans[i][j] = ans[i][-(j + 1)] = ans[-(i + 1)][j] = ans[-(i + 1)][-(j + 1)] = nums\n\t\t\t\t\tfor _ in range(4):\n\t\t\t\t\t\tstack.popleft()\n\t\t\tfor i in range(n):\n\t\t\t\tprint(*ans[i])\n\t\t\treturn\n\t\telse:\n\t\t\tprint('NO')\n\t\treturn\n\telse:\n\t\tif not odd_only_1(d):\n\t\t\tprint('NO')\n\t\t\treturn\n\t\tval = odd_only_1(d)\n\t\td[val] -= 1\n\t\tans = [[0] * n for i in range(n)]\n\t\tans[n // 2][n // 2] = val\n\t\tif sum((value // 4 for value in d.values())) < (n // 2) ** 2:\n\t\t\tprint('NO')\n\t\t\treturn\n\t\tprint('YES')\n\t\tstack = deque([])\n\t\tfor (key, value) in d.items():\n\t\t\t(u, v) = divmod(value, 4)\n\t\t\tfor _ in range(4 * u):\n\t\t\t\tstack.appendleft(key)\n\t\t\tfor _ in range(v):\n\t\t\t\tstack.append(key)\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tnums = stack[0]\n\t\t\t\tans[i][j] = ans[i][-(j + 1)] = ans[-(i + 1)][j] = ans[-(i + 1)][-(j + 1)] = nums\n\t\t\t\tfor _ in range(4):\n\t\t\t\t\tstack.popleft()\n\t\tfor i in range(n // 2):\n\t\t\tnum = stack[0]\n\t\t\tans[n // 2][i] = ans[n // 2][-(i + 1)] = num\n\t\t\tfor _ in range(2):\n\t\t\t\tstack.popleft()\n\t\tfor i in range(n // 2):\n\t\t\tnum = stack[0]\n\t\t\tans[i][n // 2] = ans[-(i + 1)][n // 2] = num\n\t\t\tfor _ in range(2):\n\t\t\t\tstack.popleft()\n\t\tfor i in range(n):\n\t\t\tprint(*ans[i])\n\treturn\n\ndef main():\n\tt = 1\n\tfor i in range(t):\n\t\tslv()\n\treturn\nmain()\n", "from collections import Counter\nn = int(input())\na = [int(o) for o in input().split()]\nu = list(set(a))\nc = dict(Counter(a))\ncm = [0] * n ** 2\nlu = len(u)\npk = 0\nfor i in range(lu):\n\tcm[i] = [u[i], c[u[i]]]\nif n % 2 == 0:\n\tflag = 0\n\tfor i in range(lu):\n\t\tif cm[i][1] % 4 != 0:\n\t\t\tflag = 1\n\tif flag == 1:\n\t\tprint('NO')\n\telse:\n\t\tk = 0\n\t\tmatr = [[0 for i in range(n)] for j in range(n)]\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tmatr[i][j] = cm[k][0]\n\t\t\t\tmatr[i][n - 1 - j] = cm[k][0]\n\t\t\t\tmatr[n - 1 - i][j] = cm[k][0]\n\t\t\t\tmatr[n - 1 - i][n - 1 - j] = cm[k][0]\n\t\t\t\tcm[k][1] -= 4\n\t\t\t\tif cm[k][1] == 0:\n\t\t\t\t\tk += 1\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tprint(*matr[i])\nelse:\n\tfp = []\n\ttp = []\n\top = []\n\tfor i in range(lu):\n\t\tif cm[i][1] >= 4:\n\t\t\tfp.append([cm[i][0], 4 * (cm[i][1] // 4)])\n\t\t\tcm[i][1] = cm[i][1] % 4\n\t\tif cm[i][1] >= 2:\n\t\t\ttp.append([cm[i][0], 2 * (cm[i][1] // 2)])\n\t\t\tcm[i][1] = cm[i][1] % 2\n\t\tif cm[i][1] >= 1:\n\t\t\top.append([cm[i][0], 1 * (cm[i][1] // 1)])\n\t\t\tcm[i][1] = cm[i][1] % 1\n\tif len(op) > 1:\n\t\tflag = 1\n\ttry:\n\t\tflag = 0\n\t\tk = 0\n\t\tmatr = [[0 for i in range(n)] for j in range(n)]\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tmatr[i][j] = fp[k][0]\n\t\t\t\tmatr[i][n - 1 - j] = fp[k][0]\n\t\t\t\tmatr[n - 1 - i][j] = fp[k][0]\n\t\t\t\tmatr[n - 1 - i][n - 1 - j] = fp[k][0]\n\t\t\t\tfp[k][1] -= 4\n\t\t\t\tif fp[k][1] == 0:\n\t\t\t\t\tk += 1\n\texcept:\n\t\tflag = 1\n\tif flag == 0:\n\t\ttry:\n\t\t\tloll = n // 2\n\t\t\tfor i in range(n // 2):\n\t\t\t\tmatr[loll][i] = fp[k][0]\n\t\t\t\tmatr[loll][n - 1 - i] = fp[k][0]\n\t\t\t\tfp[k][1] -= 2\n\t\t\t\tif fp[k][1] == 0:\n\t\t\t\t\tk += 1\n\t\texcept:\n\t\t\ttry:\n\t\t\t\tk = 0\n\t\t\t\tfor j in range(i, n // 2):\n\t\t\t\t\tmatr[loll][j] = tp[k][0]\n\t\t\t\t\tmatr[loll][n - 1 - j] = tp[k][0]\n\t\t\t\t\ttp[k][1] -= 2\n\t\t\t\t\tif tp[k][1] == 0:\n\t\t\t\t\t\tk += 1\n\t\t\t\tpk = k\n\t\t\texcept:\n\t\t\t\tflag = 1\n\t\ttry:\n\t\t\tif pk == 0:\n\t\t\t\tloll = n // 2\n\t\t\t\tfor i in range(n // 2):\n\t\t\t\t\tmatr[i][loll] = fp[k][0]\n\t\t\t\t\tmatr[n - 1 - i][loll] = fp[k][0]\n\t\t\t\t\tfp[k][1] -= 2\n\t\t\t\t\tif fp[k][1] == 0:\n\t\t\t\t\t\tk += 1\n\t\t\telse:\n\t\t\t\tk = pk\n\t\t\t\ttry:\n\t\t\t\t\tfor j in range(i, n // 2):\n\t\t\t\t\t\tmatr[j][loll] = tp[k][0]\n\t\t\t\t\t\tmatr[n - 1 - j][loll] = tp[k][0]\n\t\t\t\t\t\ttp[k][1] -= 2\n\t\t\t\t\t\tif tp[k][1] == 0:\n\t\t\t\t\t\t\tk += 1\n\t\t\t\texcept:\n\t\t\t\t\tflag = 1\n\t\texcept:\n\t\t\ttry:\n\t\t\t\tk = 0\n\t\t\t\tfor j in range(i, n // 2):\n\t\t\t\t\tmatr[j][loll] = tp[k][0]\n\t\t\t\t\tmatr[n - 1 - j][loll] = tp[k][0]\n\t\t\t\t\ttp[k][1] -= 2\n\t\t\t\t\tif tp[k][1] == 0:\n\t\t\t\t\t\tk += 1\n\t\t\texcept:\n\t\t\t\tflag = 1\n\tif flag == 0:\n\t\tyoyo = op[0][0]\n\t\tmatr[n // 2][n // 2] = yoyo\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tprint(*matr[i])\n\telse:\n\t\tprint('NO')\n", "n = int(input())\na = [int(t) for t in input().split(' ')]\ncount = {}\nfor num in a:\n\tcount[num] = count.get(num, 0) + 1\n\ndef invalid():\n\tprint('NO')\n\texit()\nmx = [[0] * n for _ in range(n)]\ncenter = n // 2\nleft = 0\ntop = 0\nfor num in count:\n\twhile count[num] >= 4 and top < center:\n\t\tmx[left][top] = mx[-1 - left][top] = mx[left][-1 - top] = mx[-1 - left][-1 - top] = num\n\t\tcount[num] -= 4\n\t\tleft += 1\n\t\tif left >= center:\n\t\t\tleft = 0\n\t\t\ttop += 1\n\tif top >= n:\n\t\tbreak\nif n % 2 == 1:\n\tleft = 0\n\tfor num in count:\n\t\twhile count[num] >= 2 and left < center:\n\t\t\tcount[num] -= 2\n\t\t\tmx[center][left] = mx[center][-1 - left] = num\n\t\t\tleft += 1\n\ttop = 0\n\tfor num in count:\n\t\twhile count[num] >= 2 and top < center:\n\t\t\tcount[num] -= 2\n\t\t\tmx[top][center] = mx[-1 - top][center] = num\n\t\t\ttop += 1\n\tfor num in count:\n\t\tif count[num] >= 1:\n\t\t\tcount[num] -= 1\n\t\t\tmx[center][center] = num\n\t\t\tbreak\nfor num in count:\n\tif count[num] > 0:\n\t\tinvalid()\nprint('YES')\nfor x in range(n):\n\tprint(' '.join((str(t) for t in mx[x])))\n", "from sys import stdin\nn = int(stdin.readline().strip())\ns = list(map(int, stdin.readline().strip().split()))\nd = dict()\nfor i in s:\n\tif i not in d:\n\t\td[i] = 1\n\telse:\n\t\td[i] += 1\ns = []\nfor i in d:\n\ts.append([d[i], i])\ns.sort(reverse=True)\nmt = [[-1 for i in range(n)] for j in range(n)]\nif n % 2 == 0:\n\tt = True\n\ty = 0\n\tfor i in d:\n\t\tif d[i] % 4 != 0:\n\t\t\tt = False\n\t\t\tbreak\n\tif t == False:\n\t\tprint('NO')\n\t\texit(0)\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tn1 = n - 1\n\t\t\tif mt[i][j] != -1:\n\t\t\t\tcontinue\n\t\t\tmt[i][j] = s[y][1]\n\t\t\tmt[i][n1 - j] = s[y][1]\n\t\t\tmt[n1 - i][j] = s[y][1]\n\t\t\tmt[n1 - i][n1 - j] = s[y][1]\n\t\t\ts[y][0] -= 4\n\t\t\tif s[y][0] == 0:\n\t\t\t\ty += 1\nelse:\n\tt = False\n\tcen = -1\n\tfor i in range(len(s)):\n\t\tif s[i][0] % 2 != 0:\n\t\t\tcen = s[i][1]\n\t\t\ts[i][0] -= 1\n\t\t\tbreak\n\tmt[n // 2][n // 2] = cen\n\ty = 0\n\tn1 = n - 1\n\tfor i in range(n):\n\t\tif n == 1:\n\t\t\tbreak\n\t\tfor j in range(n):\n\t\t\tif mt[i][j] != -1:\n\t\t\t\tcontinue\n\t\t\tif j == n // 2 or i == n // 2:\n\t\t\t\tcontinue\n\t\t\tmt[i][j] = s[y][1]\n\t\t\tmt[i][n1 - j] = s[y][1]\n\t\t\tmt[n1 - i][j] = s[y][1]\n\t\t\tmt[n1 - i][n1 - j] = s[y][1]\n\t\t\ts[y][0] -= 4\n\t\t\ty1 = y\n\t\t\tif s[y][0] < 4:\n\t\t\t\ty += 1\n\t\t\tif s[y1][0] < 0:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\ts.sort(reverse=True)\n\ty = 0\n\tfor i in range(n):\n\t\tif n == 1:\n\t\t\tbreak\n\t\tfor j in range(n):\n\t\t\tn1 = n - 1\n\t\t\tif mt[i][j] != -1:\n\t\t\t\tcontinue\n\t\t\tif j != n // 2 and i != n // 2:\n\t\t\t\tcontinue\n\t\t\tmt[i][j] = s[y][1]\n\t\t\tmt[i][n1 - j] = s[y][1]\n\t\t\tmt[n1 - i][j] = s[y][1]\n\t\t\tmt[n1 - i][n1 - j] = s[y][1]\n\t\t\ts[y][0] -= 2\n\t\t\tif s[y][0] < 0:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\t\t\tif s[y][0] == 0:\n\t\t\t\ty += 1\nprint('YES')\nfor k in mt:\n\tfor k1 in k:\n\t\tprint(k1, end=' ')\n\tprint()\n", "from collections import Counter\nn = int(input())\ns = list(map(int, input().split()))\nc = Counter(s)\nm = {}\ntwo_pos = []\nfour_pos = []\none_pos = []\nif n % 2 == 1:\n\tone_pos.append((n // 2, n // 2))\n\tfor i in range(n // 2):\n\t\ttwo_pos.append(((i, n // 2), (n - i - 1, n // 2)))\n\t\ttwo_pos.append(((n // 2, i), (n // 2, n - i - 1)))\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tfour_pos.append(((i, j), (i, n - j - 1), (n - i - 1, j), (n - i - 1, n - j - 1)))\nIS_POSSIBLE = True\nfor (k, v) in c.items():\n\tif v % 2 == 1:\n\t\tif len(one_pos) == 0:\n\t\t\tIS_POSSIBLE = False\n\t\t\tbreak\n\t\tpos = one_pos.pop()\n\t\tm[pos] = k\n\t\tv -= 1\n\twhile v >= 4 and len(four_pos) > 0:\n\t\tfor e in four_pos.pop():\n\t\t\tm[e] = k\n\t\tv -= 4\n\twhile v >= 2 and len(two_pos) > 0:\n\t\tfor e in two_pos.pop():\n\t\t\tm[e] = k\n\t\tv -= 2\n\tif v > 0:\n\t\tIS_POSSIBLE = False\n\t\tbreak\nif not IS_POSSIBLE:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(m[i, j], end=' ')\n\t\tprint()\n", "def getNum(nums):\n\tfor i in nums.keys():\n\t\tif nums[i] >= 4:\n\t\t\tnums[i] -= 4\n\t\t\tif nums[i] == 0:\n\t\t\t\tdel nums[i]\n\t\t\treturn i\n\treturn -1\n\ndef getNum1(nums):\n\tfor i in nums.keys():\n\t\tif nums[i] >= 2:\n\t\t\tnums[i] -= 2\n\t\t\tif nums[i] == 0:\n\t\t\t\tdel nums[i]\n\t\t\treturn i\n\treturn -1\n\ndef main():\n\tn = int(input())\n\tnums = list(map(int, input().split()))\n\tdist_nums = {}\n\tfor i in nums:\n\t\tif i not in dist_nums.keys():\n\t\t\tdist_nums[i] = 1\n\t\telse:\n\t\t\tdist_nums[i] += 1\n\tmatrix = [[0 for i in range(n)] for j in range(n)]\n\tfor col in range(n // 2):\n\t\tfor row in range(n // 2):\n\t\t\tnum = getNum(dist_nums)\n\t\t\tif num == -1:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\t\tmatrix[row][col] = num\n\t\t\tmatrix[n - row - 1][col] = num\n\t\t\tmatrix[row][n - col - 1] = num\n\t\t\tmatrix[n - row - 1][n - col - 1] = num\n\tif n % 2 != 0:\n\t\trow = n // 2\n\t\tfor col in range(n // 2):\n\t\t\tnum = getNum1(dist_nums)\n\t\t\tif num == -1:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\t\tmatrix[row][col] = num\n\t\t\tmatrix[row][n - 1 - col] = num\n\t\tcol = n // 2\n\t\tfor row in range(n // 2):\n\t\t\tnum = getNum1(dist_nums)\n\t\t\tif num == -1:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\t\tmatrix[row][col] = num\n\t\t\tmatrix[n - 1 - row][col] = num\n\t\tnum = -1\n\t\tfor i in dist_nums.keys():\n\t\t\tif dist_nums[i] >= 1:\n\t\t\t\tnum = i\n\t\t\t\tbreak\n\t\tif num == -1:\n\t\t\tprint('NO')\n\t\t\treturn\n\t\tmatrix[n // 2][n // 2] = num\n\tprint('YES')\n\tfor i in matrix:\n\t\tfor j in i:\n\t\t\tprint(j, end=' ')\n\t\tprint()\nmain()\n", "n = int(input())\na = [int(x) for x in input().split()]\nct = {}\nnotfourdiv = 0\nnottwodiv = 0\noddnum = -1\ntwonums = []\nfournums = []\nfor v in a:\n\tct[v] = ct.get(v, 0) + 1\nfor (k, v) in ct.items():\n\tif v % 4:\n\t\tnotfourdiv += 1\n\t\tif v % 2:\n\t\t\tnottwodiv += 1\n\t\t\toddnum = k\n\t\t\tif v > 1:\n\t\t\t\tif (v - 1) % 4 != 0:\n\t\t\t\t\tnotfourdiv += 1\n\t\t\t\t\ttwonums.append(k)\n\t\t\t\telse:\n\t\t\t\t\tfournums.append(k)\n\t\telse:\n\t\t\ttwonums.append(k)\n\telse:\n\t\tfournums.append(k)\nif n % 2 == 0:\n\tif notfourdiv > 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tres = [[-1 for i in range(n)] for j in range(n)]\n\t\ti = 0\n\t\tj = 0\n\t\tfor (k, v) in ct.items():\n\t\t\tx = 0\n\t\t\twhile x < v // 4:\n\t\t\t\tres[i][j] = k\n\t\t\t\tres[n - 1 - i][j] = k\n\t\t\t\tres[i][n - j - 1] = k\n\t\t\t\tres[n - 1 - i][n - j - 1] = k\n\t\t\t\tif j == n // 2 - 1:\n\t\t\t\t\ti += 1\n\t\t\t\t\tj = 0\n\t\t\t\telse:\n\t\t\t\t\tj += 1\n\t\t\t\tx += 1\n\t\tfor row in res:\n\t\t\trow = [str(entry) for entry in row]\n\t\t\tprint(' '.join(row))\nelif nottwodiv > 1:\n\tprint('NO')\nelif notfourdiv > n:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tres = [[-1 for i in range(n)] for j in range(n)]\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tif res[i][j] != -1:\n\t\t\t\tcontinue\n\t\t\tif i == j and i == n - i - 1:\n\t\t\t\tres[i][j] = oddnum\n\t\t\t\tct[oddnum] -= 1\n\t\t\telif i == n - i - 1:\n\t\t\t\tif twonums:\n\t\t\t\t\tval = twonums[-1]\n\t\t\t\t\tres[i][j] = val\n\t\t\t\t\tres[i][n - j - 1] = val\n\t\t\t\t\tct[val] -= 2\n\t\t\t\t\ttwonums.pop()\n\t\t\t\t\tif ct[val] > 0:\n\t\t\t\t\t\tfournums.insert(0, val)\n\t\t\t\telse:\n\t\t\t\t\tval = fournums[-1]\n\t\t\t\t\tres[i][j] = val\n\t\t\t\t\tres[i][n - j - 1] = val\n\t\t\t\t\tct[val] -= 2\n\t\t\t\t\tfournums.pop()\n\t\t\t\t\ttwonums.insert(0, val)\n\t\t\telif j == n - j - 1:\n\t\t\t\tif twonums:\n\t\t\t\t\tval = twonums[-1]\n\t\t\t\t\tres[i][j] = val\n\t\t\t\t\tres[n - i - 1][j] = val\n\t\t\t\t\tct[val] -= 2\n\t\t\t\t\ttwonums.pop()\n\t\t\t\t\tif ct[val] > 0:\n\t\t\t\t\t\tfournums.insert(0, val)\n\t\t\t\telse:\n\t\t\t\t\tval = fournums[-1]\n\t\t\t\t\tres[i][j] = val\n\t\t\t\t\tres[n - i - 1][j] = val\n\t\t\t\t\tct[val] -= 2\n\t\t\t\t\tfournums.pop()\n\t\t\t\t\ttwonums.insert(0, val)\n\ti = 0\n\tj = 0\n\tfor (k, v) in ct.items():\n\t\tif v == 0:\n\t\t\tcontinue\n\t\tx = 0\n\t\twhile x < v:\n\t\t\tif res[i][j] == -1:\n\t\t\t\tres[i][j] = k\n\t\t\t\tx += 1\n\t\t\t\tif res[n - i - 1][j] == -1:\n\t\t\t\t\tres[n - i - 1][j] = k\n\t\t\t\t\tx += 1\n\t\t\t\tif res[i][n - j - 1] == -1:\n\t\t\t\t\tres[i][n - j - 1] = k\n\t\t\t\t\tx += 1\n\t\t\t\tif res[n - i - 1][n - j - 1] == -1:\n\t\t\t\t\tres[n - i - 1][n - j - 1] = k\n\t\t\t\t\tx += 1\n\t\t\tif j == n // 2:\n\t\t\t\ti += 1\n\t\t\t\tj = 0\n\t\t\telse:\n\t\t\t\tj += 1\n\tfor row in res:\n\t\trow = [str(entry) for entry in row]\n\t\tprint(' '.join(row))\n", "import sys, re\nfrom collections import deque, defaultdict, Counter\nfrom math import sqrt, hypot, factorial, pi, sin, cos, radians, log10\nif sys.version_info.minor >= 5:\n\tfrom math import gcd\nelse:\n\tfrom fractions import gcd\nfrom heapq import heappop, heappush, heapify, heappushpop\nfrom bisect import bisect_left, bisect_right\nfrom itertools import permutations, combinations, product, accumulate\nfrom operator import itemgetter, mul\nfrom copy import copy, deepcopy\nfrom functools import reduce, partial\nfrom fractions import Fraction\nfrom string import ascii_lowercase, ascii_uppercase, digits\n\ndef input():\n\treturn sys.stdin.readline().strip()\n\ndef list2d(a, b, c):\n\treturn [[c] * b for i in range(a)]\n\ndef list3d(a, b, c, d):\n\treturn [[[d] * c for j in range(b)] for i in range(a)]\n\ndef ceil(x, y=1):\n\treturn int(-(-x // y))\n\ndef round(x):\n\treturn int((x * 2 + 1) // 2)\n\ndef fermat(x, y, MOD):\n\treturn x * pow(y, MOD - 2, MOD) % MOD\n\ndef lcm(x, y):\n\treturn x * y // gcd(x, y)\n\ndef lcm_list(nums):\n\treturn reduce(lcm, nums, 1)\n\ndef gcd_list(nums):\n\treturn reduce(gcd, nums, nums[0])\n\ndef INT():\n\treturn int(input())\n\ndef MAP():\n\treturn map(int, input().split())\n\ndef LIST():\n\treturn list(map(int, input().split()))\nsys.setrecursionlimit(10 ** 9)\nINF = float('inf')\nMOD = 10 ** 9 + 7\nN = INT()\nA = LIST()\nC = Counter(A)\nC2 = Counter()\ncenter = 0\nfor (k, v) in C.items():\n\tC2[v % 4] += 1\n\tif (v % 4 == 1 or v % 4 == 3) and center != 0:\n\t\tprint('NO')\n\t\texit()\n\tif v % 4 == 1 or v % 4 == 3:\n\t\tcenter = k\ngrid = list2d(N, N, 0)\nif N % 2 == 0:\n\tfor (k, v) in C2.items():\n\t\tif k != 0 and v != 0:\n\t\t\tprint('NO')\n\t\t\texit()\n\tl = []\n\tfor (k, v) in C.items():\n\t\tfor _ in range(v // 4):\n\t\t\tl.append(k)\n\tk = 0\n\tfor i in range(N // 2):\n\t\tfor j in range(N // 2):\n\t\t\tgrid[i][j] = l[k]\n\t\t\tgrid[N - 1 - i][N - 1 - j] = l[k]\n\t\t\tgrid[i][N - 1 - j] = l[k]\n\t\t\tgrid[N - 1 - i][j] = l[k]\n\t\t\tk += 1\n\tprint('YES')\n\tfor i in range(N):\n\t\tprint(*grid[i])\nelse:\n\tfor (k, v) in C2.items():\n\t\tif k == 2 and v > N - 1:\n\t\t\tprint('NO')\n\t\t\texit()\n\tC[center] -= 1\n\tgrid[N // 2][N // 2] = center\n\tl = []\n\tfor (k, v) in C.items():\n\t\tif v % 4 == 2:\n\t\t\tl.append(k)\n\t\t\tC[k] -= 2\n\tk = 0\n\tfor i in range(len(l) // 2):\n\t\tgrid[N // 2][i] = l[k]\n\t\tgrid[N // 2][N - 1 - i] = l[k]\n\t\tk += 1\n\t\tgrid[i][N // 2] = l[k]\n\t\tgrid[N - 1 - i][N // 2] = l[k]\n\t\tk += 1\n\tl = []\n\tfor (k, v) in C.items():\n\t\tif v % 4 == 0:\n\t\t\tfor _ in range(v // 4):\n\t\t\t\tl.append(k)\n\tk = 0\n\tfor i in range(N // 2):\n\t\tfor j in range(ceil(N, 2)):\n\t\t\tif grid[i][j] == 0:\n\t\t\t\tgrid[i][j] = l[k]\n\t\t\t\tgrid[N - 1 - i][N - 1 - j] = l[k]\n\t\t\t\tgrid[i][N - 1 - j] = l[k]\n\t\t\t\tgrid[N - 1 - i][j] = l[k]\n\t\t\t\tk += 1\n\tfor i in range(N // 2):\n\t\tif grid[N // 2][i] == 0:\n\t\t\tgrid[N // 2][i] = grid[i][N // 2]\n\t\t\tgrid[N // 2][N - 1 - i] = grid[N - 1 - i][N // 2]\n\tprint('YES')\n\tfor i in range(N):\n\t\tprint(*grid[i])\n", "from collections import Counter\nn = int(input())\na = list(map(int, input().split()))\nx = list(set(a))\ny = []\none = []\ntwo = []\nc = Counter(a)\nans = [[0] * n for _ in range(n)]\nif n % 2:\n\tfor i in x:\n\t\tif c[i] % 4 == 1 or c[i] % 4 == 3:\n\t\t\tone.append(i)\n\t\t\tif c[i] % 4 == 3:\n\t\t\t\ttwo.append(i)\n\t\t\tif len(one) > 1 or len(two) > n - 1:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\telif c[i] % 4 == 2:\n\t\t\ttwo.append(i)\n\t\t\tif len(two) > n - 1:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\tfor j in range(c[i] // 4):\n\t\t\ty.append(i)\n\tk = one.pop()\n\tans[n // 2][n // 2] = k\n\tif len(two) < n - 1:\n\t\tm = n - 1 - len(two)\n\t\tif m % 2:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\tfor i in range(m // 2):\n\t\t\tk = y.pop()\n\t\t\ttwo.append(k)\n\t\t\ttwo.append(k)\n\tfor i in range(n // 2):\n\t\tk = two.pop()\n\t\tans[i][n // 2] = k\n\t\tans[n - 1 - i][n // 2] = k\n\tfor i in range(n // 2):\n\t\tk = two.pop()\n\t\tans[n // 2][i] = k\n\t\tans[n // 2][n - 1 - i] = k\n\tif y:\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tk = y.pop()\n\t\t\t\tans[i][j] = k\n\t\t\t\tans[i][n - 1 - j] = k\n\t\t\t\tans[n - 1 - i][j] = k\n\t\t\t\tans[n - 1 - i][n - 1 - j] = k\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*ans[i])\nelse:\n\tfor i in x:\n\t\tif c[i] % 4:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\telse:\n\t\t\tfor j in range(c[i] // 4):\n\t\t\t\ty.append(i)\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tk = y.pop()\n\t\t\tans[i][j] = k\n\t\t\tans[i][n - 1 - j] = k\n\t\t\tans[n - 1 - i][j] = k\n\t\t\tans[n - 1 - i][n - 1 - j] = k\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*ans[i])\n", "from heapq import heappush as insert, heappop as extract_maximum\nfrom collections import Counter\nn = int(input())\ncount = {}\nnums = list(map(int, input().split()))\nc = Counter(nums)\nhp = []\nfor (num, counter) in c.items():\n\tinsert(hp, (-counter, num))\nm = n // 2\nresult = True\ntry:\n\tres = [[0 for _ in range(n)] for _ in range(n)]\n\tfor i in range(m):\n\t\tfor j in range(m):\n\t\t\tp = extract_maximum(hp)\n\t\t\tif p[0] > -4:\n\t\t\t\traise Exception\n\t\t\tres[i][j] = p[1]\n\t\t\tres[n - i - 1][j] = p[1]\n\t\t\tres[i][n - j - 1] = p[1]\n\t\t\tres[n - i - 1][n - j - 1] = p[1]\n\t\t\tnew_p = (p[0] + 4, p[1])\n\t\t\tif new_p[0]:\n\t\t\t\tinsert(hp, new_p)\n\tif n % 2:\n\t\tfor i in range(m):\n\t\t\tp = extract_maximum(hp)\n\t\t\tif p[0] > -2:\n\t\t\t\traise Exception\n\t\t\tres[i][m] = p[1]\n\t\t\tres[n - i - 1][m] = p[1]\n\t\t\tnew_p = (p[0] + 2, p[1])\n\t\t\tif new_p[0]:\n\t\t\t\tinsert(hp, new_p)\n\t\t\tp = extract_maximum(hp)\n\t\t\tif p[0] > -2:\n\t\t\t\traise Exception\n\t\t\tres[m][i] = p[1]\n\t\t\tres[m][n - i - 1] = p[1]\n\t\t\tnew_p = (p[0] + 2, p[1])\n\t\t\tif new_p[0]:\n\t\t\t\tinsert(hp, new_p)\n\t\tres[m][m] = extract_maximum(hp)[1]\nexcept Exception as e:\n\tprint('NO ', e)\n\tresult = False\nif result:\n\tprint('YES')\n\tfor r in res:\n\t\tprint(' '.join(map(str, r)))\n", "from collections import defaultdict\n\ndef main():\n\tn = int(input())\n\ta = [*map(int, input().split())]\n\tD = defaultdict(lambda : 0)\n\tfor i in range(n ** 2):\n\t\tD[a[i]] += 1\n\tif n % 2 == 0:\n\t\tflag = True\n\t\tfor x in D.keys():\n\t\t\tif D[x] % 4 != 0:\n\t\t\t\tflag = False\n\t\tprint('YES') if flag else print('NO')\n\t\tif flag:\n\t\t\tans = [[0] * n for _ in range(n)]\n\t\t\tpath = []\n\t\t\tfor i in range(n // 2):\n\t\t\t\tfor j in range(n // 2):\n\t\t\t\t\tpath.append([(i, j), (n - 1 - i, j), (i, n - 1 - j), (n - 1 - i, n - 1 - j)])\n\t\t\tz = 0\n\t\t\tfor x in D.keys():\n\t\t\t\twhile D[x]:\n\t\t\t\t\tfor i in range(4):\n\t\t\t\t\t\t(nx, ny) = path[z][i]\n\t\t\t\t\t\tans[nx][ny] = x\n\t\t\t\t\tD[x] -= 4\n\t\t\t\t\tz += 1\n\t\t\tfor i in range(n):\n\t\t\t\tprint(*ans[i])\n\telse:\n\t\todd = [0, 0]\n\t\ttwo = 0\n\t\tfor x in D.keys():\n\t\t\tif D[x] % 2:\n\t\t\t\todd[0] += 1\n\t\t\t\todd[1] = x\n\t\t\tif D[x] % 4 == 2:\n\t\t\t\ttwo += 1\n\t\tflag = True\n\t\tif odd[0] != 1 or two > n - 1:\n\t\t\tflag = False\n\t\tprint('YES') if flag else print('NO')\n\t\tif flag:\n\t\t\tans = [[0] * n for _ in range(n)]\n\t\t\tans[n // 2][n // 2] = odd[1]\n\t\t\tD[odd[1]] -= 1\n\t\t\tpath1 = []\n\t\t\tpath2 = []\n\t\t\tfor i in range(n // 2):\n\t\t\t\tfor j in range(n // 2):\n\t\t\t\t\tpath1.append([(i, j), (n - 1 - i, j), (i, n - 1 - j), (n - 1 - i, n - 1 - j)])\n\t\t\tfor i in range(n // 2):\n\t\t\t\tpath2.append([(n // 2, i), (n // 2, n - 1 - i)])\n\t\t\t\tpath2.append([(i, n // 2), (n - 1 - i, n // 2)])\n\t\t\tz = 0\n\t\t\tfor x in D.keys():\n\t\t\t\tif z == len(path1):\n\t\t\t\t\tbreak\n\t\t\t\twhile D[x]:\n\t\t\t\t\tif D[x] < 4 or z == len(path1):\n\t\t\t\t\t\tbreak\n\t\t\t\t\tfor i in range(4):\n\t\t\t\t\t\t(nx, ny) = path1[z][i]\n\t\t\t\t\t\tans[nx][ny] = x\n\t\t\t\t\tD[x] -= 4\n\t\t\t\t\tz += 1\n\t\t\tz = 0\n\t\t\tfor x in D.keys():\n\t\t\t\twhile D[x]:\n\t\t\t\t\tfor i in range(2):\n\t\t\t\t\t\t(nx, ny) = path2[z][i]\n\t\t\t\t\t\tans[nx][ny] = x\n\t\t\t\t\tD[x] -= 2\n\t\t\t\t\tz += 1\n\t\t\tfor i in range(n):\n\t\t\t\tprint(*ans[i])\nmain()\n", "import math\nimport bisect\nimport itertools\nimport sys\nI = lambda : sys.stdin.readline()\nmod = 10 ** 9 + 7\n\ndef modu(a, m):\n\tif a % m:\n\t\treturn a % m\n\treturn m\n\ndef mindiff(a):\n\tb = a[:]\n\tb.sort()\n\tm = 10000000000\n\tfor i in range(len(b) - 1):\n\t\tif b[i + 1] - b[i] < m:\n\t\t\tm = b[i + 1] - b[i]\n\treturn m\n\ndef lcm(a, b):\n\treturn a * b // math.gcd(a, b)\n\ndef merge(a, b):\n\ti = 0\n\tj = 0\n\tc = 0\n\tans = []\n\twhile i < len(a) and j < len(b):\n\t\tif a[i] < b[j]:\n\t\t\tans.append(a[i])\n\t\t\ti += 1\n\t\telse:\n\t\t\tans.append(b[j])\n\t\t\tc += len(a) - i\n\t\t\tj += 1\n\tans += a[i:]\n\tans += b[j:]\n\treturn (ans, c)\n\ndef mergesort(a):\n\tif len(a) == 1:\n\t\treturn (a, 0)\n\tmid = len(a) // 2\n\t(left, left_inversion) = mergesort(a[:mid])\n\t(right, right_inversion) = mergesort(a[mid:])\n\t(m, c) = merge(left, right)\n\tc += left_inversion + right_inversion\n\treturn (m, c)\n\ndef is_prime(num):\n\tif num == 1:\n\t\treturn False\n\tif num == 2:\n\t\treturn True\n\tif num == 3:\n\t\treturn True\n\tif num % 2 == 0:\n\t\treturn False\n\tif num % 3 == 0:\n\t\treturn False\n\tt = 5\n\ta = 2\n\twhile t <= int(math.sqrt(num)):\n\t\tif num % t == 0:\n\t\t\treturn False\n\t\tt += a\n\t\ta = 6 - a\n\treturn True\n\ndef ceil(a, b):\n\tif a % b == 0:\n\t\treturn a // b\n\telse:\n\t\treturn a // b + 1\n\ndef binsearch(arr, b, low, high):\n\tif low == high:\n\t\treturn low\n\tif arr[math.ceil((low + high) / 2)] < b:\n\t\treturn binsearch(arr, b, low, math.ceil((low + high) / 2) - 1)\n\telse:\n\t\treturn binsearch(arr, b, math.ceil((low + high) / 2), high)\n\ndef ncr1(n, r):\n\ts = 1\n\tfor i in range(min(n - r, r)):\n\t\ts *= n - i\n\t\ts %= mod\n\t\ts *= pow(i + 1, mod - 2, mod)\n\t\ts %= mod\n\treturn s\n\ndef calc(n, m, r):\n\ts = 0\n\tfor i in range(0, r + 1, 2):\n\t\ts += ncr1(n, i) * ncr1(m, i)\n\t\ts %= mod\n\treturn s\nn = int(input())\nd = {}\na = list(map(int, input().split()))\nb = []\na.sort()\nc = []\np = a[0]\nfor i in a:\n\tif i == p and len(c) != 4:\n\t\tc.append(i)\n\telif i != p or len(c) == 4:\n\t\tb.append([len(c)] + c)\n\t\tc = [i]\n\t\tp = i\nb.append([len(c)] + c)\nb.sort(reverse=True)\nkk = []\nans = [[0 for i in range(n + 1)] for j in range(n + 1)]\n\ndef printarr(ans):\n\tfor i in range(1, len(ans)):\n\t\tprint(*ans[i][1:])\nd = {}\nfor i in b:\n\tif i[0] == 3:\n\t\td[i[0] - 1] = d.get(i[0] - 1, 0) + 1\n\telse:\n\t\td[i[0]] = d.get(i[0], 0) + 1\n\tif i[0] == 3:\n\t\tkk += i[1:3]\n\t\tp = i[3]\n\telse:\n\t\tkk += i[1:]\ntry:\n\tkk += [p]\nexcept:\n\tp = 1\nif n % 2 == 0:\n\tk = 0\n\tf = 0\n\tif d.get(4, 0) < (n // 2) ** 2:\n\t\tprint('NO')\n\telse:\n\t\tfor i in range(1, n // 2 + 1):\n\t\t\tfor j in range(1, n // 2 + 1):\n\t\t\t\tif True:\n\t\t\t\t\tans[i][j] = kk[k]\n\t\t\t\t\tk += 1\n\t\t\t\t\tans[n + 1 - i][j] = kk[k]\n\t\t\t\t\tk += 1\n\t\t\t\t\tans[i][n - j + 1] = kk[k]\n\t\t\t\t\tk += 1\n\t\t\t\t\tans[n - i + 1][n - j + 1] = kk[k]\n\t\t\t\t\tk += 1\n\t\tprint('YES')\n\t\tprintarr(ans)\nelif d.get(4, 0) < (n // 2) ** 2 or (d.get(4, 0) - (n // 2) ** 2) * 2 + d.get(2, 0) < n // 2 * 2:\n\tprint('NO')\nelse:\n\tk = 0\n\tfor i in range(1, n // 2 + 1):\n\t\tfor j in range(1, n // 2 + 1):\n\t\t\tans[i][j] = kk[k]\n\t\t\tk += 1\n\t\t\tans[n + 1 - i][j] = kk[k]\n\t\t\tk += 1\n\t\t\tans[i][n - j + 1] = kk[k]\n\t\t\tk += 1\n\t\t\tans[n - i + 1][n - j + 1] = kk[k]\n\t\t\tk += 1\n\tfor i in range(1, n // 2 + 1):\n\t\tans[i][math.ceil(n / 2)] = kk[k]\n\t\tk += 1\n\t\tans[n - i + 1][math.ceil(n / 2)] = kk[k]\n\t\tk += 1\n\tfor j in range(1, n // 2 + 1):\n\t\tans[math.ceil(n / 2)][j] = kk[k]\n\t\tk += 1\n\t\tans[math.ceil(n / 2)][n - j + 1] = kk[k]\n\t\tk += 1\n\tans[math.ceil(n / 2)][math.ceil(n / 2)] = kk[k]\n\tprint('YES')\n\tprintarr(ans)\n", "n = int(input())\na = list(map(int, input().split()))\nn2 = (n - 1) * (n - 1)\nnn = len(a)\ns = [0] * 1001\nfor i in a:\n\ts[i] += 1\ncnt = 0\ncnt1 = 0\ncnt2 = 0\ncnt3 = 0\nfor i in range(1, 1001):\n\tif s[i] % 4 == 0:\n\t\tcnt += 1\n\tif s[i] % 4 == 1:\n\t\tcnt1 += 1\n\tif s[i] % 4 == 2:\n\t\tcnt2 += 1\n\tif s[i] % 4 == 3:\n\t\tcnt3 += 1\nif n % 2 == 0:\n\tif cnt1 + cnt2 + cnt3 > 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tk = int(n / 2)\n\t\tb = [[0 for count in range(n)] for count in range(n)]\n\t\tj = 0\n\t\tfor i in range(1, 1001):\n\t\t\twhile s[i] > 0:\n\t\t\t\ts[i] -= 4\n\t\t\t\tb[int(j / k)][int(j % k)] = i\n\t\t\t\tj += 1\n\t\tfor i in range(k):\n\t\t\tfor j in range(k):\n\t\t\t\tb[n - 1 - i][j] = b[i][j]\n\t\t\t\tb[i][n - 1 - j] = b[i][j]\n\t\t\t\tb[n - 1 - i][n - 1 - j] = b[i][j]\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tprint(b[i][j], end=' ')\n\t\t\tprint()\nelse:\n\tpos = True\n\tk = int(n / 2)\n\tif cnt1 + cnt3 != 1:\n\t\tpos = False\n\tif cnt3 == 1:\n\t\tif cnt2 > 2 * k - 1:\n\t\t\tpos = False\n\telif cnt2 > 2 * k:\n\t\tpos = False\n\tif pos == False:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tb = [[0 for count in range(n)] for count in range(n)]\n\t\tj = 0\n\t\tfor i in range(1, 1001):\n\t\t\tif s[i] % 2 == 1:\n\t\t\t\tb[k][k] = i\n\t\t\t\ts[i] -= 1\n\t\t\twhile s[i] >= 4 and j < k * k:\n\t\t\t\ts[i] -= 4\n\t\t\t\tb[int(j / k)][int(j % k)] = i\n\t\t\t\tj += 1\n\t\tfor i in range(k):\n\t\t\tfor j in range(k):\n\t\t\t\tb[n - 1 - i][j] = b[i][j]\n\t\t\t\tb[i][n - 1 - j] = b[i][j]\n\t\t\t\tb[n - 1 - i][n - 1 - j] = b[i][j]\n\t\tj = 0\n\t\tfor i in range(1, 1001):\n\t\t\tif s[i] % 2 == 0:\n\t\t\t\twhile s[i] > 0:\n\t\t\t\t\ts[i] -= 2\n\t\t\t\t\tif j < k:\n\t\t\t\t\t\tb[k][j] = i\n\t\t\t\t\telse:\n\t\t\t\t\t\tb[j - k][k] = i\n\t\t\t\t\tj += 1\n\t\tfor i in range(k):\n\t\t\tb[n - 1 - i][k] = b[i][k]\n\t\t\tb[k][n - 1 - i] = b[k][i]\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tprint(b[i][j], end=' ')\n\t\t\tprint()\n", "n = int(input())\na = list(map(int, input().split(' ')))\nmat = [[-1 for x in range(n)] for x in range(n)]\nfrom collections import Counter\ncount = Counter(a)\nnotpos = False\nchk = 0\nif n % 2 != 0:\n\tfor (x, y) in count.items():\n\t\tif y % 2 != 0:\n\t\t\tmat[n // 2][n // 2] = x\n\t\t\tcount[x] -= 1\n\t\t\tchk = 1\n\t\t\tbreak\n\tif chk == 0:\n\t\tnotpos = True\nfor x in range(n):\n\tfor y in range(n):\n\t\tif mat[x][y] != -1:\n\t\t\tcontinue\n\t\tif n % 2 != 0:\n\t\t\tchk = 0\n\t\t\tif x == n // 2:\n\t\t\t\tcontinue\n\t\t\telif y == n // 2:\n\t\t\t\tcontinue\n\t\tchk = 0\n\t\tfor (z, zz) in count.items():\n\t\t\tif zz >= 4:\n\t\t\t\tcount[z] -= 4\n\t\t\t\tmat[x][y] = z\n\t\t\t\tmat[n - x - 1][y] = z\n\t\t\t\tmat[x][n - y - 1] = z\n\t\t\t\tmat[n - x - 1][n - y - 1] = z\n\t\t\t\tchk = 1\n\t\t\t\tbreak\n\t\tif chk == 0:\n\t\t\tnotpos = True\nfor x in range(n):\n\tfor y in range(n):\n\t\tif mat[x][y] != -1:\n\t\t\tcontinue\n\t\tif n % 2 != 0:\n\t\t\tchk = 0\n\t\t\tif x == n // 2:\n\t\t\t\tfor (z, zz) in count.items():\n\t\t\t\t\tif zz >= 2:\n\t\t\t\t\t\tcount[z] -= 2\n\t\t\t\t\t\tmat[x][y] = z\n\t\t\t\t\t\tmat[x][n - y - 1] = z\n\t\t\t\t\t\tchk = 1\n\t\t\t\t\t\tbreak\n\t\t\t\tif chk == 0:\n\t\t\t\t\tnotpos = True\n\t\t\t\tcontinue\n\t\t\telif y == n // 2:\n\t\t\t\tfor (z, zz) in count.items():\n\t\t\t\t\tif zz >= 2:\n\t\t\t\t\t\tcount[z] -= 2\n\t\t\t\t\t\tmat[x][y] = z\n\t\t\t\t\t\tmat[n - x - 1][y] = z\n\t\t\t\t\t\tchk = 1\n\t\t\t\t\t\tbreak\n\t\t\t\tif chk == 0:\n\t\t\t\t\tnotpos = True\n\t\t\t\tcontinue\nif notpos:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tfor x in mat:\n\t\tprint(*x)\n", "from collections import Counter\nfrom sys import exit\nn = int(input())\nnumbers = list(map(int, input().split()))\nnum_counter = Counter(numbers)\nis_odd = n % 2\nsize4 = n // 2\nsize = size4 + n % 2\nres = [[None] * size for i in range(size)]\npushed4 = 0\npushed2 = 0\npushed = 0\nfor (num, count) in num_counter.items():\n\tif pushed4 < size4 ** 2:\n\t\tfor i in range(count // 4):\n\t\t\tif pushed4 == size4 ** 2:\n\t\t\t\tbreak\n\t\t\tres[pushed4 // size4][pushed4 % size4] = num\n\t\t\tpushed4 += 1\n\t\t\tnum_counter[num] -= 4\n\t\t\tcount -= 4\n\tif count and count // 2 and is_odd and (pushed2 < 2 * size4):\n\t\tfor i in range(count // 2):\n\t\t\tif pushed2 == 2 * size4:\n\t\t\t\tbreak\n\t\t\tif pushed2 < size4:\n\t\t\t\tres[pushed2][size - 1] = num\n\t\t\t\tpushed2 += 1\n\t\t\t\tnum_counter[num] -= 2\n\t\t\t\tcount -= 2\n\t\t\telse:\n\t\t\t\tres[size - 1][pushed2 % size4] = num\n\t\t\t\tpushed2 += 1\n\t\t\t\tnum_counter[num] -= 2\n\t\t\t\tcount -= 2\n\tif count and is_odd and (not pushed):\n\t\tres[size - 1][size - 1] = num\n\t\tpushed += 1\n\t\tnum_counter[num] -= 1\n\t\tcount -= 1\n\tif count:\n\t\tprint('NO')\n\t\texit(0)\nprint('YES')\nfor s in res:\n\tprint(' '.join(map(str, s)), ' '.join(map(str, s[:size - is_odd][::-1])))\nfor s in res[:size - is_odd][::-1]:\n\tprint(' '.join(map(str, s)), ' '.join(map(str, s[:size - is_odd][::-1])))\n", "n = int(input())\na = list(map(int, input().split()))\ndp = [[0] * n for i in range(n)]\nd = dict()\nfor i in range(n * n):\n\tif a[i] in d:\n\t\td[a[i]] += 1\n\telse:\n\t\td[a[i]] = 1\nif n % 2 == 0:\n\ta.sort()\n\tct = 0\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tif a[ct] != a[ct + 3]:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\t\telse:\n\t\t\t\tdp[i][j] = dp[n - i - 1][j] = dp[i][n - j - 1] = dp[n - i - 1][n - j - 1] = a[ct]\n\t\t\t\tct = ct + 4\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(dp[i][j], end=' ')\n\t\tprint()\nelse:\n\tflag = 0\n\tfor i in d:\n\t\tif d[i] % 2 == 1:\n\t\t\tdp[n // 2][n // 2] = i\n\t\t\td[i] -= 1\n\t\t\tif d[i] == 0:\n\t\t\t\tdel d[i]\n\t\t\tflag = 1\n\t\t\tbreak\n\tif flag == 0:\n\t\tprint('NO')\n\t\texit()\n\telse:\n\t\tfor i in d:\n\t\t\tif d[i] % 2 == 1:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\ts = list(set(a))\n\tlt = len(s)\n\tct = 0\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\twhile True:\n\t\t\t\tif ct == lt:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\texit()\n\t\t\t\tif s[ct] not in d or d[s[ct]] == 2 or d[s[ct]] % 2 != 0 or (d[s[ct]] == 0):\n\t\t\t\t\tct += 1\n\t\t\t\t\tcontinue\n\t\t\t\telse:\n\t\t\t\t\td[s[ct]] -= 4\n\t\t\t\t\tif d[s[ct]] == 0:\n\t\t\t\t\t\tdel d[s[ct]]\n\t\t\t\t\tbreak\n\t\t\tdp[i][j] = dp[n - i - 1][j] = dp[i][n - j - 1] = dp[n - i - 1][n - j - 1] = s[ct]\n\tct = 0\n\tfor i in range(n // 2):\n\t\twhile True:\n\t\t\tif ct == lt:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\t\tif s[ct] not in d or d[s[ct]] % 2 != 0 or d[s[ct]] == 0:\n\t\t\t\tct += 1\n\t\t\t\tcontinue\n\t\t\telse:\n\t\t\t\td[s[ct]] -= 2\n\t\t\t\tif d[s[ct]] == 0:\n\t\t\t\t\tdel d[s[ct]]\n\t\t\t\tbreak\n\t\tdp[n // 2][i] = dp[n // 2][n - i - 1] = s[ct]\n\tct = 0\n\tfor i in range(n // 2):\n\t\twhile True:\n\t\t\tif ct == lt:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\t\tif s[ct] not in d or d[s[ct]] % 2 != 0 or d[s[ct]] == 0:\n\t\t\t\tct += 1\n\t\t\t\tcontinue\n\t\t\telse:\n\t\t\t\td[s[ct]] -= 2\n\t\t\t\tif d[s[ct]] == 0:\n\t\t\t\t\tdel d[s[ct]]\n\t\t\t\tbreak\n\t\tdp[i][n // 2] = dp[n - i - 1][n // 2] = s[ct]\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(dp[i][j], end=' ')\n\t\tprint()\n", "from collections import *\nn = int(input())\na = list(map(int, input().split()))\ncnt = Counter(a)\nans = [[-1] * n for _ in range(n)]\nq = deque([])\nif n % 2 == 0:\n\tfor (k, v) in cnt.items():\n\t\tif v % 4 != 0:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\tfor _ in range(v // 4):\n\t\t\tq.append(k)\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tv = q.popleft()\n\t\t\tans[i][j] = v\n\t\t\tans[n - 1 - i][j] = v\n\t\t\tans[i][n - 1 - j] = v\n\t\t\tans[n - 1 - i][n - 1 - j] = v\nelse:\n\tflag = False\n\tfor (k, v) in cnt.items():\n\t\tif v % 2 == 1:\n\t\t\tif flag:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\t\tflag = True\n\t\t\tans[n // 2][n // 2] = k\n\t\t\tcnt[k] -= 1\n\t\tfor _ in range(v // 4):\n\t\t\tif len(q) == (n // 2) ** 2:\n\t\t\t\tbreak\n\t\t\tq.append(k)\n\t\t\tcnt[k] -= 4\n\tif len(q) < (n // 2) ** 2:\n\t\tprint('NO')\n\t\texit()\n\tfor (k, v) in cnt.items():\n\t\tfor _ in range(v // 2):\n\t\t\tq.append(k)\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tv = q.popleft()\n\t\t\tans[i][j] = v\n\t\t\tans[n - 1 - i][j] = v\n\t\t\tans[i][n - 1 - j] = v\n\t\t\tans[n - 1 - i][n - 1 - j] = v\n\tfor i in range(n // 2):\n\t\tv = q.popleft()\n\t\tans[i][n // 2] = v\n\t\tans[n - 1 - i][n // 2] = v\n\tfor i in range(n // 2):\n\t\tv = q.popleft()\n\t\tans[n // 2][i] = v\n\t\tans[n // 2][n - 1 - i] = v\nprint('YES')\nfor ans_i in ans:\n\tprint(*ans_i)\n", "def print_mat(n, l):\n\tlst = [[[0] for _ in range(int(n / 2))]] * int(n / 2)\n\tz = 1\n\tfor i in range(int(n / 2)):\n\t\tfor j in range(int(n / 2)):\n\t\t\twhile not z in l or l[z] == 0:\n\t\t\t\tz += 1\n\t\t\tlst[i] = lst[i][:j] + [z] + lst[i][j + 1:]\n\t\t\tl[z] -= 4\n\tfor i in lst:\n\t\tprint(' '.join(list(map(str, i + i[::-1]))))\n\tfor i in lst[::-1]:\n\t\tprint(' '.join(list(map(str, i + i[::-1]))))\n\ndef print_mat_odd(n, l):\n\tlst = [[[0] for _ in range(n)]] * n\n\tz = 1\n\tfor i in range(int((n - 1) / 2)):\n\t\tfor j in range(int((n - 1) / 2)):\n\t\t\twhile not z in l or l[z] < 4:\n\t\t\t\tz += 1\n\t\t\tlst[i] = lst[i][:j] + [z] + lst[i][j + 1:]\n\t\t\tlst[n - i - 1] = lst[n - i - 1][:j] + [z] + lst[n - i - 1][j + 1:]\n\t\t\tlst[i] = lst[i][:n - j - 1] + [z] + lst[i][n - j:]\n\t\t\tlst[n - i - 1] = lst[n - i - 1][:n - j - 1] + [z] + lst[n - i - 1][n - j:]\n\t\t\tl[z] -= 4\n\tz = 1\n\tfor i in range(int((n - 1) / 2)):\n\t\twhile not z in l or l[z] < 2:\n\t\t\tz += 1\n\t\tlst[int((n - 1) / 2)] = lst[int((n - 1) / 2)][:i] + [z] + lst[int((n - 1) / 2)][i + 1:]\n\t\tlst[int((n - 1) / 2)] = lst[int((n - 1) / 2)][:n - i - 1] + [z] + lst[int((n - 1) / 2)][n - i:]\n\t\tl[z] -= 2\n\tz = 1\n\tfor i in range(int((n - 1) / 2)):\n\t\twhile not z in l or l[z] < 2:\n\t\t\tz += 1\n\t\tlst[i] = lst[i][:int((n - 1) / 2)] + [z] + lst[i][int((n - 1) / 2) + 1:]\n\t\tlst[n - i - 1] = lst[n - i - 1][:int((n - 1) / 2)] + [z] + lst[n - i - 1][int((n - 1) / 2) + 1:]\n\t\tl[z] -= 2\n\tz = 1\n\tfor i in range(2000):\n\t\tif i in l and l[i] == 1:\n\t\t\tlst[int((n - 1) / 2)] = lst[int((n - 1) / 2)][:int((n - 1) / 2)] + [i] + lst[int((n - 1) / 2)][int((n - 1) / 2) + 1:]\n\t\t\tbreak\n\tfor i in lst:\n\t\tprint(' '.join(list(map(str, i))))\nn = int(input())\na = list(map(int, input().split()))\nl = {}\nfor i in a:\n\tif i in l:\n\t\tl[i] = l[i] + 1\n\telse:\n\t\tl[i] = 1\nc = [0, 0, 0, 0]\nfor i in l:\n\tc[l[i] % 4] += 1\nif n % 2 == 0:\n\tif c[1] == 0 and c[2] == 0 and (c[3] == 0):\n\t\tprint('YES')\n\t\tprint_mat(n, l)\n\telse:\n\t\tprint('NO')\nelif c[1] == 1 and c[3] == 0 and (c[2] + c[3] <= n) or (c[1] == 0 and c[3] == 1 and (c[2] + c[3] <= n)):\n\tprint('YES')\n\tprint_mat_odd(n, l)\nelse:\n\tprint('NO')\n", "from collections import Counter\nn = int(input())\nc = Counter(map(int, input().split()))\nfor (_, i) in c.most_common():\n\tif i % 2 != 0 and n % 2 == 0:\n\t\tprint('NO')\n\t\texit(0)\n\tif i == 2 and n % 2 == 0 and (n > 2):\n\t\tprint('NO')\n\t\texit(0)\nif n == 2:\n\t(e, count) = c.most_common()[0]\n\tif count == 4:\n\t\tprint('YES')\n\t\tprint('{} {}\\n{} {}'.format(e, e, e, e))\n\telif count == 3:\n\t\tprint('NO')\n\telif count == 2:\n\t\t(d, count2) = c.most_common()[1]\n\t\tif count2 == 1:\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tprint('YES')\n\t\t\tprint('{} {}\\n{} {}'.format(e, e, d, d))\n\texit(0)\na = [[0] * n for i in range(n)]\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\t(e, count) = c.most_common()[0]\n\t\tif count < 4 and n > 2:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\t\ta[i][j] = e\n\t\ta[n - 1 - i][j] = e\n\t\ta[i][n - 1 - j] = e\n\t\ta[n - 1 - i][n - j - 1] = e\n\t\tc[e] = count - 4\nif n % 2 != 0:\n\tfor i in range(n // 2):\n\t\t(e, count) = c.most_common()[0]\n\t\tif count <= 1:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\t\ta[n // 2][i] = e\n\t\ta[n // 2][n - 1 - i] = e\n\t\tc[e] = count - 2\n\tfor i in range(n // 2):\n\t\t(e, count) = c.most_common()[0]\n\t\tif count <= 1:\n\t\t\tprint('NO')\n\t\t\texit(0)\n\t\ta[i][n // 2] = e\n\t\ta[n - 1 - i][n // 2] = e\n\t\tc[e] = count - 2\n\tm = c.most_common()[0]\n\tif m[1] <= 0:\n\t\tprint('NO')\n\t\texit(0)\n\ta[n // 2][n // 2] = m[0]\nprint('YES')\nfor i in range(n):\n\tprint(*a[i])\n", "import sys\n\ndef print_mat(m, n):\n\tfor row in m:\n\t\tstr = '%s ' * n\n\t\tprint(str % tuple(row))\n\ndef calculate_quadruplas(l, ret=[]):\n\tif len(l) < 4:\n\t\treturn ret\n\tif l[0] == l[1] == l[2] == l[3]:\n\t\tret.append(l[0])\n\t\treturn calculate_quadruplas(l[4:], ret=ret)\n\telse:\n\t\treturn calculate_quadruplas(l[1:], ret=ret)\n\ndef calcu_tuples(l, ret=[]):\n\tif len(l) < 2:\n\t\treturn ret\n\tif l[0] == l[1]:\n\t\tret.append(l[0])\n\t\treturn calcu_tuples(l[2:], ret=ret)\n\telse:\n\t\treturn calcu_tuples(l[1:], ret=ret)\nn = int(input())\nA = list(map(int, input().split()))\nm = [[0 for i in range(n)] for j in range(n)]\nif n % 2 == 0:\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\ta = A[0]\n\t\t\tm[i][j] = a\n\t\t\ttry:\n\t\t\t\tfor k in range(4):\n\t\t\t\t\tA.remove(a)\n\t\t\texcept ValueError:\n\t\t\t\tprint('NO')\n\t\t\t\tsys.exit(0)\nelse:\n\ttry:\n\t\tA.sort()\n\t\tnmbrs_to_be_used_four_time = calculate_quadruplas(A[:])\n\t\tfor i in nmbrs_to_be_used_four_time:\n\t\t\tfor j in range(4):\n\t\t\t\tA.remove(i)\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\ta = nmbrs_to_be_used_four_time[0]\n\t\t\t\tm[i][j] = a\n\t\t\t\tnmbrs_to_be_used_four_time.remove(a)\n\t\tnmbrs_to_be_used_two_time = calcu_tuples(A[:])\n\t\tfor i in nmbrs_to_be_used_two_time:\n\t\t\tA.remove(i)\n\t\t\tA.remove(i)\n\t\tnmbrs_to_be_used_two_time += nmbrs_to_be_used_four_time * 2\n\t\tfor i in range(n // 2):\n\t\t\tm[i][n // 2] = nmbrs_to_be_used_two_time.pop(0)\n\t\t\tm[n // 2][i] = nmbrs_to_be_used_two_time.pop(0)\n\t\tm[n // 2][n // 2] = A.pop(0)\n\texcept:\n\t\tprint('NO')\n\t\tsys.exit(0)\nfor i in range(0, n // 2):\n\tfor j in range(n // 2, n):\n\t\tjj = n - j - 1\n\t\tm[i][j] = m[i][jj]\nfor i in range(n // 2, n):\n\tfor j in range(n // 2):\n\t\tii = n - i - 1\n\t\tm[i][j] = m[ii][j]\nfor i in range(n // 2, n):\n\tfor j in range(n // 2, n):\n\t\tjj = n - j - 1\n\t\tii = n - i - 1\n\t\tm[i][j] = m[ii][jj]\nprint('YES')\nprint_mat(m, n)\n", "from sys import stdin, stdout\nfrom collections import Counter as co\ninp = lambda : (int(o) for o in stdin.readline().split())\nsinp = lambda : (o for o in stdin.readline().split())\nout = lambda : stdout.write('YES\\n')\nout2 = lambda : stdout.write('NO\\n')\nI = lambda : map(int, input().split())\nY = lambda : int(input())\nx = 100\nfor i in range(100):\n\tx += 1.2\nx = 0\nn = Y()\nd = co(list(I()))\nans = [[0] * n for i in range(n)]\nst = []\nc = 0\nfor i in d.keys():\n\tst.append(i)\n\tif d[i] % 2:\n\t\tc += 1\n\t\tif c == 2 or (c == 1 and n % 2 == 0):\n\t\t\tout2()\n\t\t\texit(0)\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\ttry:\n\t\t\twhile len(st) > 0 and d[st[-1]] == 0:\n\t\t\t\tst.pop()\n\t\t\twhile len(st) > 0 and d[st[-1]] < 4:\n\t\t\t\tst.pop()\n\t\t\tif ans[i][n - j - 1] == 0:\n\t\t\t\tans[i][n - j - 1] = st[-1]\n\t\t\t\td[st[-1]] -= 1\n\t\t\tif ans[n - i - 1][n - j - 1] == 0:\n\t\t\t\tans[n - i - 1][n - j - 1] = st[-1]\n\t\t\t\td[st[-1]] -= 1\n\t\t\tif ans[i][j] == 0:\n\t\t\t\tans[i][j] = st[-1]\n\t\t\t\td[st[-1]] -= 1\n\t\t\tif ans[n - i - 1][j] == 0:\n\t\t\t\tans[n - i - 1][j] = st[-1]\n\t\t\t\td[st[-1]] -= 1\n\t\texcept:\n\t\t\tout2()\n\t\t\texit(0)\nst = list(d.keys())\nfor i in range(n // 2):\n\ttry:\n\t\twhile len(st) > 0 and d[st[-1]] == 0:\n\t\t\tst.pop()\n\t\twhile len(st) > 0 and d[st[-1]] < 2:\n\t\t\tst.pop()\n\t\tif n % 2:\n\t\t\tans[i][n // 2] = st[-1]\n\t\t\tans[n - 1 - i][n // 2] = st[-1]\n\t\t\td[st[-1]] -= 2\n\texcept:\n\t\tout2()\n\t\texit()\nr = list(d.keys())\nfor i in range(n // 2):\n\ttry:\n\t\twhile len(r) > 0 and d[r[-1]] == 0:\n\t\t\tr.pop()\n\t\twhile len(r) > 0 and d[r[-1]] < 2:\n\t\t\tr.pop()\n\t\tif n % 2:\n\t\t\tans[n // 2][i] = r[-1]\n\t\t\tans[n // 2][n - i - 1] = r[-1]\n\t\t\td[r[-1]] -= 2\n\texcept:\n\t\tout2()\n\t\texit(0)\nfor i in d.keys():\n\tif d[i] != 0 and n % 2:\n\t\tans[n // 2][n // 2] = i\n\t\tbreak\nfor i in range(n):\n\tfor j in range(n):\n\t\tif ans[i][j] == 0:\n\t\t\tout2()\n\t\t\texit(0)\nout()\nfor i in range(n):\n\tprint(*ans[i])\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nimport math\nfrom queue import Queue\nimport collections\nimport itertools\nimport bisect\nimport heapq\nimport random\n\ndef main():\n\tpass\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\n\ndef binary(n):\n\treturn bin(n).replace('0b', '')\n\ndef decimal(s):\n\treturn int(s, 2)\n\ndef pow2(n):\n\tp = 0\n\twhile n > 1:\n\t\tn //= 2\n\t\tp += 1\n\treturn p\n\ndef primeFactors(n):\n\tl = []\n\twhile n % 2 == 0:\n\t\tl.append(2)\n\t\tn = n / 2\n\tfor i in range(3, int(math.sqrt(n)) + 1, 2):\n\t\twhile n % i == 0:\n\t\t\tl.append(i)\n\t\t\tn = n / i\n\tif n > 2:\n\t\tl.append(int(n))\n\treturn l\n\ndef isPrime(n):\n\tif n == 1:\n\t\treturn False\n\telse:\n\t\troot = int(n ** 0.5)\n\t\troot += 1\n\t\tfor i in range(2, root):\n\t\t\tif n % i == 0:\n\t\t\t\treturn False\n\t\treturn True\n\ndef maxPrimeFactors(n):\n\tmaxPrime = -1\n\twhile n % 2 == 0:\n\t\tmaxPrime = 2\n\t\tn >>= 1\n\tfor i in range(3, int(math.sqrt(n)) + 1, 2):\n\t\twhile n % i == 0:\n\t\t\tmaxPrime = i\n\t\t\tn = n / i\n\tif n > 2:\n\t\tmaxPrime = n\n\treturn int(maxPrime)\n\ndef countcon(s, i):\n\tc = 0\n\tch = s[i]\n\tfor i in range(i, len(s)):\n\t\tif s[i] == ch:\n\t\t\tc += 1\n\t\telse:\n\t\t\tbreak\n\treturn c\n\ndef lis(arr):\n\tn = len(arr)\n\tlis = [1] * n\n\tfor i in range(1, n):\n\t\tfor j in range(0, i):\n\t\t\tif arr[i] > arr[j] and lis[i] < lis[j] + 1:\n\t\t\t\tlis[i] = lis[j] + 1\n\tmaximum = 0\n\tfor i in range(n):\n\t\tmaximum = max(maximum, lis[i])\n\treturn maximum\n\ndef isSubSequence(str1, str2):\n\tm = len(str1)\n\tn = len(str2)\n\tj = 0\n\ti = 0\n\twhile j < m and i < n:\n\t\tif str1[j] == str2[i]:\n\t\t\tj = j + 1\n\t\ti = i + 1\n\treturn j == m\n\ndef maxfac(n):\n\troot = int(n ** 0.5)\n\tfor i in range(2, root + 1):\n\t\tif n % i == 0:\n\t\t\treturn n // i\n\treturn n\n\ndef p2(n):\n\tc = 0\n\twhile n % 2 == 0:\n\t\tn //= 2\n\t\tc += 1\n\treturn c\n\ndef seive(n):\n\tprimes = [True] * (n + 1)\n\tprimes[1] = primes[0] = False\n\ti = 2\n\twhile i * i <= n:\n\t\tif primes[i] == True:\n\t\t\tfor j in range(i * i, n + 1, i):\n\t\t\t\tprimes[j] = False\n\t\ti += 1\n\tpr = []\n\tfor i in range(0, n + 1):\n\t\tif primes[i]:\n\t\t\tpr.append(i)\n\treturn pr\n\ndef ncr(n, r, p):\n\tnum = den = 1\n\tfor i in range(r):\n\t\tnum = num * (n - i) % p\n\t\tden = den * (i + 1) % p\n\treturn num * pow(den, p - 2, p) % p\n\ndef denofactinverse(n, m):\n\tfac = 1\n\tfor i in range(1, n + 1):\n\t\tfac = fac * i % m\n\treturn pow(fac, m - 2, m)\n\ndef numofact(n, m):\n\tfac = 1\n\tfor i in range(1, n + 1):\n\t\tfac = fac * i % m\n\treturn fac\n\ndef sod(n):\n\ts = 0\n\twhile n > 0:\n\t\ts += n % 10\n\t\tn //= 10\n\treturn s\n\ndef getXor(n):\n\txor = 1\n\tfor i in range(2, n + 1):\n\t\txor = xor ^ i\n\treturn xor\nn = int(input())\nl = list(map(int, input().split()))\ncnt = [0] * (max(l) + 1)\nfor i in l:\n\tcnt[i] += 1\nif n % 2 == 0:\n\tf = True\n\tfor i in cnt:\n\t\tif i % 4:\n\t\t\tf = False\n\t\t\tbreak\n\tif f:\n\t\tprint('YES')\n\t\tele = []\n\t\tfor i in range(0, len(cnt)):\n\t\t\tele += [i] * (cnt[i] // 4)\n\t\tmat = []\n\t\tptr = 0\n\t\tfor i in range(0, n // 2):\n\t\t\tcl = []\n\t\t\tfor j in range(0, n // 2):\n\t\t\t\tcl.append(ele[ptr])\n\t\t\t\tptr += 1\n\t\t\tmat.append(cl)\n\t\tfor i in range(0, n // 2):\n\t\t\tmat[i] += mat[i][::-1]\n\t\tfor i in range(0, n // 2):\n\t\t\tmat.append(mat[n // 2 - 1 - i])\n\t\tfor i in mat:\n\t\t\tprint(*i)\n\telse:\n\t\tprint('NO')\nelse:\n\todd = []\n\tfor i in range(0, len(cnt)):\n\t\tif cnt[i] % 2:\n\t\t\todd.append(i)\n\tif len(odd) != 1:\n\t\tprint('NO')\n\telse:\n\t\tcnt[odd[0]] -= 1\n\t\tele = []\n\t\tmid = []\n\t\tfor i in range(0, len(cnt)):\n\t\t\tele += [i] * (cnt[i] // 4)\n\t\t\tcnt[i] %= 4\n\t\t\tmid += [i] * (cnt[i] // 2)\n\t\tif len(ele) < n // 2 * (n // 2):\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tfor i in range(n // 2 * (n // 2), len(ele)):\n\t\t\t\tmid += [ele[i]] * 2\n\t\t\tele = ele[:n // 2 * (n // 2)]\n\t\t\tmat = [[] for i in range(0, n // 2)]\n\t\t\tptr = 0\n\t\t\tfor i in range(0, n // 2):\n\t\t\t\tfor j in range(0, n // 2):\n\t\t\t\t\tmat[i].append(ele[ptr])\n\t\t\t\t\tptr += 1\n\t\t\tmr = mid[:len(mid) // 2]\n\t\t\tmc = mid[len(mid) // 2:]\n\t\t\tfor i in range(0, n // 2):\n\t\t\t\tmat[i] += mat[i][::-1].copy()\n\t\t\tfor i in range(0, n // 2):\n\t\t\t\tmat.append(mat[n // 2 - 1 - i].copy())\n\t\t\tmat.insert(n // 2, mr + odd + mr[::-1])\n\t\t\tfor i in range(0, n // 2):\n\t\t\t\tcolval = mc[i]\n\t\t\t\tmat[i].insert(n // 2, colval)\n\t\t\t\tmat[n - 1 - i].insert(n // 2, colval)\n\t\t\tprint('YES')\n\t\t\tfor i in mat:\n\t\t\t\tprint(*i)\n", "from collections import Counter\nn = int(input())\na = map(int, input().split())\nc = Counter(a)\nr = []\nfor _ in range(n):\n\tr.append([0] * n)\nflag = True\nj = 0\nh = n // 2\nwhile True:\n\tx = c.most_common(1)[0]\n\tif x[1] == 0:\n\t\tbreak\n\tif j < (n - n % 2) ** 2:\n\t\tif x[1] < 4:\n\t\t\tflag = False\n\t\t\tbreak\n\t\t(b, a) = (int(j // 4 // h), int(j // 4 % h))\n\t\tr[b][a] = x[0]\n\t\tr[n - b - 1][a] = x[0]\n\t\tr[b][n - a - 1] = x[0]\n\t\tr[n - b - 1][n - a - 1] = x[0]\n\t\tj += 4\n\t\tc[x[0]] -= 4\n\telif j < n * n - 1:\n\t\tif x[1] < 2:\n\t\t\tflag = False\n\t\t\tbreak\n\t\tj += 2\n\t\tc[x[0]] -= 2\n\t\tif j < n * n - n + 2:\n\t\t\ta = (j - 2 - (n - 1) ** 2) // 2\n\t\t\tr[h][a] = x[0]\n\t\t\tr[h][n - a - 1] = x[0]\n\t\telse:\n\t\t\ta = (j - 1 - n - (n - 1) ** 2) // 2\n\t\t\tr[a][h] = x[0]\n\t\t\tr[n - a - 1][h] = x[0]\n\telse:\n\t\tr[h][h] = x[0]\n\t\tc[x[0]] -= 1\nif flag:\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*r[i])\nelse:\n\tprint('NO')\n", "N = int(input())\nM = [[0 for _ in range(N)] for _ in range(N)]\nA = {}\nfor a in map(int, input().split(' ')):\n\tif a in A:\n\t\tA[a] += 1\n\telse:\n\t\tA[a] = 1\nC = []\nE = []\nX = []\nfor (k, v) in A.items():\n\twhile v >= 4:\n\t\tC.append(k)\n\t\tv -= 4\n\tif v >= 2:\n\t\tE.append(k)\n\t\tv -= 2\n\tif v:\n\t\tX.append(k)\nif N % 2:\n\tif len(X) != 1:\n\t\tprint('NO')\n\t\texit()\n\twhile C and len(E) < N - 1:\n\t\tx = C.pop()\n\t\tE.append(x)\n\t\tE.append(x)\n\tif len(E) != N - 1:\n\t\tprint('NO')\n\t\texit()\n\tM[N // 2][N // 2] = X.pop()\n\tfor i in range(N // 2):\n\t\tM[N // 2][i] = M[N // 2][-i - 1] = E.pop()\n\t\tM[i][N // 2] = M[-i - 1][N // 2] = E.pop()\n\tfor i in range(N // 2):\n\t\tfor j in range(N // 2):\n\t\t\tM[i][j] = M[-i - 1][j] = M[i][-j - 1] = M[-i - 1][-j - 1] = C.pop()\nelse:\n\tif E or X:\n\t\tprint('NO')\n\t\texit()\n\tfor i in range(N // 2):\n\t\tfor j in range(N // 2):\n\t\t\tM[i][j] = M[-i - 1][j] = M[i][-j - 1] = M[-i - 1][-j - 1] = C.pop()\nprint('YES')\nprint('\\n'.join((' '.join(map(str, l)) for l in M)))\n", "n = int(input())\nar = list(map(int, input().split()))\nkek = [0] * 1001\nfor elem in ar:\n\tkek[elem] += 1\nans = [[0] * n for i in range(n)]\nlol = True\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tflag = True\n\t\tfor z in range(1001):\n\t\t\tif kek[z] >= 4:\n\t\t\t\tans[i][j] = z\n\t\t\t\tans[i][n - j - 1] = z\n\t\t\t\tans[n - i - 1][j] = z\n\t\t\t\tans[n - i - 1][n - j - 1] = z\n\t\t\t\tflag = False\n\t\t\t\tkek[z] -= 4\n\t\t\t\tbreak\n\t\tif flag:\n\t\t\tlol = False\nif n % 2 == 0:\n\tif lol:\n\t\tprint('YES')\n\t\tfor elem in ans:\n\t\t\tprint(*elem)\n\telse:\n\t\tprint('NO')\nelse:\n\tfor j in range(n // 2):\n\t\tflag = True\n\t\tfor z in range(1001):\n\t\t\tif kek[z] >= 2:\n\t\t\t\tans[n // 2][j] = z\n\t\t\t\tans[n // 2][n - j - 1] = z\n\t\t\t\tflag = False\n\t\t\t\tkek[z] -= 2\n\t\t\t\tbreak\n\t\tif flag:\n\t\t\tlol = False\n\tfor i in range(n // 2):\n\t\tflag = True\n\t\tfor z in range(1001):\n\t\t\tif kek[z] >= 2:\n\t\t\t\tans[i][n // 2] = z\n\t\t\t\tans[n - i - 1][n // 2] = z\n\t\t\t\tflag = False\n\t\t\t\tkek[z] -= 2\n\t\t\t\tbreak\n\t\tif flag:\n\t\t\tlol = False\n\tif lol and sum(kek) == 1:\n\t\tans[n // 2][n // 2] = kek.index(1)\n\t\tprint('YES')\n\t\tfor elem in ans:\n\t\t\tprint(*elem)\n\telse:\n\t\tprint('NO')\n", "n = int(input())\ncl = sorted(list(map(int, input().split())))\nk = n ** 2\nif n % 2 == 0:\n\tst = list(set(cl))\n\tdic = {}\n\tfor x in st:\n\t\tdic.update({x: cl.count(x)})\n\tres = [[0 for _ in range(n // 2)] for _ in range(n // 2)]\n\ti = 0\n\tj = 0\n\tt = n // 2\n\tfor x in dic.keys():\n\t\twhile dic[x] >= 4 and i < t:\n\t\t\tres[i][j] = x\n\t\t\tj += 1\n\t\t\tdic[x] -= 4\n\t\t\tif j == t:\n\t\t\t\tj = 0\n\t\t\t\ti += 1\n\tif i < t or j != 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tfor line in res:\n\t\t\ts = ''\n\t\t\tfor c in line:\n\t\t\t\ts += str(c) + ' '\n\t\t\tfor c in line[::-1]:\n\t\t\t\ts += str(c) + ' '\n\t\t\tprint(s + ' ')\n\t\tfor line in res[::-1]:\n\t\t\ts = ''\n\t\t\tfor c in line:\n\t\t\t\ts += str(c) + ' '\n\t\t\tfor c in line[::-1]:\n\t\t\t\ts += str(c) + ' '\n\t\t\tprint(s + ' ')\nelse:\n\tst = list(set(cl))\n\tdic = {}\n\tfor x in st:\n\t\tdic.update({x: cl.count(x)})\n\tres = [[0 for _ in range(n // 2)] for _ in range(n // 2)]\n\ti = 0\n\tj = 0\n\tt = n // 2\n\tfor x in dic.keys():\n\t\tif i == t:\n\t\t\tbreak\n\t\twhile dic[x] >= 4 and i < t:\n\t\t\tres[i][j] = x\n\t\t\tj += 1\n\t\t\tdic[x] -= 4\n\t\t\tif j == t:\n\t\t\t\tj = 0\n\t\t\t\ti += 1\n\tcount1 = 0\n\tcount2 = 0\n\tres1 = []\n\tres2 = []\n\tfor x in dic.keys():\n\t\twhile dic[x] >= 2:\n\t\t\tif count1 < n // 2:\n\t\t\t\tres1.append(x)\n\t\t\t\tdic[x] -= 2\n\t\t\t\tcount1 += 1\n\t\t\telse:\n\t\t\t\tres2.append(x)\n\t\t\t\tdic[x] -= 2\n\t\t\t\tcount2 += 1\n\tif i < t or j != 0 or count1 != n // 2 or (count2 != n // 2):\n\t\tprint('NO')\n\telse:\n\t\tost = 0\n\t\tfor x in dic.keys():\n\t\t\tif dic[x] != 0:\n\t\t\t\tost = x\n\t\t\t\tbreak\n\t\tprint('YES')\n\t\ti = 0\n\t\tfor line in res:\n\t\t\ts = ''\n\t\t\tfor c in line:\n\t\t\t\ts += str(c) + ' '\n\t\t\ts += str(res1[i]) + ' '\n\t\t\ti += 1\n\t\t\tfor c in line[::-1]:\n\t\t\t\ts += str(c) + ' '\n\t\t\tprint(s + ' ')\n\t\tss = ''\n\t\tfor i in range(n // 2):\n\t\t\tss += str(res2[i]) + ' '\n\t\tss += str(ost) + ' '\n\t\tfor i in range(n // 2):\n\t\t\tss += str(res2[::-1][i]) + ' '\n\t\tprint(ss + ' ')\n\t\ti = 0\n\t\tfor line in res[::-1]:\n\t\t\ts = ''\n\t\t\tfor c in line:\n\t\t\t\ts += str(c) + ' '\n\t\t\ts += str(res1[::-1][i]) + ' '\n\t\t\ti += 1\n\t\t\tfor c in line[::-1]:\n\t\t\t\ts += str(c) + ' '\n\t\t\tprint(s + ' ')\n", "n = int(input())\na = []\ncnt_num = dict()\nfor i in map(int, input().split()):\n\tcnt_num[i] = cnt_num.get(i, 0) + 1\n\ta.append(i)\nif n == 1:\n\tprint('YES')\n\tprint(*a)\n\texit(0)\nmatrix = [[0] * n for _ in range(n)]\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tfor (k, am) in cnt_num.items():\n\t\t\tif am >= 4:\n\t\t\t\tmatrix[i][j] = k\n\t\t\t\tmatrix[i][n - j - 1] = k\n\t\t\t\tmatrix[n - i - 1][j] = k\n\t\t\t\tmatrix[n - i - 1][n - j - 1] = k\n\t\t\t\tcnt_num[k] -= 4\n\t\t\t\tbreak\n\t\telse:\n\t\t\tprint('no')\n\t\t\texit(0)\nif n % 2:\n\tfor i in range(n // 2):\n\t\tfor (k, am) in cnt_num.items():\n\t\t\tif am >= 2:\n\t\t\t\tmatrix[i][n // 2] = k\n\t\t\t\tmatrix[n - i - 1][n // 2] = k\n\t\t\t\tcnt_num[k] -= 2\n\t\t\t\tbreak\n\t\telse:\n\t\t\tprint('no')\n\t\t\texit(0)\n\tfor i in range(n // 2):\n\t\tfor (k, am) in cnt_num.items():\n\t\t\tif am >= 2:\n\t\t\t\tmatrix[n // 2][i] = k\n\t\t\t\tmatrix[n // 2][n - i - 1] = k\n\t\t\t\tcnt_num[k] -= 2\n\t\t\t\tbreak\n\t\telse:\n\t\t\tprint('no')\n\t\t\texit(0)\n\tfor (k, am) in cnt_num.items():\n\t\tif am >= 1:\n\t\t\tmatrix[n // 2][n // 2] = k\n\t\t\tbreak\n\telse:\n\t\tprint('no')\n\t\texit(0)\nprint('YES')\nfor i in matrix:\n\tprint(*i)\n", "n = int(input())\na = list(map(int, input().split()))\nd = {}\nfor i in a:\n\td[i] = d.get(i, 0) + 1\nans = [[0] * n for i in range(n)]\nst = []\nfor i in d.keys():\n\tst.append(i)\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\ttry:\n\t\t\twhile len(st) > 0 and d[st[-1]] == 0:\n\t\t\t\tst.pop()\n\t\t\twhile len(st) > 0 and d[st[-1]] < 4:\n\t\t\t\tst.pop()\n\t\t\tif ans[i][j] == 0:\n\t\t\t\tans[i][j] = st[-1]\n\t\t\t\td[st[-1]] -= 1\n\t\t\tif ans[n - i - 1][j] == 0:\n\t\t\t\tans[n - i - 1][j] = st[-1]\n\t\t\t\td[st[-1]] -= 1\n\t\t\tif ans[i][n - j - 1] == 0:\n\t\t\t\tans[i][n - j - 1] = st[-1]\n\t\t\t\td[st[-1]] -= 1\n\t\t\tif ans[n - i - 1][n - j - 1] == 0:\n\t\t\t\tans[n - i - 1][n - j - 1] = st[-1]\n\t\t\t\td[st[-1]] -= 1\n\t\texcept:\n\t\t\texit(print('NO'))\nst = list(d.keys())\nfor i in range(n // 2):\n\ttry:\n\t\twhile len(st) > 0 and d[st[-1]] == 0:\n\t\t\tst.pop()\n\t\twhile len(st) > 0 and d[st[-1]] < 2:\n\t\t\tst.pop()\n\t\tif n % 2:\n\t\t\tans[i][n // 2] = st[-1]\n\t\t\tans[n - 1 - i][n // 2] = st[-1]\n\t\t\td[st[-1]] -= 2\n\texcept:\n\t\texit(print('NO'))\nr = list(d.keys())\nfor i in range(n // 2):\n\ttry:\n\t\twhile len(r) > 0 and d[r[-1]] == 0:\n\t\t\tr.pop()\n\t\twhile len(r) > 0 and d[r[-1]] < 2:\n\t\t\tr.pop()\n\t\tif n % 2:\n\t\t\tans[n // 2][i] = r[-1]\n\t\t\tans[n // 2][n - i - 1] = r[-1]\n\t\t\td[r[-1]] -= 2\n\texcept:\n\t\texit(print('NO'))\nfor i in d.keys():\n\tif d[i] != 0 and n % 2:\n\t\tans[n // 2][n // 2] = i\n\t\tbreak\nfor i in range(n):\n\tfor j in range(n):\n\t\tif ans[i][j] == 0:\n\t\t\texit(print('NO'))\nprint('YES')\nfor i in range(n):\n\tprint(*ans[i])\n", "_ = input()\nn = int(_)\n_ = input()\na = _.split()\na = [int(_) for _ in a]\na_dict = {}\nfor i in a:\n\tif i in a_dict:\n\t\ta_dict[i] = a_dict[i] + 1\n\telse:\n\t\ta_dict[i] = 1\nif n % 2 == 0:\n\tanswer = [[0] * n for _ in range(n)]\n\tfor i in a_dict.values():\n\t\tif i % 4 != 0:\n\t\t\tprint('NO')\n\t\t\tbreak\n\telse:\n\t\tprint('YES')\n\t\t(a, b) = (0, 0)\n\t\tfor (i, value) in a_dict.items():\n\t\t\tfor j in range(0, value // 4):\n\t\t\t\tif b >= n // 2:\n\t\t\t\t\ta = a + 1\n\t\t\t\t\tb = 0\n\t\t\t\tanswer[a][b] = str(i)\n\t\t\t\tanswer[n - 1 - a][b] = str(i)\n\t\t\t\tanswer[a][n - 1 - b] = str(i)\n\t\t\t\tanswer[n - 1 - a][n - 1 - b] = str(i)\n\t\t\t\tb = b + 1\n\t\tfor k in answer:\n\t\t\tprint(' '.join(k))\nelse:\n\tanswer = [[0] * n for _ in range(n)]\n\tone = 0\n\tfour = 0\n\tfor i in a_dict.values():\n\t\tif i % 2 == 1:\n\t\t\tone = one + 1\n\t\t\tif i > 4:\n\t\t\t\tfour = four + i // 4\n\t\telse:\n\t\t\tfour = four + i // 4\n\tif one != 1 or four < n // 2 * (n // 2):\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\t(a, b) = (0, 0)\n\t\t(c, d) = (0, 0)\n\t\tfor (i, value) in a_dict.items():\n\t\t\tif value % 2 == 1:\n\t\t\t\tanswer[n // 2][n // 2] = str(i)\n\t\t\t\tvalue = value - 1\n\t\t\t\tif value >= 4:\n\t\t\t\t\tk = 0\n\t\t\t\t\tfor j in range(0, value // 4):\n\t\t\t\t\t\tif a < n // 2 and b <= n // 2:\n\t\t\t\t\t\t\tif b >= n // 2:\n\t\t\t\t\t\t\t\ta = a + 1\n\t\t\t\t\t\t\t\tb = 0\n\t\t\t\t\t\t\tif a >= n // 2:\n\t\t\t\t\t\t\t\tcontinue\n\t\t\t\t\t\t\tanswer[a][b] = str(i)\n\t\t\t\t\t\t\tanswer[n - 1 - a][b] = str(i)\n\t\t\t\t\t\t\tanswer[a][n - 1 - b] = str(i)\n\t\t\t\t\t\t\tanswer[n - 1 - a][n - 1 - b] = str(i)\n\t\t\t\t\t\t\tb = b + 1\n\t\t\t\t\t\t\tk = k + 4\n\t\t\t\t\tvalue = value - k\n\t\t\t\tif value >= 2:\n\t\t\t\t\tfor j in range(0, value // 2):\n\t\t\t\t\t\tif c < n // 2:\n\t\t\t\t\t\t\tanswer[n // 2][c] = str(i)\n\t\t\t\t\t\t\tanswer[n // 2][n - 1 - c] = str(i)\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tanswer[c + 1][n // 2] = str(i)\n\t\t\t\t\t\t\tanswer[n - 1 - c - 1][n // 2] = str(i)\n\t\t\t\t\t\tc = c + 1\n\t\t\telse:\n\t\t\t\tif value >= 4:\n\t\t\t\t\tk = 0\n\t\t\t\t\tfor j in range(0, value // 4):\n\t\t\t\t\t\tif a < n // 2 and b <= n // 2:\n\t\t\t\t\t\t\tif b >= n // 2:\n\t\t\t\t\t\t\t\ta = a + 1\n\t\t\t\t\t\t\t\tb = 0\n\t\t\t\t\t\t\tif a >= n // 2:\n\t\t\t\t\t\t\t\tcontinue\n\t\t\t\t\t\t\tanswer[a][b] = str(i)\n\t\t\t\t\t\t\tanswer[n - 1 - a][b] = str(i)\n\t\t\t\t\t\t\tanswer[a][n - 1 - b] = str(i)\n\t\t\t\t\t\t\tanswer[n - 1 - a][n - 1 - b] = str(i)\n\t\t\t\t\t\t\tb = b + 1\n\t\t\t\t\t\t\tk = k + 4\n\t\t\t\t\tvalue = value - k\n\t\t\t\tif value >= 2:\n\t\t\t\t\tfor j in range(0, value // 2):\n\t\t\t\t\t\tif c < n // 2:\n\t\t\t\t\t\t\tanswer[n // 2][c] = str(i)\n\t\t\t\t\t\t\tanswer[n // 2][n - 1 - c] = str(i)\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tanswer[c + 1][n // 2] = str(i)\n\t\t\t\t\t\t\tanswer[n - 1 - c - 1][n // 2] = str(i)\n\t\t\t\t\t\tc = c + 1\n\t\tfor k in answer:\n\t\t\tprint(' '.join(k))\n", "n = int(input())\nK = list(map(int, input().split()))\nA = {}\nfor i in range(len(K)):\n\tA[K[i]] = A.get(K[i], 0) + 1\nB = [[0 for _ in range(n)] for __ in range(n)]\ni1 = 0\nj1 = 0\ni2 = 0\nk = 1\nfor j in A:\n\tif n % 2 == 0:\n\t\twhile A[j] - 4 >= 0:\n\t\t\tB[i1][j1] = j\n\t\t\tB[i1][n - j1 - 1] = j\n\t\t\tB[n - i1 - 1][j1] = j\n\t\t\tB[n - i1 - 1][n - j1 - 1] = j\n\t\t\tA[j] -= 4\n\t\t\ti1 += 1\n\t\t\t(i1, j1) = (i1 % (n // 2), j1 + i1 // (n // 2))\n\telse:\n\t\twhile A[j] - 4 >= 0 and i1 < n // 2 and (j1 < n // 2):\n\t\t\tB[i1][j1] = j\n\t\t\tB[i1][n - j1 - 1] = j\n\t\t\tB[n - i1 - 1][j1] = j\n\t\t\tB[n - i1 - 1][n - j1 - 1] = j\n\t\t\tA[j] -= 4\n\t\t\ti1 += 1\n\t\t\t(i1, j1) = (i1 % (n // 2), j1 + i1 // (n // 2))\n\t\twhile A[j] >= 2 and k < 3:\n\t\t\tif k == 1:\n\t\t\t\tB[i2][n // 2] = j\n\t\t\t\tB[n - i2 - 1][n // 2] = j\n\t\t\t\tA[j] -= 2\n\t\t\t\ti2 += 1\n\t\t\t\tif i2 == n // 2:\n\t\t\t\t\tk = 2\n\t\t\t\t\ti2 = 0\n\t\t\telif k == 2:\n\t\t\t\tB[n // 2][i2] = j\n\t\t\t\tB[n // 2][n - i2 - 1] = j\n\t\t\t\tA[j] -= 2\n\t\t\t\ti2 += 1\n\t\t\t\tif i2 == n // 2:\n\t\t\t\t\tk = 3\n\t\t\t\t\ti2 = 0\n\t\tif A[j] == 1 and B[n // 2][n // 2] == 0:\n\t\t\tB[n // 2][n // 2] = j\n\t\t\tA[j] -= 1\n\tif A[j] > 0:\n\t\tprint('NO')\n\t\texit(0)\nprint('YES')\nfor i in B:\n\tprint(*i)\n", "n = int(input())\narr = list(map(int, input().strip().split()))\nmp = {}\nfor i in range(n * n):\n\tif arr[i] in mp:\n\t\tmp[arr[i]] += 1\n\telse:\n\t\tmp[arr[i]] = 1\nans = [[0] * n for i in range(n)]\nif n % 2 == 0:\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tx = 0\n\t\t\tfor y in mp:\n\t\t\t\tif mp[y] >= 4:\n\t\t\t\t\tmp[y] -= 4\n\t\t\t\t\tx = y\n\t\t\t\t\tbreak\n\t\t\tans[i][j] = x\n\t\t\tans[n - i - 1][j] = x\n\t\t\tans[i][n - j - 1] = x\n\t\t\tans[n - 1 - i][n - 1 - j] = x\nelse:\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tx = 0\n\t\t\tfor y in mp:\n\t\t\t\tif mp[y] >= 4:\n\t\t\t\t\tmp[y] -= 4\n\t\t\t\t\tx = y\n\t\t\t\t\tbreak\n\t\t\tans[i][j] = x\n\t\t\tans[n - i - 1][j] = x\n\t\t\tans[i][n - j - 1] = x\n\t\t\tans[n - 1 - i][n - 1 - j] = x\n\tfor i in range(n // 2):\n\t\tx = 0\n\t\tfor y in mp:\n\t\t\tif mp[y] >= 2:\n\t\t\t\tmp[y] -= 2\n\t\t\t\tx = y\n\t\t\t\tbreak\n\t\tans[n // 2][i] = x\n\t\tans[n // 2][n - i - 1] = x\n\tfor i in range(n // 2):\n\t\tx = 0\n\t\tfor y in mp:\n\t\t\tif mp[y] >= 2:\n\t\t\t\tmp[y] -= 2\n\t\t\t\tx = y\n\t\t\t\tbreak\n\t\tans[i][n // 2] = x\n\t\tans[n - i - 1][n // 2] = x\n\tx = 0\n\tfor y in mp:\n\t\tif mp[y] % 2 == 1:\n\t\t\tmp[y] -= 1\n\t\t\tx = y\n\t\t\tbreak\n\tans[n // 2][n // 2] = x\nzs = 0\nfor i in range(n):\n\tfor j in range(n):\n\t\tif ans[i][j] == 0:\n\t\t\tzs = 1\nif zs == 0:\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*ans[i])\nelse:\n\tprint('NO')\n", "def fastio():\n\timport sys\n\tfrom io import StringIO\n\tfrom atexit import register\n\tglobal input\n\tsys.stdin = StringIO(sys.stdin.read())\n\tinput = lambda : sys.stdin.readline().rstrip('\\r\\n')\n\tsys.stdout = StringIO()\n\tregister(lambda : sys.__stdout__.write(sys.stdout.getvalue()))\nfastio()\nMOD = 10 ** 9 + 7\nI = lambda : list(map(int, input().split()))\nfrom collections import Counter as C\n(n,) = I()\nl = I()\nl.sort()\nc = C(l)\nif n == 1:\n\tprint('YES')\n\tprint(l[0])\n\texit()\nif n % 2 == 0:\n\tfor i in c:\n\t\tif c[i] % 4:\n\t\t\tprint('NO')\n\t\t\texit()\n\tprint('YES')\n\tans = []\n\ti = 0\n\twhile i < n ** 2:\n\t\tans.append(l[i])\n\t\ti += 4\n\tres = []\n\tfor i in range(n // 2):\n\t\tres.append(ans[i * n // 2:i * n // 2 + n // 2])\n\tfor i in res:\n\t\tprint(*i, *reversed(i))\n\tfor i in reversed(res):\n\t\tprint(*i, *reversed(i))\nif n % 2:\n\ttemp = l[:]\n\tl = []\n\tk = []\n\ts = []\n\tn -= 1\n\tfor i in c:\n\t\twhile c[i] >= 4 and len(l) < n ** 2:\n\t\t\tfor x in range(4):\n\t\t\t\tl.append(i)\n\t\t\tc[i] -= 4\n\t\twhile c[i] >= 2:\n\t\t\tfor x in range(2):\n\t\t\t\ts.append(i)\n\t\t\tc[i] -= 2\n\t\twhile c[i] >= 1:\n\t\t\tk.append(i)\n\t\t\tc[i] = 0\n\tif len(k) != 1 or len(s) > 2 * n or len(l) < n ** 2:\n\t\tprint('NO')\n\t\texit()\n\tprint('YES')\n\tl.sort()\n\tans = []\n\ti = 0\n\twhile i < n ** 2:\n\t\tans.append(l[i])\n\t\ti += 4\n\tres = []\n\taiyo = []\n\ti = 0\n\twhile i < len(s):\n\t\taiyo.append(s[i])\n\t\ti += 2\n\tfor i in range(n // 2):\n\t\tres.append(ans[i * n // 2:i * n // 2 + n // 2])\n\tj = 0\n\tzz = []\n\tel = aiyo[j]\n\tfor i in res:\n\t\tprint(*i, el, *reversed(i))\n\t\tzz.append(el)\n\t\tj += 1\n\t\tel = aiyo[j]\n\tkkk = aiyo[j:]\n\tprint(*kkk, k[0], *reversed(kkk))\n\tj = 0\n\tzz = list(reversed(zz))\n\tel = zz[j]\n\tfor i in reversed(res):\n\t\tprint(*i, el, *reversed(i))\n\t\tj += 1\n\t\tif j < len(zz):\n\t\t\tel = zz[j]\n", "def p(l, n):\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(l[i][j], end=' ')\n\t\tprint('')\nn = int(input())\nl1 = list(map(int, input().split()))\nstr1 = 'YES'\nl2 = [[0 * i for i in range(1, n + 1)] for i in range(1, n + 1)]\nd = {}\nfor i in range(n ** 2):\n\tif l1[i] not in d:\n\t\td[l1[i]] = 1\n\telse:\n\t\td[l1[i]] += 1\nlis = [[x, y] for (x, y) in d.items()]\nlis.sort(key=lambda x: x[1], reverse=True)\nfor i in range(0, n // 2):\n\tfor j in range(0, n // 2):\n\t\tif lis[0][1] >= 4:\n\t\t\tl2[i][j] = lis[0][0]\n\t\t\tl2[n - i - 1][j] = lis[0][0]\n\t\t\tl2[i][n - j - 1] = lis[0][0]\n\t\t\tl2[n - i - 1][n - j - 1] = lis[0][0]\n\t\t\tlis[0][1] -= 4\n\t\t\tif lis[0][1] < 4:\n\t\t\t\tlis.sort(key=lambda x: x[1], reverse=True)\n\t\telse:\n\t\t\tstr1 = 'NO'\n\t\t\tbreak\nif str1 == 'YES' and n % 2 != 0:\n\tlis.sort(key=lambda x: x[1], reverse=True)\n\tfor i in range(n // 2):\n\t\tif lis[0][1] >= 2:\n\t\t\tl2[i][n // 2] = lis[0][0]\n\t\t\tl2[n - i - 1][n // 2] = lis[0][0]\n\t\t\tlis[0][1] -= 2\n\t\t\tif lis[0][1] < 2:\n\t\t\t\tlis.sort(key=lambda x: x[1], reverse=True)\n\t\telse:\n\t\t\tstr1 = 'NO'\n\t\t\tbreak\n\tfor i in range(n // 2):\n\t\tif lis[0][1] >= 2:\n\t\t\tl2[n // 2][i] = lis[0][0]\n\t\t\tl2[n // 2][n - i - 1] = lis[0][0]\n\t\t\tlis[0][1] -= 2\n\t\t\tif lis[0][1] < 2:\n\t\t\t\tlis.sort(key=lambda x: x[1], reverse=True)\n\t\telse:\n\t\t\tstr1 = 'NO'\n\t\t\tbreak\nif str1 == 'YES' and n % 2 != 0:\n\tlis.sort(key=lambda x: x[1], reverse=True)\n\tif lis[0][1] >= 1:\n\t\tl2[n // 2][n // 2] = lis[0][0]\nprint(str1)\nif str1 == 'YES':\n\tp(l2, n)\n", "__author__ = 'Esfandiar'\nimport sys\nfrom collections import defaultdict\ninput = sys.stdin.readline\nn = int(input())\na = list(map(int, input().split()))\nd = defaultdict(int)\nfor i in range(n * n):\n\td[a[i]] += 1\nres = [[0 for i in range(n)] for _ in range(n)]\nif n % 2 == 0:\n\tke = list(d.keys())\n\tind = 0\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tv = ke[ind]\n\t\t\tif d[v] % 4 != 0:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\t\telse:\n\t\t\t\tres[i][j] = v\n\t\t\t\tres[i][n - j - 1] = v\n\t\t\t\tres[n - i - 1][j] = v\n\t\t\t\tres[n - i - 1][n - j - 1] = v\n\t\t\td[v] -= 4\n\t\t\tif d[v] == 0:\n\t\t\t\tind += 1\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*res[i])\nelse:\n\n\tdef check(i, j, n, d, res):\n\t\tfor v in d.keys():\n\t\t\tif d[v] >= 4:\n\t\t\t\tres[i][j] = v\n\t\t\t\tres[i][n - j - 1] = v\n\t\t\t\tres[n - i - 1][j] = v\n\t\t\t\tres[n - i - 1][n - j - 1] = v\n\t\t\t\td[v] -= 4\n\t\t\t\treturn 1\n\t\treturn 0\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tif check(i, j, n, d, res) == 0:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\tf = 0\n\tkeys = -1\n\tfor v in d.keys():\n\t\tif d[v] % 2 == 1:\n\t\t\tf += 1\n\t\t\tkeys = v\n\tif f != 1:\n\t\tprint('NO')\n\t\texit()\n\tklid = list(d.keys())\n\tcou = 0\n\tyy = n // 2\n\ti = 0\n\twhile i < n // 2 and cou < len(klid):\n\t\tif klid[cou] != keys and d[klid[cou]] != 0:\n\t\t\tres[yy][i] = klid[cou]\n\t\t\tres[yy][n - i - 1] = klid[cou]\n\t\t\td[klid[cou]] -= 2\n\t\t\tif d[klid[cou]] == 0:\n\t\t\t\tcou += 1\n\t\t\ti += 1\n\t\telse:\n\t\t\tcou += 1\n\tcou = 0\n\ti = 0\n\twhile i < n // 2 and cou < len(klid):\n\t\tif klid[cou] != keys and d[klid[cou]] != 0:\n\t\t\tres[i][yy] = klid[cou]\n\t\t\tres[n - i - 1][yy] = klid[cou]\n\t\t\td[klid[cou]] -= 2\n\t\t\tif d[klid[cou]] == 0:\n\t\t\t\tcou += 1\n\t\t\ti += 1\n\t\telse:\n\t\t\tcou += 1\n\tres[yy][yy] = keys\n\td[keys] -= 1\n\tfor i in range(n // 2):\n\t\tif res[i][yy] == 0:\n\t\t\tres[i][yy] = keys\n\t\t\tres[n - i - 1][yy] = keys\n\tfor i in range(n // 2):\n\t\tif res[yy][i] == 0:\n\t\t\tres[yy][i] = keys\n\t\t\tres[yy][n - i - 1] = keys\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*res[i])\n", "inp_int = int(input())\ninp = list(map(int, input().split()))\ninp.sort()\nindices = []\nmatrix = []\nfor i in range(inp_int):\n\ttemp = []\n\tfor j in range(inp_int):\n\t\ttemp.append(0)\n\tmatrix.append(temp)\nfor i in range(1001):\n\tindices.append(0)\nflag = 0\nfor i in range(len(inp)):\n\tindices[inp[i]] += 1\nif inp_int % 2 == 0:\n\ttemp = []\n\tfor i in range(1, 1001):\n\t\tif indices[i] % 4 != 0:\n\t\t\tflag = 1\n\t\t\tbreak\n\t\telif indices[i] > 0:\n\t\t\tfor j in range(indices[i] // 4):\n\t\t\t\ttemp.append(i)\n\tif flag == 1:\n\t\tprint('NO')\n\telse:\n\t\ttemp_index = 0\n\t\tfor i in range(inp_int // 2):\n\t\t\tfor j in range(inp_int // 2):\n\t\t\t\tmatrix[i][j] = temp[temp_index]\n\t\t\t\tmatrix[inp_int - 1 - i][j] = temp[temp_index]\n\t\t\t\tmatrix[i][inp_int - 1 - j] = temp[temp_index]\n\t\t\t\tmatrix[inp_int - 1 - i][inp_int - 1 - j] = temp[temp_index]\n\t\t\t\ttemp_index += 1\n\t\tprint('YES')\n\t\tfor i in range(inp_int):\n\t\t\tfor j in range(inp_int):\n\t\t\t\tprint(matrix[i][j], end=' ')\n\t\t\tprint()\nelse:\n\ttemp = []\n\ttemp2 = []\n\tspecial = 0\n\tfor i in range(1, 1001):\n\t\tif indices[i] % 4 != 0:\n\t\t\tif indices[i] % 4 == 1 and flag == 0:\n\t\t\t\tfor j in range(indices[i] // 4):\n\t\t\t\t\ttemp.append(i)\n\t\t\t\tflag = 1\n\t\t\t\tspecial = i\n\t\t\telif indices[i] % 4 == 3 and flag == 0:\n\t\t\t\tfor j in range(indices[i] // 4):\n\t\t\t\t\ttemp.append(i)\n\t\t\t\ttemp2.append(i)\n\t\t\t\tflag = 1\n\t\t\t\tspecial = i\n\t\t\telif indices[i] % 4 == 2:\n\t\t\t\tfor j in range(indices[i] // 4):\n\t\t\t\t\ttemp.append(i)\n\t\t\t\ttemp2.append(i)\n\t\t\telse:\n\t\t\t\tflag = 2\n\t\t\t\tbreak\n\t\telif indices[i] > 0:\n\t\t\tfor j in range(indices[i] // 4):\n\t\t\t\ttemp.append(i)\n\tif flag != 1 or len(temp) < ((inp_int - 1) // 2) ** 2:\n\t\tprint('NO')\n\telse:\n\t\ttemp_index = 0\n\t\tfor i in range(inp_int // 2):\n\t\t\tfor j in range(inp_int // 2):\n\t\t\t\tmatrix[i][j] = temp[temp_index]\n\t\t\t\tmatrix[inp_int - 1 - i][j] = temp[temp_index]\n\t\t\t\tmatrix[i][inp_int - 1 - j] = temp[temp_index]\n\t\t\t\tmatrix[inp_int - 1 - i][inp_int - 1 - j] = temp[temp_index]\n\t\t\t\ttemp_index += 1\n\t\ttemp2_index = 0\n\t\tfor i in range(inp_int // 2):\n\t\t\tif temp2_index >= len(temp2):\n\t\t\t\tmatrix[i][inp_int // 2] = temp[temp_index]\n\t\t\t\tmatrix[inp_int - i - 1][inp_int // 2] = temp[temp_index]\n\t\t\t\tmatrix[inp_int // 2][i] = temp[temp_index]\n\t\t\t\tmatrix[inp_int // 2][inp_int - i - 1] = temp[temp_index]\n\t\t\t\ttemp_index += 1\n\t\t\telse:\n\t\t\t\tmatrix[i][inp_int // 2] = temp2[temp2_index]\n\t\t\t\tmatrix[inp_int - i - 1][inp_int // 2] = temp2[temp2_index]\n\t\t\t\ttemp2_index += 1\n\t\t\t\tmatrix[inp_int // 2][i] = temp2[temp2_index]\n\t\t\t\tmatrix[inp_int // 2][inp_int - i - 1] = temp2[temp2_index]\n\t\t\t\ttemp2_index += 1\n\t\tmatrix[inp_int // 2][inp_int // 2] = special\n\t\tprint('YES')\n\t\tfor i in range(inp_int):\n\t\t\tfor j in range(inp_int):\n\t\t\t\tprint(matrix[i][j], end=' ')\n\t\t\tprint()\n", "from collections import Counter\nn = int(input())\nA = Counter(map(int, input().split()))\n\ndef reflections(i, j, n):\n\treturn [(i, j), (n - 1 - i, j), (i, n - 1 - j), (n - 1 - i, n - 1 - j)]\npossible = True\nodd_val = -1\npairs = []\nif n % 2 == 1:\n\tfor (k, v) in A.items():\n\t\tif v % 2 != 0:\n\t\t\tif odd_val == -1 and v % 2 != 0:\n\t\t\t\todd_val = k\n\t\t\t\tA[k] = v - 1\n\t\t\t\tv -= 1\n\t\t\telse:\n\t\t\t\tpossible = False\n\t\tif v % 4 == 2:\n\t\t\tpairs += [k]\n\t\t\tA[k] = v - 2\n\t\t\tv -= 2\n\tif len(pairs) > n - 1:\n\t\tpossible = False\nelse:\n\tfor (k, v) in A.items():\n\t\tif v % 4 != 0:\n\t\t\tpossible = False\nif possible:\n\tprint('YES')\n\tR = [[0] * n for _ in range(n)]\n\tk = 0\n\tvals = list(A.items())\n\tif odd_val != -1:\n\t\twhile len(pairs) != n - 1:\n\t\t\twhile vals[k][1] == 0:\n\t\t\t\tk += 1\n\t\t\tpairs += [vals[k][0]] * 2\n\t\t\tvals[k] = (vals[k][0], vals[k][1] - 4)\n\t\tR[n // 2][n // 2] = odd_val\n\t\tp = 0\n\t\tfor i in range(n // 2):\n\t\t\tR[i][n // 2] = pairs[p]\n\t\t\tR[n - 1 - i][n // 2] = pairs[p]\n\t\t\tp += 1\n\t\tfor i in range(n // 2):\n\t\t\tR[n // 2][i] = pairs[p]\n\t\t\tR[n // 2][n - 1 - i] = pairs[p]\n\t\t\tp += 1\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\twhile vals[k][1] == 0:\n\t\t\t\tk += 1\n\t\t\tfor (x, y) in reflections(i, j, n):\n\t\t\t\tR[x][y] = vals[k][0]\n\t\t\tvals[k] = (vals[k][0], vals[k][1] - 4)\n\tfor row in R:\n\t\tprint(' '.join(map(str, row)))\nelse:\n\tprint('NO')\n", "from collections import Counter\n\ndef find_num(counter, mod_num):\n\tfor (k, v) in counter.items():\n\t\tif v >= mod_num:\n\t\t\treturn (True, k)\n\treturn (False, -1)\n\ndef main():\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tcounter = Counter(a)\n\tgrid = [[0] * n for _ in range(n)]\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\t(flag, val) = find_num(counter, 4)\n\t\t\tif not flag:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\t\tcounter[val] -= 4\n\t\t\tgrid[i][j] = grid[n - i - 1][j] = grid[i][n - j - 1] = grid[n - i - 1][n - j - 1] = val\n\tif n % 2 == 1:\n\t\tmid = n // 2\n\t\tfor i in range(n // 2):\n\t\t\t(flag, val) = find_num(counter, 2)\n\t\t\tif not flag:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\t\tcounter[val] -= 2\n\t\t\tgrid[mid][i] = grid[mid][n - i - 1] = val\n\t\tfor i in range(n // 2):\n\t\t\t(flag, val) = find_num(counter, 2)\n\t\t\tif not flag:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\t\tcounter[val] -= 2\n\t\t\tgrid[i][mid] = grid[n - i - 1][mid] = val\n\t\t(_, val) = find_num(counter, 1)\n\t\tgrid[mid][mid] = val\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(grid[i][j], end=' ')\n\t\tprint()\nmain()\n", "from itertools import groupby as gb\nn = int(input())\na = list(map(int, input().split()))\na = sorted(a)\ng = gb(a)\npr = (n // 2) ** 2\n(one, two, four) = ([], [], [])\nfor (k, v) in g:\n\tln = len(list(v))\n\tif ln % 2 == 1:\n\t\tif n % 2 == 0 or len(one) != 0:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\tone.append(k)\n\t\tln -= 1\n\tif ln % 4 == 2:\n\t\ttwo.append(k)\n\t\tln -= 2\n\twhile len(four) < pr and ln > 0:\n\t\tfour.append(k)\n\t\tln -= 4\n\twhile ln > 0:\n\t\ttwo.append(k)\n\t\tln -= 2\nres = [[0 for _ in range(n)] for _ in range(n)]\n(_1, _2, _4) = (len(one), len(two), len(four))\nif n % 2 == 1:\n\tif _1 != 1:\n\t\tprint('NO')\n\t\texit()\n\tres[n // 2][n // 2] = one[0]\n\tpiv = 0\n\tfor i in range(n // 2):\n\t\tif piv >= _2:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\tres[n // 2][i] = res[n // 2][n - 1 - i] = two[piv]\n\t\tpiv += 1\n\tfor i in range(n // 2):\n\t\tif piv >= _2:\n\t\t\tprint('NO')\n\t\t\texit()\n\t\tres[i][n // 2] = res[n - 1 - i][n // 2] = two[piv]\n\t\tpiv += 1\n\tpiv = 0\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tif piv >= _4:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\t\tres[i][j] = res[n - 1 - i][j] = res[i][n - 1 - j] = res[n - 1 - i][n - 1 - j] = four[piv]\n\t\t\tpiv += 1\nelse:\n\tpiv = 0\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tif piv >= _4:\n\t\t\t\tprint('NO')\n\t\t\t\texit()\n\t\t\tres[i][j] = res[n - 1 - i][j] = res[i][n - 1 - j] = res[n - 1 - i][n - 1 - j] = four[piv]\n\t\t\tpiv += 1\nprint('YES')\nfor l in res:\n\tprint(*l)\n", "n = int(input())\nl = list(map(int, input().split()))\nif n == 1:\n\tprint('YES')\n\tprint(l[0])\nelse:\n\td = {}\n\tfor i in l:\n\t\ttry:\n\t\t\td[i] += 1\n\t\texcept KeyError:\n\t\t\td[i] = 1\n\tif n % 2 != 0:\n\t\to = 0\n\t\tt = 0\n\t\tfor i in d.values():\n\t\t\tif i % 2 != 0:\n\t\t\t\to += 1\n\t\t\t\tif (i - 1) % 4 == 2:\n\t\t\t\t\tt += 1\n\t\t\telif i % 4 == 2:\n\t\t\t\tt += 1\n\t\tif o > 1:\n\t\t\tprint('NO')\n\t\telif t > n - 1:\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tprint('YES')\n\t\t\tl1 = []\n\t\t\tfor i in range(n):\n\t\t\t\tl2 = []\n\t\t\t\tfor j in range(n):\n\t\t\t\t\tl2.append(0)\n\t\t\t\tl1.append(l2)\n\t\t\tfor i in range(n // 2 + 1):\n\t\t\t\tfor j in range(n // 2 + 1):\n\t\t\t\t\tif i != n // 2:\n\t\t\t\t\t\tif j != n // 2:\n\t\t\t\t\t\t\ta = 0\n\t\t\t\t\t\t\tfor k in d.keys():\n\t\t\t\t\t\t\t\tif d[k] != 0:\n\t\t\t\t\t\t\t\t\tif d[k] // 4 >= 1:\n\t\t\t\t\t\t\t\t\t\ta = k\n\t\t\t\t\t\t\t\t\t\td[k] -= 4\n\t\t\t\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\t\tl1[i][j] = a\n\t\t\t\t\t\t\tl1[n - i - 1][j] = a\n\t\t\t\t\t\t\tl1[n - i - 1][n - j - 1] = a\n\t\t\t\t\t\t\tl1[i][n - j - 1] = a\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\ta = 0\n\t\t\t\t\t\t\tfor k in d.keys():\n\t\t\t\t\t\t\t\tif d[k] != 0:\n\t\t\t\t\t\t\t\t\tif d[k] % 4 == 2 or (d[k] - 1) % 4 == 2:\n\t\t\t\t\t\t\t\t\t\ta = k\n\t\t\t\t\t\t\t\t\t\td[k] -= 2\n\t\t\t\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\t\tif a == 0:\n\t\t\t\t\t\t\t\tfor k in d.keys():\n\t\t\t\t\t\t\t\t\tif d[k] != 0 and d[k] - 1 != 0:\n\t\t\t\t\t\t\t\t\t\tif d[k] % 4 == 0 or (d[k] - 1) % 4 == 0:\n\t\t\t\t\t\t\t\t\t\t\ta = k\n\t\t\t\t\t\t\t\t\t\t\td[k] -= 2\n\t\t\t\t\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\t\tl1[i][j] = a\n\t\t\t\t\t\t\tl1[n - i - 1][j] = a\n\t\t\t\t\telif j != n // 2:\n\t\t\t\t\t\ta = 0\n\t\t\t\t\t\tfor k in d.keys():\n\t\t\t\t\t\t\tif d[k] != 0:\n\t\t\t\t\t\t\t\tif d[k] % 4 == 2 or (d[k] - 1) % 4 == 2:\n\t\t\t\t\t\t\t\t\ta = k\n\t\t\t\t\t\t\t\t\td[k] -= 2\n\t\t\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tif a == 0:\n\t\t\t\t\t\t\tfor k in d.keys():\n\t\t\t\t\t\t\t\tif d[k] != 0 and d[k] - 1 != 0:\n\t\t\t\t\t\t\t\t\tif d[k] % 4 == 0 or (d[k] - 1) % 4 == 0:\n\t\t\t\t\t\t\t\t\t\ta = k\n\t\t\t\t\t\t\t\t\t\td[k] -= 2\n\t\t\t\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tl1[i][j] = a\n\t\t\t\t\t\tl1[i][n - j - 1] = a\n\t\t\t\t\telse:\n\t\t\t\t\t\ta = 0\n\t\t\t\t\t\tfor k in d.keys():\n\t\t\t\t\t\t\tif d[k] != 0:\n\t\t\t\t\t\t\t\tif d[k] % 2 != 0:\n\t\t\t\t\t\t\t\t\ta = k\n\t\t\t\t\t\t\t\t\td[k] -= 1\n\t\t\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tl1[i][j] = a\n\t\t\tfor i in l1:\n\t\t\t\ts = ''\n\t\t\t\tfor j in i:\n\t\t\t\t\ts += str(j) + ' '\n\t\t\t\tprint(s)\n\telse:\n\t\tc = 0\n\t\tfor i in d.values():\n\t\t\tif i % 4 != 0:\n\t\t\t\tc = 1\n\t\t\t\tbreak\n\t\tif c == 1:\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tprint('YES')\n\t\t\tl1 = []\n\t\t\tfor i in range(n):\n\t\t\t\tl2 = []\n\t\t\t\tfor j in range(n):\n\t\t\t\t\tl2.append(0)\n\t\t\t\tl1.append(l2)\n\t\t\tfor i in range(n // 2):\n\t\t\t\tfor j in range(n // 2):\n\t\t\t\t\ta = 0\n\t\t\t\t\tfor k in d.keys():\n\t\t\t\t\t\tif d[k] != 0:\n\t\t\t\t\t\t\ta = k\n\t\t\t\t\t\t\td[k] -= 4\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\tl1[i][j] = a\n\t\t\t\t\tl1[i][n - j - 1] = a\n\t\t\t\t\tl1[n - i - 1][j] = a\n\t\t\t\t\tl1[n - i - 1][n - j - 1] = a\n\t\t\tfor i in l1:\n\t\t\t\ts = ''\n\t\t\t\tfor j in i:\n\t\t\t\t\ts += str(j) + ' '\n\t\t\t\tprint(s)\n", "class Solution:\n\n\tdef __init__(self):\n\t\tn = int(input())\n\t\tnum = []\n\t\tmat = []\n\t\tres = []\n\t\tfor i in range(0, 1001):\n\t\t\tnum.append(0)\n\t\tm = input().split()\n\t\tfor i in range(n * n):\n\t\t\tif num[int(m[i])] == 0:\n\t\t\t\tres.append(int(m[i]))\n\t\t\tnum[int(m[i])] += 1\n\t\tlen = int(n / 2)\n\t\tf = True\n\t\tfor i in range(len):\n\t\t\tfor j in range(len):\n\t\t\t\tf = False\n\t\t\t\tfor k in res:\n\t\t\t\t\tif num[k] >= 4:\n\t\t\t\t\t\tnum[k] -= 4\n\t\t\t\t\t\tmat.append(k)\n\t\t\t\t\t\tf = True\n\t\t\t\t\t\tbreak\n\t\t\t\tif not f:\n\t\t\t\t\tprint('No')\n\t\t\t\t\treturn\n\t\tif n & 1:\n\t\t\tcolum = []\n\t\t\trow = []\n\t\t\tfor i in range(len):\n\t\t\t\tf = False\n\t\t\t\tfor k in res:\n\t\t\t\t\tif num[k] >= 2:\n\t\t\t\t\t\tnum[k] -= 2\n\t\t\t\t\t\tcolum.append(k)\n\t\t\t\t\t\tf = True\n\t\t\t\t\t\tbreak\n\t\t\t\tif not f:\n\t\t\t\t\tprint('No')\n\t\t\t\t\treturn\n\t\t\tfor i in range(len):\n\t\t\t\tf = False\n\t\t\t\tfor k in res:\n\t\t\t\t\tif num[k] >= 2:\n\t\t\t\t\t\tnum[k] -= 2\n\t\t\t\t\t\trow.append(k)\n\t\t\t\t\t\tf = True\n\t\t\t\t\t\tbreak\n\t\t\t\tif not f:\n\t\t\t\t\tprint('No')\n\t\t\t\t\treturn\n\t\t\tcenter = -1\n\t\t\tfor i in res:\n\t\t\t\tif num[i] == 1:\n\t\t\t\t\tnum[i] = 0\n\t\t\t\t\tcenter = i\n\t\t\t\t\tbreak\n\t\t\tif center == -1:\n\t\t\t\tprint('No')\n\t\t\t\treturn\n\t\t\tprint('Yes')\n\t\t\tfor i in range(len):\n\t\t\t\tfor j in range(len * i, len * i + len):\n\t\t\t\t\tprint(mat[j], end=' ')\n\t\t\t\tk = len * i + len - 1\n\t\t\t\tprint(colum[i], end=' ')\n\t\t\t\tfor j in range(len):\n\t\t\t\t\tprint(mat[k], end='')\n\t\t\t\t\tif j == len - 1:\n\t\t\t\t\t\tprint()\n\t\t\t\t\telse:\n\t\t\t\t\t\tprint(end=' ')\n\t\t\t\t\tk -= 1\n\t\t\ti = len - 1\n\t\t\tfor j in row:\n\t\t\t\tprint(j, end=' ')\n\t\t\tprint(center, end='')\n\t\t\tfor j in row:\n\t\t\t\tprint(' ' + str(row[i]), end='')\n\t\t\t\ti -= 1\n\t\t\tprint()\n\t\t\tl = len - 1\n\t\t\tfor i in range(len):\n\t\t\t\tresult = []\n\t\t\t\tfor j in range(len * len - (i + 1) * len, len * len - (i + 1) * len + len):\n\t\t\t\t\tresult.append(mat[j])\n\t\t\t\t\tprint(mat[j], end=' ')\n\t\t\t\tk = len - 1\n\t\t\t\tprint(colum[l], end=' ')\n\t\t\t\tl -= 1\n\t\t\t\tfor j in range(len):\n\t\t\t\t\tprint(result[k], end='')\n\t\t\t\t\tif j == len - 1:\n\t\t\t\t\t\tprint()\n\t\t\t\t\telse:\n\t\t\t\t\t\tprint(end=' ')\n\t\t\t\t\tk -= 1\n\t\telse:\n\t\t\tprint('Yes')\n\t\t\tfor i in range(len):\n\t\t\t\tfor j in range(len * i, len * i + len):\n\t\t\t\t\tprint(mat[j], end=' ')\n\t\t\t\tk = len * i + len - 1\n\t\t\t\tfor j in range(len):\n\t\t\t\t\tprint(mat[k], end='')\n\t\t\t\t\tif j == len - 1:\n\t\t\t\t\t\tprint()\n\t\t\t\t\telse:\n\t\t\t\t\t\tprint(end=' ')\n\t\t\t\t\tk -= 1\n\t\t\tl = len - 1\n\t\t\tfor i in range(len):\n\t\t\t\tfor j in range(len * len - len * i - len, len * len - len * i):\n\t\t\t\t\tprint(mat[j], end=' ')\n\t\t\t\tk = len * len - len * i - 1\n\t\t\t\tfor j in range(len):\n\t\t\t\t\tprint(mat[k], end='')\n\t\t\t\t\tif j == len - 1:\n\t\t\t\t\t\tprint()\n\t\t\t\t\telse:\n\t\t\t\t\t\tprint(end=' ')\n\t\t\t\t\tk -= 1\nSolution()\n", "import io, sys, atexit, os\nimport math as ma\nfrom sys import exit\nfrom decimal import Decimal as dec\nfrom itertools import permutations\n\ndef li():\n\treturn list(map(int, input().split()))\n\ndef num():\n\treturn map(int, input().split())\n\ndef nu():\n\treturn int(input())\n\ndef find_gcd(x, y):\n\twhile y:\n\t\t(x, y) = (y, x % y)\n\treturn x\n\ndef solve():\n\tt = 1\n\tfor it in range(t):\n\t\tn = nu()\n\t\ta = li()\n\t\tff = [0] * 1001\n\t\tqq = -1\n\t\tfor i in a:\n\t\t\tff[i] += 1\n\t\tzz = 0\n\t\tfor i in range(len(ff)):\n\t\t\tif ff[i] % 2 == 1:\n\t\t\t\tzz += 1\n\t\t\t\tqq = i\n\t\tpy = -1\n\t\tfor i in range(n):\n\t\t\tif a[i] == qq:\n\t\t\t\tpy = i\n\t\tif n % 2 == 0:\n\t\t\tif zz > 0:\n\t\t\t\tprint('NO')\n\t\t\telse:\n\t\t\t\txx = [0] * n\n\t\t\t\tfor i in range(n):\n\t\t\t\t\txx[i] = [0] * n\n\t\t\t\tcc = 0\n\t\t\t\tfl = True\n\t\t\t\tfor i in range(n // 2):\n\t\t\t\t\tfor j in range(n // 2):\n\t\t\t\t\t\tzp = False\n\t\t\t\t\t\tfor k in range(len(ff)):\n\t\t\t\t\t\t\tif ff[k] >= 4:\n\t\t\t\t\t\t\t\txx[i][j] = k\n\t\t\t\t\t\t\t\tzp = True\n\t\t\t\t\t\t\t\txx[i][n - 1 - j] = k\n\t\t\t\t\t\t\t\txx[n - 1 - i][j] = k\n\t\t\t\t\t\t\t\txx[n - 1 - i][n - 1 - j] = k\n\t\t\t\t\t\t\t\tff[k] -= 4\n\t\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\tif zp == False:\n\t\t\t\t\t\t\tfl = False\n\t\t\t\tif fl == False:\n\t\t\t\t\tprint('NO')\n\t\t\t\t\tcontinue\n\t\t\t\tprint('YES')\n\t\t\t\tfor i in range(n):\n\t\t\t\t\tfor j in range(n):\n\t\t\t\t\t\tprint(xx[i][j], end=' ')\n\t\t\t\t\tprint()\n\t\telif zz > 1:\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tfl = True\n\t\t\tif zz == 0:\n\t\t\t\tprint('NO')\n\t\t\t\tcontinue\n\t\t\txx = [0] * n\n\t\t\tfor i in range(n):\n\t\t\t\txx[i] = [0] * n\n\t\t\trr = a[py]\n\t\t\txx[n // 2][n // 2] = qq\n\t\t\tff[qq] -= 1\n\t\t\tcc = 0\n\t\t\tfor i in range(n // 2):\n\t\t\t\tfor j in range(n // 2):\n\t\t\t\t\tzp = False\n\t\t\t\t\tfor k in range(len(ff)):\n\t\t\t\t\t\tif ff[k] >= 4:\n\t\t\t\t\t\t\txx[i][j] = k\n\t\t\t\t\t\t\txx[i][n - 1 - j] = k\n\t\t\t\t\t\t\txx[n - 1 - i][j] = k\n\t\t\t\t\t\t\txx[n - 1 - i][n - 1 - j] = k\n\t\t\t\t\t\t\tff[k] -= 4\n\t\t\t\t\t\t\tzp = True\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\tif zp == False:\n\t\t\t\t\t\tfl = False\n\t\t\tfor i in range(n // 2):\n\t\t\t\tzp = False\n\t\t\t\tfor j in range(len(ff)):\n\t\t\t\t\tif ff[j] >= 2:\n\t\t\t\t\t\txx[n // 2][i] = j\n\t\t\t\t\t\txx[n // 2][n - 1 - i] = j\n\t\t\t\t\t\tzp = True\n\t\t\t\t\t\tff[j] -= 2\n\t\t\t\t\t\tbreak\n\t\t\t\tif zp == False:\n\t\t\t\t\tfl = False\n\t\t\tfor i in range(n // 2):\n\t\t\t\tzp = True\n\t\t\t\tfor j in range(len(ff)):\n\t\t\t\t\tif ff[j] >= 2:\n\t\t\t\t\t\txx[i][n // 2] = j\n\t\t\t\t\t\tzp = True\n\t\t\t\t\t\txx[n - 1 - i][n // 2] = j\n\t\t\t\t\t\tff[j] -= 2\n\t\t\t\t\t\tbreak\n\t\t\t\tif zp == False:\n\t\t\t\t\tfl = False\n\t\t\tif fl == False:\n\t\t\t\tprint('NO')\n\t\t\t\tcontinue\n\t\t\tprint('YES')\n\t\t\tfor i in range(n):\n\t\t\t\tfor j in range(n):\n\t\t\t\t\tprint(xx[i][j], end=' ')\n\t\t\t\tprint()\nsolve()\n", "import sys\nfrom collections import defaultdict as dd\nfrom itertools import permutations as pp\nfrom itertools import combinations as cc\nfrom collections import Counter as ccd\nfrom random import randint as rd\nfrom bisect import bisect_left as bl\nimport heapq\nmod = 10 ** 9 + 7\n\ndef ri(flag=0):\n\tif flag == 0:\n\t\treturn [int(i) for i in sys.stdin.readline().split()]\n\telse:\n\t\treturn int(sys.stdin.readline())\nn = ri(1)\na = ri()\ncheck = dd(int)\nfor i in a:\n\tcheck[i] += 1\ntake = []\ntwo = []\none = []\ndone = 0\nfor i in check:\n\tif check[i] % 4 == 0:\n\t\tfor j in range(check[i] // 4):\n\t\t\ttake.append(i)\n\telif check[i] % 2 == 0:\n\t\tfor j in range(check[i] // 4):\n\t\t\ttake.append(i)\n\t\ttwo.append(i)\n\telif check[i] % 2 == 1:\n\t\tif check[i] > 4:\n\t\t\tkeep = i\n\t\t\tcooo = check[i] // 4\n\t\t\tif check[i] % 4 == 3:\n\t\t\t\ttwo.append(i)\n\t\t\tone.append(i)\n\t\telif check[i] > 2:\n\t\t\ttwo.append(i)\n\t\t\tone.append(i)\n\t\telse:\n\t\t\tone.append(i)\n\telse:\n\t\tprint('NO')\n\t\tdone = 1\ntry:\n\tfor i in range(cooo):\n\t\ttake.append(keep)\nexcept:\n\tpass\nif n % 2 == 0 and done == 0:\n\tif len(two) == 0 and len(one) == 0:\n\t\tvis = [[0 for i in range(n)] for j in range(n)]\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tif vis[i][j] == 0:\n\t\t\t\t\tif take:\n\t\t\t\t\t\tk = take.pop(0)\n\t\t\t\t\t\tvis[i][j] = k\n\t\t\t\t\t\tvis[n - i - 1][j] = k\n\t\t\t\t\t\tvis[i][n - j - 1] = k\n\t\t\t\t\t\tvis[n - i - 1][n - j - 1] = k\n\t\tdone = 1\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tif vis[i][j] == 0:\n\t\t\t\t\tdone = 0\n\t\tif done:\n\t\t\tprint('YES')\n\t\t\tfor i in vis:\n\t\t\t\tprint(*i)\n\t\telse:\n\t\t\tprint('NO')\n\telse:\n\t\tprint('NO')\nif n % 2 == 1 and done == 0:\n\tif len(one) == 1:\n\t\tvis = [[0 for i in range(n)] for j in range(n)]\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tif vis[i][j] == 0 and take:\n\t\t\t\t\tk = take.pop(0)\n\t\t\t\t\tvis[i][j] = k\n\t\t\t\t\tvis[n - i - 1][j] = k\n\t\t\t\t\tvis[i][n - j - 1] = k\n\t\t\t\t\tvis[n - i - 1][n - j - 1] = k\n\t\tx = 0\n\t\ty = n // 2\n\t\tx1 = n // 2\n\t\ty1 = n // 2\n\t\tfor i in range(n // 2 + 1):\n\t\t\tfor j in range(n // 2 + 1):\n\t\t\t\tif vis[i][j] == 0 and take:\n\t\t\t\t\tk = take.pop(0)\n\t\t\t\t\tvis[i][j] = k\n\t\t\t\t\tvis[j][i] = k\n\t\t\t\t\tvis[n - j - 1][n - i - 1] = k\n\t\t\t\t\tvis[n - i - 1][n - j - 1] = k\n\t\tfor i in range(n // 2):\n\t\t\tx = n // 2\n\t\t\tif vis[i][x] == 0:\n\t\t\t\tif two:\n\t\t\t\t\tk = two.pop(0)\n\t\t\t\t\tvis[i][x] = k\n\t\t\t\t\tvis[n - i - 1][x] = k\n\t\tfor i in range(n // 2):\n\t\t\tx = n // 2\n\t\t\tif vis[x][i] == 0:\n\t\t\t\tif two:\n\t\t\t\t\tk = two.pop(0)\n\t\t\t\t\tvis[x][i] = k\n\t\t\t\t\tvis[x][n - i - 1] = k\n\t\tvis[n // 2][n // 2] = one[0]\n\t\tdone = 1\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tif vis[i][j] == 0:\n\t\t\t\t\tdone = 0\n\t\tif done:\n\t\t\tprint('YES')\n\t\t\tfor i in vis:\n\t\t\t\tprint(*i)\n\t\telse:\n\t\t\tprint('NO')\n\telse:\n\t\tprint('NO')\n", "from collections import Counter\nn = int(input().strip())\nentries = list(map(int, input().strip().split()))\nif n == 1:\n\tprint('Yes')\n\tprint(entries[0])\n\texit()\nsize_4_entries = n // 2 * (n // 2)\nsize_2_entries = 2 * (n // 2) * (n % 2)\nsize_1_entries = n % 2\ncounts = Counter(entries)\nsize_1 = []\nsize_2 = []\nsize_4 = []\ndigits = list(counts.keys())\ndcounts = list(counts.values())\nhas_one = size_1_entries\nfor c in dcounts:\n\tif c % 2 == 1:\n\t\thas_one -= 1\n\t\tif has_one < 0:\n\t\t\tprint('No')\n\t\t\texit()\nhas_two = size_1_entries + size_2_entries\nfor c in dcounts:\n\tif c % 4 != 0:\n\t\thas_two -= 1\n\t\tif has_two < 0:\n\t\t\tprint('No')\n\t\t\texit()\n\ndef get_assign(s4, s2, s1, dcounts):\n\tif s4 == s2 == s1 == 0:\n\t\tres = dict()\n\t\tres['4'] = []\n\t\tres['2'] = []\n\t\tres['1'] = []\n\t\tyield res\n\t\treturn\n\tif s4 != 0:\n\t\tfor (idx, count) in enumerate(dcounts):\n\t\t\tif count >= 4:\n\t\t\t\tdcounts[idx] -= 4\n\t\t\t\tfor sol in get_assign(s4 - 1, s2, s1, dcounts):\n\t\t\t\t\tif sol is not None:\n\t\t\t\t\t\tsol['4'].append(digits[idx])\n\t\t\t\t\t\tyield sol\n\t\t\t\tdcounts[idx] += 4\n\tif s2 != 0:\n\t\tfor (idx, count) in enumerate(dcounts):\n\t\t\tif count >= 2:\n\t\t\t\tdcounts[idx] -= 2\n\t\t\t\tfor sol in get_assign(s4, s2 - 1, s1, dcounts):\n\t\t\t\t\tif sol is not None:\n\t\t\t\t\t\tsol['2'].append(digits[idx])\n\t\t\t\t\t\tyield sol\n\t\t\t\tdcounts[idx] += 2\n\tif s1 != 0:\n\t\tfor (idx, count) in enumerate(dcounts):\n\t\t\tif count >= 1:\n\t\t\t\tdcounts[idx] -= 1\n\t\t\t\tfor sol in get_assign(s4, s2, s1 - 1, dcounts):\n\t\t\t\t\tif sol is not None:\n\t\t\t\t\t\tsol['1'].append(digits[idx])\n\t\t\t\t\t\tyield sol\n\t\t\t\tdcounts[idx] += 1\n\tyield None\nfor res in get_assign(size_4_entries, size_2_entries, size_1_entries, dcounts):\n\tbreak\nif res is None:\n\tprint('No')\n\texit()\nelse:\n\tsize_4 = res['4']\n\tsize_2 = res['2']\n\tsize_1 = res['1']\nprint('Yes')\nlow = n // 2\nif n % 2 == 0:\n\thigh = n // 2\nelse:\n\thigh = n // 2 + 1\nfor i in range(n):\n\trow = []\n\tfor j in range(n):\n\t\tif i < low and j < low:\n\t\t\trow.append(size_4[j + n // 2 * i])\n\t\telif i < low and j >= high:\n\t\t\trow.append(size_4[n - 1 - j + n // 2 * i])\n\t\telif i >= high and j < low:\n\t\t\trow.append(size_4[j + n // 2 * (n - 1 - i)])\n\t\telif i >= high and j >= high:\n\t\t\trow.append(size_4[n - 1 - j + n // 2 * (n - 1 - i)])\n\t\telif n % 2 == 1 and i == n // 2:\n\t\t\tif j < low:\n\t\t\t\trow.append(size_2[j])\n\t\t\telif j == n // 2:\n\t\t\t\trow.append(size_1[0])\n\t\t\telse:\n\t\t\t\trow.append(size_2[n - 1 - j])\n\t\telif n % 2 == 1 and j == n // 2:\n\t\t\tif i < low:\n\t\t\t\trow.append(size_2[n // 2 + i])\n\t\t\telse:\n\t\t\t\trow.append(size_2[n - 1 - (n // 2 + i)])\n\tprint(' '.join(map(str, row)))\n", "import sys\nimport math\nfrom collections import defaultdict\n\ndef do():\n\tprint('NO')\n\tsys.exit()\nn = int(sys.stdin.readline())\nnums = list(map(int, sys.stdin.readline().split()))\ndic = defaultdict(int)\nif n == 1:\n\tprint('YES')\n\tprint(nums[0])\n\tsys.exit()\nm = n * n\nfor i in range(m):\n\tdic[nums[i]] += 1\n(count2, count0) = (0, 0)\nans = [[-1 for _ in range(n)] for x in range(n)]\n(four, two, one) = ([], [], [])\nfor i in dic:\n\tif dic[i] % 4 == 0:\n\t\tfor j in range(dic[i]):\n\t\t\tfour.append(i)\n\t\tcontinue\n\tif dic[i] % 2 == 0:\n\t\ttwo.append(i)\n\t\ttwo.append(i)\n\t\tdic[i] -= 2\n\t\tif dic[i] >= 4:\n\t\t\tfor j in range(dic[i]):\n\t\t\t\tfour.append(i)\n\t\tcontinue\n\tif dic[i] % 2 == 1:\n\t\tone.append(i)\n\t\tdic[i] -= 1\n\t\tif dic[i] % 2 == 0 and dic[i] % 4 != 0:\n\t\t\ttwo.append(i)\n\t\t\ttwo.append(i)\n\t\t\tdic[i] -= 2\n\t\tif dic[i] >= 4:\n\t\t\tfor j in range(dic[i]):\n\t\t\t\tfour.append(i)\n\t\tcontinue\n\tprint(dic, 'dic')\n\tprint('NO')\n\tsys.exit()\nfour.sort()\none.sort()\ntwo.sort()\n(o, t, f) = (len(one), len(two), len(four))\nif n % 2 != 0:\n\t(ind1, ind2, ind4) = (0, 0, 0)\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tif ind4 >= f:\n\t\t\t\tdo()\n\t\t\tans[i][j] = four[ind4]\n\t\t\tind4 += 1\n\t\t\tif ind4 >= f:\n\t\t\t\tdo()\n\t\t\tans[i][n - 1 - j] = four[ind4]\n\t\t\tind4 += 1\n\t\t\tif ind4 >= f:\n\t\t\t\tdo()\n\t\t\tans[n - i - 1][j] = four[ind4]\n\t\t\tind4 += 1\n\t\t\tif ind4 >= f:\n\t\t\t\tdo()\n\t\t\tans[n - i - 1][n - 1 - j] = four[ind4]\n\t\t\tind4 += 1\n\tfor x in range(ind4, f):\n\t\ttwo.append(four[x])\n\tt = len(two)\n\tfor i in range(n // 2):\n\t\tif ind2 >= t:\n\t\t\tdo()\n\t\tans[i][n // 2] = two[ind2]\n\t\tind2 += 1\n\t\tif ind2 >= t:\n\t\t\tdo()\n\t\tans[n - i - 1][n // 2] = two[ind2]\n\t\tind2 += 1\n\tfor i in range(n // 2):\n\t\tif ind2 >= t:\n\t\t\tdo()\n\t\tans[n // 2][i] = two[ind2]\n\t\tind2 += 1\n\t\tif ind2 >= t:\n\t\t\tdo()\n\t\tans[n // 2][n - i - 1] = two[ind2]\n\t\tind2 += 1\n\tif ind1 >= o:\n\t\tdo()\n\tans[n // 2][n // 2] = one[ind1]\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*ans[i])\nelse:\n\tif o > 0 or t > 0:\n\t\tdo()\n\tind4 = 0\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tif ind4 >= f:\n\t\t\t\tdo()\n\t\t\tans[i][j] = four[ind4]\n\t\t\tind4 += 1\n\t\t\tif ind4 >= f:\n\t\t\t\tdo()\n\t\t\tans[i][n - 1 - j] = four[ind4]\n\t\t\tind4 += 1\n\t\t\tif ind4 >= f:\n\t\t\t\tdo()\n\t\t\tans[n - i - 1][j] = four[ind4]\n\t\t\tind4 += 1\n\t\t\tif ind4 >= f:\n\t\t\t\tdo()\n\t\t\tans[n - i - 1][n - 1 - j] = four[ind4]\n\t\t\tind4 += 1\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*ans[i])\n", "import os\nimport sys\nimport math\nfrom io import BytesIO, IOBase\nfrom fractions import Fraction\nfrom collections import defaultdict\nfrom itertools import permutations\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nn = int(input())\na = list(map(int, input().split()))\nd = dict()\nfor i in a:\n\tif i in d:\n\t\td[i] += 1\n\telse:\n\t\td[i] = 1\nfour = list()\ntwo = list()\none = list()\nfor i in d:\n\tc = d[i]\n\tfor j in range(c // 4):\n\t\tfour.append(i)\n\tc %= 4\n\tfor j in range(c // 2):\n\t\ttwo.append(i)\n\tc %= 2\n\tif c > 0:\n\t\tone.append(i)\nif n % 2 == 0:\n\tif len(four) != n // 2 * (n // 2):\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tr = [[0 for i in range(n)] for j in range(n)]\n\t\tk = 0\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tr[i][j] = four[k]\n\t\t\t\tr[i][n - 1 - j] = four[k]\n\t\t\t\tr[n - 1 - i][j] = four[k]\n\t\t\t\tr[n - 1 - i][n - 1 - j] = four[k]\n\t\t\t\tk += 1\n\t\tfor i in range(n):\n\t\t\tprint(*r[i])\nelif len(one) > 1 or len(two) > n // 2 * 2:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tr = [[0 for i in range(n)] for j in range(n)]\n\tk = 0\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tr[i][j] = four[k]\n\t\t\tr[i][n - 1 - j] = four[k]\n\t\t\tr[n - 1 - i][j] = four[k]\n\t\t\tr[n - 1 - i][n - 1 - j] = four[k]\n\t\t\tk += 1\n\twhile k < len(four):\n\t\ttwo.append(four[k])\n\t\ttwo.append(four[k])\n\t\tk += 1\n\tr[n // 2][n // 2] = one[0]\n\tk = 0\n\tfor i in range(n // 2):\n\t\tr[i][n // 2] = two[k]\n\t\tr[n - 1 - i][n // 2] = two[k]\n\t\tk += 1\n\tfor j in range(n // 2):\n\t\tr[n // 2][j] = two[k]\n\t\tr[n // 2][n - 1 - j] = two[k]\n\t\tk += 1\n\tfor i in range(n):\n\t\tprint(*r[i])\n", "def print_mat(m):\n\tn = len(m)\n\tfor i in range(n):\n\t\tprint(' '.join([str(j) for j in m[i]]))\n\ndef mat_unity4(b):\n\tglobal n\n\t(b1, b2, b3, b4) = (b, tr_mat(b, 2), tr_mat(b, 3), tr_mat(b, 4))\n\tr = [['' for i in range(n)] for j in range(n)]\n\tfor i in range(n // 2):\n\t\tfor j in range(n):\n\t\t\tif j < n // 2:\n\t\t\t\tr[i][j] = b1[i][j]\n\t\t\telse:\n\t\t\t\tr[i][j] = b2[i][j % (n // 2)]\n\tfor i in range(n // 2):\n\t\tfor j in range(n):\n\t\t\tif j < n // 2:\n\t\t\t\tr[i + n // 2][j] = b4[i][j]\n\t\t\telse:\n\t\t\t\tr[i + n // 2][j] = b3[i][j % (n // 2)]\n\treturn r\n\ndef mat_unity9(b, m, c):\n\tglobal n\n\t(b1, b2, b3, b4) = (b, tr_mat(b, 2), tr_mat(b, 3), tr_mat(b, 4))\n\t(b5, b6, b7, b8) = (m[0], m[1], m[0][::-1], m[1][::-1])\n\tb9 = c\n\tr = [['' for i in range(n)] for j in range(n)]\n\tfor i in range(n // 2):\n\t\tfor j in range(n):\n\t\t\tif j < n // 2:\n\t\t\t\tr[i][j] = b1[i][j]\n\t\t\telif j == n // 2:\n\t\t\t\tr[i][j] = b5[i]\n\t\t\telse:\n\t\t\t\tr[i][j] = b2[i][j % (n // 2 + 1)]\n\tfor j in range(n):\n\t\tif j < n // 2:\n\t\t\tr[n // 2][j] = b8[j]\n\t\telif j == n // 2:\n\t\t\tr[n // 2][j] = b9\n\t\telse:\n\t\t\tr[n // 2][j] = b6[j % (n // 2 + 1)]\n\tfor i in range(n // 2):\n\t\tfor j in range(n):\n\t\t\tif j < n // 2:\n\t\t\t\tr[i + n // 2 + 1][j] = b4[i][j]\n\t\t\telif j == n // 2:\n\t\t\t\tr[i + n // 2 + 1][j] = b7[i]\n\t\t\telse:\n\t\t\t\tr[i + n // 2 + 1][j] = b3[i][j % (n // 2 + 1)]\n\treturn r\n\ndef tr_mat(m, t):\n\tn = len(m)\n\tif n == 1:\n\t\treturn m\n\tr = [['' for i in range(n)] for j in range(n)]\n\tif t == 2:\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tr[i][j] = m[i][n - j - 1]\n\telif t == 4:\n\t\tfor j in range(n):\n\t\t\tfor i in range(n):\n\t\t\t\tr[i][j] = m[n - i - 1][j]\n\telse:\n\t\tr = tr_mat(tr_mat(m, 2), 4)\n\treturn r\n\ndef test(m):\n\tfor i in range(2, 5):\n\t\tif m != tr_mat(m, i):\n\t\t\treturn False\n\treturn True\nfrom collections import Counter\nn = int(input())\na = [int(i) for i in input().split()]\nd = Counter(a)\nif n % 2 == 0:\n\ti = 0\n\tc = list(d.keys())\n\twhile i < len(d) and d[c[i]] % 4 == 0:\n\t\ti += 1\n\telse:\n\t\tif i < len(d):\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tprint('YES')\n\t\t\tb = [['' for i in range(n // 2)] for j in range(n // 2)]\n\t\t\tsc = 0\n\t\t\tfor i in c:\n\t\t\t\td[i] //= 4\n\t\t\t\tfor j in range(d[i]):\n\t\t\t\t\tb[sc // (n // 2)][sc % (n // 2)] = i\n\t\t\t\t\tsc += 1\n\t\t\tprint_mat(mat_unity4(b))\nelse:\n\tc = list(d.keys())\n\t(k1, k2, k4) = ([], [], [])\n\tb = 0\n\tfor i in range(len(c)):\n\t\tit = d[c[i]]\n\t\tif it % 4 == 1:\n\t\t\tk1.append(c[i])\n\t\telif it % 4 == 2:\n\t\t\tk2.append(c[i])\n\t\telif it % 4 == 3:\n\t\t\tk2.append(c[i])\n\t\t\tk1.append(c[i])\n\t\tfor j in range(it // 4):\n\t\t\tk4.append(c[i])\n\ts = len(k4) - (n // 2) ** 2\n\tfor i in range(s):\n\t\tk2.append(k4[0])\n\t\tk2.append(k4[0])\n\t\tdel k4[0]\n\tif len(k1) != 1 or len(k2) != 2 * (n // 2) or len(k4) != (n // 2) ** 2:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tb1 = [['' for i in range(n // 2)] for j in range(n // 2)]\n\t\tb2 = [['' for i in range(n // 2)] for j in range(2)]\n\t\tb3 = k1[0]\n\t\tsc = 0\n\t\tfor i in k4:\n\t\t\tb1[sc // (n // 2)][sc % (n // 2)] = i\n\t\t\tsc += 1\n\t\tsc = 0\n\t\tfor i in k2:\n\t\t\tb2[sc // (n // 2)][sc % (n // 2)] = i\n\t\t\tsc += 1\n\t\tprint_mat(mat_unity9(b1, b2, b3))\n", "n = int(input())\na = list(map(int, input().split()))\nans = [[-1] * n for i in range(n)]\ndp = [0] * 1001\nfor i in a:\n\tdp[i] += 1\nflag = 1\nire = []\nni = []\nfor i in range(1001):\n\tif dp[i]:\n\t\tif dp[i] % 4 == 0:\n\t\t\tdp[i] //= 4\n\t\t\tfor j in range(dp[i]):\n\t\t\t\tire.append(i)\n\t\telif dp[i] % 4 == 1 and flag and (n % 2 == 1):\n\t\t\tflag = 0\n\t\t\tans[n // 2][n // 2] = i\n\t\t\tdp[i] //= 4\n\t\t\tfor j in range(dp[i]):\n\t\t\t\tire.append(i)\n\t\telif dp[i] % 4 == 2 and n % 2 == 1:\n\t\t\tni.append(i)\n\t\t\tdp[i] //= 4\n\t\t\tfor j in range(dp[i]):\n\t\t\t\tire.append(i)\n\t\telif dp[i] % 4 == 3 and flag and (n % 2 == 1):\n\t\t\tflag = 0\n\t\t\tans[n // 2][n // 2] = i\n\t\t\tni.append(i)\n\t\t\tdp[i] //= 4\n\t\t\tfor j in range(dp[i]):\n\t\t\t\tire.append(i)\n\t\telse:\n\t\t\tprint('NO')\n\t\t\texit()\nif len(ni) > n - 1:\n\tprint('NO')\n\texit()\nprint('YES')\nif n % 2 == 0:\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tb = ire.pop()\n\t\t\tans[i][j] = ans[n - i - 1][j] = ans[i][n - j - 1] = ans[n - i - 1][n - j - 1] = b\nelse:\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tb = ire.pop()\n\t\t\tans[i][j] = ans[n - i - 1][j] = ans[i][n - j - 1] = ans[n - i - 1][n - j - 1] = b\n\twhile ire:\n\t\tb = ire.pop()\n\t\tni.append(b)\n\t\tni.append(b)\n\tfor i in range(n // 2):\n\t\tb = ni.pop()\n\t\tans[i][n // 2] = ans[n - 1 - i][n // 2] = b\n\tfor i in range(n // 2):\n\t\tb = ni.pop()\n\t\tans[n // 2][i] = ans[n // 2][n - 1 - i] = b\nfor i in ans:\n\tprint(*i)\n", "def is_palindromic(lst, n):\n\tlen_set = len(set(lst))\n\tset_ = set(lst)\n\tif n % 2 == 0:\n\t\ta = n * n / 4\n\telse:\n\t\ta = (n + 1) * (n + 1) / 4\n\tif len_set > a:\n\t\tprint('NO')\n\t\treturn\n\tif len(lst) != n * n:\n\t\tprint('no')\n\t\treturn\n\tdict_ = {}\n\tfor i in lst:\n\t\tif i not in dict_:\n\t\t\tdict_[i] = 1\n\t\telse:\n\t\t\tdict_[i] += 1\n\tif n == 1:\n\t\tprint('YES')\n\t\tmatrix = print_matrix(dict_, n)\n\t\treturn matrix\n\telif n == 2:\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tmatrix = print_matrix(dict_, n)\n\t\t\treturn matrix\n\telif n % 2 == 0 and n != 2:\n\t\tfor (key, value) in enumerate(dict_.items()):\n\t\t\tif value[1] % 4 != 0:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\telse:\n\t\t\tprint('yes')\n\t\t\tmatrix = print_matrix(dict_, n)\n\t\t\treturn matrix\n\telif n % 2 != 0:\n\t\tksi = n - 1\n\t\tpsi = 0\n\t\tfor (key, value) in enumerate(dict_.items()):\n\t\t\tif value[1] % 4 != 0 and value[1] % 2 == 0:\n\t\t\t\tpsi += 1\n\t\tif psi > ksi:\n\t\t\tprint('NO')\n\t\t\treturn\n\t\telse:\n\t\t\todd = 0\n\t\t\ttwo = 0\n\t\t\tfor (key, value) in enumerate(dict_.items()):\n\t\t\t\tif value[1] % 2 != 0:\n\t\t\t\t\todd += 1\n\t\t\t\t\tif odd > 1:\n\t\t\t\t\t\tprint('NO')\n\t\t\t\t\t\treturn\n\t\t\t\telif value[1] == 2:\n\t\t\t\t\ttwo += 1\n\t\t\t\t\tif two > n - 1:\n\t\t\t\t\t\tprint('no')\n\t\t\t\t\t\treturn\n\t\t\telse:\n\t\t\t\tprint('yes')\n\t\t\t\tmatrix = print_matrix(dict_, n)\n\t\t\t\treturn matrix\n\ndef print_matrix(dict_, n):\n\tMatrix = [[0 for x in range(n)] for y in range(n)]\n\tlst_2 = []\n\tlst_2_2 = []\n\tlst_2_2_2 = []\n\tlst_4 = []\n\todd_num = 0\n\tpairs_without_quarters = 0\n\tfor (key, value) in enumerate(dict_.items()):\n\t\tif value[1] % 2 == 0 and value[1] % 4 != 0:\n\t\t\tlst_2.append(value[0])\n\t\t\tpairs_without_quarters += 1\n\t\telif value[1] % 4 == 0:\n\t\t\tlst_4.append(value[0])\n\t\telse:\n\t\t\todd_num += value[0]\n\tif n % 2 != 0:\n\t\todd_num_num = dict_[odd_num]\n\t\tif odd_num != 0:\n\t\t\tMatrix[n // 2][n // 2] = odd_num\n\t\t\tdict_[odd_num] -= 1\n\t\tif dict_[odd_num] == 0:\n\t\t\todd_num = 0\n\t\telif dict_[odd_num] % 4 != 0:\n\t\t\tMatrix[n // 2][n // 2 - 1] = odd_num\n\t\t\tMatrix[n // 2][n // 2 + 1] = odd_num\n\t\t\tif dict_[odd_num] == 2:\n\t\t\t\todd_num = 0\n\t\t\telse:\n\t\t\t\tdict_[odd_num] -= 2\n\t\tif len(lst_2) != 0:\n\t\t\tif len(lst_2) >= n // 2:\n\t\t\t\tfor i in range(n // 2):\n\t\t\t\t\tMatrix[i][n // 2] = lst_2[i]\n\t\t\t\t\tMatrix[n - 1 - i][n // 2] = lst_2[i]\n\t\t\t\t\tdict_[lst_2[i]] -= 2\n\t\t\t\t\tif dict_[lst_2[i]] == 0:\n\t\t\t\t\t\tlst_2[i] = 0\n\t\t\telse:\n\t\t\t\tfor i in range(len(lst_2)):\n\t\t\t\t\tMatrix[i][n // 2] = lst_2[i]\n\t\t\t\t\tMatrix[n - 1 - i][n // 2] = lst_2[i]\n\t\t\t\t\tdict_[lst_2[i]] -= 2\n\t\t\tfor item in lst_2:\n\t\t\t\tif item > 0 and dict_[item] % 4 == 0:\n\t\t\t\t\tlst_4.append(item)\n\t\t\t\telif item > 0:\n\t\t\t\t\tlst_2_2.append(item)\n\t\tif len(lst_2_2) != 0:\n\t\t\tif len(lst_2_2) >= n // 2:\n\t\t\t\tfor j in range(n // 2):\n\t\t\t\t\tMatrix[n // 2][j] = lst_2_2[j]\n\t\t\t\t\tMatrix[n // 2][n - 1 - j] = lst_2_2[j]\n\t\t\t\t\tdict_[lst_2_2[j]] -= 2\n\t\t\t\t\tif dict_[lst_2_2[j]] == 0:\n\t\t\t\t\t\tlst_2_2[j] = 0\n\t\t\telse:\n\t\t\t\tfor j in range(len(lst_2_2)):\n\t\t\t\t\tMatrix[n // 2][j] = lst_2_2[j]\n\t\t\t\t\tMatrix[n // 2][n - 1 - j] = lst_2_2[j]\n\t\t\t\t\tdict_[lst_2_2[j]] -= 2\n\t\t\tfor item in lst_2_2:\n\t\t\t\tif item > 0 and dict_[item] % 4 == 0:\n\t\t\t\t\tlst_4.append(item)\n\t\t\t\telif item > 0:\n\t\t\t\t\tlst_2_2_2.append(item)\n\tfor i in range(n):\n\t\tif len(lst_4) == 0 and odd_num == 0:\n\t\t\tbreak\n\t\tfor j in range(n // 2 + 1):\n\t\t\tif len(lst_4) == 0 and odd_num == 0:\n\t\t\t\tbreak\n\t\t\tif Matrix[i][j] == 0 and len(lst_4) != 0 and (n % 2 == 0):\n\t\t\t\tz = dict_[lst_4[0]] // 4\n\t\t\t\tii = i\n\t\t\t\tjj = j\n\t\t\t\twhile z:\n\t\t\t\t\tif Matrix[i][j] == 0:\n\t\t\t\t\t\tMatrix[i][j] = lst_4[0]\n\t\t\t\t\t\tMatrix[i][n - j - 1] = lst_4[0]\n\t\t\t\t\t\tMatrix[n - i - 1][j] = lst_4[0]\n\t\t\t\t\t\tMatrix[n - i - 1][n - j - 1] = lst_4[0]\n\t\t\t\t\t\tz -= 1\n\t\t\t\t\tif j == (n - 1) // 2:\n\t\t\t\t\t\ti += 1\n\t\t\t\t\t\tj = 0\n\t\t\t\t\telse:\n\t\t\t\t\t\tj += 1\n\t\t\t\tdel lst_4[0]\n\t\t\t\ti = ii\n\t\t\t\tj = jj\n\t\t\telif Matrix[i][j] == 0 and len(lst_4) != 0:\n\t\t\t\tb = dict_[lst_4[0]]\n\t\t\t\tz = b // 4\n\t\t\t\tii = i\n\t\t\t\tjj = j\n\t\t\t\twhile z:\n\t\t\t\t\tif Matrix[i][j] == 0 and i != (n - 1) // 2 and (j != (n - 1) // 2):\n\t\t\t\t\t\tMatrix[i][j] = lst_4[0]\n\t\t\t\t\t\tMatrix[i][n - 1 - j] = lst_4[0]\n\t\t\t\t\t\tMatrix[n - i - 1][j] = lst_4[0]\n\t\t\t\t\t\tMatrix[n - i - 1][n - 1 - j] = lst_4[0]\n\t\t\t\t\t\tz -= 1\n\t\t\t\t\telif Matrix[i][j] == 0 and j == (n - 1) // 2 and (i != (n - 1) // 2 - 1):\n\t\t\t\t\t\tMatrix[i][j] = lst_4[0]\n\t\t\t\t\t\tMatrix[i + 1][j] = lst_4[0]\n\t\t\t\t\t\tMatrix[n - i - 1][j] = lst_4[0]\n\t\t\t\t\t\tMatrix[n - i - 2][j] = lst_4[0]\n\t\t\t\t\t\tz -= 1\n\t\t\t\t\telif Matrix[i][j] == 0 and i == (n - 1) // 2:\n\t\t\t\t\t\tMatrix[i][j] = lst_4[0]\n\t\t\t\t\t\tMatrix[i][n - j - 1] = lst_4[0]\n\t\t\t\t\t\tif j + 1 != n - j - 2:\n\t\t\t\t\t\t\tMatrix[i][j + 1] = lst_4[0]\n\t\t\t\t\t\t\tMatrix[i][n - j - 2] = lst_4[0]\n\t\t\t\t\t\t\tj += 1\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tMatrix[i - 1][n // 2] = lst_4[0]\n\t\t\t\t\t\t\tMatrix[i + 1][n // 2] = lst_4[0]\n\t\t\t\t\t\tz -= 1\n\t\t\t\t\tif j == (n - 1) // 2:\n\t\t\t\t\t\ti += 1\n\t\t\t\t\t\tj = 0\n\t\t\t\t\telse:\n\t\t\t\t\t\tj += 1\n\t\t\t\ti = ii\n\t\t\t\tj = jj\n\t\t\t\tdel lst_4[0]\n\t\t\telif Matrix[i][j] == 0 and odd_num != 0:\n\t\t\t\tb = dict_[odd_num]\n\t\t\t\tk = 0\n\t\t\t\ti = 0\n\t\t\t\tj = 0\n\t\t\t\twhile b:\n\t\t\t\t\tif Matrix[i][j] == 0:\n\t\t\t\t\t\tMatrix[i][j] = odd_num\n\t\t\t\t\t\tb -= 1\n\t\t\t\t\tif j == n - 1:\n\t\t\t\t\t\ti += 1\n\t\t\t\t\t\tj = 0\n\t\t\t\t\telse:\n\t\t\t\t\t\tj += 1\n\t\t\t\todd_num = 0\n\tfor rows in Matrix:\n\t\trows = str(rows).replace(', ', ' ')\n\t\trows = rows.strip(']')\n\t\trows = rows.strip('[')\n\t\tprint(rows)\nn = int(input())\nlst = list(map(int, input().split()))\nis_palindromic(lst, n)\n", "n = int(input())\ncount = [0] * 1000\nnumbers = list(map(int, input().split()))\nfor i in numbers:\n\tcount[i - 1] += 1\npossible = True\nmiddleFoundOrEven = n % 2 == 0\nmiddle = -1\ncountFour = 0\nfour = []\ncountEight = 0\neight = []\nfor i in range(len(count)):\n\tif count[i] != 0 and count[i] % 2 == 0:\n\t\tcount[i] //= 2\n\t\tcountEight += count[i] // 2\n\t\teight += [i + 1] * (count[i] // 2)\n\t\tcountFour += count[i] % 2\n\t\tfour += [i + 1] * (count[i] % 2)\n\telif not middleFoundOrEven and count[i] % 2:\n\t\tmiddleFoundOrEven = True\n\t\tmiddle = i + 1\n\t\tcount[i] //= 2\n\t\tcountEight += count[i] // 2\n\t\teight += [i + 1] * (count[i] // 2)\n\t\tcountFour = count[i] % 2\n\t\tfour += [i + 1] * (count[i] % 2)\n\telif count[i] != 0:\n\t\tpossible = False\n\t\tbreak\nif not middleFoundOrEven:\n\tpossible = False\nif possible and n % 2 == 0:\n\tminEight = n * n / 4\n\tif countEight < minEight:\n\t\tpossible = False\nelif possible and n % 2 == 1:\n\tminEight = (n ** 2 - 2 * n - 1) / 4\n\tif countEight < minEight:\n\t\tpossible = False\nif possible:\n\tprint('YES')\n\tmatrix = [[0] * n for i in range(n)]\n\tind = 0\n\tfor i in range(n ** 2):\n\t\tx = i // n\n\t\ty = i % n\n\t\tif matrix[x][y] == 0 and (n % 2 == 0 or (x != n // 2 and y != n // 2)):\n\t\t\tmatrix[x][y] = eight[ind]\n\t\t\tmatrix[n - x - 1][y] = eight[ind]\n\t\t\tmatrix[x][n - y - 1] = eight[ind]\n\t\t\tmatrix[n - x - 1][n - y - 1] = eight[ind]\n\t\t\teight.pop(ind)\n\tfour += 2 * eight\n\teven = n % 2 == 0\n\tind = 0\n\tfor i in range(n ** 2):\n\t\tx = i // n\n\t\ty = i % n\n\t\tif matrix[x][y] == 0:\n\t\t\tif x == y and even or (not even and x != n // 2) or y != n // 2:\n\t\t\t\tmatrix[x][y] = four[ind]\n\t\t\t\tmatrix[n - x - 1][y] = four[ind]\n\t\t\t\tmatrix[x][n - y - 1] = four[ind]\n\t\t\t\tmatrix[n - x - 1][n - y - 1] = four[ind]\n\t\t\t\tfour.pop(ind)\n\tif not even:\n\t\tmatrix[n // 2][n // 2] = middle\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(matrix[i][j], end=' ')\n\t\tprint('')\nelse:\n\tprint('NO')\n", "n = int(input())\na = list(map(int, input().split()))\ne = dict()\nfor i in range(n ** 2):\n\tif a[i] in e:\n\t\te[a[i]] += 1\n\telse:\n\t\te[a[i]] = 1\nm = [[0] * n for _ in range(n)]\nif n % 2 == 0:\n\tif set(map(lambda n: n % 4, e.values())) != {0}:\n\t\tprint('NO')\n\telse:\n\t\tfor y in range(n // 2):\n\t\t\tfor x in range(n // 2):\n\t\t\t\tz = list(e.keys())[0]\n\t\t\t\t(m[y][x], m[n - y - 1][x], m[y][n - x - 1], m[n - y - 1][n - x - 1]) = (z, z, z, z)\n\t\t\t\te[z] -= 4\n\t\t\t\tif not e[z]:\n\t\t\t\t\tdel e[z]\n\t\tprint('YES', *map(lambda l: ' '.join(map(str, l)), m), sep='\\n')\nelif list(map(lambda i: i % 2, e.values())).count(1) != 1:\n\tprint('NO')\nelse:\n\tfor z in set(a):\n\t\tif a.count(z) % 2 == 1:\n\t\t\te[z] -= 1\n\t\t\tif not e[z]:\n\t\t\t\tdel e[z]\n\t\t\tm[n // 2][n // 2] = z\n\t\t\tbreak\n\tx = 0\n\ty = 0\n\twhile 2 in map(lambda i: i % 4, e.values()) and x < n // 2:\n\t\tfor z in set(e.keys()):\n\t\t\tif e[z] % 4 == 2:\n\t\t\t\t(m[n // 2][x], m[n // 2][n - x - 1]) = (z, z)\n\t\t\t\te[z] -= 2\n\t\t\t\tif not e[z]:\n\t\t\t\t\tdel e[z]\n\t\t\t\tbreak\n\t\tx += 1\n\twhile 2 in map(lambda i: i % 4, e.values()) and y < n // 2:\n\t\tfor z in set(e.keys()):\n\t\t\tif e[z] % 4 == 2:\n\t\t\t\t(m[y][n // 2], m[n - y - 1][n // 2]) = (z, z)\n\t\t\t\te[z] -= 2\n\t\t\t\tif not e[z]:\n\t\t\t\t\tdel e[z]\n\t\t\t\tbreak\n\t\ty += 1\n\tif x < n // 2 or y < n // 2:\n\t\twhile x < n // 2:\n\t\t\tz = list(e.keys())[0]\n\t\t\t(m[n // 2][x], m[n // 2][n - x - 1]) = (z, z)\n\t\t\te[z] -= 2\n\t\t\tif not e[z]:\n\t\t\t\tdel e[z]\n\t\t\tx += 1\n\t\twhile y < n // 2:\n\t\t\tz = list(e.keys())[0]\n\t\t\t(m[y][n // 2], m[n - y - 1][n // 2]) = (z, z)\n\t\t\te[z] -= 2\n\t\t\tif not e[z]:\n\t\t\t\tdel e[z]\n\t\t\ty += 1\n\tif 2 in map(lambda i: i % 4, e.values()):\n\t\tprint('NO')\n\telse:\n\t\tfor y in range(n // 2):\n\t\t\tfor x in range(n // 2):\n\t\t\t\tz = list(e.keys())[0]\n\t\t\t\t(m[y][x], m[n - y - 1][x], m[y][n - x - 1], m[n - y - 1][n - x - 1]) = (z, z, z, z)\n\t\t\t\te[z] -= 4\n\t\t\t\tif not e[z]:\n\t\t\t\t\tdel e[z]\n\t\tprint('YES', *map(lambda l: ' '.join(map(str, l)), m), sep='\\n')\n", "n = int(input())\narr = list(map(int, input().split()))\nfield = []\nfor i in range(n):\n\tfield.append([-1] * n)\ncount = [0] * (max(arr) + 1)\nfor i in range(int(n ** 2)):\n\tcount[arr[i]] += 1\nx = 0\ny = 0\ni = 0\nwhile i < len(count):\n\tfl = False\n\twhile count[i] >= 4:\n\t\tcount[i] -= 4\n\t\tfield[y][x] = i\n\t\tfield[n - y - 1][x] = i\n\t\tfield[y][n - x - 1] = i\n\t\tfield[n - y - 1][n - x - 1] = i\n\t\tx += 1\n\t\ty += x // (n // 2)\n\t\tx %= n // 2\n\t\tif y == n // 2:\n\t\t\tfl = True\n\t\t\tbreak\n\tif fl:\n\t\tbreak\n\ti += 1\nif n % 2:\n\tx = n // 2\n\ty = 0\n\ti = 0\n\twhile i < len(count):\n\t\tfl = False\n\t\twhile count[i] >= 2:\n\t\t\tcount[i] -= 2\n\t\t\tfield[y][x] = i\n\t\t\tfield[n - y - 1][x] = i\n\t\t\ty += 1\n\t\t\tif y == n // 2:\n\t\t\t\tfl = True\n\t\t\t\tbreak\n\t\tif fl:\n\t\t\tbreak\n\t\ti += 1\n\tx = 0\n\ty = n // 2\n\ti = 0\n\twhile i < len(count):\n\t\tfl = False\n\t\twhile count[i] >= 2:\n\t\t\tcount[i] -= 2\n\t\t\tfield[y][x] = i\n\t\t\tfield[y][n - x - 1] = i\n\t\t\tx += 1\n\t\t\tif x == n // 2:\n\t\t\t\tfl = True\n\t\t\t\tbreak\n\t\tif fl:\n\t\t\tbreak\n\t\ti += 1\n\tfor i in range(len(count)):\n\t\tif count[i] == 1:\n\t\t\tfield[n // 2][n // 2] = i\nfl = False\nfor i in range(n):\n\tfor g in range(n):\n\t\tif field[i][g] == -1:\n\t\t\tfl = True\nif fl:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*field[i])\n", "from collections import defaultdict\nn = int(input())\na = list(map(int, input().split()))\nif n == 1:\n\tprint('YES')\n\tprint(a[0])\nelse:\n\td = {}\n\td = defaultdict(lambda : 0, d)\n\tfor i in a:\n\t\td[i] += 1\n\tarr = []\n\tfor i in d.keys():\n\t\tarr.append([d[i], i])\n\tarr = sorted(arr, reverse=True)\n\tarr1 = []\n\tx11 = n // 2\n\tmatrix = [[0 for i in range(n)] for j in range(n)]\n\tfor i in arr:\n\t\tx = i[0] // 4\n\t\tflag = 0\n\t\tif x >= 1:\n\t\t\tfor j in range(n // 2):\n\t\t\t\tfor k in range(n // 2):\n\t\t\t\t\tif matrix[j][k] == 0:\n\t\t\t\t\t\tmatrix[j][k] = i[1]\n\t\t\t\t\t\tmatrix[n - j - 1][k] = i[1]\n\t\t\t\t\t\tmatrix[j][n - k - 1] = i[1]\n\t\t\t\t\t\tmatrix[n - j - 1][n - k - 1] = i[1]\n\t\t\t\t\t\tx -= 1\n\t\t\t\t\tif x == 0:\n\t\t\t\t\t\tbreak\n\t\t\t\tif x == 0:\n\t\t\t\t\tbreak\n\t\ty = i[0] % 4\n\t\tif y + x * 4 > 0:\n\t\t\tarr1.append([y + x * 4, i[1]])\n\tif len(arr1) == 0:\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tprint(matrix[i][j], end=' ')\n\t\t\tprint()\n\telif n % 2 == 0:\n\t\tprint('NO')\n\telif matrix[n // 2 - 1][n // 2 - 1] == 0:\n\t\tprint('NO')\n\telse:\n\t\tarr1 = sorted(arr1, reverse=True)\n\t\tflag = 0\n\t\tstart = -1\n\t\tk = 0\n\t\tex = -1\n\t\tfor i in arr1:\n\t\t\tif i[0] % 2 != 0 and flag == 0:\n\t\t\t\tflag = 1\n\t\t\t\tif i[0] == 1:\n\t\t\t\t\tstart = i[1]\n\t\t\t\t\tex = k\n\t\t\t\telse:\n\t\t\t\t\tstart = i[1]\n\t\t\t\t\ti[0] -= 1\n\t\t\telif i[0] % 2 != 0 and flag == 1:\n\t\t\t\tflag = 2\n\t\t\t\tbreak\n\t\t\tk += 1\n\t\tif ex != -1:\n\t\t\tarr.pop(ex)\n\t\tif flag == 2 or len(arr1) == 0:\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tarr1 = sorted(arr1, reverse=True)\n\t\t\ti = 0\n\t\t\tk = 0\n\t\t\twhile i < n // 2 and len(arr1) > 0:\n\t\t\t\tmatrix[i][n // 2] = arr1[k][1]\n\t\t\t\tmatrix[n - i - 1][n // 2] = arr1[k][1]\n\t\t\t\tarr1[k][0] -= 2\n\t\t\t\tif arr1[k][0] <= 0:\n\t\t\t\t\tk += 1\n\t\t\t\ti += 1\n\t\t\ti = 0\n\t\t\twhile i < n // 2 and len(arr1) > 0:\n\t\t\t\tmatrix[n // 2][i] = arr1[k][1]\n\t\t\t\tmatrix[n // 2][n - i - 1] = arr1[k][1]\n\t\t\t\tarr1[k][0] -= 2\n\t\t\t\tif arr1[k][0] <= 0:\n\t\t\t\t\tk += 1\n\t\t\t\ti += 1\n\t\t\tif i != n // 2:\n\t\t\t\tprint('NO')\n\t\t\telse:\n\t\t\t\tmatrix[n // 2][n // 2] = start\n\t\t\t\tprint('YES')\n\t\t\t\tfor i in range(n):\n\t\t\t\t\tfor j in range(n):\n\t\t\t\t\t\tprint(matrix[i][j], end=' ')\n\t\t\t\t\tprint()\n", "from collections import Counter\nfrom heapq import heappush, heappop\nN = int(input())\ncounter = Counter(map(int, input().split()))\nheap = sorted([(-cnt, n) for (n, cnt) in counter.items()])\nmatrix = [[0] * N for _ in [0] * N]\n\ndef no():\n\tprint('NO')\n\texit()\nif N % 2 == 1:\n\tneed_4 = (N ** 2 - (N - 1) * 2 - 1) // 4\nelse:\n\tneed_4 = N ** 2 // 4\n(y, x) = (0, 0)\nfor _ in [0] * need_4:\n\t(count, n) = heappop(heap)\n\tif count > -4:\n\t\tno()\n\theappush(heap, (count + 4, n))\n\tmatrix[y][x] = matrix[N - y - 1][x] = matrix[y][N - x - 1] = matrix[N - y - 1][N - x - 1] = n\n\tx += 1\n\tif x >= N // 2:\n\t\ty += 1\n\t\tx = 0\nif N % 2 == 1:\n\t(y, x) = (0, N // 2)\n\tfor _ in [0] * (N // 2):\n\t\t(count, n) = heappop(heap)\n\t\tif count > -2:\n\t\t\tno()\n\t\theappush(heap, (count + 2, n))\n\t\tmatrix[y][x] = matrix[N - y - 1][x] = n\n\t\ty += 1\n\t(y, x) = (N // 2, 0)\n\tfor _ in [0] * (N // 2):\n\t\t(count, n) = heappop(heap)\n\t\tif count > -2:\n\t\t\tno()\n\t\theappush(heap, (count + 2, n))\n\t\tmatrix[y][x] = matrix[y][N - x - 1] = n\n\t\tx += 1\n\tmatrix[N // 2][N // 2] = heappop(heap)[1]\nprint('YES')\nfor row in matrix:\n\tprint(*row)\n", "from collections import defaultdict, Counter\nfrom itertools import product, groupby, count, permutations, combinations\nfrom math import pi, sqrt, sin, cos, ceil, floor\nfrom collections import deque\nfrom bisect import bisect, bisect_left, bisect_right\nfrom string import ascii_lowercase\nfrom functools import lru_cache\nimport sys\nsys.setrecursionlimit(1000000000)\nINF = float('inf')\n(YES, Yes, yes, NO, No, no) = ('YES', 'Yes', 'yes', 'NO', 'No', 'no')\n(dy4, dx4) = ([0, 1, 0, -1], [1, 0, -1, 0])\n(dy8, dx8) = ([0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1])\n\ndef inside(y, x, H, W):\n\treturn 0 <= y < H and 0 <= x < W\n\ndef ceil(a, b):\n\treturn (a + b - 1) // b\n\ndef gcd(a, b):\n\tif b == 0:\n\t\treturn a\n\treturn gcd(b, a % b)\n\ndef lcm(a, b):\n\tg = gcd(a, b)\n\treturn a / g * b\n\ndef euclidean_distance(y1, x1, y2, x2):\n\treturn sqrt((y1 - y2) ** 2 + (x1 - x2) ** 2)\n\ndef manhattan_distance(y1, x1, y2, x2):\n\treturn abs(y1 - y2) + abs(x1 - x2)\n\ndef main():\n\tN = int(input())\n\tD = list(map(int, input().split()))\n\t(f, t, o) = ([], [], [])\n\tC = Counter(D)\n\tfor (k, v) in C.items():\n\t\twhile v >= 4:\n\t\t\tv -= 4\n\t\t\tf.append(k)\n\t\twhile v >= 2:\n\t\t\tv -= 2\n\t\t\tt.append(k)\n\t\tif v:\n\t\t\to.append(k)\n\tans = [[0] * N for _ in range(N)]\n\tif N % 2 == 0:\n\t\tif len(t) > 0 or len(o) > 0:\n\t\t\tprint(NO)\n\t\t\treturn\n\t\ti = 0\n\t\tfor y in range(N // 2):\n\t\t\tfor x in range(N // 2):\n\t\t\t\tans[y][x] = ans[-y - 1][x] = ans[y][-x - 1] = ans[-y - 1][-x - 1] = f[i]\n\t\t\t\ti += 1\n\telse:\n\t\tif len(f) < N // 2 * (N // 2) or len(o) != 1:\n\t\t\tprint(NO)\n\t\t\treturn\n\t\ti = 0\n\t\tfor y in range(N // 2):\n\t\t\tfor x in range(N // 2):\n\t\t\t\tans[y][x] = ans[-y - 1][x] = ans[y][-x - 1] = ans[-y - 1][-x - 1] = f[i]\n\t\t\t\ti += 1\n\t\twhile i < len(f):\n\t\t\tt.append(f[i])\n\t\t\tt.append(f[i])\n\t\t\ti += 1\n\t\ti = 0\n\t\tfor k in range(N // 2):\n\t\t\tans[N // 2][k] = ans[N // 2][-k - 1] = t[i]\n\t\t\ti += 1\n\t\tfor k in range(N // 2):\n\t\t\tans[k][N // 2] = ans[-k - 1][N // 2] = t[i]\n\t\t\ti += 1\n\t\tans[N // 2][N // 2] = o[0]\n\tprint(YES)\n\tfor line in ans:\n\t\tprint(*line, sep=' ')\nmain()\n", "n = int(input())\na = list(map(int, input().split()))\nc = [0] * 1001\nfor i in range(len(a)):\n\tc[a[i]] += 1\nsym = 0\nsin = 0\nfor i in range(1001):\n\tsym += c[i] // 4\n\tif c[i] % 2 == 1:\n\t\tsin += 1\nif n % 2 == 0 and sym == n * n // 4:\n\tmat = [[0] * (n // 2) for i in range(n // 2)]\n\tar = []\n\tfor i in range(1001):\n\t\twhile c[i] >= 4:\n\t\t\tar.append(i)\n\t\t\tc[i] -= 4\n\tk = 0\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tmat[i][j] = ar[k]\n\t\t\tk += 1\n\tnewm = [[0] * n for i in range(n)]\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tnewm[i][j] = mat[i][j]\n\t\t\tnewm[n - i - 1][j] = mat[i][j]\n\t\t\tnewm[n - i - 1][n - j - 1] = mat[i][j]\n\t\t\tnewm[i][n - j - 1] = mat[i][j]\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(newm[i][j], end=' ')\n\t\tprint()\nelif n % 2 == 1 and (sym >= n // 2 * (n // 2) and sin == 1):\n\tmat = [[0] * (n // 2) for i in range(n // 2)]\n\tar = []\n\tfor i in range(1001):\n\t\twhile c[i] >= 4:\n\t\t\tar.append(i)\n\t\t\tc[i] -= 4\n\tk = 0\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tmat[i][j] = ar[k]\n\t\t\tar.pop(k)\n\tnewm = [[0] * n for i in range(n)]\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tnewm[i][j] = mat[i][j]\n\t\t\tnewm[n - i - 1][j] = mat[i][j]\n\t\t\tnewm[n - i - 1][n - j - 1] = mat[i][j]\n\t\t\tnewm[i][n - j - 1] = mat[i][j]\n\tna = len(ar)\n\tar2 = []\n\tfor i in range(na):\n\t\tar2.append(ar[i])\n\t\tar2.append(ar[i])\n\tfor i in range(1001):\n\t\twhile c[i] >= 2:\n\t\t\tar2.append(i)\n\t\t\tc[i] -= 2\n\tfor i in range(n // 2):\n\t\tnewm[n // 2][i] = ar2[0]\n\t\tnewm[n // 2][n - i - 1] = ar2[0]\n\t\tar2.pop(0)\n\t\tnewm[i][n // 2] = ar2[0]\n\t\tnewm[n - i - 1][n // 2] = ar2[0]\n\t\tar2.pop(0)\n\tfor i in range(1001):\n\t\tif c[i] == 1:\n\t\t\tnewm[n // 2][n // 2] = i\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(newm[i][j], end=' ')\n\t\tprint()\nelse:\n\tprint('NO')\n", "n = int(input())\na = map(int, input().split())\nd = {}\nfor v in a:\n\td[v] = d.get(v, 0) + 1\nq = []\ne = []\no = []\nfor v in d:\n\tk = d[v]\n\tif k % 2:\n\t\to.append(v)\n\tk //= 2\n\tif k % 2:\n\t\te.append(v)\n\tfor _ in range(k // 2):\n\t\tq.append(v)\nlq = n // 2 * (n // 2)\nif len(o) != n % 2:\n\tprint('NO')\nelif len(q) < lq:\n\tprint('NO')\nelse:\n\tprint('YES')\n\tif n % 2 and len(e) < n - 1:\n\t\te.extend(q[lq:])\n\t\te.extend(q[lq:])\n\tm = []\n\tfor i in range(n):\n\t\tif i < n - 1 - i:\n\t\t\tmi = [0] * n\n\t\t\tfor k in range(n):\n\t\t\t\tif k < n - 1 - k:\n\t\t\t\t\tmi[k] = q.pop(0)\n\t\t\t\telif k == n - 1 - k:\n\t\t\t\t\tmi[k] = e.pop(0)\n\t\t\t\telse:\n\t\t\t\t\tmi[k] = mi[n - 1 - k]\n\t\t\tprint(*mi)\n\t\t\tm.append(mi)\n\t\telif i == n - 1 - i:\n\t\t\tmi = [0] * n\n\t\t\tfor k in range(n):\n\t\t\t\tif k < n - 1 - k:\n\t\t\t\t\tmi[k] = e.pop(0)\n\t\t\t\telif k == n - 1 - k:\n\t\t\t\t\tmi[k] = o.pop(0)\n\t\t\t\telse:\n\t\t\t\t\tmi[k] = mi[n - 1 - k]\n\t\t\tprint(*mi)\n\t\t\tm.append(mi)\n\t\telse:\n\t\t\tprint(*m[n - 1 - i])\n", "n = int(input())\nlst = list(map(int, input().split()))\nd = {}\nfor (i, x) in enumerate(lst):\n\tif d.get(x) == None:\n\t\td[x] = 0\n\td[x] += 1\na = (n // 2) ** 2\namount = 0\narea1 = []\nmat = [[0 for i in range(n)] for i in range(n)]\nhalf = n // 2\nshablon = [[0 for j in range(half)] for i in range(half)]\nfor (i, p) in enumerate(d):\n\tx = d[p]\n\tif x // 4 + amount < a:\n\t\tamount += x // 4\n\t\td[p] = x % 4\n\t\tfor i in range(x // 4):\n\t\t\tarea1.append(p)\n\telse:\n\t\td[p] = x - (a - amount) * 4\n\t\tfor i in range(a - amount):\n\t\t\tarea1.append(p)\n\t\tamount = a\n\t\tbreak\nfrom sys import exit\nif amount != a:\n\tprint('NO')\n\texit()\nfor i in range(half):\n\tfor j in range(half):\n\t\titem = area1[i * half + j]\n\t\tmat[i][j] = item\n\t\tmat[i][n - j - 1] = item\n\t\tmat[n - i - 1][j] = item\n\t\tmat[n - i - 1][n - j - 1] = item\nif n % 2 == 0:\n\tprint('YES')\n\tfor i in range(n):\n\t\tprint(*mat[i])\n\texit()\n(amount, a) = (0, n - 1)\narea2 = []\nfor (i, p) in enumerate(d):\n\tx = d[p]\n\tif x // 2 + amount < a:\n\t\tamount += x // 2\n\t\td[p] = x % 2\n\t\tfor i in range(x // 2):\n\t\t\tarea2.append(p)\n\telse:\n\t\td[p] = x - (a - amount) * 2\n\t\tfor i in range(a - amount):\n\t\t\tarea2.append(p)\n\t\tamount = a\n\t\tbreak\nif amount != a:\n\tprint('NO')\n\texit()\nfor i in range(half):\n\tx = area2[i]\n\tmat[half][i] = x\n\tmat[half][n - i - 1] = x\nq = i + 1\nfor i in range(half):\n\tx = area2[q]\n\tmat[i][half] = x\n\tmat[n - i - 1][half] = x\n\tq += 1\n(amount, a) = (0, 1)\narea3 = 0\nfor (i, p) in enumerate(d):\n\tif d[p] == 1:\n\t\tamount = 1\n\t\tarea3 = p\nif amount != a:\n\tprint('NO')\n\texit()\nmat[half][half] = area3\nprint('YES')\nfor i in range(n):\n\tprint(*mat[i])\n", "from collections import Counter\nn = int(input())\na = list(map(int, input().split()))\nc = Counter(a)\n\ndef printv(v, n, stri=[], mid=[]):\n\tif n % 2 == 0:\n\t\tans = []\n\t\tfor i in range(n // 2):\n\t\t\tstring = []\n\t\t\tfor j in range(n // 2):\n\t\t\t\tstring.append(v[i * (n // 2) + j])\n\t\t\tans.append(string + string[::-1])\n\t\tans = ans + ans[::-1]\n\t\treturn ans\n\telse:\n\t\tans = []\n\t\tfor i in range(n // 2):\n\t\t\tstring = []\n\t\t\tfor j in range(n // 2):\n\t\t\t\tstring.append(v[i * (n // 2) + j])\n\t\t\tans.append(string + [stri[i]] + string[::-1])\n\t\tmiddle = stri[n // 2:] + mid + list(reversed(stri[n // 2:]))\n\t\tans = ans + [middle] + ans[::-1]\n\t\treturn ans\n\ndef solve(n, c):\n\tvals = []\n\tfor key in c:\n\t\tif c[key] >= 4:\n\t\t\tvals.extend([key] * (c[key] // 4))\n\t\t\tc[key] = c[key] % 4\n\tif n % 2 == 1:\n\t\tmid = []\n\t\tstri = []\n\t\tfor key in c:\n\t\t\tif c[key] != 0:\n\t\t\t\tif c[key] >= 2:\n\t\t\t\t\tstri.append(key)\n\t\t\t\t\tc[key] = c[key] % 2\n\t\t\t\tif c[key] == 1:\n\t\t\t\t\tmid.append(key)\n\t\tif len(vals) > (n // 2) ** 2:\n\t\t\tk = 0\n\t\t\tfor i in range(len(vals) - (n // 2) ** 2):\n\t\t\t\tstri.extend([vals[i]] * 2)\n\t\t\t\tk = i\n\t\t\tvals = vals[k + 1:]\n\t\tif len(vals) != (n // 2) ** 2:\n\t\t\treturn (0, 1, 0, 0)\n\t\tif len(stri) != n - 1:\n\t\t\treturn (0, 2, 0, 0)\n\t\tif len(mid) > 1:\n\t\t\treturn (0, 3, 0, 0)\n\t\treturn (2, vals, stri, mid)\n\telif sum(c.values()) > 0:\n\t\treturn (0, 0, 0, 0)\n\telse:\n\t\treturn (1, vals, 0, 0)\n(r, v, s, m) = solve(n, c)\nif r > 0:\n\tprint('YES')\n\tv = printv(v, n, s, m)\n\tfor i in v:\n\t\tfor j in i:\n\t\t\tprint(j, end=' ')\n\t\tprint()\nelse:\n\tprint('NO')\n", "from collections import Counter\n\ndef solve(n, nums):\n\tif n == 1:\n\t\treturn False if len(nums) < 1 else [[nums.pop()]]\n\tcounts = Counter(nums)\n\tif n % 2 == 0:\n\t\tkeys = []\n\t\tfor (num, count) in counts.items():\n\t\t\tif count >= 4 and count % 4 == 0:\n\t\t\t\twhile count > 0:\n\t\t\t\t\tkeys.append(num)\n\t\t\t\t\tcount -= 4\n\t\t\telse:\n\t\t\t\treturn False\n\t\tmatrix = [None for _ in range(n)]\n\t\tfor r in range(0, n // 2):\n\t\t\trow = [0] * n\n\t\t\tfor c in range(0, n // 2):\n\t\t\t\tkey = keys.pop()\n\t\t\t\trow[c] = key\n\t\t\t\trow[n - c - 1] = key\n\t\t\tmatrix[r] = row\n\t\t\tmatrix[n - r - 1] = row\n\t\treturn matrix\n\telse:\n\t\tnum_fours_needed = n // 2 * (n // 2)\n\t\tkeys = []\n\t\tfor (key, count) in counts.items():\n\t\t\twhile count >= 4 and len(keys) < num_fours_needed:\n\t\t\t\tkeys.append(key)\n\t\t\t\tcount -= 4\n\t\t\tcounts[key] = count\n\t\t\tif len(keys) == num_fours_needed:\n\t\t\t\tbreak\n\t\tif len(keys) != num_fours_needed:\n\t\t\treturn False\n\t\tcount_nums = {1: [], 2: []}\n\t\tfor (key, count) in counts.items():\n\t\t\twhile count >= 4:\n\t\t\t\tcount_nums[2].append(key)\n\t\t\t\tcount -= 2\n\t\t\twhile count >= 2:\n\t\t\t\tcount_nums[2].append(key)\n\t\t\t\tcount -= 2\n\t\t\tif count != 0:\n\t\t\t\tcount_nums[count].append(key)\n\t\tmatrix = [[] for _ in range(n)]\n\t\tfor r in range(0, n // 2):\n\t\t\trow = [0] * n\n\t\t\tfor c in range(0, n // 2):\n\t\t\t\tkey = keys.pop()\n\t\t\t\trow[c] = key\n\t\t\t\trow[n - c - 1] = key\n\t\t\tmatrix[r] = row\n\t\t\tmatrix[n - r - 1] = row\n\t\ttry:\n\t\t\trow = [0] * n\n\t\t\tmatrix[n // 2] = row\n\t\t\tfor i in range(0, n // 2):\n\t\t\t\tkey = count_nums[2].pop()\n\t\t\t\trow[i] = key\n\t\t\t\trow[n - i - 1] = key\n\t\t\trow[n // 2] = count_nums[1].pop()\n\t\t\tmid_col = n // 2\n\t\t\tfor i in range(0, n // 2):\n\t\t\t\tkey = count_nums[2].pop()\n\t\t\t\tmatrix[i][mid_col] = key\n\t\t\t\tmatrix[n - i - 1][mid_col] = key\n\t\texcept Exception as E:\n\t\t\treturn False\n\t\treturn matrix\nn = int(input())\nnums = list(map(int, input().split()))\nres = solve(n, nums)\nif res:\n\tprint('YES')\n\tfor r in res:\n\t\tprint(*map(str, r))\nelse:\n\tprint('NO')\n", "from sys import stdin, stdout\nimport operator\n\ndef main():\n\tn = int(stdin.readline())\n\tarr = [int(k) for k in stdin.readline().split()]\n\tfreq = [0] * 1001\n\tfor i in range(n * n):\n\t\tfreq[arr[i]] += 1\n\tmatrix = []\n\tcounter = 0\n\tfor i in range(n):\n\t\tlis = []\n\t\tfor j in range(n):\n\t\t\tlis.append(0)\n\t\tmatrix.append(lis)\n\tfor i in range((n + 1) // 2):\n\t\tfor j in range((n + 1) // 2):\n\t\t\tb1 = (i, j)\n\t\t\tb2 = (i, n - j - 1)\n\t\t\tb3 = (n - i - 1, j)\n\t\t\tb4 = (n - i - 1, n - j - 1)\n\t\t\tif b1[1] == b2[1] and b1[0] == b3[0]:\n\t\t\t\tfor k in range(1, 1001):\n\t\t\t\t\tif freq[k] == 1:\n\t\t\t\t\t\tmatrix[i][j] = k\n\t\t\t\t\t\tbreak\n\t\t\telif b1[1] == b2[1] and b1[0] != b3[0]:\n\t\t\t\tmx = 0\n\t\t\t\tind = -1\n\t\t\t\tfilled = 0\n\t\t\t\tfor k in range(1, 1001):\n\t\t\t\t\tif freq[k] == 2:\n\t\t\t\t\t\tmatrix[i][j] = k\n\t\t\t\t\t\tmatrix[n - i - 1][j] = k\n\t\t\t\t\t\tfreq[k] -= 2\n\t\t\t\t\t\tfilled = 1\n\t\t\t\t\t\tbreak\n\t\t\t\t\tif freq[k] > mx:\n\t\t\t\t\t\tmx = freq[k]\n\t\t\t\t\t\tind = k\n\t\t\t\tif filled == 0:\n\t\t\t\t\tif mx > 2:\n\t\t\t\t\t\tmatrix[i][j] = ind\n\t\t\t\t\t\tmatrix[n - i - 1][j] = ind\n\t\t\t\t\t\tfreq[ind] -= 2\n\t\t\t\t\telse:\n\t\t\t\t\t\tcounter = 1\n\t\t\t\t\t\tbreak\n\t\t\telif b1[0] == b3[0] and b1[1] != b2[1]:\n\t\t\t\tmx = 0\n\t\t\t\tfilled = 0\n\t\t\t\tind = -1\n\t\t\t\tfor k in range(1, 1001):\n\t\t\t\t\tif freq[k] == 2:\n\t\t\t\t\t\tmatrix[i][j] = k\n\t\t\t\t\t\tmatrix[i][n - j - 1] = k\n\t\t\t\t\t\tfreq[k] -= 2\n\t\t\t\t\t\tfilled = 1\n\t\t\t\t\t\tbreak\n\t\t\t\t\tif freq[k] > mx:\n\t\t\t\t\t\tmx = freq[k]\n\t\t\t\t\t\tind = k\n\t\t\t\tif filled == 0:\n\t\t\t\t\tif mx >= 2:\n\t\t\t\t\t\tmatrix[i][j] = ind\n\t\t\t\t\t\tmatrix[i][n - j - 1] = ind\n\t\t\t\t\t\tfreq[ind] -= 2\n\t\t\t\t\telse:\n\t\t\t\t\t\tcounter = 1\n\t\t\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tfilled = 0\n\t\t\t\tfor k in range(1, 1001):\n\t\t\t\t\tif freq[k] >= 4:\n\t\t\t\t\t\tel = k\n\t\t\t\t\t\tmatrix[i][j] = el\n\t\t\t\t\t\tmatrix[i][n - j - 1] = el\n\t\t\t\t\t\tmatrix[n - i - 1][j] = el\n\t\t\t\t\t\tmatrix[n - i - 1][n - j - 1] = el\n\t\t\t\t\t\tfreq[k] -= 4\n\t\t\t\t\t\tfilled = 1\n\t\t\t\t\t\tbreak\n\t\t\t\tif filled == 0:\n\t\t\t\t\tcounter = 1\n\t\t\t\t\tbreak\n\t\t\tif counter == 1:\n\t\t\t\tbreak\n\t\tif counter == 1:\n\t\t\tbreak\n\tif counter == 1:\n\t\tprint('No')\n\telse:\n\t\tprint('Yes')\n\t\tfor i in range(n):\n\t\t\tfor j in range(n):\n\t\t\t\tprint(matrix[i][j], end=' ')\n\t\t\tprint()\nmain()\n", "N = int(input())\ndata = [[0] * N for _ in range(N)]\narr = input()\narr = [int(x) for x in arr.split(' ')]\narr.sort()\nfreq = {}\nfor num in arr:\n\tif num not in freq:\n\t\tfreq[num] = 1\n\telse:\n\t\tfreq[num] += 1\nfor k in range(25):\n\tup = k\n\tdown = N - k - 1\n\tif up > down:\n\t\tbreak\n\tleft_c = k\n\tright_c = N - k - 1\n\twhile up < down:\n\t\trequire = 4\n\t\tr = [up, down]\n\t\tc = [left_c, right_c]\n\t\tfill = -1\n\t\tfor key in freq:\n\t\t\tif freq[key] >= require:\n\t\t\t\tfill = key\n\t\t\t\tfreq[key] -= require\n\t\t\t\tbreak\n\t\tif fill == -1:\n\t\t\tprint('NO')\n\t\t\tquit()\n\t\tfor i in r:\n\t\t\tfor j in c:\n\t\t\t\tdata[i][j] = fill\n\t\tup += 1\n\t\tdown -= 1\n\tup = k\n\tdown = N - k - 1\n\tleft = k + 1\n\tright = N - k - 2\n\twhile left < right:\n\t\trequire = 4\n\t\tr = [up, down]\n\t\tc = [left, right]\n\t\tfill = -1\n\t\tfor key in freq:\n\t\t\tif freq[key] >= require:\n\t\t\t\tfill = key\n\t\t\t\tfreq[key] -= require\n\t\t\t\tbreak\n\t\tif fill == -1:\n\t\t\tprint('NO')\n\t\t\tquit()\n\t\tfor i in r:\n\t\t\tfor j in c:\n\t\t\t\tdata[i][j] = fill\n\t\tleft += 1\n\t\tright -= 1\nif N % 2 == 1:\n\tleft = 0\n\tright = N - 1\n\twhile left < right:\n\t\tfill = -1\n\t\tc = [left, right]\n\t\tr = [(N - 1) // 2]\n\t\trequire = 2\n\t\tfor key in freq:\n\t\t\tif freq[key] >= require:\n\t\t\t\tfill = key\n\t\t\t\tfreq[key] -= require\n\t\t\t\tbreak\n\t\tif fill == -1:\n\t\t\tprint('NO')\n\t\t\tquit()\n\t\tfor i in r:\n\t\t\tfor j in c:\n\t\t\t\tdata[i][j] = fill\n\t\tleft += 1\n\t\tright -= 1\n\tup = 0\n\tdown = N - 1\n\twhile up < down:\n\t\tfill = -1\n\t\tr = [up, down]\n\t\tc = [(N - 1) // 2]\n\t\trequire = 2\n\t\tfor key in freq:\n\t\t\tif freq[key] >= require:\n\t\t\t\tfill = key\n\t\t\t\tfreq[key] -= require\n\t\t\t\tbreak\n\t\tif fill == -1:\n\t\t\tprint('NO')\n\t\t\tquit()\n\t\tfor i in r:\n\t\t\tfor j in c:\n\t\t\t\tdata[i][j] = fill\n\t\tup += 1\n\t\tdown -= 1\n\tfill = -1\n\trequire = 1\n\tfor key in freq:\n\t\tif freq[key] >= require:\n\t\t\tfill = key\n\t\t\tfreq[key] -= require\n\t\t\tbreak\n\tif fill == -1:\n\t\tprint('NO')\n\t\tquit()\n\tdata[(N - 1) // 2][(N - 1) // 2] = fill\nprint('YES')\nfor i in range(N):\n\tfor j in range(N):\n\t\tif j == N - 1:\n\t\t\tprint(data[i][j])\n\t\telse:\n\t\t\tprint(data[i][j], end=' ')\n", "import time\nn = int(input())\na = [int(i) for i in input().split()]\nstart = time.time()\nd = {i: 0 for i in set(a)}\nfor i in a:\n\td[i] += 1\nq = []\nl = []\nc = []\nans = 'YES'\nfor i in d.keys():\n\twhile d[i] >= 4:\n\t\tq.append(i)\n\t\td[i] -= 4\n\tif d[i] >= 2:\n\t\tl.append(i)\n\t\td[i] -= 2\n\tif d[i] >= 1:\n\t\tc.append(i)\n\t\td[i] -= 0\nif n % 2 == 0 and (len(l) > 0 or len(c) > 0) or (n % 2 == 1 and (len(c) != 1 or len(l) > n - 1)):\n\tprint('NO')\nelse:\n\th = len(q) - 1\n\twhile n % 2 == 1 and len(l) < n - 1:\n\t\tl.append(q[h])\n\t\tl.append(q[h])\n\t\th -= 1\n\tm = [[0 for i in range(n)] for j in range(n)]\n\tnow = 0\n\tnowl = 0\n\tif n % 2 == 1:\n\t\tm[n // 2][n // 2] = c[0]\n\tfor i in range(n // 2):\n\t\tfor j in range(n // 2):\n\t\t\tm[i][j] = q[now]\n\t\t\tm[i][n - j - 1] = q[now]\n\t\t\tm[n - i - 1][j] = q[now]\n\t\t\tm[n - i - 1][n - j - 1] = q[now]\n\t\t\tnow += 1\n\t\tif n % 2 == 1:\n\t\t\tm[n // 2][i] = l[nowl]\n\t\t\tm[n // 2][n - i - 1] = l[nowl]\n\t\t\tnowl += 1\n\t\t\tm[i][n // 2] = l[nowl]\n\t\t\tm[n - i - 1][n // 2] = l[nowl]\n\t\t\tnowl += 1\n\tprint('YES')\n\tfor i in range(n):\n\t\tfor j in range(n):\n\t\t\tprint(m[i][j], end=' ')\n\t\tprint()\nfinish = time.time()\n", "import os\nimport sys\nfrom math import *\nfrom collections import *\nfrom heapq import *\nfrom bisect import *\nfrom io import BytesIO, IOBase\n\ndef vsInput():\n\tsys.stdin = open('input.txt', 'r')\n\tsys.stdout = open('output.txt', 'w')\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nALPHA = 'abcdefghijklmnopqrstuvwxyz/'\nM = 1000000007\nEPS = 1e-06\n\ndef Ceil(a, b):\n\treturn a // b + int(a % b > 0)\n\ndef value():\n\treturn tuple(map(int, input().split()))\n\ndef array():\n\treturn [int(i) for i in input().split()]\n\ndef Int():\n\treturn int(input())\n\ndef Str():\n\treturn input()\n\ndef arrayS():\n\treturn [i for i in input().split()]\nn = Int()\na = array()\nC = Counter(a)\nhave = [(-C[i], i) for i in C]\nheapify(have)\nans = [[-1 for i in range(n)] for j in range(n)]\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tele = heappop(have)\n\t\theappush(have, (ele[0] + 4, ele[1]))\n\t\tans[i][j] = ele[1]\n\t\tans[n - i - 1][j] = ele[1]\n\t\tans[i][n - j - 1] = ele[1]\n\t\tans[n - i - 1][n - j - 1] = ele[1]\nif n % 2:\n\tr = n // 2\n\tc = n // 2\n\tfor i in range(n // 2):\n\t\tele = heappop(have)\n\t\theappush(have, (ele[0] + 2, ele[1]))\n\t\tans[r][i] = ele[1]\n\t\tans[r][n - i - 1] = ele[1]\n\t\tele = heappop(have)\n\t\theappush(have, (ele[0] + 2, ele[1]))\n\t\tans[i][c] = ele[1]\n\t\tans[n - i - 1][c] = ele[1]\n\tele = heappop(have)\n\theappush(have, (ele[0] + 1, ele[1]))\n\tans[r][c] = ele[1]\nfor (c, _) in have:\n\tif c:\n\t\tprint('NO')\n\t\texit()\nprint('YES')\nfor i in ans:\n\tprint(*i)\n", "from collections import Counter\nimport math\nn = int(input())\na = [int(x) for x in input().split()]\n\ndef unfold_matrix(mat, odd=False):\n\tif not odd:\n\t\tfor row in mat:\n\t\t\trow += list(reversed(row))\n\t\tmat += list(reversed(mat))\n\telse:\n\t\tfor row in mat:\n\t\t\trow += list(reversed(row[:-1]))\n\t\tmat += list(reversed(mat[:-1]))\nfreq = Counter(a)\nkeys = freq.keys()\nif n % 2 == 0:\n\tkeys4 = []\n\tfor key in keys:\n\t\twhile freq[key] >= 4:\n\t\t\tkeys4.append(key)\n\t\t\tfreq[key] -= 4\n\t\tif freq[key] > 0:\n\t\t\tprint('No')\n\t\t\texit()\n\tsize = n // 2\n\tmat = [keys4[size * i:size * (i + 1)] for i in range(size)]\n\tunfold_matrix(mat)\nelse:\n\tkeys4 = []\n\tkeys2 = []\n\tkeys1 = []\n\tcount4 = 0\n\tcount2 = 0\n\tmx4 = ((n - 1) // 2) ** 2\n\tmx2 = n - 1\n\tfor key in keys:\n\t\twhile freq[key] >= 4 and count4 < mx4:\n\t\t\tkeys4.append(key)\n\t\t\tfreq[key] -= 4\n\t\t\tcount4 += 1\n\t\twhile freq[key] >= 2 and count2 < mx2:\n\t\t\tkeys2.append(key)\n\t\t\tfreq[key] -= 2\n\t\t\tcount2 += 1\n\t\tif freq[key] == 1:\n\t\t\tkeys1.append(key)\n\t\telif freq[key] > 1:\n\t\t\tprint('No')\n\t\t\texit()\n\tif len(keys2) != n - 1:\n\t\tprint('No')\n\t\texit()\n\tif len(keys1) != 1:\n\t\tprint('No')\n\t\texit()\n\tsize = n // 2\n\tmat = [keys4[size * i:size * (i + 1)] + [keys2[i]] for i in range(size)]\n\tmat += [keys2[size:] + keys1]\n\tunfold_matrix(mat, True)\nprint('Yes')\nfor row in mat:\n\tfor elem in row:\n\t\tprint(elem, end=' ')\n\tprint()\n", "from collections import Counter\nfrom itertools import product\nn = int(input())\nA = [int(i) for i in input().split()]\nC = list((list(t) for t in Counter(A).items()))\nans = [[0] * n for i in range(n)]\nm = n // 2\n\ndef no(cur):\n\tif cur >= len(C):\n\t\tprint('NO')\n\t\texit()\ncur = 0\nfor i in range(m):\n\tfor j in range(m):\n\t\twhile C[cur][1] < 4:\n\t\t\tcur += 1\n\t\t\tno(cur)\n\t\tC[cur][1] -= 4\n\t\tfor (k, l) in product((i, n - i - 1), (j, n - j - 1)):\n\t\t\tans[k][l] = C[cur][0]\nif n % 2 == 1:\n\tfor (k, v) in C:\n\t\tif v % 2 == 1:\n\t\t\tans[m][m] = k\n\tcur = 0\n\tfor i in range(m):\n\t\twhile C[cur][1] < 2:\n\t\t\tcur += 1\n\t\t\tno(cur)\n\t\tC[cur][1] -= 2\n\t\tans[i][m] = C[cur][0]\n\t\tans[n - i - 1][m] = C[cur][0]\n\t\twhile C[cur][1] < 2:\n\t\t\tcur += 1\n\t\t\tno(cur)\n\t\tC[cur][1] -= 2\n\t\tans[m][i] = C[cur][0]\n\t\tans[m][n - i - 1] = C[cur][0]\nprint('YES')\nfor a in ans:\n\tprint(*a)\n", "from __future__ import division, print_function\nfrom fractions import Fraction\nimport sys\nimport os\nfrom io import BytesIO, IOBase\nfrom itertools import *\nimport bisect\nfrom heapq import *\nfrom math import *\nfrom copy import *\nfrom collections import deque\nfrom collections import Counter as counter\nfrom itertools import combinations as comb\nfrom itertools import permutations as permutate\nfrom bisect import bisect_left as bl\nfrom bisect import bisect_right as br\nfrom bisect import bisect\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n\ndef print(*args, **kwargs):\n\t(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))\n\tat_start = True\n\tfor x in args:\n\t\tif not at_start:\n\t\t\tfile.write(sep)\n\t\tfile.write(str(x))\n\t\tat_start = False\n\tfile.write(kwargs.pop('end', '\\n'))\n\tif kwargs.pop('flush', False):\n\t\tfile.flush()\nif sys.version_info[0] < 3:\n\t(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))\nelse:\n\t(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\nfrom types import GeneratorType\n\ndef iterative(f, stack=[]):\n\n\tdef wrapped_func(*args, **kwargs):\n\t\tif stack:\n\t\t\treturn f(*args, **kwargs)\n\t\tto = f(*args, **kwargs)\n\t\twhile True:\n\t\t\tif type(to) is GeneratorType:\n\t\t\t\tstack.append(to)\n\t\t\t\tto = next(to)\n\t\t\t\tcontinue\n\t\t\tstack.pop()\n\t\t\tif not stack:\n\t\t\t\tbreak\n\t\t\tto = stack[-1].send(to)\n\t\treturn to\n\treturn wrapped_func\nmod = 1000000007\n\ndef inp():\n\treturn sys.stdin.readline().rstrip('\\r\\n')\n\ndef out(var):\n\tsys.stdout.write(str(var))\n\ndef lis():\n\treturn list(map(int, inp().split()))\n\ndef stringlis():\n\treturn list(map(str, inp().split()))\n\ndef sep():\n\treturn map(int, inp().split())\n\ndef strsep():\n\treturn map(str, inp().split())\n\ndef zerolist(n):\n\treturn [0] * n\n\ndef nextline():\n\tout('\\n')\n\ndef testcase(t):\n\tfor p in range(t):\n\t\tsolve()\n\ndef printlist(a):\n\tfor p in range(0, len(a)):\n\t\tout(str(a[p]) + ' ')\n\ndef lcm(a, b):\n\treturn a * b // gcd(a, b)\n\ndef power(a, b):\n\tans = 1\n\twhile b > 0:\n\t\tif b % 2 == 1:\n\t\t\tans *= a\n\t\ta *= a\n\t\tb //= 2\n\treturn ans\n\ndef ncr(n, r):\n\treturn factorial(n) // (factorial(r) * factorial(max(n - r, 1)))\n\ndef isPrime(n):\n\tif n <= 1:\n\t\treturn False\n\tif n <= 3:\n\t\treturn True\n\tif n % 2 == 0 or n % 3 == 0:\n\t\treturn False\n\ti = 5\n\twhile i * i <= n:\n\t\tif n % i == 0 or n % (i + 2) == 0:\n\t\t\treturn False\n\t\ti = i + 6\n\treturn True\n\ndef solve():\n\tn = int(inp())\n\ta = lis()\n\tcnt = {}\n\tfor i in range(n * n):\n\t\tif a[i] not in cnt:\n\t\t\tcnt[a[i]] = 1\n\t\telse:\n\t\t\tcnt[a[i]] += 1\n\tif n % 2 == 0:\n\t\tfor i in cnt:\n\t\t\tif cnt[i] % 4 != 0:\n\t\t\t\tprint('NO')\n\t\t\t\treturn\n\t\tgrid = [[0] * n for i in range(n)]\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tfor ii in cnt:\n\t\t\t\t\tif cnt[ii] != 0:\n\t\t\t\t\t\tgrid[i][j] = ii\n\t\t\t\t\t\tgrid[i][n - j - 1] = ii\n\t\t\t\t\t\tgrid[n - i - 1][j] = ii\n\t\t\t\t\t\tgrid[n - i - 1][n - j - 1] = ii\n\t\t\t\t\t\tcnt[ii] -= 4\n\t\t\t\t\t\tbreak\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tprint(*grid[i])\n\telse:\n\t\tbasic = []\n\t\ttemp = []\n\t\tfor i in cnt:\n\t\t\tif cnt[i] % 4 == 0 or cnt[i] > 4:\n\t\t\t\tfor j in range(cnt[i] // 4):\n\t\t\t\t\tbasic.append(i)\n\t\t\t\tcnt[i] = cnt[i] % 4\n\t\ttwos = []\n\t\tif len(basic) > ((n - 1) // 2) ** 2:\n\t\t\tind = ((n - 1) // 2) ** 2\n\t\t\tfor i in range(ind, len(basic)):\n\t\t\t\ttwos.append(basic[i])\n\t\t\t\ttwos.append(basic[i])\n\t\tchck = 0\n\t\tfor i in cnt:\n\t\t\tif cnt[i] % 2 == 0 or cnt[i] > 2:\n\t\t\t\tfor j in range(cnt[i] // 2):\n\t\t\t\t\ttwos.append(i)\n\t\t\t\tcnt[i] = cnt[i] % 2\n\t\t\tif cnt[i] % 2 == 1:\n\t\t\t\tchck += 1\n\t\tif chck != 1 or len(twos) != n - 1:\n\t\t\tprint('NO')\n\t\t\treturn\n\t\tcurr = 0\n\t\tgrid = [[0] * n for i in range(n)]\n\t\tfor i in range((n - 1) // 2):\n\t\t\tfor j in range((n - 1) // 2):\n\t\t\t\tgrid[i][j] = basic[curr]\n\t\t\t\tgrid[i][n - j - 1] = basic[curr]\n\t\t\t\tgrid[n - i - 1][j] = basic[curr]\n\t\t\t\tgrid[n - i - 1][n - j - 1] = basic[curr]\n\t\t\t\tcurr += 1\n\t\tcurr = 0\n\t\tfor i in range(n // 2):\n\t\t\tgrid[n // 2][i] = twos[curr]\n\t\t\tgrid[n // 2][n - i - 1] = twos[curr]\n\t\t\tcurr += 1\n\t\tfor i in range(n // 2):\n\t\t\tgrid[i][n // 2] = twos[curr]\n\t\t\tgrid[n - i - 1][n // 2] = twos[curr]\n\t\t\tcurr += 1\n\t\tfor i in cnt:\n\t\t\tif cnt[i] == 1:\n\t\t\t\tgrid[n // 2][n // 2] = i\n\t\t\t\tbreak\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tprint(*grid[i])\ntestcase(1)\n", "from os import path\nimport sys\nfrom functools import cmp_to_key as ctk\nfrom collections import deque, defaultdict as dd\nfrom bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right\nfrom itertools import permutations\nfrom datetime import datetime\nfrom math import ceil, sqrt, log, gcd\n\ndef ii():\n\treturn int(input())\n\ndef si():\n\treturn input()\n\ndef mi():\n\treturn map(int, input().split())\n\ndef li():\n\treturn list(mi())\nabc = 'abcdefghijklmnopqrstuvwxyz'\nabd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}\nmod = 1000000007\ninf = float('inf')\nvow = ['a', 'e', 'i', 'o', 'u']\n(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])\n\ndef bo(i):\n\treturn ord(i) - ord('a')\n\ndef solve():\n\tn = ii()\n\ta = li()\n\tx = [[0] * n for i in range(n)]\n\ta.sort()\n\tm = dd(int)\n\tfor i in a:\n\t\tm[i] += 1\n\tif n % 2 == 0:\n\t\t(r, c) = (0, 0)\n\t\tfor i in m.keys():\n\t\t\tif m[i] % 4:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\t\t\twhile m[i]:\n\t\t\t\t(x[r][c], x[r][n - c - 1], x[n - r - 1][c], x[n - r - 1][n - c - 1]) = (i, i, i, i)\n\t\t\t\tm[i] -= 4\n\t\t\t\tc += 1\n\t\t\t\tif c == n // 2:\n\t\t\t\t\tr += 1\n\t\t\t\t\tc = 0\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tprint(*x[i])\n\telse:\n\t\t(r, c) = (0, 0)\n\t\tfor i in m.keys():\n\t\t\twhile m[i] >= 4:\n\t\t\t\tif r == n // 2:\n\t\t\t\t\tbreak\n\t\t\t\t(x[r][c], x[r][n - c - 1], x[n - r - 1][c], x[n - r - 1][n - c - 1]) = (i, i, i, i)\n\t\t\t\tm[i] -= 4\n\t\t\t\tc += 1\n\t\t\t\tif c == n // 2:\n\t\t\t\t\tr += 1\n\t\t\t\t\tc = 0\n\t\t(r, c) = (0, 0)\n\t\tfor i in m.keys():\n\t\t\twhile m[i] >= 2:\n\t\t\t\tif r == n // 2:\n\t\t\t\t\tbreak\n\t\t\t\t(x[r][n // 2], x[n - r - 1][n // 2]) = (i, i)\n\t\t\t\tm[i] -= 2\n\t\t\t\tr += 1\n\t\tfor i in m.keys():\n\t\t\twhile m[i] >= 2:\n\t\t\t\tif c == n // 2:\n\t\t\t\t\tbreak\n\t\t\t\t(x[n // 2][c], x[n // 2][n - c - 1]) = (i, i)\n\t\t\t\tm[i] -= 2\n\t\t\t\tc += 1\n\t\tfor i in m.keys():\n\t\t\tif m[i] > 0:\n\t\t\t\tx[n // 2][n // 2] = i\n\t\tfor i in x:\n\t\t\tif 0 in i:\n\t\t\t\tprint('NO')\n\t\t\t\texit(0)\n\t\tprint('YES')\n\t\tfor i in range(n):\n\t\t\tprint(*x[i])\nsolve()\n", "n = int(input())\nnum = [0 for j in range(1000)]\na = [int(j) for j in input().split()]\nvar = 0\narr = [[0 for j in range(n)] for j in range(n)]\ncount = 0\nt = 0\ncount1 = 0\ncount2 = 0\ncount3 = 0\ncount4 = 0\nfor i in range(n * n):\n\tnum[a[i] - 1] += 1\nif n % 2 == 0:\n\ti = 0\n\tfor x in num:\n\t\tif x % 4 != 0:\n\t\t\tvar = 1\n\t\t\tbreak\n\tif var == 1:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\tfor x in range(n // 2):\n\t\t\tfor y in range(n // 2):\n\t\t\t\tif num[i] == 0:\n\t\t\t\t\twhile num[i] == 0:\n\t\t\t\t\t\ti += 1\n\t\t\t\tarr[x][y] = i + 1\n\t\t\t\tarr[n - x - 1][y] = i + 1\n\t\t\t\tarr[x][n - y - 1] = i + 1\n\t\t\t\tarr[n - x - 1][n - y - 1] = i + 1\n\t\t\t\tnum[i] -= 4\n\t\tfor x in arr:\n\t\t\tprint(*x)\nelse:\n\tfor x in num:\n\t\tif x % 4 == 0:\n\t\t\tcount4 += 1\n\t\telif x % 4 == 2:\n\t\t\tcount2 += 1\n\t\telif x % 4 == 1:\n\t\t\tcount1 += 1\n\t\telif x % 4 == 3:\n\t\t\tcount2 += 1\n\t\t\tcount1 += 1\n\tif count2 > 2 * (n // 2) or count1 > 1:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n\t\ti = 0\n\t\tfor x in range(n // 2):\n\t\t\tfor y in range(n // 2):\n\t\t\t\tif num[i] < 4:\n\t\t\t\t\twhile num[i] < 4:\n\t\t\t\t\t\ti += 1\n\t\t\t\tarr[x][y] = i + 1\n\t\t\t\tarr[n - x - 1][y] = i + 1\n\t\t\t\tarr[x][n - y - 1] = i + 1\n\t\t\t\tarr[n - x - 1][n - y - 1] = i + 1\n\t\t\t\tnum[i] -= 4\n\t\ti = 0\n\t\tfor x in range(n // 2):\n\t\t\ty = n // 2\n\t\t\tif num[i] < 2:\n\t\t\t\twhile num[i] < 2:\n\t\t\t\t\ti += 1\n\t\t\tarr[x][y] = i + 1\n\t\t\tarr[n - x - 1][y] = i + 1\n\t\t\tnum[i] -= 2\n\t\ti = 0\n\t\tfor y in range(n // 2):\n\t\t\tx = n // 2\n\t\t\tif num[i] < 2:\n\t\t\t\twhile num[i] < 2:\n\t\t\t\t\ti += 1\n\t\t\tarr[x][y] = i + 1\n\t\t\tarr[x][n - y - 1] = i + 1\n\t\t\tnum[i] -= 2\n\t\tfor i in range(1000):\n\t\t\tif num[i] == 1:\n\t\t\t\tarr[n // 2][n // 2] = i + 1\n\t\tfor x in arr:\n\t\t\tprint(*x)\n", "def main():\n\tn = int(input())\n\tcorner = []\n\tmiddle = []\n\tcentre = []\n\tarr = input().split()\n\tcat = []\n\tfor x in range(n * n):\n\t\thold = int(arr[x])\n\t\tbo = False\n\t\tfor y in range(len(cat)):\n\t\t\tif hold == cat[y][0]:\n\t\t\t\tcat[y][1] += 1\n\t\t\t\tbo = True\n\t\t\t\tbreak\n\t\tif not bo:\n\t\t\tcat.append([hold, 1])\n\tif n % 2 == 1:\n\t\tfor x in range(len(cat)):\n\t\t\tif cat[x][1] % 2 == 1:\n\t\t\t\tcentre.append(cat[x][0])\n\t\t\t\tcat[x][1] -= 1\n\t\tif len(centre) != 1:\n\t\t\tprint('NO')\n\t\t\treturn 0\n\t\tfor x in range(len(cat)):\n\t\t\tif cat[x][1] % 4 == 2:\n\t\t\t\tmiddle.append(cat[x][0])\n\t\t\t\tcat[x][1] -= 2\n\t\tif len(middle) > n // 2 * 2:\n\t\t\tprint('NO')\n\t\t\treturn 0\n\telse:\n\t\tfor x in range(len(cat)):\n\t\t\tif cat[x][1] % 4 != 0:\n\t\t\t\tprint('NO')\n\t\t\t\treturn 0\n\tprint('YES')\n\tx = 0\n\twhile len(corner) < n // 2 * (n // 2) and x < len(cat):\n\t\tif cat[x][1] > 0:\n\t\t\tcat[x][1] -= 4\n\t\t\tcorner.append(cat[x][0])\n\t\telse:\n\t\t\tx += 1\n\twhile x < len(cat):\n\t\tif cat[x][1] > 0:\n\t\t\tcat[x][1] -= 2\n\t\t\tmiddle.append(cat[x][0])\n\t\telse:\n\t\t\tx += 1\n\tarr = [[0 for x in range(n)] for y in range(n)]\n\tif n % 2 == 1:\n\t\tarr[n // 2][n // 2] = centre[0]\n\t\tfor x in range(n // 2):\n\t\t\tarr[n // 2][x] = middle[x]\n\t\t\tarr[n // 2][n - 1 - x] = middle[x]\n\t\t\tarr[x][n // 2] = middle[n - 2 - x]\n\t\t\tarr[n - 1 - x][n // 2] = middle[n - 2 - x]\n\tx = 0\n\tfor y in range(n // 2):\n\t\tfor z in range(n // 2):\n\t\t\tarr[y][z] = corner[x]\n\t\t\tarr[y][n - 1 - z] = corner[x]\n\t\t\tarr[n - 1 - y][z] = corner[x]\n\t\t\tarr[n - 1 - y][n - 1 - z] = corner[x]\n\t\t\tx += 1\n\tfor x in range(n):\n\t\tfor y in range(n - 1):\n\t\t\tprint(arr[x][y], end=' ')\n\t\tprint(arr[x][n - 1])\nmain()\n", "import sys\nread = sys.stdin.buffer.read\ninput = sys.stdin.buffer.readline\nreadlines = sys.stdin.buffer.readlines\nt = int(input())\nb = sorted(list(map(int, input().split())))\nc = dict()\nif t % 2 == 0:\n\tfor j in b:\n\t\tif j in c.keys():\n\t\t\tc[j] += 1\n\t\telse:\n\t\t\tc[j] = 1\n\tk = list(set(b))\n\tf = 0\n\tfor j in k:\n\t\tif c[j] % 4 != 0:\n\t\t\tf = 1\n\t\t\tbreak\n\tif f == 1:\n\t\tprint('No')\n\telse:\n\t\ta = []\n\t\tfor j in range(t):\n\t\t\tp = [0] * t\n\t\t\ta.append(p)\n\t\tl = 0\n\t\tfor j in range(t // 2):\n\t\t\tfor p in range(t // 2):\n\t\t\t\tq = b[l]\n\t\t\t\ta[j][p] = q\n\t\t\t\ta[t - 1 - j][p] = q\n\t\t\t\ta[j][t - p - 1] = q\n\t\t\t\ta[t - 1 - j][t - 1 - p] = q\n\t\t\t\tl += 4\n\t\tprint('YES')\n\t\tfor j in a:\n\t\t\tprint(*j)\nelse:\n\tfor j in b:\n\t\tif j in c.keys():\n\t\t\tc[j] += 1\n\t\telse:\n\t\t\tc[j] = 1\n\tk = list(set(b))\n\tf = 0\n\td = []\n\th = 0\n\tr = (t - 1) * (t - 1)\n\tfor j in k:\n\t\tif c[j] // 4 >= 1:\n\t\t\tm = c[j] - c[j] % 4\n\t\t\th += m\n\t\t\tfor p in range(min(r, m)):\n\t\t\t\td.append(j)\n\t\t\tif len(d) >= r:\n\t\t\t\tbreak\n\tif h < r:\n\t\tprint('No')\n\telse:\n\t\ta = []\n\t\tfor j in range(t):\n\t\t\tp = [0] * t\n\t\t\ta.append(p)\n\t\tl = 0\n\t\tfor j in range(t // 2):\n\t\t\tfor p in range(t // 2):\n\t\t\t\tq = d[l]\n\t\t\t\ta[j][p] = q\n\t\t\t\ta[t - 1 - j][p] = q\n\t\t\t\ta[j][t - p - 1] = q\n\t\t\t\ta[t - 1 - j][t - 1 - p] = q\n\t\t\t\tc[d[l]] += -4\n\t\t\t\tl += 4\n\t\td = []\n\t\tg = t // 2\n\t\tr = (t - 1) * 2\n\t\th = 0\n\t\tfor j in k:\n\t\t\tif c[j] // 2 > 0:\n\t\t\t\tm = c[j] - c[j] % 2\n\t\t\t\th += m\n\t\t\t\tfor p in range(min(r, m)):\n\t\t\t\t\td.append(j)\n\t\t\t\tif len(d) >= r:\n\t\t\t\t\tbreak\n\t\tif h < r:\n\t\t\tprint('No')\n\t\telse:\n\t\t\tl = 0\n\t\t\tfor j in range(t // 2):\n\t\t\t\tq = d[l]\n\t\t\t\ta[j][t // 2] = q\n\t\t\t\ta[t - 1 - j][t // 2] = q\n\t\t\t\tc[d[l]] += -2\n\t\t\t\tl += 2\n\t\t\t\tq = d[l]\n\t\t\t\ta[t // 2][j] = q\n\t\t\t\ta[t // 2][t - 1 - j] = q\n\t\t\t\tc[d[l]] += -2\n\t\t\t\tl += 2\n\t\t\tf = 0\n\t\t\tfor j in k:\n\t\t\t\tif c[j] > 0:\n\t\t\t\t\ta[t // 2][t // 2] = j\n\t\t\t\t\tf = 1\n\t\t\t\t\tbreak\n\t\t\tif f == 0:\n\t\t\t\tprint('No')\n\t\t\telse:\n\t\t\t\tprint('Yes')\n\t\t\t\tfor j in a:\n\t\t\t\t\tprint(*j)\n", "n = int(input())\nnumbers = list(map(int, input().split()))\nnum = [0] * 1001\nmatrix = [[0 for i in range(n)] for j in range(n)]\nnumbers_set = set()\nfor i in numbers:\n\tnumbers_set.add(i)\n\tnum[i] += 1\nones = 0\none = -1\ntwos = 0\nfor i in num:\n\tif i % 2 == 1:\n\t\tones += 1\n\t\tone = num.index(i)\n\telif i % 4 == 2:\n\t\ttwos += 1\nif n % 2 == 0:\n\tif ones or twos:\n\t\tprint('NO')\n\t\texit()\nelif ones != 1 or twos >= n:\n\tprint('NO')\n\texit()\niterate = sorted(list(numbers_set))\ncurrent_num = iterate[0]\ncurrent_pos = 0\nfor i in range(n // 2):\n\tfor j in range(n // 2):\n\t\tif num[current_num] < 4:\n\t\t\twhile num[current_num] < 4:\n\t\t\t\tcurrent_pos += 1\n\t\t\t\tcurrent_num = iterate[current_pos]\n\t\tmatrix[i][j] = matrix[i][n - j - 1] = matrix[n - i - 1][j] = matrix[n - i - 1][n - j - 1] = current_num\n\t\tnum[current_num] -= 4\nif n % 2 == 1:\n\tcurrent_num = iterate[0]\n\tcurrent_pos = 0\n\tnum[one] -= 1\n\tmatrix[n // 2][n // 2] = one\n\tfor i in range(n // 2):\n\t\tif num[current_num] < 2:\n\t\t\twhile num[current_num] < 2:\n\t\t\t\tcurrent_pos += 1\n\t\t\t\tcurrent_num = iterate[current_pos]\n\t\tmatrix[i][n // 2] = matrix[n - i - 1][n // 2] = current_num\n\t\tnum[current_num] -= 2\n\t\tif num[current_num] < 2:\n\t\t\twhile num[current_num] < 2:\n\t\t\t\tcurrent_pos += 1\n\t\t\t\tcurrent_num = iterate[current_pos]\n\t\tmatrix[n // 2][i] = matrix[n // 2][n - i - 1] = current_num\n\t\tnum[current_num] -= 2\nprint('YES')\nfor i in range(n):\n\tfor j in range(n):\n\t\tprint(matrix[i][j], end=' ')\n\tprint()\n", "(n, d, remainders) = (int(input()), {}, [0] * 4)\n(arr, matrix) = (sorted([int(x) for x in input().split()]), [[0] * n for _ in range(n)])\nfor number in arr:\n\td[number] = d.get(number, 0) + 1\nfor i in d:\n\tremainders[d[i] % 4] += 1\nif not n % 2 and remainders[0] != sum(remainders) or (n % 2 and remainders[1] + remainders[3] > 1 or remainders[2] > n - 1):\n\tprint('NO')\nelse:\n\t(i, j, x, y) = (0, 0, 0, 0)\n\tif not n % 2:\n\t\tfor element in arr[::4]:\n\t\t\t(matrix[i][j], matrix[i][n - j - 1], matrix[n - i - 1][n - j - 1], matrix[n - i - 1][j]) = 4 * [element]\n\t\t\tif i < n // 2 - 1:\n\t\t\t\ti += 1\n\t\t\telse:\n\t\t\t\t(i, j) = (0, j + 1)\n\telse:\n\t\tarr.sort(key=lambda x: d[x] % 2 == 0)\n\t\t(matrix[n // 2][n // 2], d[arr[0]]) = (arr[0], d[arr[0]] - 1)\n\t\tfor element in arr:\n\t\t\tif d[element] and d[element] % 4 == 0 and (not (i == n // 2 or j == n // 2)):\n\t\t\t\td[element] -= 4\n\t\t\t\t(matrix[i][j], matrix[i][n - j - 1], matrix[n - i - 1][n - j - 1], matrix[n - i - 1][j]) = 4 * [element]\n\t\t\t\tif i < n // 2 - 1:\n\t\t\t\t\ti += 1\n\t\t\t\telse:\n\t\t\t\t\t(i, j) = (0, j + 1)\n\t\t\telif d[element]:\n\t\t\t\td[element] -= 2\n\t\t\t\tif x < y:\n\t\t\t\t\t(matrix[x][n // 2], matrix[-x - 1][n // 2], x) = (element, element, x + 1)\n\t\t\t\telse:\n\t\t\t\t\t(matrix[n // 2][-y - 1], matrix[n // 2][y], y) = (element, element, y + 1)\n\tprint('YES')\n\tfor i in matrix:\n\t\tprint(*i)\n", "from collections import Counter\n\ndef is_possible(n, counter):\n\tdup_4_arr = []\n\tdup_2_arr = []\n\tonly_one_value = None\n\tif n & 1 == 0:\n\t\tfor v in counter.values():\n\t\t\tif v % 4 != 0:\n\t\t\t\treturn (False, dup_4_arr, dup_2_arr, only_one_value)\n\telse:\n\t\tcount_4 = 0\n\t\tmin_count_4 = (n // 2) ** 2\n\t\tfor (k, v) in counter.items():\n\t\t\twhile v >= 4:\n\t\t\t\tv = v - 4\n\t\t\t\tcount_4 += 1\n\t\t\t\tcounter[k] -= 4\n\t\t\t\tdup_4_arr.append(k)\n\t\t\t\tif count_4 == min_count_4:\n\t\t\t\t\tbreak\n\t\t\tif count_4 == min_count_4:\n\t\t\t\tbreak\n\t\tif count_4 < min_count_4:\n\t\t\treturn (False, dup_4_arr, dup_2_arr, only_one_value)\n\t\tcount_2 = 0\n\t\tmin_count_2 = n // 2 * 2\n\t\tfor (k, v) in counter.items():\n\t\t\twhile v >= 2:\n\t\t\t\tv = v - 2\n\t\t\t\tcount_2 += 1\n\t\t\t\tcounter[k] -= 2\n\t\t\t\tdup_2_arr.append(k)\n\t\t\t\tif count_2 == min_count_2:\n\t\t\t\t\tbreak\n\t\t\tif count_2 == min_count_2:\n\t\t\t\tbreak\n\t\tif count_2 < min_count_2:\n\t\t\treturn (False, dup_4_arr, dup_2_arr, only_one_value)\n\t\tfor (k, v) in counter.items():\n\t\t\tif v == 1:\n\t\t\t\tonly_one_value = k\n\treturn (True, dup_4_arr, dup_2_arr, only_one_value)\n\ndef make_matrix(n, arr, counter, dup_4_arr, dup_2_arr, only_one_value):\n\tret = [['-1'] * n for _ in range(n)]\n\tarr = sorted(arr)\n\tif n & 1 == 0:\n\t\tcurrent = 0\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tret[i][j] = str(arr[current])\n\t\t\t\tcurrent += 1\n\t\t\t\tret[i][n - 1 - j] = str(arr[current])\n\t\t\t\tcurrent += 1\n\t\t\t\tret[n - 1 - i][j] = str(arr[current])\n\t\t\t\tcurrent += 1\n\t\t\t\tret[n - 1 - i][n - 1 - j] = str(arr[current])\n\t\t\t\tcurrent += 1\n\telse:\n\t\tcurrent_4 = 0\n\t\tfor i in range(n // 2):\n\t\t\tfor j in range(n // 2):\n\t\t\t\tret[i][j] = str(dup_4_arr[current_4])\n\t\t\t\tret[i][n - 1 - j] = str(dup_4_arr[current_4])\n\t\t\t\tret[n - 1 - i][j] = str(dup_4_arr[current_4])\n\t\t\t\tret[n - 1 - i][n - 1 - j] = str(dup_4_arr[current_4])\n\t\t\t\tcurrent_4 += 1\n\t\tcurrent_2 = 0\n\t\tcenter = n // 2\n\t\tfor i in range(n // 2):\n\t\t\tret[center][i] = str(dup_2_arr[current_2])\n\t\t\tret[center][n - 1 - i] = str(dup_2_arr[current_2])\n\t\t\tcurrent_2 += 1\n\t\tfor i in range(n // 2):\n\t\t\tret[i][center] = str(dup_2_arr[current_2])\n\t\t\tret[n - 1 - i][center] = str(dup_2_arr[current_2])\n\t\t\tcurrent_2 += 1\n\t\tret[center][center] = str(only_one_value)\n\treturn ret\nn = int(input())\narr = tuple(map(int, input().split(' ')))\ncounter = Counter(arr)\n(possible, dup_4_arr, dup_2_arr, only_one_value) = is_possible(n, counter)\nif possible:\n\tprint('YES')\n\tmat = make_matrix(n, arr, counter, dup_4_arr, dup_2_arr, only_one_value)\n\tfor i in range(n):\n\t\tprint(' '.join(mat[i]))\nelse:\n\tprint('NO')\n"]

source: codeforces

raw_problem:
C. Palindromic Matrix

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Let's call some square matrix with integer values in its cells palindromic if it doesn't change after the order of rows is reversed and it doesn't change after the order of columns is reversed.

For example, the following matrices are palindromic:

The following matrices are not palindromic because they change after the order of rows is reversed:

The following matrices are not palindromic because they change after the order of columns is reversed:

You are given $$$n^2$$$ integers. Put them into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic. If there are multiple answers, print any. If there is no solution, print "NO".

Input

The first line contains one integer $$$n$$$ ($$$1 \le n \le 20$$$).

The second line contains $$$n^2$$$ integers $$$a_1, a_2, \dots, a_{n^2}$$$ ($$$1 \le a_i \le 1000$$$) — the numbers to put into a matrix of $$$n$$$ rows and $$$n$$$ columns.

Output

If it is possible to put all of the $$$n^2$$$ numbers into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic, then print "YES". Then print $$$n$$$ lines with $$$n$$$ space-separated numbers — the resulting matrix.

If it's impossible to construct any matrix, then print "NO".

You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.

Examples

Input

4
1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1

Output

YES
1 2 2 1
8 2 2 8
8 2 2 8
1 2 2 1

Input

3
1 1 1 1 1 3 3 3 3

Output

YES
1 3 1
3 1 3
1 3 1

Input

4
1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1

Output

NO

Input

1
10

Output

YES
10

Note

Note that there exist multiple answers for the first two examples.

question:
Let's call some square matrix with integer values in its cells palindromic if it doesn't change after the order of rows is reversed and it doesn't change after the order of columns is reversed.
For example, the following matrices are palindromic:
 ![image](1.png) The following matrices are not palindromic because they change after the order of rows is reversed:
 ![image](2.png) The following matrices are not palindromic because they change after the order of columns is reversed:
 ![image](3.png) You are given $$$n^2$$$ integers. Put them into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic. If there are multiple answers, print any. If there is no solution, print "NO".

Input
The first line contains one integer $$$n$$$ ($$$1 \le n \le 20$$$).
The second line contains $$$n^2$$$ integers $$$a_1, a_2, \dots, a_{n^2}$$$ ($$$1 \le a_i \le 1000$$$) — the numbers to put into a matrix of $$$n$$$ rows and $$$n$$$ columns.

Output
If it is possible to put all of the $$$n^2$$$ numbers into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic, then print "YES". Then print $$$n$$$ lines with $$$n$$$ space-separated numbers — the resulting matrix.
If it's impossible to construct any matrix, then print "NO".
You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.

Examples
Input
4
1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1

Output
YES
1 2 2 1
8 2 2 8
8 2 2 8
1 2 2 1

Input
3
1 1 1 1 1 3 3 3 3

Output
YES
1 3 1
3 1 3
1 3 1

Input
4
1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1

Output
NO

Input
1
10

Output
YES
10

Note
Note that there exist multiple answers for the first two examples.

image_tags: Math Math Math

data_split: test