Problem p03498 (python)

Fixed submission:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**7
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    A = LI()
    ans = []

    max_a = max(A)
    min_a = min(A)

    if max_a * min_a < 0:
        p = max_a if abs(max_a) > abs(min_a) else min_a
        q = A.index(p)
        for i in range(N):
            ans.append((q+1,i+1))
            A[i] += p
        min_a = min(A)

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

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

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**7
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    A = [0] + LI()
    ans = []
    x = []
    xn = []

    def make_x(x,xn,p):
        for i in range(N+1):
            for j in range(N+1):
                if j == p:
                    x.append((A[i]*2+A[j],i,j))
                    xn.append(A[i]*2+A[j])
                else:
                    x.append((A[i]+A[j],i,j))
                    xn.append(A[i]+A[j])

        x.sort()
        xn.sort()

    for i in range(2,N+1):
        if A[i-1] > A[i]:
            y = False
            make_x(x,xn,i-1)
            j = bisect.bisect(xn,A[i]-A[i-1])
            if i > 2:
                if A[i-2] > A[i-1] + xn[j-1]:
                    y = True
            if y == False:
                if j > 0 and i == 2:
                    for k in x[j-1][1:]:
                        if k > 0:
                            ans.append((k,i-1))
                        A[i-1] += xn[j-1]
                else:
                    y = True
            if y:
                make_x(x,xn,i)
                j = bisect.bisect_left(xn,A[i-1]-A[i])
                for k in x[j][1:]:
                    if k > 0: ans.append((k,i))
                A[i] += xn[j]

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

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**7
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    A = [0] + LI()
    ans = []
    x = []
    xn = []

    def make_x(x,xn,p):
        for i in range(N+1):
            for j in range(N+1):
                if j == p:
                    x.append((A[i]*2+A[j],i,j))
                    xn.append(A[i]*2+A[j])
                else:
                    x.append((A[i]+A[j],i,j))
                    xn.append(A[i]+A[j])

        x.sort()
        xn.sort()

    for i in range(2,N+1):
        if A[i-1] > A[i]:
            make_x(x,xn,i-1)
            j = bisect.bisect(xn,A[i]-A[i-1])
            if j > 0 and i == 2:
                for k in x[j-1][1:]:
                    if k > 0:
                        ans.append((k,i-1))
                    A[i-1] += xn[j-1]
            else:
                make_x(x,xn,i)
                j = bisect.bisect_left(xn,A[i-1]-A[i])
                for k in x[j][1:]:
                    if k > 0: ans.append((k,i))
                A[i] += xn[j]

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

if __name__ == '__main__':
    main()