Problem p03007 (python)

Fixed submission:
n = int(input())
a = [int(i) for i in input().split()]
a_2 = [abs(a[i]) for i in range(n)]
a.sort()

if a[0] >= 0:
    print([sum(a_2) -2, sum(a_2)][a[0] == 0])
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(aa, a[i + 1])
            aa = aa - a[i + 1]
        elif i == n - 2:
            print(a[i + 1], aa)
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

elif a[-1] <= 0:

    print([sum(a_2) -2, sum(a_2)][a[-1] == 0])
    a.reverse()
    if a[-1] == 0:
        a = [a[-1]] + a[0:]
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(aa, a[i + 1])
            aa = aa - a[i + 1]
        elif i == n - 2:
            print(aa, a[i + 1])
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

else:
    print(sum(a_2))
    minus = []
    plus = []
    for i in range(n):
        if a[i] < 0:
            minus.append(a[i])
        else:
            plus.append(a[i])
    minus2 = minus.pop()
    plus2 = plus.pop()
    for i in range(len(minus)):
        print(plus2 ,minus[i])
        plus2 -= minus[i]
    for i in range(len(plus)):
        print(minus2, plus[i])
        minus2 -= plus[i]
    print(plus2, minus2)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(i) for i in input().split()]
a_2 = [abs(a[i]) for i in range(n)]
a.sort()

if a[0] >= 0:
    print([sum(a_2) -2, sum(a_2)][a[0] == 0])
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(aa, a[i + 1])
            aa = aa - a[i + 1]
        elif i == n - 2:
            print(a[i + 1], aa)
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

elif a[-1] <= 0:
    print(aaaaaa)

    print([sum(a_2) -2, sum(a_2)][a[-1] == 0])
    if a[-1] == 0:
        a = [a[0]] + [a[-1]] + a[1:]
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(a[i+1], aa)
            aa = a[i + 1] - aa
        elif i == n - 2:
            print(aa, a[i + 1])
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

else:
    print(sum(a_2))
    minus = []
    plus = []
    for i in range(n):
        if a[i] < 0:
            minus.append(a[i])
        else:
            plus.append(a[i])
    minus2 = minus.pop()
    plus2 = plus.pop()
    for i in range(len(minus)):
        print(plus2 ,minus[i])
        plus2 -= minus[i]
    for i in range(len(plus)):
        print(minus2, plus[i])
        minus2 -= plus[i]
    print(plus2, minus2)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(i) for i in input().split()]
a_2 = [abs(a[i]) for i in range(n)]
a.sort()

if a[0] >= 0:
    print([sum(a_2) -2, sum(a_2)][a[0] == 0])
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(aa, a[i + 1])
            aa = aa - a[i + 1]
        elif i == n - 2:
            print(a[i + 1], aa)
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

elif a[-1] <= 0:
    if:
        
    print([sum(a_2) -2, sum(a_2)][a[-1] == 0])
    if a[-1] == 0:
        a = [a[0]] + [a[-1]] + a[1:]
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(a[i+1], aa)
            aa = a[i + 1] - aa
        elif i == n - 2:
            print(aa, a[i + 1])
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

else:
    print(sum(a_2))
    minus = []
    plus = []
    for i in range(n):
        if a[i] < 0:
            minus.append(a[i])
        else:
            plus.append(a[i])
    minus2 = minus.pop()
    plus2 = plus.pop()
    for i in range(len(minus)):
        print(plus2 ,minus[i])
        plus2 -= minus[i]
    for i in range(len(plus)):
        print(minus2, plus[i])
        minus2 -= plus[i]
    print(plus2, minus2)