Problem:
 Score : 600 points    Problem Statement Snuke has an integer sequence, a , of length N . The i -th element of a ( 1 -indexed) is a_{i} .  He can perform the following operation any number of times:   Operation: Choose integers x and y between 1 and N (inclusive), and add a_x to a_y .   He would like to perform this operation between 0 and 2N times (inclusive) so that a satisfies the condition below. Show one such sequence of operations.
It can be proved that such a sequence of operations always exists under the constraints in this problem.   Condition: a_1 \leq a_2 \leq ...  \leq a_{N}       Constraints  2 \leq N \leq 50  -10^{6} \leq a_i \leq 10^{6}  All input values are integers.         Input Input is given from Standard Input in the following format:  N  a_1  a_2  ...  a_{N}       Output Let m be the number of operations in your solution. In the first line, print m .
In the i -th of the subsequent m lines, print the numbers x and y chosen in the i -th operation, with a space in between.
The output will be considered correct if m is between 0 and 2N (inclusive) and a satisfies the condition after the m operations.        Sample Input 1 3
-2 5 -1      Sample Output 1 2
2 3
3 3   After the first operation, a = (-2,5,4) .  After the second operation, a = (-2,5,8) , and the condition is now satisfied.        Sample Input 2 2
-1 -3      Sample Output 2 1
2 1   After the first operation, a = (-4,-3) and the condition is now satisfied.        Sample Input 3 5
0 0 0 0 0      Sample Output 3 0   The condition is satisfied already in the beginning.

Solution (Python):
def inpl(): return [int(i) for i in input().split()]

N = int(input())
a = inpl()
mini = float('inf')
maxi = -mini
for i in a:
    mini = min(mini, i)
    maxi = max(maxi, i)
print(2*N-1)
if maxi > -mini:
    x = a.index(maxi)+1
    for i in range(1,N+1):
        print(x,i)
    for i in range(1,N):
        print(i,i+1)
else:
    x = a.index(mini)+1
    for i in range(1,N+1):
        print(x,i)
    for i in range(N,1,-1):
        print(i,i-1)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
maxi = max(A)
mini = min(A)
maxiidx = A.index(maxi)
miniidx = A.index(mini)
print(2 * N - 1)
if maxi >= abs(mini):
    for i in range(N):
        print(maxiidx + 1, i + 1)
    for i in range(1, N):
        print(i, i + 1)
else:
    for i in range(N):
        print(miniidx + 1, i + 1)
    for i in reversed(range(1, N)):
        print(i + 1, i)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))


M, M_index = 0, -1
for i, a in enumerate(A):
    if abs(M) <= abs(a):
        M = a
        M_index = i


for i in range(N):
    A[i] += A[M_index]
ans = [(M_index, i) for i in range(N)]


if M >= 0:
    for i in range(N - 1):
        if A[i] > A[i + 1]:
            A[i + 1] += A[i]
            ans.append((i, i + 1))
else:
    for i in range(N - 1, 0, -1):
        if A[i - 1] > A[i]:
            A[i - 1] += A[i]
            ans.append((i, i - 1))

print(len(ans))
for x, y in ans:
    print(x + 1, y + 1)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect
from random import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]
 
N = read()
A = reads()

print(2 * N)
m = min(A)
M = max(A)
if abs(m) <= abs(M):
  I = A.index(M) + 1
  print(I, 1)
  print(I, 1)
  for i in range(1, N):
    print(i, i+1)
    print(i, i+1)
else:
  I = A.index(m) + 1
  print(I, N)
  print(I, N)
  for i in range(N, 1, -1):
    print(i, i-1)
    print(i, i-1)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))

maxA = max(As)
minA = min(As)

if minA >= 0:
    
    flg = 'GE'
    print(N-1)
elif maxA <= 0:
    
    flg = 'LE'
    print(N-1)
else:
    if -minA <= maxA:
        flg = 'GE'
        print(2*N-1)
        
        iMax = As.index(maxA) + 1
        for i in range(1, N+1):
            print(iMax, i)
    else:
        flg = 'LE'
        print(2*N-1)
        
        iMin = As.index(minA) + 1
        for i in range(1, N+1):
            print(iMin, i)

