Problem:


Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0



Sample input:
3
1 -1 2


Sample output:
4
-1 1
2 -2

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))

B = [abs(A[i]) for i in range(N)]

det = 1
s = A[0]//B[0]#最初のやつの符号
for i in range(1,N):
    if s * A[i] < 0:
        det = -1
        break

if det == 1:#全て同符号
    m = min(B)

    M = sum(B) - 2 * m
    print(M)

    for i in range(N):
        if m == B[i]:
            p = i
            break

    C = [A[i]//B[i] for i in range(N)]

    C[p] *= -1

else:#異符号が含まれる
    M = sum(B)
    print(M)
    C = [A[i]//B[i] for i in range(N)]

fp = -1 #firstplus
fm = -1 #firstminus
for i in range(N):
    if fp == -1 and C[i] == 1:
        fp = i
    elif fm == -1 and C[i] == -1:
        fm = i
    elif fp != -1 and fm != -1:
        break

X = []
Y = []

for i in range(N):
    if i == fp or i == fm:
        continue
    if C[i] == -1:
        X.append(i)
    else:
        Y.append(i)

x = A[fp]
for i in X:
    print(x,A[i])
    x -= A[i]

y = A[fm]
for i in Y:
    print(y,A[i])
    y -= A[i]

print(x,y)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int,input().split()))

a, b = max(A), min(A)
for t in [a, b]:
    for i in range(len(A)):
        if A[i] == t:
            A.pop(i)

ans = a-b
for i in range(n-2):
    ans += abs(A[i])
print(ans)

for i in range(n-2):
    if A[i] > 0:
        print(b, A[i])
        b -= A[i]
    else:
        print(a, A[i])
        a -= A[i]
print(a, b)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int,input().split()))

a = max(A)
b = min(A)
A.pop(a)
A.pop(b)

ans = a-b
for i in range(n-2):
    ans += abs(A[i])
print(ans)

for i in range(n-2):
    if A[i] > 0:
        print(b, A[i])
        b -= A[i]
    else:
        print(a, A[i])
        a -= A[i]
print(a, b)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))

S = sum(map(abs,A))

if all([a>0 for a in A]):
    print(S-2*min(A))
    A = sorted(A)
    B = [A[0]]
    for i in range(1,N):
        B.append(B[-1]-A[i])
    for i in range(N-2):
        print(B[i],A[i+1])
    print(A[N-1],B[N-2])

elif all([a<0 for a in A]):
    print(S+2*max(A))
    A = sorted(A)
    B = [A[-1]]
    for i in range(N-1):
        B.append(B[-1]-A[i])
    for i in range(N-1):
        print(B[i],A[i])

else:
    print(S)
    A = sorted(A)
    B = [A[0]]
    for i in range(N):
        if A[-2-i]<0:
            print(A[-1-i],B[-1])
            B.append(A[-1-i]-B[-1])
            break
        print(B[-1],A[-1-i])
        B.append(B[-1]-A[-1-i])
    C = [B[-1]]
    for i in range(1,N):
        if A[i]>=0:
            break
        print(C[-1],A[i])
        C.append(C[-1]-A[i])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))

S = sum(map(abs,A))

if all([a>0 for a in A]):
    print(S-2*min(A))
    A = sorted(A)
    B = [A[0]]
    for i in range(1,N):
        B.append(B[-1]-A[i])
    for i in range(N-2):
        print(B[i],A[i+1])
    print(A[N-1],B[N-2])

elif all([a<0 for a in A]):
    print(S+2*max(A))
    A = sorted(A)
    B = [A[-1]]
    for i in range(N-1):
        B.append(B[-1]-A[i])
    for i in range(N-1):
        print(B[i],A[i])

else:
    print(S)
    A = sorted(A)
    B = [A[0]]
    for i in range(N):
        if A[-2-i]<0:
            print(A[-1-i],B[-1])
            B.append(A[-1-i]-B[-1])
            break
        print(B[-1],A[-1-i])
        B.append(B[-1]-A[-1-i])
    C = [B[-1]]
    for i in range(1,N):
        if A[i]>0:
            break
        print(C[-1],A[i])
        C.append(C[-1]-A[i])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))

