## Codeforces Round #495 (Div. 2)：D. Sonya and Matrix（暴力）

time limit per test  2 seconds
memory limit per test  256 megabytes
input  standard input
output  standard output

Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.

Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so.

The Manhattan distance between two cells (x1x1y1y1) and (x2x2y2y2) is defined as |x1x2|+|y1y2||x1−x2|+|y1−y2|. For example, the Manhattan distance between the cells (5,2)(5,2) and (7,1)(7,1) equals to |57|+|21|=3|5−7|+|2−1|=3.

Example of a rhombic matrix.

Note that rhombic matrices are uniquely defined by nnmm, and the coordinates of the cell containing the zero.

She drew a n×mn×m rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of nmn⋅m numbers). Note that Sonya will not give you nn and mm, so only the sequence of numbers in this matrix will be at your disposal.

Write a program that finds such an n×mn×m rhombic matrix whose elements are the same as the elements in the sequence in some order.

Input

The first line contains a single integer tt (1t1061≤t≤106) — the number of cells in the matrix.

The second line contains tt integers a1,a2,,ata1,a2,…,at (0ai<t0≤ai<t) — the values in the cells in arbitrary order.

Output

In the first line, print two positive integers nn and mm (n×m=tn×m=t) — the size of the matrix.

In the second line, print two integers xx and yy (1xn1≤x≤n1ym1≤y≤m) — the row number and the column number where the cell with 00 is located.

If there are multiple possible answers, print any of them. If there is no solution, print the single integer 1−1.

Examples
input
20
1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4

output
4 5
2 2

input
18
2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1

output
3 6
2 3

input
6
2 1 0 2 1 2

output
-1

#include<stdio.h>
#include<stdlib.h>
int sum[1000005];
int main(void)
{
int T, n, m, i, j, x, y, p, now, temp;
scanf("%d", &T);
now = 0;
for(i=1;i<=T;i++)
{
scanf("%d", &x);
sum[x]++;
if(sum[x]==1)
now++;
}
if(sum[0]!=1)
{
printf("-1\n");
return 0;
}
for(i=1;i<=2005;i++)
{
if(sum[i]>4*i)
{
printf("-1\n");
return 0;
}
if(sum[i]<4*i)
break;
}
p = i;
for(n=1;n*n<=T;n++)
{
m = T/n;
if(T%n || n<p*2-1 || m<p*2-1)
continue;
x = y = p;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
temp = abs(i-x)+abs(j-y);
sum[temp]--;
if(sum[temp]==0)
now--;
if(sum[temp]==-1)
now++;
}
}
if(now==0)
{
printf("%d %d\n%d %d\n", n, m, x, y);
return 0;
}
for(y++;y+p-1<=m;y++)
{
for(i=1;i<=n;i++)
{
temp = abs(i-x)+abs(1-y);
sum[temp]--;
if(sum[temp]==0)
now--;
if(sum[temp]==-1)
now++;
temp = abs(i-x)+abs(m+1-y);
sum[temp]++;
if(sum[temp]==0)
now--;
if(sum[temp]==1)
now++;
}
if(now==0)
{
printf("%d %d\n%d %d\n", n, m, x, y);
return 0;
}
}
for(y--,x++;x+p-1<=n;x++)
{
for(i=1;i<=m;i++)
{
temp = abs(1-x)+abs(i-y);
sum[temp]--;
if(sum[temp]==0)
now--;
if(sum[temp]==-1)
now++;
temp = abs(n+1-x)+abs(i-y);
sum[temp]++;
if(sum[temp]==0)
now--;
if(sum[temp]==1)
now++;
}
if(now==0)
{
printf("%d %d\n%d %d\n", n, m, x, y);
return 0;
}
}
x--;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
temp = abs(i-x)+abs(j-y);
sum[temp]++;
if(sum[temp]==0)
now--;
if(sum[temp]==1)
now++;
}
}
}
printf("-1\n");
return 0;
}
/*
10
1 0 1 2 3
2 1 2 3 4
*/