if flg == 'GE':
    
    for i in range(1, N):
        print(i, i+1)
else:
    
    for i in reversed(range(1, N)):
        print(i+1, i)

Solution (Python):
import sys
N=int(input())
A=[int(i) for i in input().split()]
a=max(A)
b=min(A)
if b>=0:
    ans = []
    for i in range(1,N):
        ans.append([i,i+1])
    print(N-1)
    for a ,b in ans:
        print(a,b)
    sys.exit()
if a<=0:
    ans = []
    for i in range(1,N):
        ans.append([N-i+1,N-i])
    print(N-1)
    for a ,b in ans:
        print(a,b)
    sys.exit()  
x=A.index(a)+1
y=A.index(b)+1
if abs(a)>=abs(b):
    ans = []    
    for i in range(N):
        ans.append([x,i+1])
    for i in range(1,N):
        ans.append([i,i+1])
    print(2*N-1)
    for a ,b in ans:
        print(a,b)
    sys.exit()
if abs(b)>abs(a):
    ans = []    
    for i in range(N):
        ans.append([y,i+1])   
    for i in range(1,N):
        ans.append([N-i+1,N-i])
    print(2*N-1)
    for a ,b in ans:
        print(a,b)
    sys.exit()

Solution (Python):


import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq





def solve(f):
    n = f.read_int()
    a = f.read_int_list()

    ma = max(a)
    mi = min(a)
    ans = []
    if ma+mi >= 0:
        ind = a.index(ma)
        for i in xrange(n):
            if i == ind: continue
            ans.append("%d %d"%(ind+1, i+1))
        for i in xrange(1, n):
            ans.append("%d %d"%(i, i+1))
    else:
        ind = a.index(mi)
        for i in xrange(n):
            if i == ind: continue
            ans.append("%d %d"%(ind+1, i+1))
        for i in xrange(n-1, 0, -1):
            ans.append("%d %d"%(i+1, i))

    return "%d\n%s"%(len(ans), "\n".join(ans))


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case 
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

max_index = 1
max_abs = abs(A[0])
if A[0] >= 0:
    pos = True
else:
    pos = False

for i in range(1, N):
    if max_abs < abs(A[i]):
        max_abs = abs(A[i])
        max_index = i + 1
        if A[i] >= 0:
            pos = True
        else:
            pos = False

if pos:
    print(2*N - 2)
    for n in range(N):
        if max_index - 1 != n:
            print(max_index, n+1)
    for n in range(2, N+1):
        print(n-1, n)
else:
    print(2 * N - 2)
    for n in range(N):
        if max_index - 1 != n:
            print(max_index, n + 1)
    for n in reversed(range(2, N + 1)):
        print(n, n-1)

Solution (Python):
N=int(input())
A=[int(x) for x in input().split()]

mi=100000000
ma=-100000000
mi_n=0
ma_n=0
for i in range(N):
  if A[i]>ma:
    ma=A[i]
    ma_n=i+1
  if A[i]<mi:
    mi=A[i]
    mi_n=i+1

if mi>=0:
  print(N-1)
  for i in range(N-1):
    print(i+1,i+2)
elif ma<=0:
  print(N-1)
  for i in range(N-1):
    print(N-i,N-i-1)
else:
  print(N+N-1)
  if abs(ma)>abs(mi):
    for i in range(N):
      print(ma_n,i+1)
    for j in range(N-1):
      print(j+1,j+2)
  else:
    for i in range(N):
      print(mi_n,i+1)
    for j in range(N-1):
      print(N-j,N-j-1)

Solution (Python):
import numpy as np

N=int(input())
a_lst=[int(_) for _ in input().split()]
a_arr=np.array(a_lst)
idx=np.absolute(a_arr).argmax() + 1 

if a_lst[idx-1]==0:
    print(0)
else:
    print(2*N-2)
    for i in range(1, N+1):
        if i != idx:
            print(idx, i)
    if a_lst[idx-1]>0:
        for i in range(1, N):
            print(i, i+1)
    elif a_lst[idx-1]<0:
        for i in range(N, 1, -1):
            print(i, i-1)