S = sum(map(abs,A))

if all([a>0 for a in A]):
    print(S-2*min(A))
    A = sorted(A)
    B = [A[0]]
    for i in range(1,N):
        B.append(B[-1]-A[i])
    for i in range(N-2):
        print(B[i],A[i+1])
    print(A[N-1],B[N-2])

elif all([a<0 for a in A]):
    print(S+2*max(A))
    A = sorted(A)
    B = [A[-1]]
    for i in range(N-1):
        B.append(B[-1]-A[i])
    for i in range(N-1):
        print(B[i],A[i])

else:
    print(S)
    A = sorted(A)
    B = [A[0]]
    for i in range(N):
        if A[-2-i]<0:
            print(A[-1-i],B[-1])
            B.append(A[-1-i]-B[-1])
            break
        print(B[-1],A[-1-i])
        B.append(B[-1]-A[-1-i])
    C = [B[-1]]
    for i in range(1,N):
        if A[i]>0:
            break
        print(C[-1],A[i])
        C.append(C[-1],A[i])

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = sorted(LI())
if A[0] > 0:
    print(sum(A) - 2 * A[0])
    ret = A[0]
    for i in range(1, n - 1):
        print(ret, A[i])
        ret -= A[i]
    print(A[n - 1], ret)
elif A[-1] < 0:
    print(-sum(A) + A[-1] * 2)
    ret = A[-1]
    for i in range(n - 1):
        print(ret, A[i])
        ret -= A[i]
        print(1)
else:
    print(sum([abs(a) for a in A]))
    minus = [a for a in A if a < 0]
    zero = [a for a in A if a == 0]
    plus = [a for a in A if a > 0]
    if not minus:
        minus += zero
    else:
        plus += zero
    ret = minus[0]
    for j in range(1, len(plus)):
        print(ret, plus[j])
        ret -= plus[j]
    print(plus[0], ret)
    ret = plus[0] - ret
    for k in range(1, len(minus)):
        print(ret, minus[k])
        ret -= minus[k]

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = sorted(LI())
if A[0] > 0:
    print(sum(A) - 2 * A[0])
    ret = A[0]
    for i in range(1, n - 1):
        print(ret, A[i])
        ret -= A[i]
    print(A[n - 1], ret)
elif A[-1] < 0:
    print(-sum(A) + A[-1] * 2)
    ret = A[-1]
    for i in range(n - 1):
        print(ret, A[i])
        ret -= A[i]
else:
    print(sum([abs(a) for a in A]))
    minus = [a for a in A if a < 0]
    zero = [a for a in A if a == 0]
    plus = [a for a in A if a > 0]
    if not minus:
        minus += zero
    else:
        plus += zero
    ret = minus[0]
    for j in range(1, len(plus)):
        print(ret, plus[j])
        ret -= plus[j]
    print(plus[0], ret)
    ret = plus[0] - ret
    for k in range(1, len(minus)):
        print(ret, minus[k])
        ret -= minus[k]

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = sorted(LI())
if A[0] > 0:
    print(sum(A) - 2 * A[0])
    ret = A[0]
    for i in range(1, n - 1):
        print(ret, A[i])
        ret -= A[i]
    print(A[n - 1], ret)
elif A[-1] < 0:
    print(-sum(A) + A[-1] * 2)
    ret = A[-1]
    for i in range(1, n - 1):
        print(ret, A[i])
        ret -= A[i]
else:
    print(sum([abs(a) for a in A]))
    minus = [a for a in A if a < 0]
    zero = [a for a in A if a == 0]
    plus = [a for a in A if a > 0]
    if not minus:
        minus += zero
    else:
        plus += zero
    ret = minus[0]
    for j in range(1, len(plus)):
        print(ret, plus[j])
        ret -= plus[j]
    print(plus[0], ret)
    ret = plus[0] - ret
    for k in range(1, len(minus)):
        print(ret, minus[k])
        ret -= minus[k]

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = sorted(LI())
if A[0] > 0:
    print(sum(A) - 2 * A[0])
    ret = A[0]
    for i in range(1, n - 1):
        print(ret, A[i])
        ret -= A[i]
    print(A[n - 1], ret)
