Problem p03007 (python)

Fixed submission:
n = int(input())
a = list(map(int , input().split()))
pos = sorted([i for i in a if i >= 0], reverse=True)
neg = sorted([i for i in a if i < 0], reverse=True)

ans = []
if len(neg) == 0 and len(pos) == 2:
    print(pos[0] - pos[1])
    print(pos[0], pos[1])
    exit()
elif len(pos) == 0:
    tmp = neg[0]
    for i in range(1,len(neg)):
        ans.append([tmp, neg[i]])
        tmp -= neg[i]
    print(tmp)
    [print(*i) for i in ans]
    exit()
elif len(neg) == 0:
    neg.append(pos.pop())

tmp = neg.pop()
while len(pos) > 1:
    a = pos.pop()
    ans.append([tmp, a])
    tmp -= a
neg.append(tmp)

tmp = pos[0]
for i in neg:
    ans.append([tmp, i])
    tmp -= i

print(tmp)
[print(*i) for i in ans]

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int , input().split()))
pos = sorted([i for i in a if i >= 0], reverse=True)
neg = sorted([i for i in a if i < 0], reverse=True)

ans = []
if len(neg) == 0 and len(pos) == 2:
    print(pos[0] - pos[1])
    print(pos[0], pos[1])
    exit()
elif len(pos) == 0:
    tmp = neg[0]
    neg = neg[1:]
    neg.append(tmp)
elif len(neg) == 0:
    neg.append(pos.pop())

tmp = neg.pop()
while len(pos) > 1:
    a = pos.pop()
    ans.append([tmp, a])
    tmp -= a
neg.append(tmp)

tmp = pos[0]
for i in neg:
    ans.append([tmp, i])
    tmp -= i

print(tmp)
[print(*i) for i in ans]

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int , input().split()))
pos = sorted([i for i in a if i >= 0], reverse=True)
neg = sorted([i for i in a if i < 0], reverse=True)

ans = 0
if len(neg) == 0 and len(pos) == 2:
    print(pos[0] - pos[1])
    print(pos[0], pos[1])
    exit()
elif len(pos) == 0 and len(neg) == 2:
    print(neg[0] - neg[1])
    print(neg[0], neg[1])
    exit()
elif len(neg) == 0:
    neg.append(pos.pop())

tmp = neg.pop()
ans = []
while len(pos) > 1:
    a = pos.pop()
    ans.append([tmp, a])
    tmp -= a

for i in neg:
    ans.append(tmp, i)
    tmp -= i

ans.append([pos[0], tmp])
print(pos[0] - tmp)
[print(*i) for i in ans]

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int , input().split()))
pos = sorted([i for i in a if i >= 0], reverse=True)
neg = sorted([i for i in a if i < 0])

ans = 0
if len(neg) == 0 and len(pos) == 2:
    print(pos[0], pos[1])
    exit()
elif len(neg) == 0:
    neg.append(pos.pop())

tmp = neg.pop()
ans = []
while len(pos) > 1:
    a = pos.pop()
    ans.append([tmp, a])
    tmp -= a

for i in neg:
    ans.append(tmp, i)
    tmp -= i

ans.append([pos[0], tmp])
print(pos[0] - tmp)
[print(*i) for i in ans]

Buggy submission (verdict: Runtime Error):
from heapq import heappop, heappush, heapify
N = int(input())
A = list(map(int, input().split()))
p = []
for i in A:
    if i > 0:
        tmp = [i, 1]
    elif i < 0:
        tmp = [-i, -1]
    else:
        N -= 1
        continue
    heappush(p, tmp)
res = []
for i in range(N-1):
    x, y = heappop(p), heappop(p)
    x = x[0] * x[1]
    y = y[0] * y[1]
    if p == []:
        if x-y > y-x:
            res.append([x,y])
            tmp = x-y
        else:
            res.append([y,x])
            tmp = y-x
        continue
    if p[0][1] > 0:
        tmp = min(x-y, y-x)
    else:
        tmp = max(x-y, y-x)
    if tmp == y-x:
        x,y=y,x
    if tmp > 0:
        tmp = [tmp, 1]
    else:
        tmp = [-tmp, -1]
    res.append([x,y])
    heappush(p, tmp)
    
print(tmp)
for i in res:
    print(*i)