Problem p03007 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main():
    N = II()
    A = LI()

    A.sort()
    from bisect import bisect
    slice_ = bisect(A, 0)
    from collections import deque
    minus = deque(A[:slice_])
    plus = deque(A[slice_:])
    # print(minus, plus)

    if minus and plus:
        first_plus = plus.popleft()
        first_minus = minus.popleft()
    elif minus and not plus:
        first_plus = minus.pop()  # must be the max of minuses
        first_minus = minus.popleft()
    elif plus and not minus:
        first_minus = plus.popleft()  # must be the min of pluses
        first_plus = plus.popleft()

    # first_plus - (first_minus - plus - plus - ...) - minus - minus - ...
    tmp = first_minus
    ans_li = []
    while len(plus) > 0:
        p = plus.popleft()
        ans_li.append((tmp, p))
        tmp = tmp - p
    ans_li.append((first_plus, tmp))
    tmp = first_plus - tmp
    while len(minus) > 0:
        m = minus.popleft()
        ans_li.append((tmp, m))
        tmp = tmp - m
    print(tmp)
    for i in ans_li: print('{} {}'.format(*i))


main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main():
    N = II()
    A = LI()

    A.sort()
    from bisect import bisect
    slice_ = bisect(A, 0)
    from collections import deque
    minus = deque(A[:slice_])
    plus = deque(A[slice_:])
    # print(minus, plus)

    if minus and plus:
        first_plus = plus.popleft()
        first_minus = minus.popleft()
    elif minus and not plus:
        first_plus = minus.popright()  # must be the max of minuses
        first_minus = minus.popleft()
    elif plus and not minus:
        first_minus = plus.popleft()  # must be the min of pluses
        first_plus = plus.popleft()

    # first_plus - (first_minus - plus - plus - ...) - minus - minus - ...
    tmp = first_minus
    ans_li = []
    while len(plus) > 0:
        p = plus.popleft()
        ans_li.append((tmp, p))
        tmp = tmp - p
    ans_li.append((first_plus, tmp))
    tmp = first_plus - tmp
    while len(minus) > 0:
        m = minus.popleft()
        ans_li.append((tmp, m))
        tmp = tmp - m
    print(tmp)
    for i in ans_li: print('{} {}'.format(*i))

main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main():
    N = II()
    A = LI()

    A.sort()
    from collections import deque

    if N%2 != 0:
        if -sum(A[:N//2])+sum(A[N//2:]) > -sum(A[:N//2+1]) + sum(A[N//2+1:]):
            minus = deque(A[:N//2])
            plus = deque(A[N//2:])
            ans_li = []
            p = plus.popleft()
            m = minus.popleft()
            tmp = m - p
            ans_li.append('{} {}'.format(m, p))
            while True:
                try:
                    p = plus.popleft()
                    ans_li.append('{} {}'.format(p, tmp))
                    tmp = p - tmp
                    m = minus.popleft()
                    ans_li.append('{} {}'.format(m, tmp))
                    tmp = m - tmp
                except:
                    print(tmp)
                    for i in ans_li: print(i)
                    return

        else:
            minus = deque(A[:N//2+1])
            plus = deque(A[N//2+1:])
            m = minus.popleft()
            p = plus.popleft()
            tmp = p - m
            ans_li = ['{} {}'.format(p, m)]
            while True:
                try:
                    m = minus.popleft()
                    if len(minus) == 0:
                        ans_li.append('{} {}'.format(tmp, m))
                        tmp = tmp - m
                    else:
                        ans_li.append('{} {}'.format(m, tmp))
                        tmp = m - tmp
                    p = plus.popleft()
                    ans_li.append('{} {}'.format(p, tmp))
                    tmp = p - tmp
                except:
                    print(tmp)
                    for i in ans_li: print(i)
                    return

    else:
        minus = deque(A[:N//2])
        plus = deque(A[N//2:])
        m = plus.popleft()
        p = minus.popleft()
        tmp = m - p
        ans_li.append('{} {}'.format(m, p))
        while True:
            try:
                m = minus.popleft()
                ans_li.append('{} {}'.format(m, tmp))
                tmp = m - tmp
                p = plus.popleft()
                ans_li.append('{} {}'.format(p, tmp))
                tmp = p - tmp
            except:
                print(tmp)
                for i in ans_li: print(i)
                return


main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main():
    N = II()
    A = LI()

    A.sort()
    from collections import deque

    if N%2 != 0:
        if -sum(A[:N//2])+sum(A[N//2:]) > -sum(A[:N//2+1]) + sum(A[N//2+1:]):
            minus = deque(A[:N//2])
            plus = deque(A[N//2:])
            ans_li = []
            p = plus.popleft()
            m = minus.popleft()
            tmp = m - p
            ans_li.append('{} {}'.format(m, p))
            while True:
                try:
                    p = plus.popleft()
                    ans_li.append('{} {}'.format(p, tmp))
                    tmp = p - tmp
                    m = minus.popleft()
                    ans_li.append('{} {}'.format(m, tmp))
                    tmp = m - tmp
                except:
                    print(tmp)
                    for i in ans_li: print(i)
                    return

        else:
            minus = deque(A[:N//2+1])
            plus = deque(A[N//2+1:])
            m = minus.popleft()
            p = plus.popleft()
            tmp = p - m
            ans_li = ['{} {}'.format(p, m)]
            while True:
                try:
                    m = minus.popleft()
                    if len(minus) == 0:
                        ans_li.append('{} {}'.format(tmp, m))
                        tmp = tmp - m
                    else:
                        ans_li.append('{} {}'.format(m, tmp))
                        tmp = m - tmp
                    p = plus.popleft()
                    ans_li.append('{} {}'.format(p, tmp))
                    tmp = p - tmp
                except:
                    print(tmp)
                    for i in ans_li: print(i)
                    return

    else:
        m = plus.popleft()
        p = minus.popleft()
        tmp = m - p
        ans_li.append('{} {}'.format(m, p))
        while True:
            try:
                m = minus.popleft()
                ans_li.append('{} {}'.format(m, tmp))
                tmp = m - tmp
                p = plus.popleft()
                ans_li.append('{} {}'.format(p, tmp))
                tmp = p - tmp
            except:
                print(tmp)
                for i in ans_li: print(i)
                return


main()