elif A[-1] < 0:
    print(-sum(A) + A[-1] * 2)
    ret = A[0]
    for i in range(1, n):
        print(ret, A[i])
        ret -= A[i]
else:
    print(sum([abs(a) for a in A]))
    minus = [a for a in A if a < 0]
    zero = [a for a in A if a == 0]
    plus = [a for a in A if a > 0]
    if not minus:
        minus += zero
    else:
        plus += zero
    ret = minus[0]
    for j in range(1, len(plus)):
        print(ret, plus[j])
        ret -= plus[j]
    print(plus[0], ret)
    ret = plus[0] - ret
    for k in range(1, len(minus)):
        print(ret, minus[k])
        ret -= minus[k]

Submission status: Runtime Error

Solution code:
from collections import deque
N = int(input())
A = sorted(list(map(int,input().split())))
A = deque(A)
C = []
if A[-1]<=0:
    a = A.pop()
    b = A.pop()
    C.append((a,b))
    A.append(a-b)
B = deque(list(A)[:-1])
while B[-1]>0:
    a = B.popleft()
    b = B.pop()
    C.append((a,b))
    B.appendleft(a-b)
B.append(A[-1])
while len(B)>1:
    a = B.pop()
    b = B.popleft()
    C.append((a,b))
    B.append(a-b)
print(a-b)
for i in range(N-1):
    print(C[i][0],C[i][1])

Submission status: Runtime Error

Solution code:
#%%
n = int(input())
a = list(map(int ,input().split()))

a.sort(reverse=True)
ans = []
for i in range(n):
    if i == 0:
        tmp = a[i]
    elif 1 <= i < n - 1:
        if a[i] > 0:
            ans.append(str(a[-1]) + ' ' +  str(a[i]))
            a[-1] -= a[i]
        else:
            ans.append(tmp + ' ' +  str(a[i]))
            tmp -= a[i]
    else:
        ans.append(str(tmp) + ' ' +  str(a[-1]))
        a = tmp - a[-1]
        print(a)

for i in range(len(ans)):
    x, y = map(int, ans[i].split())
    print(x, y)

Submission status: Runtime Error

Solution code:
#%%
n = int(input())
a = list(map(int ,input().split()))

a.sort(reverse=True)
ans = []
for i in range(n):
    if i == 0:
        tmp = a[i]
    elif 1 <= i < n - 1:
        if a[i] > 0:
            a[-1] -= a[i]
            ans.append(str(a[-1]) + ' ' +  str(a[i]))
        else:
            tmp -= a[i]
            ans.append(tmp + ' ' +  str(a[i]))
    else:
        ans.append(str(tmp) + ' ' +  str(a[-1]))
        a = tmp - a[-1]
        print(a)

for i in range(len(ans)):
    x, y = map(int, ans[i].split())
    print(x, y)

Submission status: Runtime Error

Solution code:
#%%
n = int(input())
a = list(map(int ,input().split()))

a.sort(reverse=True)
ans = []
for i in range(n):
    if i == 0:
        tmp = a[i]
    elif 1 <= i < n - 1:
        if a[i] > 0:
            a[-1] -= a[i]
            ans.append(str(a[-1]) + ' ' +  str(a[i]))
        else:
            tmp -= a[i]
            ans.append(tmp + ' ' +  str(a[i]))
    else:
        ans.append(str(tmp) + ' ' +  str(a[-1]))
        a = tmp - a[-1]
        print(a)

for i in range(len(ans)):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import bisect
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
sum_L = sum(L)
N_neg = bisect.bisect_left(L, 0)
process = []
if N_neg == 0:  # minのみ-にする
    ans = sum_L-min(L)*2
    l = min(L)
    for r in L[1:-1]:
        process.append(str(l)+" "+str(r))
        l -= r
    process.append(str(L[-1])+" "+str(l))
elif N_neg == N:  # maxのみを+にする
    ans = -sum_L(L)+max(L)*2
    l = L[-1]
    for r in reversed(L[:-1]):
        process.append(str(l)+" "+str(r))
        l -= r
