1635 {
1636 int N;
1637 cin >> N;
1638 int m = N;
1639 int n = 1;
1640 for(int i = 1; i * i <= N; ++i) {
1641 if(N % i == 0) {
1642 n = i;
1643 m = N / i;
1644 }
1645 }
1646 vector matrix(m, vector(n, 0));
1647 vector<int> vec(N);
1648 for(int i = 0; i < N; i++) {
1649 cin >> vec[i];
1650 }
1651 sort(vec.rbegin(), vec.rend());
1652 int direction = 0;
1653 int x = 0;
1654 int y = 0;
1655 int current = 0;
1656 matrix[0][0] = vec[current];
1657 --N;
1658 while(N != 0) {
1659 int next_x = x;
1660 int next_y = y;
1661 switch(direction) {
1662 case 0:
1663 next_y += 1;
1664 break;
1665 case 1:
1666 next_x += 1;
1667 break;
1668 case 2:
1669 next_y -= 1;
1670 break;
1671 case 3:
1672 next_x -= 1;
1673 break;
1674 }
1675 if(next_x < 0 || next_x >= m || next_y < 0 || next_y >= n || matrix[next_x][next_y] != 0) {
1678 } else {
1679 matrix[next_x][next_y] =
vec[++current];
1680 x = next_x;
1681 y = next_y;
1683 }
1684 }
1685 for(int i = 0; i < m; i++) {
1686 for(int j = 0; j < n; j++) {
1687 cout << matrix[i][j];
1688 if(j != n - 1) {
1689 cout << ' ';
1690 }
1691 }
1692 if(i != m - 1) {
1693 cout << endl;
1694 }
1695 }
1696 return 0;
1697 }