Problem p03007 (python)

Fixed submission:
import sys

def bigsmall(x,y):
    return (x,y) if x>y else (y,x)

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[-1])
    if a[1] <= 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    del a[-1]
    a.append(-1) # to set answer > 0

    for i in range(1, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] <= 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

def bigsmall(x,y):
    return (x,y) if x>y else (y,x)

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[-1])
    if a[1] <= 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    a.remove(len(a))
    a.append(-1) # to set answer > 0

    for i in range(1, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] <= 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

def bigsmall(x,y):
    return x,y if x>y else y,x

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    if a[0] < 0 and a[-1] > 0:
        first = a.pop(-1)
        a.insert(0,first)

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[1])
    if a[2] <= 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    for i in range(2, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] <= 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    if a[-1] <= 0:
        ops.append((curr, a[-1]))
        curr = curr - a[-1]
    else:
        ops.append((a[-1], curr))
        curr = a[-1] - curr

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

def main():
    n = int(input())
    a = [int(x) for x in input().split()]

    diff_idx = -1
    min_idx = -1
    _min = 10*6
    for i in range(len(a)-1):
        if a[i] * a[i+1] <= 0 and (a[i] != 0 or a[i+1] != 0):
            diff_idx = i
            break
        tmp_min = abs(a[i]) + abs(a[i+1])
        if tmp_min < _min:
            _min = tmp_min
            min_idx = i

    start_idx = diff_idx if diff_idx != -1 else tmp_min
    ops = []

    a2 = a[start_idx:]
    a2.extend(a[:start_idx])

    b,s = (a[0],a[1]) if a[0] > a[1] else (a[1],a[0])

    if a2[2] < 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    for i in range(2,len(a2)-1):
        b = curr
        s = a[i]

        if curr < a[i]:
            b,s = s,b

        if a2[i+1] < 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    if a2[-1] < 0:
        ops.append((curr, a2[-1]))
        curr = curr - a2[-1]
    else:
        ops.append((a2[-1], curr))
        curr = a2[-1] - curr

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

def main():
    n = int(input())
    a = [int(x) for x in input().split()]

    diff_idx = -1
    min_idx = -1
    _min = 10*6
    for i in range(len(a)-1):
        if a[i] * a[i+1] <= 0 and (a[i] != 0 or a[i+1] != 0):
            diff_idx = i
            break
        tmp_min = abs(a[i]) + abs(a[i+1])
        if tmp_min < _min:
            _min = tmp_min
            min_idx = i

    start_idx = diff_idx if diff_idx != -1 else tmp_min
    ops = []

    a2 = a[start_idx:]
    a2.extend(a[:start_idx])

    b,s = (a[0],a[1]) if a[0] > a[1] else (a[1],a[0])

    if a2[2] < 0:
        curr = b - s
        ops.append((b,s))
    else:
        curr = s - b
        ops.append((s,b))

    for i in range(2,len(a2)-1):
        b = curr
        s = a[i]

        if curr < a[i]:
            b,s = s,b

        if a2[i+1] < 0:
            curr = b - s
            ops.append((b,s))
        else:
            curr = s - b
            ops.append((s,b))

    if a2[-1] < 0:
        curr = curr - a2[-1]
        ops.append((curr, a2[-1]))
    else:
        curr = a2[-1] - curr
        ops.append((a2[-1], curr))

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()