Make It Ascending
Contest: Educational Codeforces Round 86 (Rated for Div. 2)
Difficulty rating: 3000
Tags: bitmasks, brute force, dp

You are given an array $$$a$$$ consisting of $$$n$$$ elements. You may apply several operations (possibly zero) to it.

During each operation, you choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le n$$$; $$$i \ne j$$$), increase $$$a_j$$$ by $$$a_i$$$, and remove the $$$i$$$-th element from the array (so the indices of all elements to the right to it decrease by $$$1$$$, and $$$n$$$ also decreases by $$$1$$$).

Your goal is to make the array $$$a$$$ strictly ascending. That is, the condition $$$a_1 < a_2 < \dots < a_n$$$ should hold (where $$$n$$$ is the resulting size of the array).

Calculate the minimum number of actions required to make the array strictly ascending.

Input format:
The first line contains one integer $$$T$$$ ($$$1 \le T \le 10000$$$) — the number of test cases.

Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$1 \le n \le 15$$$) — the number of elements in the initial array $$$a$$$.

The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 10^6$$$).

It is guaranteed that:

- the number of test cases having $$$n \ge 5$$$ is not greater than $$$5000$$$;
- the number of test cases having $$$n \ge 8$$$ is not greater than $$$500$$$;
- the number of test cases having $$$n \ge 10$$$ is not greater than $$$100$$$;
- the number of test cases having $$$n \ge 11$$$ is not greater than $$$50$$$;
- the number of test cases having $$$n \ge 12$$$ is not greater than $$$25$$$;
- the number of test cases having $$$n \ge 13$$$ is not greater than $$$10$$$;
- the number of test cases having $$$n \ge 14$$$ is not greater than $$$3$$$;
- the number of test cases having $$$n \ge 15$$$ is not greater than $$$1$$$.

Output format:
For each test case, print the answer as follows:

In the first line, print $$$k$$$ — the minimum number of operations you have to perform. Then print $$$k$$$ lines, each containing two indices $$$i$$$ and $$$j$$$ for the corresponding operation. Note that the numeration of elements in the array changes after removing elements from it. If there are multiple optimal sequences of operations, print any one of them.

Constraints:
Time limit: 7.0 s
Memory limit: 512.0 MB

Note:
In the first test case, the sequence of operations changes $$$a$$$ as follows:

$$$[2, 1, 3, 5, 1, 2, 4, 5] \rightarrow [2, 1, 3, 5, 1, 4, 7] \rightarrow [1, 3, 5, 1, 6, 7] \rightarrow [2, 3, 5, 6, 7]$$$.

Example input:
4
8
2 1 3 5 1 2 4 5
15
16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
2
3 3
14
1 2 3 4 5 6 7 8 9 10 11 12 13 14

Example output:
3
6 8
1 6
4 1
7
1 15
1 13
1 11
1 9
1 7
1 5
1 3
1
2 1
0

Editorial:
Suppose we don't have any constraints on the order of elements, the resulting array just should not contain any duplicates. Let's build the result one element after another in ascending order, so each element we create is strictly greater than the previous. To create an element, just use some subset of elements and merge them into new element. This process can be efficiently modeled with the following dynamic programming: $$$dp_{cnt, mask}$$$ is the minimum value of the last element, if we merged all the elements from $$$mask$$$ into $$$cnt$$$ ascending numbers. To model transitions, we simply iterate on the mask of elements that will be merged into a new one, and check if its sum is greater than the last element we created. This runs in $$$O(n3^n)$$$, if we use an efficient way to iterate on all masks that don't intersect with the given mask.
Okay, how about maintaining the order? When we create an element by merging some elements of the original array, let's choose some position of an element we use in merging and state that all other elements are added to it. Then, to ensure that the result is ascending, the position of this element should be greater than the position of the element we chose while building the previous number. We can add the position we have chosen for the last element to the states of our dynamic programming, so it becomes $$$dp_{cnt, mask, pos}$$$ — the minimum value of the last element, if we merged the $$$mask$$$ of elements into $$$cnt$$$ numbers, and the last element originally had index $$$pos$$$ in the array.
Using some greedy optimizations (for example, we should not iterate on the position we are choosing to merge — it can be chosen greedily as the leftmost position after the position of previous element we are taking into consideration), we can make it $$$O(n^2 3^n)$$$, yet with a small constant factor. To restore the answer, we can maintain the previous values of $$$mask$$$ and $$$pos$$$ in each state, since $$$cnt$$$ just increases by $$$1$$$ with each transition.