Iahub is playing an uncommon game. Initially, he has n boxes, numbered 1, 2, 3, ..., n.…
- User
Iahub is playing an uncommon game. Initially, he has n boxes, numbered 1, 2, 3, ..., n. Each box has some number of candies in it, described by a sequence a1, a2, ..., an. The number ak represents the number of candies in box k.
The goal of the game is to move all candies into exactly two boxes. The rest of n - 2 boxes must contain zero candies. Iahub is allowed to do several (possible zero) moves. At each move he chooses two different boxes i and j, such that ai ≤ aj. Then, Iahub moves from box j to box i exactly ai candies. Obviously, when two boxes have equal number of candies, box number j becomes empty.
Your task is to give him a set of moves such as Iahub to archive the goal of the game. If Iahub can't win the game for the given configuration of boxes, output -1. Please note that in case there exist a solution, you don't need to print the solution using minimal number of moves. Input
The first line of the input contains integer n (3 ≤ n ≤ 1000). The next line contains n non-negative integers: a1, a2, ..., an — sequence elements. It is guaranteed that sum of all numbers in sequence a is up to 106.
Output
In case there exists no solution, output -1. Otherwise, in the first line output integer c (0 ≤ c ≤ 106), representing number of moves in your solution. Each of the next c lines should contain two integers i and j (1 ≤ i, j ≤ n, i ≠ j): integers i, j in the kth line mean that at the k-th move you will move candies from the j-th box to the i-th one.
Examples
Input
3 3 6 9
Output
2 2 3 1 3
Input
3 0 1 0
Output
-1
Input
4 0 1 1 0
Output
0
Note
For the first sample, after the first move the boxes will contain 3, 12 and 3 candies. After the second move, the boxes will contain 6, 12 and 0 candies. Now all candies are in exactly 2 boxes.
For the second sample, you can observe that the given configuration is not valid, as all candies are in a single box and they should be in two boxes. Also, any move won't change the configuration, so there exists no solution.
For the third sample, all candies are already in 2 boxes. Hence, no move is needed.
Assistant
n = int(input()) a = list(map(int, input().split())) total = sum(a) if total < 2: print(-1) exit() non_zero = [x for x in a if x != 0] if len(non_zero) < 2: print(-1) exit() if len(non_zero) == 2: print(0) exit() # Find the first two non-zero indices A_idx = None B_idx = None for i in range(n): if a[i] != 0: if A_idx is None: A_idx = i else: B_idx = i break moves = [] for k in range(n): if k == A_idx or k == B_idx or a[k] == 0: continue while a[k] > 0: # Check if can move to A or B while a[k] >= a[A_idx] or a[k] >= a[B_idx]: if a[A_idx] <= a[k]: moves.append((A_idx + 1, k + 1)) a[A_idx] += a[A_idx] a[k] -= a[A_idx] // 2 # since a[A_idx] was doubled else: moves.append((B_idx + 1, k + 1)) a[B_idx] += a[B_idx] a[k] -= a[B_idx] // 2 # Now a[k] is smaller than both A and B # Process between A and B to reduce one of them while a[k] < a[A_idx] and a[k] < a[B_idx]: if a[A_idx] <= a[B_idx]: # move from B to A moves.append((A_idx + 1, B_idx + 1)) transferred = a[A_idx] a[A_idx] += transferred a[B_idx] -= transferred else: # move from A to B moves.append((B_idx + 1, A_idx + 1)) transferred = a[B_idx] a[B_idx] += transferred a[A_idx] -= transferred # Check if either is now <= a[k] if a[A_idx] <= a[k] or a[B_idx] <= a[k]: break # Verify that all except A and B are zero (should be) print(len(moves)) for move in moves: print(move[0], move[1])
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.