#P9077. [PA 2018] Poddrzewo

[PA 2018] Poddrzewo

Problem Description

This problem is translated from PA 2018 Round 1 Poddrzewo.

You are given a sequence aa of length nn.

Construct an undirected tree with kk nodes, numbered 1k1 \cdots k. The degree of node ii in this tree must be aia_i.

A solution may not exist. You are allowed to perform the following operations to make it solvable:

  1. Modify the ii-th number in the sequence.
  2. Delete the ii-th number from the sequence.
  3. Swap the ii-th and jj-th numbers in the sequence.

It can be proven that after a finite number of operations, a solution will always exist.

Your task is to minimize the number of times operation 1 is used.

Input Format

The first line contains an integer nn, the length of the sequence aa.

The second line contains nn integers. The ii-th integer is aia_i.

Output Format

The first line contains an integer xx, the minimum number of times operation 1 is used.

The second line contains an integer k (2kn)k\ (2 \le k \le n), the number of nodes in the tree you construct.

In the next k1k - 1 lines, each line contains two integers u,v (1u,vk)u, v\ (1 \le u, v \le k), indicating an edge between nodes uu and vv.

If there are multiple solutions, output any one.

6
2 1 5 3 1 1
0
5
1 2
2 3
1 4
1 5
3
1 2 2
1
3
1 2
2 3

Hint

Explanation for Sample 1

We can delete the 33-rd number and then change the order of the elements.

The final sequence becomes (3,2,1,1,1)(3,2,1,1,1).

This is a schematic diagram of the constructed tree:


Explanation for Sample 2

We can modify the 33-rd number, and the final sequence becomes (1,2,1)(1,2,1). It can be proven that operation 1 must be used at least 11 time.

This is a schematic diagram of the constructed tree:


Constraints

This problem uses bundled testdata.

For 100%100\% of the testdata:

  • 2n1062 \le n \le 10^6.
  • 1ain11 \le a_i \le n - 1.

It is guaranteed that there exists at least one subtask where the minimum value of the number of times operation 1 is used is 00.

Translated by ChatGPT 5