Solution (Python):



N = int(input())

As = list(map(int, input().split()))

if min(As) >= 0:
    print(len(As)-1)
    for i in range(len(As)-1):
        print(i+1, i+2)
elif max(As) <= 0:
    print(len(As)-1)
    for i in range(len(As)-1,0,-1):
        print(i+1, i)
elif abs(max(As)) >= abs(min(As)):
    max_index = As.index(max(As))
    print(len(As)*2-1)
    for i in range(len(As)):
        print(max_index+1, i+1)
    for i in range(len(As)-1):
        print(i+1, i+2)
else:
    min_index = As.index(min(As))
    print(len(As)*2-1)
    for i in range(len(As)):
        print(min_index+1, i+1)
    for i in range(len(As)-1,0,-1):
        print(i+1, i)

Solution (Python):
N = int(input())
a = [int(n) for n in input().split()]

steps = []

a_argmax, a_max = max(enumerate(a), key = lambda x:x[1])
a_argmin, a_min = min(enumerate(a), key = lambda x:x[1])



if a_max + a_min >= 0:
    for i in range(N):
        steps.append((a_argmax, i))
    for i in range(N-1):
        steps.append((i, i+1))
else:
    for i in range(N):
        steps.append((a_argmin, i))
    for i in range(N-1, 0, -1):
        steps.append((i, i-1))

print(2*N-1)
for x, y in steps:
    print("{} {}".format(x+1, y+1))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
seq = []

for i in range(0, n + 1):
    b = [None] * n
    for j in range(0, n): b[j] = a[j]
    seq = []
    for j in range(i - 2, -1, -1):
        seq.append((j + 1, j))
        seq.append((j + 1, j))
        b[j] += b[j + 1] * 2
    for j in range(i, n - 1):
        seq.append((j, j + 1))
        seq.append((j, j + 1))
        b[j + 1] += b[j] * 2
    valid = True
    for j in range(n - 1):
        if b[j] > b[j + 1]:
            valid = False
            break
    if valid: break

print(len(seq))
print('\n'.join(map(lambda x: '%d %d' % (x[0] + 1, x[1] + 1), seq)))

Solution (Python):


def solve(a,begin,end,queue):
  if begin>=end:
    return
  maxx = sorted(list(map(abs,a[begin:end+1])),reverse=True)[0]
  if maxx in a[begin:end+1]:
    m = a[begin:end+1].index(maxx)+begin
  else:
    m = a[begin:end+1].index(-1*maxx)+begin
    maxx = -1*maxx
  if maxx==0:
    return []
  if maxx>0:
    solve(a,begin,m-1,queue)
    for i in range(max(0,m-1),end):
      if a[i+1]>=a[i]:
        continue
      else:
        a[i+1]+=a[i]
        queue.append((i,i+1))
        if a[i+1]<a[i]:
          a[i+1]+=a[i]
          queue.append((i,i+1))
    return queue
  if maxx<0:
    solve(a,m+1,end,queue)
    for i in reversed(range(begin,min(len(a)-1,m+1))):
      if a[i]<=a[i+1]:
        continue
      else:
        a[i]+= a[i+1]
        queue.append((i+1,i))
        if a[i]>a[i+1]:
          a[i]+=a[i+1]
          queue.append((i+1,i))
    return queue

if __name__ == '__main__':
  n = int(input())
  a = list(map(int,input().split()))
  queue = solve(a,0,len(a)-1,[])
  print(len(queue))
  for i in range(len(queue)):
    print(queue[i][0]+1,queue[i][1]+1)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ma = -100000000
mi = 100000000
for i in range(n):
    if a[i] > ma:
        ma = a[i]
        ma_index = i
    if a[i] < mi:
        mi = a[i]
        mi_index = i
ans = []
if ma >= -mi:
    for i in range(n):
        ans.append((ma_index,i))
    for i in range(n-1):
        ans.append((i,i+1))
else:
    for i in range(n):
        ans.append((mi_index,i))
    for i in range(n-1):
        ans.append((n-1-i,n-2-i))
print(len(ans))
for x in ans:
    print(x[0]+1,x[1]+1)

Solution (Python):

