Problem p03496 (python)

Fixed submission:
if __name__=="__main__":
    N = int(input())
    l = list(map(int,input().split(" ")))
    record = []
    dic = {i:l[i] for i in range(N)}
    sorted_list = sorted(dic.items(),key=lambda d:d[1],reverse=True)
    if sorted_list[N-1][1]>=0:
        for i in range(N-1):
            record.append([i, i+1])
    elif sorted_list[0][1]<0:
        for i in range(N-1,0,-1):
            record.append([i, i-1])
    elif sorted_list[0][1]+sorted_list[N-1][1]>=0:
        record.append([sorted_list[0][0], 0])
        record.append([sorted_list[0][0], 1])
        record.append([0,1])
        for i in range(1,N-1):
            record.append([sorted_list[0][0], i+1])
            record.append([i,i+1])
    elif sorted_list[0][1]+sorted_list[N-1][1]<0:
        record.append([sorted_list[N-1][0], N-1])
        record.append([sorted_list[N-1][0], N-2])
        record.append([N-1,N-2])
        for i in range(N-2,0,-1):
            record.append([sorted_list[N-1][0], i-1])
            record.append([i,i-1])
    print(len(record))
    for i in range(len(record)):
         print(str(record[i][0]+1)+" "+str(record[i][1]+1))

Buggy submission (verdict: Runtime Error):
if __name__=="__main__":
    N = int(input())
    l = list(map(int,input().split(" ")))
    record = []
    dic = {i:l[i] for i in range(N)}
    count = 0
    sorted_list = sorted(dic.items(),key=lambda d:d[1],reverse=True)
    if sorted_list[0][1]+sorted_list[N-1][1]>=0:
        while dic[N-1]<dic[N-2]:
            dic[N-1]=dic[N-1]+sorted_list[count][1]
            record.append([sorted_list[count][0],N-1])
        for i in range(N-2,0,-1):
            count = 0
            if dic[i]>=dic[i-1]:
                continue
            while dic[i]<dic[i-1]:
                if dic[i] + sorted_list[count][1]>dic[i+1]:
                    count+=1
                    continue
                dic[i] = dic[i] + sorted_list[count][1]
            record.append([sorted_list[count][0], i])
    elif sorted_list[0][1]+sorted_list[N-1][1]<0:
        sorted_list = sorted(dic.items(),key=lambda d:d[1],reverse=False)
        while dic[N - 1] < dic[N - 2]:
            dic[N - 2] = dic[N - 2] + sorted_list[count][1]
            record.append([sorted_list[count][0], N - 2])
        for i in range(N - 2, 0, -1):
            count = 0
            if dic[i] >= dic[i - 1]:
                continue
            while dic[i] < dic[i - 1]:
                dic[i-1] = dic[i-1] + sorted_list[count][1]
            record.append([sorted_list[count][0], i-1])
        
    print(len(record))
    for i in range(len(record)):
         print(str(record[i][0]+1)+" "+str(record[i][1]+1))

Buggy submission (verdict: Runtime Error):
if __name__=="__main__":
    N = int(input())
    l = list(map(int,input().split(" ")))
    record = []
    dic = {i:l[i] for i in range(N)}
    count = 0
    sorted_list = sorted(dic.items(),key=lambda d:d[1],reverse=True)
    if sorted_list[0][1]>0:
        while dic[N-1]<dic[N-2]:
            dic[N-1]=dic[N-1]+sorted_list[count][1]
            record.append([sorted_list[count][0],N-1])
        for i in range(N-2,0,-1):
            count = 0
            if dic[i]>=dic[i-1]:
                continue
            while dic[i]<dic[i-1]:
                if dic[i] + sorted_list[count][1]>dic[i+1]:
                    count+=1
                    continue
                dic[i] = dic[i] + sorted_list[count][1]
            record.append([sorted_list[count][0], i])
    else:
        sorted_list = sorted(dic.items(),key=lambda d:d[1],reverse=False)
        while dic[N - 1] < dic[N - 2]:
            dic[N - 2] = dic[N - 2] + sorted_list[count][1]
            record.append([sorted_list[count][0], N - 2])
        for i in range(N - 2, 0, -1):
            count = 0
            if dic[i] >= dic[i - 1]:
                continue
            while dic[i] < dic[i - 1]:
                dic[i-1] = dic[i-1] + sorted_list[count][1]
            record.append([sorted_list[count][0], i-1])
    print(len(record))
    for i in range(len(record)):
         print(str(record[i][0]+1)+" "+str(record[i][1]+1))

Buggy submission (verdict: Runtime Error):
if __name__=="__main__":
    N = int(input())
    l = list(map(int,input().split(" ")))
    record = []
    dic = {i:l[i] for i in range(N)}
    count = 0
    sorted_list = sorted(dic.items(),key=lambda d:d[1],reverse=True)
    if sorted_list[0][1]>0:
        while dic[N-1]<dic[N-2]:
            dic[N-1]=dic[N-1]+sorted_list[count][1]
            record.append([sorted_list[count][0],N-1])
        for i in range(N-2,0,-1):
            count = 0
            if dic[i]>=dic[i-1]:
                continue
            while dic[i]<dic[i-1]:
                if dic[i] + sorted_list[count][1]>dic[i+1]:
                    count+=1
                    continue
                dic[i] = dic[i] + sorted_list[count][1]
            record.append([sorted_list[count][0], i])
    else:
        sorted_list = sorted(dic.items(),key=lambda d:d[1],reverse=False)
        while dic[N - 1] < dic[N - 2]:
            dic[N - 2] = dic[N - 2] + sorted_list[count][1]
            record.append([sorted_list[count][0], N - 2])
        for i in range(N - 2, 0, -1):
            count = 0
            if dic[i] >= dic[i - 1]:
                continue
            while dic[i] < dic[i - 1]:
                dic[i-1] = dic[i-1] + sorted_list[count][1]
            record.append([sorted_list[count][0], i-1])
    print(len(record))
    for i in range(len(record)):
         print(str(record[i][0]+1)+" "+str(record[i][1]+1))