else:  # negのやつを-にする
    ans = sum([abs(v) for v in L])
    l = L[0]
    for r in L[N_neg:-1]:
        process.append(str(l)+" "+str(r))
        l -= r
    process.append(str(L[-1])+" "+str(l))
    l = L[-1]-l
    for r in L[1:N_neg]:
        process.append(str(l)+" "+str(r))
        l -= r
print(ans)
print("\n".join(process))

Submission status: Runtime Error

Solution code:
n=int(input())
lists=list(map(int,input().split()))

mini=min(lists)
maxi=max(lists)

if mini>=0 or maxi<=0:
    
    lists=sorted(lists)
    ans=0
    length=len(lists)//2
    
    for i in range(len(lists)):
        if i<=length-1:
            ans-=lists[i]
        else:
            ans+=lists[i]
    print(ans)
    
    some=lists[-1]
    
    if len(lists)%2!=0:
        some=lists[length]
        print(lists[0],some)
        some=lists[0]-some
        for i in range(1,length):
            print(lists[i+length],some)
            some=lists[i+length]-some
            print(lists[i],some)
            some=lists[i]-some
        print(lists[-1],some)

        
    elif len(lists)%2==0:
        some=lists[length]
        print(lists[0],some)
        some=lists[0]-some
        for i in range(1,length-1):
            print(lists[i+length],some)
            some=lists[i+length]-some
            print(lists[i],some)
            some=lists[i]-some
        print(lists[-1],some)
        some=lists[-1]-some
        print(some,lists[length-1])
        some=some-lists[length-1]


else:
    lists=sorted(lists)
    positive=0
    negative=0
    zero=0
    
    for some in lists:
        if some >0:
            positive+=1  
        elif some==0:
            zero+=1 
        elif some<0:
            negative+=1
    ans=0
    if negative>positive:

        z=0
        for _ in  lists:
            z+=abs(_)
        print(z)
        #解答の出力
        ans=lists[-1]
        for i in range(negative-positive):
            print(ans,lists[i])
            ans=ans-lists[i]
        x=negative-positive
        for j in range(positive-1):
            print(ans,lists[x+j])
            ans=ans-lists[x+j]
            print(lists[x+j+1],ans)
            ans=lists[x+j+1]-ans

        print(lists[-negative],ans)
        for i in range(zero):
            print(z,0)

    elif negative<positive:
        z=0
        for _ in  lists:
            z+=abs(_)
        print(z)
        #解答の出力        
        ans=lists[0]
        for i in range(1,positive-negative+1):
            print(ans,lists[-i])
            ans=ans-lists[-i]
        x=positive-negative+1
        for i in range(negative-1):
            print(lists[-x-i],ans)
            ans=lists[-x-i]-ans
            print(lists[i+1],ans)  
            ans=lists[i+1]-ans
        print(lists[-positive],ans)
        for i in range(zero):
            print(z,0)
    else:   
        
        ans=0
        length=len(lists)//2
    
        for i in range(len(lists)):
            if i<=length-1:
                ans-=lists[i]
            else:
                ans+=lists[i]
        print(ans) 
        
        ans=lists[0]
        for j in range(1,positive):
            print(lists[-j],ans)
            ans=lists[-j]-ans
            print(lists[j],ans)
            ans=lists[j]-ans    
        print(lists[positive],ans)
        for i in range(zero):
            print(z,0)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))
p = [t for t in a if t >= 0]
m = [t for t in a if t < 0]
p.sort(reverse = True)
m.sort()
l = []
#print(a)
if len(p) == 0:
  u = m.pop()
  v = m.pop()
  l.append((u, v))
  p.append(u - v)
if len(m) == 0 and len(p) >= 2:
  u = p.pop()
  v = p.pop()
  l.append((u, v))
  m.append(u - v)
while len(p) >= 2:
  u = m.pop()
  v = p.pop()
  l.append((u, v))
  m.append(u - v)
while len(m):
  t = p[0]
  u = m.pop()
  l.append((t, u))
  p[0] = t - u
print(p[0])
for res in l:
  print(*res)

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted([int(i) for i in input().split()])

M = 0
ans = []