import sys


def solve(a, begin, end, queue, cnt):
    
    if begin >= end:
        return
    maxx = sorted(list(map(abs, a[begin:end + 1])), reverse=True)[0]
    
    if maxx in a[begin:end + 1]:
        m = a[begin:end + 1].index(maxx) + begin
    else:
        m = a[begin:end + 1].index(-1 * maxx) + begin
        maxx = -1 * maxx
    
    if maxx == 0:
        return []
    if maxx > 0:
        solve(a, begin, m - 1, queue, cnt + 1)
        for i in range(max(0, m - 1), end):
            if a[i + 1] >= a[i]:
                continue
            else:
                a[i + 1] += a[i]
                queue.append((i, i + 1))
                if a[i + 1] < a[i]:
                    a[i + 1] += a[i]
                    queue.append((i, i + 1))
        return queue
    if maxx < 0:
        solve(a, m + 1, end, queue, cnt + 1)
        for i in reversed(range(begin, min(len(a) - 1, m + 1))):
            if a[i] <= a[i + 1]:
                continue
            else:
                a[i] += a[i + 1]
                queue.append((i + 1, i))
                if a[i] > a[i + 1]:
                    a[i] += a[i + 1]
                    queue.append((i + 1, i))
        return queue


if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    queue = solve(a, 0, len(a) - 1, [], 0)
    print(len(queue))
    for i in range(len(queue)):
        print(queue[i][0] + 1, queue[i][1] + 1)

Solution (Python):
def positive(n):
  for i in range(n-1):
    print(i+1,i+2)
def negative(n):
  for i in range(n-1):
    print(n-i,n-i-1)
N=int(input())
A=list(map(int,input().split()))
mina=min(A)
minin=A.index(mina)
maxa=max(A)
maxin=A.index(maxa)
if mina>=0:
  print(N-1)
  positive(N)
elif maxa<=0:
  print(N-1)
  negative(N)
elif maxa+mina>=0:
  print(2*N-1)
  for i in range(N):
    print(maxin+1, i+1)
  positive(N)
else:
  print(2*N-1)
  for i in range(N):
    print(minin+1, i+1)
  negative(N)

Solution (Python):
n = int(input())

a = [int(x) for x in input().split()]

a_min = min(a)
a_max = max(a)
a_min_index = 0
a_max_index = 0

for i,x in enumerate(a):
    if x == a_min:
        a_min_index = i+1
    elif x == a_max:
        a_max_index = i+1

if a_min >= 0:
    
    print(n-1)
    for i in range(1,n):
        print(i, i+1)
else:
    if abs(a_min) <= abs(a_max):
        
        print(2*n-1)
        for i in range(1, n+1):
            print(a_max_index, i)   
        for i in range(1,n):
            print(i, i+1)
    else:
        
        print(2*n-1)
        for i in range(1, n+1):
            print(a_min_index, i)
        for i in reversed(range(2,n+1)):
            print(i, i-1)

Solution (Python):
from itertools import starmap


def solve(n, a):
    max_a, min_a = float('-inf'), float('inf')
    max_i, min_i = 0, 0
    for i, b in enumerate(a):
        if b > max_a:
            max_i, max_a = i + 1, b
        if b < min_a:
            min_i, min_a = i + 1, b

    if max_a >= 0 and min_a >= 0:
        return [(i, i + 1) for i in range(1, n)]
    if max_a <= 0 and min_a <= 0:
        return [(i, i - 1) for i in range(n, 1, -1)]
    buf = []
    if max_a >= -min_a:
        for i, b in enumerate(a):
            if b < 0:
                buf.append((max_i, i + 1))
        buf.extend((i, i + 1) for i in range(1, n))
    else:
        for i, b in enumerate(a):
            if b > 0:
                buf.append((min_i, i + 1))
        buf.extend((i, i - 1) for i in range(n, 1, -1))
    return buf


n = int(input())
a = list(map(int, input().split()))
buf = solve(n, a)
print(len(buf))
print(''.join(starmap('{} {}\n'.format, buf)))

