You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that:
- there are exactly dn + 1 vertices;
- there are no self-loops;
- there are no multiple edges;
- there are no more than 106 edges;
- its degree set is equal to d.
Vertices should be numbered 1 through (dn + 1).
Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.
Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.
It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 106 edges.
Print the resulting graph.
The first line contains one integer n (1 ≤ n ≤ 300) — the size of the degree set.
The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 1000, d1 < d2 < ... < dn) — the degree set.
In the first line print one integer m (1 ≤ m ≤ 106) — the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.
Each of the next m lines should contain two integers vi and ui (1 ≤ vi, ui ≤ dn + 1) — the description of the i-th edge.
#include<bits/stdc++.h>
using namespace std;
const int maxn=303;
int a[maxn],tol,last,cnt;
stack<int>P,Q;
void add(int x)
{
for(int i=x+1;i<=tol;i++)
{
P.push(x);
Q.push(i);
}
}
int main()
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
tol=a[n]+1,last=0,cnt=0;
int i,l,r;
for(i=1,l=1,r=n;i<=tol&&l<=r;i++)
{
if(cnt+tol-i==a[r])
{
cnt++;r--;
add(i);last=1;
}
else if(cnt==a[l]) l++,last=0;
else if(last) cnt++,add(i);
}
if(last)
{
for( ;i<=tol;i++)add(i);
}
int T=Q.size();
printf("%d\n",T);
while(T--)
{
printf("%d %d\n",P.top(),Q.top());
P.pop();Q.pop();
}
return 0;
}