#P12227. Lexicographically Minimal Operation Sequence

    ID: 14332 Type: Default 1000ms 256MiB

Lexicographically Minimal Operation Sequence

Lexicographically Minimal Operation Sequence

You are given a sequence \(a\) of length \(n\) consisting of non-negative integers. You are allowed to perform the following operation:

  • In one operation, choose two positions \(i\) and \(j\) (they can be the same). Then, decrease \(a_i\) and \(a_j\) by 1. Note that if \(i = j\), the value at position \(i\) is decreased only once.

Your task is to reduce the entire sequence to zeros in the minimum number of operations. Additionally, if there are multiple ways to achieve the minimum number of operations, you must output the lexicographically smallest sequence of operations.

Lexicographical Order: Consider the operations as a concatenated list of numbers. The lexicographically smallest plan is the one whose concatenated sequence of numbers is the smallest in dictionary order.

Note: The positions are 1-indexed.

inputFormat

The first line contains a single integer \(n\) \( (1 \leq n \leq 10^5)\) representing the length of the sequence.

The second line contains \(n\) non-negative integers \(a_1, a_2, \ldots, a_n\) separated by spaces.

outputFormat

On the first line, output the minimum number of operations required.

Each of the next lines should contain two integers \(i\) and \(j\) representing an operation. The operations must form the lexicographically smallest plan among all optimal solutions.

sample

3
1 2 3
4

1 2 2 3 3 3 3 3

</p>