Solution (Python):
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
INF = float("inf")
sys.setrecursionlimit(10**7)


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def main():
    N = int(input())
    A = list(map(int, input().split()))

    ans = []
    a_min, a_max = min(A), max(A)
    idx = A.index(a_min) if abs(a_min) > abs(a_max) else A.index(a_max)
    for i in range(N):
        if i != idx:
            A[i] += A[idx]
            ans.append((idx, i))
    ans.append((idx, idx))

    if abs(a_min) > abs(a_max):
        for i in range(N - 1, 0, -1):
            ans.append((i, i - 1))
    else:
        for i in range(0, N - 1):
            ans.append((i, i + 1))

    print(len(ans))
    for a, b in ans:
        print(a + 1, b + 1)

if __name__ == '__main__':
    main()

Solution (Python):
import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque








def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)


def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))





def main():
	def negative():
		for i in reversed(range(1,N)):
			print(i+1, i)

	def positive():
		for i in range(N-1):
			print(i+1, i+2)

	N = I()
	A = LI()
	max_A = max(A)
	maxidx = A.index(max(A)) +1
	min_A = min(A)
	minidx = A.index(min_A) +1
	
	p=[]

	
	if max_A < 0:
		print(N-1)
		negative()

	
	elif min_A < 0:
		print(2*N-1)

		
		if abs(max_A) >= abs(min_A):
			for i in range(N):
				if i+1 != maxidx:
					print(maxidx, i+1)
			print(maxidx, maxidx)
			positive()

		
		else:
			for i in range(N):
				if i+1 != minidx:
					print(minidx, i+1)
			print(minidx,minidx)
			negative()


	
	else:
		print(N-1)
		positive()


if __name__ == '__main__':
	main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
x = max(range(n), key=lambda i: abs(a[i]))
print(2*n)
if a[x] >= 0:
    print(x+1, 1)
    print(x+1, 1)
    for i in range(2, n+1):
        print(i-1, i)
        print(i-1, i)
else:
    print(x+1, n)
    print(x+1, n)
    for i in range(n-1, 0, -1):
        print(i+1, i)
        print(i+1, i)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
ans = []

max_a, min_a = (-10**6, 10**6)
max_i, min_i = 0, 0
for i in range(N):
    if max_a <= a[i]:
        max_a, max_i = a[i], i
    if min_a >= a[i]:
        min_a, min_i = a[i], i

if max_a + min_a >= 0:
    for i in range(N):
        a[i] += a[max_i]
        ans.append((max_i, i))
    for j in range(N - 1):
        a[j + 1] += a[j]
        ans.append((j, j + 1))
else:
    for i in range(N):
        a[i] += a[min_i]
        ans.append((min_i, i))
    for j in range(N - 1, 0, -1):
        a[j - 1] += a[j]
        ans.append((j, j - 1))


print(len(ans))
for (x, y) in ans:
    print(x + 1, y + 1)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

command = []

s = min(a)
si = a.index(s)
t = max(a)
ti = a.index(t)

def positive(a):
    for i in range(n-1):
        if a[i] > a[i+1]:
            a[i+1] += a[i]
            command.append([i+1,i+2])
        
def negative(a):
    for i in range(n-1,0,-1):
        if a[i] < a[i-1]:
            a[i-1] += a[i]
            command.append([i+1,i])

if s > 0:
    positive(a)
elif t < 0:
    negative(a)
elif -s < t:
    
    for i in range(n):
        if a[i] <= 0:
            a[i] += t
            command.append([ti+1,i+1])
    positive(a)
else:
    
    for i in range(n):
        if a[i] >= 0:
            a[i] += s
            command.append([si+1,i+1])
    negative(a)


print(len(command))
for c in command:
    print(*c)

Solution (Python):
N = int(input())
arr = map(int, input().split())
max_abs = -1
max_val = 0
max_arg = 0
for i, v in enumerate(arr):
    if abs(v) >= max_abs:
        max_abs = abs(v)
        max_arg = i
        max_val = v
print(2*N-2)
if max_val >= 0:
    for i in range(1, max_arg+1):
        print(max_arg+1, i)
    for i in range(max_arg+2, N+1):
        print(max_arg+1, i)
    for i in range(1, N):
        print(i, i+1)
