Problem p03007 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split(" ")))
A.sort()

n_pos = len([a for a in A if a > 0])
operations = []
if n_pos == 0:
    pos = [A[-1]]
    neg = A[:-1]
elif n_pos == N:
    pos = A[1:]
    neg = A[:1]
else:
    pos = [a for a in A if a > 0]
    neg = [a for a in A if a <= 0]
if len(pos) >= 2:
    tmp = neg[0]
    for i in range(len(pos)-1):
        operations.append((tmp, pos[i]))
        tmp = tmp - pos[i]
    operations.append((pos[-1], tmp))
    tmp = pos[-1] - tmp
    for i in range(1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
else:
    tmp = pos[0]
    for i in range(len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
print(tmp)
for o in operations:
    print("{} {}".format(o[0], o[1]))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split(" ")))
A.sort()

n_pos = len([a for a in A if a > 0])
if n_pos == 0:
    pos = [A[-1]]
    neg = A[:-1]
elif n_pos == N:
    pos = A[1:]
    neg = A[:1]
else:
    pos = [a for a in A if a > 0]
    neg = [a for a in A if a <= 0]
if len(pos) >= 2:
    tmp = neg[0]
    for i in range(len(pos)-1):
        operations.append((tmp, pos[i]))
        tmp = tmp - pos[i]
    operations.append((pos[-1], tmp))
    tmp = pos[-1] - tmp
    for i in range(1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
else:
    tmp = pos[0]
    for i in range(1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
print(tmp)
for o in operations:
    print("{} {}".format(o[0], o[1]))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split(" ")))
A.sort()

n_pos = len([a for a in A if a > 0])
operations = []
if n_pos <= (N + 1) // 2:
    pos = [a for a in A if a > 0]
    neg = [a for a in A if a <= 0]
    tmp = pos[0]
    for i in range(n_pos-1):
        operations.append((neg[i], tmp))
        tmp = neg[i] - tmp
        operations.append((pos[i+1], tmp))
        tmp = pos[i+1] - tmp
        
    for i in range(n_pos-1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
else:
    neg = A[:N//2]
    pos = A[N//2:]
    if N % 2 == 1:
        tmp = pos[0]
        for i in range(N//2):
            operations.append((neg[i], tmp))
            tmp = neg[i] - tmp
            operations.append((pos[i+1], tmp))
            tmp = pos[i+1] - tmp
    else:
        operations.append((pos[0], neg[0]))
        tmp = pos[0] - neg[0]
        for i in range(1, N//2):
            operations.append((neg[i], tmp))
            tmp = neg[i] - tmp
            operations.append((pos[i], tmp))
            tmp = pos[i] - tmp

print(tmp)
for o in operations:
    print("{} {}".format(o[0], o[1]))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split(" ")))
A.sort()

n_pos = len([a for a in A if a > 0])
operations = []
if n_pos <= (N + 1) // 2:
    pos = [a for a in A if a > 0]
    neg = [a for a in A if a <= 0]
    tmp = pos[0]
    for i in range(n_pos-1):
        operations.append((neg[i], tmp))
        tmp = neg[i] - tmp
        operations.append((pos[i+1], tmp))
        tmp = pos[i+1] - tmp
        
    for i in range(n_pos-1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
else:
    neg = A[:N//2]
    pos = A[N//2:]
    if N % 2 == 1:
        tmp = pos[0]
        for i in range(N//2):
            operations.append((neg[i], tmp))
            tmp = neg[i] - tmp
            operations.append((pos[i+1], tmp))
            tmp = pos[i+1] - tmp
    else:
        operations.append((pos[0], neg[0]))
        tmp = pos[0] - neg[0]
        if i in range(1, N//2):
            operations.append((neg[i], tmp))
            tmp = neg[i] - tmp
            operations.append((pos[i], tmp))
            tmp = pos[i] - tmp

print(tmp)
for o in operations:
    print("{} {}".format(o[0], o[1]))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split(" ")))
A.sort()

n_pos = len([a for a in A if a > 0])
operations = []
if n_pos <= (N + 1) // 2:
    pos = [a for a in A if a > 0]
    neg = [a for a in A if a <= 0]
    tmp = pos[0]
    for i in range(n_pos-1):
        operations.append((neg[i], tmp))
        tmp = neg[i] - tmp
        operations.append((pos[i+1], tmp))
        tmp = pos[i+1] - tmp
        
    for i in range(n_pos-1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
else:
    neg = A[:N//2]
    pos = A[N//2:]
    if N % 2 == 1:
        tmp = pos[0]
        for i in range(N//2):
            operations.append((neg[i], tmp))
            tmp = neg[i] - tmp
            operations.append((pos[i+1], tmp))
            tmp = pos[i+1] - tmp
    else:
        operations.append(pos[0], neg[0])
        tmp = pos[0] - neg[0]
        if i in range(1, N//2):
            operations.append(neg[i], tmp)
            tmp = neg[i] - tmp
            operations.append(pos[i], tmp)
            tmp = pos[i] - tmp

print(tmp)
for o in operations:
    print("{} {}".format(o[0], o[1]))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split(" ")))
A.sort()

n_pos = len([a for a in A if a > 0])
operations = []
if n_pos <= (N + 1) // 2:
    pos = [a for a in A if a > 0]
    neg = [a for a in A if a <= 0]
    tmp = pos[0]
    for i in range(n_pos-1):
        operations.append((neg[i], tmp))
        tmp = neg[i] - tmp
        operations.append((pos[i+1], tmp))
        tmp = pos[i+1] - tmp
        
    for i in range(n_pos-1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
else:
    neg = A[:N//2]
    pos = A[N//2:]
    tmp = pos[0]
    for i in range(N//2):
        operations.append((neg[i], tmp))
        tmp = neg[i] - tmp
        operations.append((pos[i+1], tmp))
        tmp = pos[i+1] - tmp

print(tmp)
for o in operations:
    print("{} {}".format(o[0], o[1]))