Problem p03007 (python)

Fixed submission:
import sys, math, collections, heapq, itertools
from bisect import bisect_left
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [[A[1], A[0]]]
    else:
        plus = bisect_left(A, 0)
        if plus == 0:
            ans = A[0]
            for i in range(1, N-1):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[N-1], ans])
            ans = A[N-1] - ans
        elif plus < N:
            ans = A[0]
            for i in range(plus, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[N-1], ans])
            ans = A[N-1] - ans
            for i in range(1, plus):
                out.append([ans, A[i]])
                ans -= A[i]
        else:
            ans = A[N-1]
            for i in range(N-1):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))


    return 0
  
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
from bisect import bisect_left
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    else:
        plus = bisect_left(A, 0)
        if plus == 0:
            ans = A[0]
            for i in range(1, N-1):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[N-1], ans])
            ans = A[N-1] - ans
        elif plus < N:
            ans = A[0]
            for i in range(plus, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[N-1], ans])
            ans = A[N-1] - ans
            for i in range(1, plus):
                out.append([ans, A[i]])
                ans -= A[i]
        else:
            ans = A[N-1]
            for i in range(N-1):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))


    return 0
  
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
from bisect import bisect_left
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    else:
        plus = bisect_left(A, 0)
        if plus == 0: plus = 1
        if plus < N:
            ans = A[0]
            for i in range(plus, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[N-1], ans])
            ans = A[N-1] - ans
            for i in range(1, plus):
                out.append([ans, A[i]])
                ans -= A[i]
        else:
            ans = A[N-1]
            for i in range(N-1):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))


    return 0
  
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
from bisect import bisect_left
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    elif A[N-2] <= 0:
        ans = A[N-1] - A[N-2]
        out.append([A[N-1], A[N-2]])
        for i in range(N-2):
            out.append([ans, A[i]])
            ans -= A[i]
    elif A[1] >= 0:
        ans = A[0] - A[1]
        out.append([A[0], A[1]])
        for i in range(2, N-1):
            out.append([ans, A[i]])
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
    else:
        start = bisect_left(A, 0)
        start -= 1
        ans = A[start] - A[start + 1]
        out.append([A[start], A[start + 1]])
        if start + 2 < N - 1:
            for i in range(start + 2, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
        if start > 0:
            for i in range(start):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))

    return 0
  
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
from bisect import bisect_left
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    elif A[N-1] <= 0:
        ans = A[N-1] - A[N-2]
        out.append([A[N-1], A[N-2]])
        for i in range(N-2):
            out.append([ans, A[i]])
            ans -= A[i]
    elif A[0] >= 0:
        ans = A[0] - A[1]
        out.append([A[0], A[1]])
        for i in range(2, N-1):
            out.append([ans, A[i]])
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
    else:
        start = bisect_left(A, 0)
        start -= 1
        ans = A[start] - A[start + 1]
        out.append([A[start], A[start + 1]])
        if start + 2 < N - 1:
            for i in range(start + 2, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
        if start > 0:
            for i in range(start):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))

    return 0
  
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    elif A[N-1] <= 0:
        ans = A[N-1] - A[N-2]
        out.append([A[N-1], A[N-2]])
        for i in range(N-2):
            out.append([ans, A[i]])
            ans -= A[i]
    elif A[0] >= 0:
        ans = A[0] - A[1]
        out.append([A[0], A[1]])
        for i in range(2, N-1):
            out.append([ans, A[i]])
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
    else:
        for i in range(1, N-1):
            if A[i] >= 0:
                start = i-1
                break
        else: start = N - 2
        ans = A[start] - A[start + 1]
        out.append([A[start], A[start + 1]])
        if start + 2 < N - 1:
            for i in range(start + 2, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
        if start > 0:
            for i in range(start):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))

    return 0
  
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    elif A[N-1] <= 0:
        ans = A[N-1] - A[N-2]
        out.append([A[N-1], A[N-2]])
        for i in range(N-2):
            out.append([ans, A[i]])
            ans -= A[i]
    elif A[0] >= 0:
        ans = A[0] - A[1]
        out.append([A[0], A[1]])
        for i in range(2, N-1):
            out.append([ans, A[i]])
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
    else:
        for i in range(1, N-1):
            if A[i] >= 0:
                start = i-1
                break
        ans = A[start] - A[start + 1]
        out.append([A[start], A[start + 1]])
        if start + 2 < N - 1:
            for i in range(start + 2, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
        if start > 0:
            for i in range(start):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))

    return 0
  
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    elif A[N-2] <= 0:
        ans = A[N-1] - A[N-2]
        out.append([A[N-1], A[N-2]])
        for i in range(N-2):
            out.append([ans, A[i]])
            ans -= A[i]
    elif A[1] >= 0:
        ans = A[0] - A[1]
        out.append([A[0], A[1]])
        for i in range(2, N-1):
            out.append([ans, A[i]])
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
    else:
        for i in range(1, N-1):
            if A[i] >= 0:
                start = i-1
                break
        ans = A[start] - A[start + 1]
        out.append([A[start], A[N-1]])
        if start + 2 < N - 1:
            for i in range(start + 2, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
        for i in range(start):
            out.append([ans, A[i]])
            ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))

    return 0
  
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    elif A[N-2] <= 0:
        ans = A[N-1] - A[N-2]
        out.append([A[N-1], A[N-2]])
        for i in range(N-2):
            out.append([ans, A[i]])
            ans -= A[i]
    elif A[1] >= 0:
        ans = A[0] - A[1]
        out.append([A[0], A[1]])
        for i in range(2, N-1):
            out.append([ans, A[i]])
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
    else:
        for i in range(1, N-1):
            if A[i] >= 0:
                start = i-1
                break
        ans = A[0] - A[N-1]
        out.append([A[0], A[N-1]])
        if start + 2 < N - 1:
            for i in range(start + 2 , N -1):
                out.append([ans, A[i]])
                ans -= A[i]
        if abs(A[start]) <= A[start + 1]:
            out.append([ans, A[start + 1]])
            ans -= A[start + 1]
            out.append([A[start], ans])
            ans = A[start] - ans
            for i in range(1, start):
                out.append([ans, A[i]])
                ans -= A[i]
        else:
            out.append([A[start + 1], ans])
            ans = A[start + 1] - ans
            for i in range(1, start + 1):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))

    return 0
  
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    elif A[N-2] <= 0:
        ans = A[N-1] - A[N-2]
        out.append([A[N-1], A[N-2]])
        for i in range(N-2):
            out.append([ans, A[i]])
            ans -= A[i]
    elif A[1] >= 0:
        ans = A[0] - A[1]
        out.append([A[0], A[1]])
        for i in range(2, N-1):
            out.append([ans, A[i]])
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
    else:
        for i in range(1, N-1):
            if A[i] >= 0:
                start = i-1
                break
        Ans = A[0] - A[N-1]
        out.append([A[0], A[N-1]])
        if start + 2 < N - 1:
            for i in range(start + 2 , N -1):
                out.append([ans, A[i]])
                ans -= A[i]
        if abs(A[start]) <= A[start + 1]:
            out.append([ans, A[start + 1]])
            ans -= A[start + 1]
            out.append([A[start], ans])
            ans = A[start] - ans
            for i in range(1, start):
                out.append([ans, A[i]])
                ans -= A[i]
        else:
            out.append([A[start + 1], ans])
            ans = A[start + 1] - ans
            for i in range(1, start + 1):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))

    return 0
  
if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    elif A[N-2] < 0:
        ans = A[N-1] - A[N-2]
        out.append([A[N-1], A[N-2]])
        for i in reversed(range(N-2)):
            out.append([ans, A[i]])
            ans -= A[i]
    elif A[1] >= 0:
        ans = A[0] - A[1]
        out.append([A[0], A[1]])
        for i in range(2, N-1):
            out.append([ans, A[i]])
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
    else:
        for i in range(1, N-1):
            if A[i] >= 0:
                start = i-1
                break
        if -A[start] <= A[start + 1]:
            ans = A[0] - A[start + 1]
            out.append([A[0], A[start + 1]])
            for i in range(start + 2, N):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[start], ans])
            ans = A[start] - ans
            for i in range(1, start):
                out.append([ans, A[i]])
                ans -= A[i]
        else:
            ans = A[0] - A[start + 2]
            out.append([A[0], A[start + 2]])
            for i in range(start + 3, N):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[start + 1], ans])
            ans = A[start + 1] - ans
            for i in range(1, start + 1):
                out.append([ans, A[i]])
                ans -= A[i] 

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))

    return 0
  
if __name__ == "__main__":
    solve()