else:
    for i in range(1, max_arg+1):
        print(max_arg+1, i)
    for i in range(max_arg+2, N+1):
        print(max_arg+1, i)
    for i in range(N-1, 0, -1):
        print(i+1, i)

Solution (Python):
def solve():
    n = int(input())
    A = tuple(map(int, input().split()))

    def check(B):
        for i in range(n - 1):
            if B[i] > B[i + 1]:
                return False
        return True

    def out(ans):
        print(len(ans))
        for a in ans:
            print(a[1] + 1, a[0] + 1)
        import sys
        sys.exit(0)

    mai = -1
    mii = -1
    ma = -10 ** 30
    mi = 10 ** 30

    for i in range(n):
        if A[i] > ma:
            ma = A[i]
            mai = i
        if A[i] < mi:
            mi = A[i]
            mii = i

    

    B = list(A)
    ans = []
    for i in range(n):
        if i != mai:
            B[i] += B[mai]
            B[i] += B[mai]
            ans.append((i, mai))
            ans.append((i, mai))
            mai = i

    if check(B):
        out(ans)
        return


    B = list(A)
    ans = []
    for i in range(n - 1, -1, -1):
        if i != mii:
            B[i] += B[mii]
            B[i] += B[mii]
            ans.append((i, mii))
            ans.append((i, mii))
            mii = i

    if check(B):
        out(ans)
        return


solve()

Solution (Python):
import bisect

N = int(input())
As = list(map(int, input().split()))

ans = ''
if abs(max(As)) >= abs(min(As)):
    for i in range(1, N):
        while As[i - 1] - As[i] > 0:
            Bs = [(As[i], i) for i in range(N)]
            Bs.sort()

            key = (As[i - 1] - As[i], 0)
            iB = bisect.bisect_left(Bs, key)

            if iB >= N: iB = N - 1
            As[i] += Bs[iB][0]

            ans += str(Bs[iB][1] + 1) + ' ' + str(i + 1) + '\n'

else:
    for i in range(N - 1)[::-1]:
        while As[i] - As[i + 1] > 0:
            Bs = [(As[i], i) for i in range(N)]
            Bs.sort()

            key = (-(As[i] - As[i + 1]), 0)
            iB = bisect.bisect_right(Bs, key) - 1

            if iB < 0: iB = 0
            As[i] += Bs[iB][0]

            ans += str(Bs[iB][1] + 1) + ' ' + str(i + 1) + '\n'

print(ans.count('\n'))
print(ans, end='')

Solution (Python):



n = raw_input()
n = int(n)

a = raw_input()
a = a.split()
a = map(int,a)

if min(a) >= 0:
    print n-1
    for i in range(n-1):
        print i+1,i+2
elif max(a) <= 0:
    print n-1
    for i in range(n-1):
        print (n-i),(n-i-1)
elif abs(min(a)) <= max(a):
    max_index = a.index(max(a))
    print 2*n-1
    for i in range(n):
        if i == max_index:
            pass
        else:
            print max_index+1,i+1
    print max_index+1,max_index+1
    for i in range(n-1):
        print i+1,i+2
else:
    min_index = a.index(min(a))
    print 2*n-1
    for i in range(n):
        if i == min_index:
            pass
        else:
            print min_index+1,i+1
    print min_index+1,min_index+1
    for i in range(n-1):
        print (n-i),(n-i-1)

Solution (Python):

from copy import deepcopy
def inpl(): return tuple(map(int, input().split()))

N = int(input())
A = inpl()
L = list(A)
cms = []

if abs(max(L)) > abs(min(L)):
    m = max(L)
    mi = L.index(max(L))
    
    for i in range(1, N):
        if L[i-1] <= L[i]:
            continue
        elif L[i-1] <= L[i] + m:
            cms.append("{} {}".format(mi+1, i+1))
            L[i] += m
            if L[i] > m:
                m = L[i]
                mi = i
        elif L[i-1] <= L[i] + 2*m:
            cms.append("{} {}".format(mi+1, i+1))
            cms.append("{} {}".format(mi+1, i+1))
            L[i] += 2*m
            if L[i] > m:
                m = L[i]
                mi = i
        else:
            print("Error11")
            break
