Let's define the cover time of a graph G with n vertices (numbered 0 through n−1) as the expected value of the length of a random walk that starts in vertex 0 and stops immediately after visiting each vertex at least once. In other words, the following algorithm is performed:
define current vertex v; initially v=0
define the set of visited vertices S; initially S={0}
while S does not contain all vertices:
select an edge e randomly uniformly among all edges incident on v
denote the other endpoint of e (different from v) as u
u becomes the current vertex v and it's added to the set S
The length of this walk is the number of times the last step is performed plus one (equal to the number of visited vertices).
You are given two integers n,k and a sequence of n integers d0,d1,…,dn−1. Construct an undirected graph G with n vertices such that for each 0≤i<n, vertex i has degree equal to di. The cover time of this graph should be as close to k as possible.
The resulting graph has to be connected. The graph must not contain loops (i.e. edges from a vertex to itself), but it may contain multiple edges between the same pair of vertices.
Constraints
Input format
On the first line of the input, there is a single integer n.
On the second line, there are n space-separated integers d0,d1,…,dn−1.
On the third line, there is a single integer k.
Output format
On the first line, print one integer m denoting the number of edges in the graph.
Then, print m lines. On the i-th of these lines, print two space-separated integers ui and vi (0≤ui,vi<n) denoting an edge between vertices ui and vi in the graph.
Test case generation
Each test file has manually assigned values of n and a range [A,B]. A sequence f0,f1,…,fn−1 will be drawn randomly independently from the uniform integer distribution over the range [A,B]. Next, the sequence d0,d1,…,dn−1 is generated from f0,f1,…,fn−1 using the expected_degree_graph function (without self-loops) from the Python networkx module.
The value of k is chosen in the following way: Several random walks in graphs are generated; let's denote the length of the shortest and longest walk by lmin and lmax respectively. Then, k is chosen randomly uniformly from the range [lmin,lmax].
Scoring
For each test file, the checker will take the graph generated by your program, run the random walk algorithm described at the beginning of the problem statement W=25000 times and compute the average of their lengths; let's denote this average by X. This value will be used as an approximation of the cover time of the graph. The random walks will be generated using a fixed seed for each test file.
If X≥10k, the verdict of your submission for the given test file will be Wrong Answer. Otherwise, the score of your submission will be (X−k)2/k.
During the contest, every submission will be evaluated on 50 test files. The total score for a submission is the sum of the scores for all test files. The goal is to minimize the total score.
After the contest, every submission will be re-evaluated on 100 test files. This set will be generated using the method described above in the following way: for each test with parameters n,A,B in the test set used during the contest, two tests with the same parameters n,A,B (but not necessarily the same seed) will appear in this final test set. The final score of your solution will be its total score on these 100 test files.
The cover time of the output graph is exactly 20/3. The checker computes X≐6.6786 with score ≐0.01578.
This is an optimal solution — the only possible cover times of a graph with the given degree sequence are 20/3 and 8.