if A[0] >= 0:
    ans.append((A[0], A[-1]))
    M = A[0]-A[-1]
    for i in range(1, N-1):
        if i != N-2:
            ans.append((M, A[i]))
            M -= A[i]
        else:
            ans.append((A[i], M))
            M = A[i]-M
elif A[-1] < 0:
    ans.append((A[0], A[-1]))
    M = A[0]-A[-1]
    for i in range(1, N-1):
        ans.append(M, A[i])
        M -= A[i]
else:
    exit()

print(M)
for t in ans:
    print(*t)

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted([int(i) for i in input().split()])

M = 0
ans = []

if A[0] >= 0:
    ans.append((A[0], A[-1]))
    M = A[0]-A[-1]
    for i in range(1, N-1):
        if i != N-2:
            ans.append((M, A[i]))
            M -= A[i]
        else:
            ans.append((A[i], M))
            M = A[i]-M
elif A[-1] < 0:
    ans.append((A[0], A[-1]))
    M = A[0]-A[-1]
    for i in range(1, N-1):
        ans.append(M, A[i])
        M -= A[i]
else:
    exit()

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]

n = i()
a = wi()

minus = 0
plus = 0

a.sort()
for i in range(n):
    if a[i] >= 0:
        plus += 1
    elif a[i] < 0:
        minus += 1

if plus == 0:
    minus -= 1
    plus += 1
elif plus == n:
    minus += 1
    plus -= 1


mli = []
pli = []
for i in range(1, n + 1):
    if i <= minus:
        ans += -a[i - 1]
        mli.append(a[i - 1])
    else:
        ans += a[i - 1]
        pli.append(a[i - 1])

print(ans)
for i in range(n - 1):
    if i == 0:
        print(mli[0], pli[0])
        tmp = mli[0] - pli[0]
    elif i < plus - 1:
        print(tmp, pli[i])
        tmp = tmp - pli[i]
    elif i == plus - 1:
        print(pli[i], tmp)
        tmp = pli[i] - tmp
    else:
        print(tmp, mli[i])
        tmp = mli[i] - tmp

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]

n = i()
a = wi()

minus = n // 2
plus = (n + 2 - 1) // 2

a.sort()
pa = 0; ma = 0; za = 0
for i in range(n):
    if a[i] > 0:
        pa += 1
    elif a[i] < 0:
        ma += 1
    else:
        za += 1
mli = []
pli = []
for i in range(1, n + 1):
    if i <= minus:
        ans += - a[i - 1]
        mli.append(a[i - 1])
    else:
        ans += a[i - 1]
        pli.append(a[i - 1])

kisucnt = 1
gusucnt = 1
print(ans)
if minus == plus:
    for i in range(n - 1):
        if i == 0:
            print(pli[0], mli[0])
            tmp = pli[0] - mli[0]
        elif i % 2 == 1:
            print(mli[kisucnt], tmp)
            tmp = mli[kisucnt] - tmp
            kisucnt += 1
        else:
            print(pli[gusucnt], tmp)
            tmp = pli[kisucnt] - tmp
            gusucnt += 1
else:
    for i in range(n - 1):
        if i == 0:
            print(mli[0], pli[0])
            tmp = mli[0] - pli[0]
        elif i % 2 == 1:
            print(pli[kisucnt], tmp)
            tmp = pli[kisucnt] - tmp
            kisucnt += 1
        else:
            print(mli[gusucnt], tmp)
            tmp = mli[kisucnt] - tmp
            gusucnt += 1

Submission status: Runtime Error