else :
    m = min(L)
    mi = L.index(min(L))
    
    for i in range(N-2, -1, -1):
        if L[i] <= L[i+1]:
            continue
        elif L[i] + m <= L[i+1]:
            cms.append("{} {}".format(mi+1, i+1))
            L[i] += m
            if L[i] < m:
                m = L[i]
                mi = i
        elif L[i] + 2*m <= L[i+1]:
            cms.append("{} {}".format(mi+1, i+1))
            cms.append("{} {}".format(mi+1, i+1))
            L[i] += 2*m
            if L[i] < m:
                m = L[i]
                mi = i
        else:
            print("Error12")
            break

print(len(cms))
if len(cms) > 2*N:
    print("Error2")
for cm in cms:
    print(cm)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

ah = -1e7
al = 1e7
ih = 1
il = 1
for i in range(n):
    if ah < a[i]:
        ah = a[i]
        ih = i + 1
    if a[i] < al:
        al = a[i]
        il = i + 1
ans = []
if 0 < al or abs(al) <= abs(ah):
    ans = [ih, 2, ih, 2, 2, 2, 2, 2]
    for i in range(3, n + 1):
        for _ in range(2):
            ans.append(i - 1)
            ans.append(i)
elif ah < 0 or abs(ah) < abs(al):
    ans = [il, n - 1, il, n - 1, n - 1, n - 1, n - 1, n - 1]
    for i in range(n - 2, 0, -1):
        for _ in range(2):
            ans.append(i + 1)
            ans.append(i)