Solution code:
def sub(A, key, D):
    if tuple(key) in D:
        return D[tuple(key)]
    if len(key) == 1:
        D[tuple(key)] = (A[key[0]], None), (A[key[0]], None)
        return (A[key[0]], None), (A[key[0]], None)
    min_sub_list = []
    max_sub_list = []
    for i in range(len(key)):
        key_i = key.copy()
        index = key_i.pop(i)
        ikey = tuple(key_i)
        if ikey not in D:
            res_min_i, res_max_i = sub(A, key_i, D)
            D[ikey] = res_min_i, res_max_i
        else:
            res_min_i, res_max_i = D[ikey]
        a_i = A[index]
        b_i_min, pair_i_min = res_min_i
        b_i_max, pair_i_max = res_max_i
        min_sub_list.append((-abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
        min_sub_list.append((-abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
        max_sub_list.append((abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
        max_sub_list.append((abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
    res_min = min(min_sub_list)
    res_max = max(max_sub_list)
    D[tuple(key)] = res_min, res_max
    return res_min, res_max


def pair_extract(p, pair_list):
    if p is not None:
        a = p[0]
        b = p[1]
        p_next = p[2]
        pair_list.append((a, b))
        pair_extract(p_next, pair_list)


N = int(input())
A_temp = map(int, input().split())
A = list(A_temp)
key = [i for i in range(N)]
D = {}
_, res_max = sub(A, key, D)
max_val = res_max[0]
pair_list = []
pair_extract(res_max[1], pair_list)
pair_list.reverse()
print(max_val)
for p in pair_list:
    print(p[0], p[1])

Submission status: Runtime Error

Solution code:
D = {}


def sub(A, key):
    if len(A) == 1:
        if tuple(key) not in D:
            D[tuple(key)] = (A[0], None), (A[0], None)
        return (A[0], None), (A[0], None)
    min_sub_list = []
    max_sub_list = []
    for i in range(len(A)):
        A_i = A.copy()
        key_i = key.copy()
        a_i = A_i.pop(i)
        key_i.pop(i)
        dkey = tuple(key_i)
        if dkey not in D:
            res_min_i, res_max_i = sub(A_i, key_i)
            D[dkey] = res_min_i, res_max_i
        else:
            res_min_i, res_max_i = D[dkey]
        b_i_min, pair_i_min = res_min_i
        b_i_max, pair_i_max = res_max_i
        min_sub_list.append((-abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
        min_sub_list.append((-abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
        max_sub_list.append((abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
        max_sub_list.append((abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
    res_min = min(min_sub_list)
    res_max = max(max_sub_list)
    if tuple(key) not in D:
        D[tuple(key)] = res_min, res_max
    return res_min, res_max


def pair_extract(p, pair_list):
    if p is not None:
        a = p[0]
        b = p[1]
        p_next = p[2]
        pair_list.append((a, b))
        pair_extract(p_next, pair_list)


N = int(input())
A_temp = map(int, input().split())
A = list(A_temp)
key = [i for i in range(N)]
_, res_max = sub(A, key)
max_val = res_max[0]
pair_list = []
pair_extract(res_max[1], pair_list)
pair_list.reverse()
print(max_val)
for p in pair_list:
    print(p[0], p[1])

Submission status: Runtime Error

Solution code:
D = {}


def sub(A, key):
    if len(A) == 1:
        return (A[0], None), (A[0], None)
    min_sub_list = []
    max_sub_list = []
    for i in range(len(A)):
        A_i = A.copy()
        key_i = key.copy()
        a_i = A_i.pop(i)
        key_i.pop(i)
        dkey = tuple(key_i)
        if dkey not in D:
            res_min_i, res_max_i = sub(A_i, key_i)
            D[dkey] = res_min_i, res_max_i
        else:
            res_min_i, res_max_i = D[dkey]
        b_i_min, pair_i_min = res_min_i
        b_i_max, pair_i_max = res_max_i
        min_sub_list.append((-abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
        min_sub_list.append((-abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
        max_sub_list.append((abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
        max_sub_list.append((abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
    res_min = min(min_sub_list)
    res_max = max(max_sub_list)
    D[tuple(key)] = res_min, res_max
    return res_min, res_max


def pair_extract(p, pair_list):
    if p is not None:
        a = p[0]
        b = p[1]
        p_next = p[2]
        pair_list.append((a, b))
        pair_extract(p_next, pair_list)


N = int(input())
A_temp = map(int, input().split())
A = list(A_temp)
key = [i for i in range(N)]
_, res_max = sub(A, key)
max_val = res_max[0]
pair_list = []
pair_extract(res_max[1], pair_list)
pair_list.reverse()
print(max_val)
for p in pair_list:
    print(p[0], p[1])