print(len(ans) // 2)
for i in range(0, len(ans), 2):
    print('%d %d' % (ans[i], ans[i + 1]))

Solution (Python):
import sys
N = int(input())
ar = list(map(int,input().split()))

mx, mn = -(10**7), 10**7
mxi = mni = -1
for i in range(N):
    if mx < ar[i]:
        mx = ar[i]
        mxi = i
    if mn > ar[i]:
        mn = ar[i]
        mni = i
if mx == mn == 0:
    print(0)
    sys.exit()

sign = 1
ans = []
if mx * mn < 0:
    if mx + mn >= 0:
        for i in range(N):
            if ar[i] < 0:
                ar[i] += mx
                ans.append((mxi+1, i+1))
    else:
        sign = -1
        for i in range(N):
            if ar[i] > 0:
                ar[i] += mn
                ans.append((mni+1, i+1))
elif mx <= 0:
    sign = -1

if sign > 0:
    for i in range(N-1):
        if ar[i] > ar[i+1]:
            ar[i+1] += ar[i]
            ans.append((i+1, i+2))
else:
    for i in reversed(range(1,N)):
        if ar[i] < ar[i-1]:
            ar[i-1] += ar[i]
            ans.append((i+1, i))

print(len(ans))
for a in ans:
    print(str(a[0]) + ' ' + str(a[1]))

Solution (Python):
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
INF = float("inf")
sys.setrecursionlimit(10**7)


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def main():
    N = int(input())
    A = list(map(int, input().split()))

    ans = []

    t = A[:]
    a = min(A)
    a_idx = A.index(a)
    for i in range(N):
        t[i] += a
        if t[i] > 0:
            ans = []
            break
        else:
            if i != a_idx:
                ans.append((a_idx + 1, i + 1))

    if len(ans) != 0:
        ans.append((a_idx + 1, a_idx + 1))
        for i in range(N, 1, -1):
            ans.append((i, i - 1))
        print(len(ans))
        for a in ans:
            print(a[0], a[1])
        return

    t = A[:]
    a = max(A)
    a_idx = A.index(a)
    for i in range(N):
        t[i] += a
        if i != a_idx:
            ans.append((a_idx + 1, i + 1))
    ans.append((a_idx + 1, a_idx + 1))
    for i in range(1, N):
        ans.append((i, i + 1))
    print(len(ans))
    for a in ans:
        print(a[0], a[1])
    return

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

def maxAbsIndex(arr):
  return max(range(len(arr)), key=lambda x: abs(arr[x]))

def p(x, y):
  print(str(x+1) + " " + str(y+1))


idx = maxAbsIndex(A)
print(2*N)

for i in range(N):
  if i != idx:
    p(idx, i)
    A[i] += A[idx]
p(idx, idx)
A[idx] += A[idx]


sign = A[0] >= 0
for i in range(N):
  idx = maxAbsIndex(A)
  if sign:
    p(idx, i)
    A[i] += A[idx]
  else:
    p(idx, N-i-1)
    A[N-i-1] += A[idx]

Solution (Python):


n = int(input())
a = list(map(int, input().split()))

ma = max(a)
mi = min(a)
flag = 0

if ma <= 0:
    flag = 1

elif mi >= 0:
    flag = 0
else:
    
    if abs(ma) < abs(mi):
        flag = 2
    
    else:
        flag = 3
cnt = 0
printer = ""
if flag == 2:
    x = a.index(mi)
    for i in range(n):
        if i == x:
            continue
        cnt += 1
        printer += "{0} {1}\n".format(x + 1, i + 1)
if flag == 3:
    x = a.index(ma)
    for i in range(n):
        if i == x:
            continue
        cnt += 1
        printer += "{0} {1}\n".format(x + 1, i + 1)
if flag == 0 or flag == 3:
    for i in range(n - 1):
        cnt += 1
        printer += "{0} {1}\n".format(i + 1, i + 2)
elif flag == 1 or flag == 2:
    for i in range(n-1):
        cnt += 1
        printer += "{0} {1}\n".format(n - i, n - i - 1)
print(cnt)
print(printer[:-1])

Solution (Python):


import sys
import math
import collections

N, = list(map(int, sys.stdin.readline().rstrip().split()))
As = list(map(int, sys.stdin.readline().rstrip().split()))

Amin = min(As)
Amax = max(As)

if abs(Amax) >= abs(Amin):
    index = [i for i, item in enumerate(As) if item == Amax][0]
    print(2 * N - 1)
    for i in range(len(As)):
        print(index + 1, i + 1)
        As[i] += Amax
    for i in range(1, len(As), 1):
        print(i, i + 1)
        As[i] += As[i - 1]
else:
    index = [i for i, item in enumerate(As) if item == Amin][0]
    print(2 * N - 1)
    for i in range(len(As)):
        print(index + 1, i + 1)
        As[i] += Amin
    for i in range(len(As) - 2, -1, -1):
        print(i + 2, i + 1)
        As[i] += As[i + 1]

exit(0)

Solution (Python):
n, a = input(), map(int, raw_input().split())

mi, mx = min(a), max(a)

b = []

p = a.index(mx if abs(mx) > abs(mi) else mi)
for i in xrange(n):
    if i != p:
        a[i] += a[p]
        b.append((p, i))

if a[0] > 0:
    for i in xrange(n - 1):
        if a[i] > a[i + 1]:
            a[i + 1] += a[i]
            b.append((i, i + 1))
else:
    for i in xrange(n - 1, 0, -1):
        if a[i] < a[i - 1]:
            a[i - 1] += a[i]
            b.append((i, i - 1))

print len(b)
for x in b:
    print x[0] + 1, x[1] + 1

Solution (Python):
N = input()
A = map(int, raw_input().split())

mi = -1
mv = -1

for i in xrange(N):
    if abs(A[i]) > mv:
        mv = abs(A[i])
        mi = i

if mv == 0:
    print 0
    exit()

ans = []

for i in xrange(N):
    if i != mi:
        ans.append((mi+1, i+1))

if A[mi] > 0:
    for i in xrange(N-1):
        ans.append((i+1, i+2))
else:
    for i in xrange(N-1, 0, -1):
        ans.append((i+1, i))

print len(ans)
for a, b in ans:
    print a, b

Solution (Python):
N = int(input())

A = list(map(int,input().split()))


_,mi = max((abs(v),i) for i,v in enumerate(A))
mi += 1

print(2*N-2)
for i in range(1,N+1):
  if i != mi:
    print(mi,i)

if A[mi-1] > 0:
  for i in range(1,N):
    print(i,i+1)
else:
  for i in reversed(range(1,N)):
    print(i+1,i)