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

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n = int(input())
a = list(map(int,input().split()))
a.sort()
ma = a[-1]
mi = a[0]
log = []
for i in a[1:-1]:
    if i >= 0:
        log.append([mi,i])
        mi -= i
    else:
        log.append([ma,i])
        ma -= i
log.append([ma,mi])
print(ma-mi)
for i in range(n-1):
    x,y = log[i]
    print(x,y)

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

min_value = inputs[0]
max_value = inputs[-1]
process = []
for i in range(1, N-1):
    value = inputs[i]
    if value >= 0:
        process.append([min_value, value])
        min_value -= value
    elif value < 0:
        process.append([max_value, value])
        max_value -= value
process.append([max_value, min_value])

print(max_value - min_value)
for item in process:
    print(item[0], item[1])

Solution (Python):

def main():
    n = int(input())
    lst = sorted(map(int, input().rstrip().split()))
    s = lst[0]  
    l = lst[-1]  
    log = []
    for i in lst[1:-1]:
        if i > 0:
            log += [(s, i)]
            s -= i
        else:
            log += [(l, i)]
            l -= i
    log += [(l, s)]
    print(l - s)
    for i, j in log:
        print(i, j)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
*A, = map(int, input().split())
operations = []
pos = []; zero = []; neg = [];
for a in A:
    if a > 0: pos.append(a)
    elif a == 0: zero.append(a)
    else: neg.append(a)
pos.sort(); neg.sort()
if N == 2:
    a, b = max(A), min(A)
    ans = a - b
    operations.append((a, b))
else:
    if len(neg) == 0:
        pos = zero + pos
        operations.append((pos[0], pos[-1]))
        neg = [pos[0] - pos[-1]]
        pos = pos[1:-1]
    elif len(pos) == 0:
        neg = neg + zero
        operations.append((neg[-1], neg[0]))
        pos = [neg[-1] - neg[0]]
        neg = neg[1:-1]
    else:
        neg = neg + zero
    neg = neg[::-1]
    while len(pos) > 1 and len(neg) > 1:
        n, p = neg.pop(), pos.pop()
        operations.append((p, n))
        pos.append(p-n)
    if len(neg) == 1:
        while len(pos) > 1:
            n, p = neg.pop(), pos.pop()
            operations.append((n, p))
            neg.append(n-p)
    else:
        while len(neg) > 1:
            n, p = neg.pop(), pos.pop()
            operations.append((p, n))
            pos.append(p-n)
    operations.append((pos[0], neg[0]))
    ans = pos[0] - neg[0]
print(ans)
for x, y in operations:
    print(x, y)

Solution (Python):
N = int(input())
AAA = list(map(int, input().split()))

AAA.sort()

m = AAA[0]
M = AAA[N-1]

procedures = []
for i in range(1, N-1):
    if AAA[i] > 0:
        procedures.append((m, AAA[i]))
        m -= AAA[i]
    else:
        procedures.append((M, AAA[i]))
        M -= AAA[i]
procedures.append((M, m))

print(M-m)
for x, y in procedures:
    print(x, y)

Solution (Python):
n,a=int(input()),sorted(list(map(int,input().split())))
q,p=b,c=a[0],a[-1]
d=a[1:-1]
for i in d:
  if i<0:p-=i
  else:q-=i
print(p-q)
for i in d:
  if i<0:print(c,i);c-=i
  else:print(b,i);b-=i
print(c,b)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

n=int(input())
a=[int(i) for i in input().split()]
a.sort()
if a[-1]<=0:
    x=a.pop()
    print(x-sum(a))
    for y in a:
        print(x,y)
        x=x-y
elif a[0]>=0:
    a=a[::-1]
    x=a.pop()
    print(-x+sum(a))
    while a:
        y=a.pop()
        if len(a)>0:
            print(x,y)
            x=x-y
        else:
            print(y,x)
            
else:
    for i in range(n):
        if a[i]>0:
            ap=a[i:]
            an=a[:i]
            break
    print(sum(ap)-sum(an))
    z=ap.pop()
    x=an.pop()
    for y in ap:
        print(x,y)
        x=x-y
    for y in an:
        print(z,y)
        z=z-y
    print(z,x)

Solution (Python):
N = int(input())
AAA = list(map(int, input().split()))

AAA.sort()  

m = AAA[0]
M = AAA[N-1]
ANS = []

for i in range(1, N-1):
    if AAA[i] > 0:
        ANS.append((m, AAA[i]))
        m -= AAA[i]
        continue
    ANS.append((M, AAA[i]))
    M -= AAA[i]
ANS.append((M, m))

print(M-m)
for (x, y) in ANS:
    print(x, y)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a.sort()
q = 0
for ai in a:
    if ai < 0:
        q += 1
    else:
        break
if q == 0:
    q = 1
elif q == n:
    q = n - 1
ans = []
x = a[0]
for ai in a[q:-1]:
    ans.append([x, ai])
    x -= ai

ans.append([a[-1], x])
y = a[-1] - x

for ai in a[1:q]:
    ans.append([y, ai])
    y -= ai

print(y)
for i in ans:
    print(*i)

Solution (Python):

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


P = sorted((a for a in A if a >= 0), reverse=True)
Q = sorted(a for a in A if a < 0)

x, y = [], []
if len(P) == 2 and not Q:
    Y, X = P.pop(), P.pop()
    x.append(X)
    y.append(Y)
    P.append(X - Y)
if len(P) >= 3 and not Q:
    X, Y = P.pop(), P.pop()
    x.append(X)
    y.append(Y)
    Q.append(X - Y)
if not P and len(Q) >= 2:
    X, Y = Q.pop(), Q.pop()
    x.append(X)
    y.append(Y)
    P.append(X - Y)
while len(P) >= 2 and Q:
    X, Y = Q.pop(), P.pop()
    x.append(X)
    y.append(Y)
    Q.append(X - Y)
while Q:
    X, Y = P.pop(), Q.pop()
    x.append(X)
    y.append(Y)
    P.append(X - Y)

ans = (
    '{}\n{}'.format(
        P[0],
        '\n'.join(
            '{} {}'.format(X, Y)
            for X, Y in zip(x, y)
        )
    )
)


print(ans)

Solution (Python):

def solve():
    N = int(raw_input())
    A = [int(_) for _ in raw_input().split()]
    A.sort()
    ret = abs(A[N-1] - A[0])
    rever = [0] * (N - 1)
    for i in range(1, N-1):
        if A[i] >= 0:
            ret = ret + A[i]
            rever[i-1] = 1
        else:
            ret = ret - A[i]
    print ret
    def PrintAndCalcSave(a, b, rever):
        if rever == 0:
            print a, b
            return a - b
        else:
            print b, a
            return b - a

    cnt = 0
    if A[N-1] > A[0]:
        save = PrintAndCalcSave(A[N-1], A[0], rever[cnt])
    else:
        save = PrintAndCalcSave(A[0], A[N-1], rever[cnt])
    cnt += 1
    for i in range(1, N - 1):
        if A[i] >= 0:
            save = PrintAndCalcSave(A[i], save, rever[cnt])
        else:
            save = PrintAndCalcSave(save, A[i], rever[cnt])
        cnt += 1

if __name__ == '__main__':
    solve()

Solution (Python):
n,a=int(input()),sorted(list(map(int,input().split())))
mn,mx=a[0],a[-1]
for i in a[1:-1]:
  if i<0:mx-=i
  else:mn-=i
print(mx-mn)
mn,mx=a[0],a[-1]
for i in a[1:-1]:
  if i<0:print(mx,i);mx-=i
  else:print(mn,i);mn-=i
print(mx,mn)

Solution (Python):
from collections import deque

n = int(input())
A = list(map(int, input().split()))

A.sort()
A_d = deque(A)

if (A_d[0] <= 0 and A_d[n-1] > 0) or (A_d[0] < 0 and A_d[n-1] >= 0):
    print(sum(list(map(abs, A_d))))
    while A_d[-2] >= 0:
        atama = A_d.popleft()
        ketu = A_d.pop()
        print(atama, ketu)
        new = atama - ketu
        A_d.appendleft(new)
    while len(A_d) != 1:
        atama = A_d.popleft()
        ketu = A_d.pop()
        print(ketu, atama)
        new = ketu - atama
        A_d.append(new)
elif A_d[0] > 0:
    atama = A_d.popleft()
    tugi = A_d.popleft()
    new = atama - tugi
    A_d.appendleft(new)
    print(sum(list(map(abs, A_d))))
    print(atama, tugi)
    while len(A_d) != 2:
        atama = A_d.popleft()
        ketu = A_d.pop()
        print(atama, ketu)
        new = atama - ketu
        A_d.appendleft(new)
    atama = A_d.popleft()
    ketu = A_d.pop()
    print(ketu, atama)
else:
    ketu = A_d.pop()
    tugi = A_d.pop()
    new = ketu - tugi
    A_d.append(new)
    print(sum(list(map(abs, A_d))))
    print(ketu, tugi)
    while len(A_d) != 1:
        atama = A_d.popleft()
        ketu = A_d.pop()
        print(ketu, atama)
        new = ketu - atama
        A_d.append(new)

Solution (Python):
import collections
N = int(input())
A = list(map(int, input().split()))
A.sort()
A2 = collections.deque(A)

x = [0] * (N-1)
y = [0] * (N-1)
for i in range(N -1):
    amin = A2.popleft()
    amax = A2.pop()
    mainasuflag = 0
    plusflag = 0
    if i != N - 2:
        if A2[0] > 0:
            mainasuflag = 1
        if A2[len(A2) - 1] < 0:
            plusflag = 1
        amin2 = abs(A2[0])
        amax2 = abs(A2[len(A2) -1])
        if i == N - 3:
            if A2[0] > 0:
                A2.appendleft(amin - amax)
                x[i] = amin
                y[i] = amax
            else:
                A2.append(amax - amin)
                x[i] = amax
                y[i] = amin
        else:
            if mainasuflag == 1:
                A2.appendleft(amin - amax)
                x[i] = amin
                y[i] = amax
            elif plusflag == 1:
                A2.append(amax - amin)
                x[i] = amax
                y[i] = amin
            else:
                if amax2 >= amin2:
                    A2.appendleft(amin - amax)
                    x[i] = amin
                    y[i] = amax
                else:
                    A2.append(amax - amin)
                    x[i] = amax
                    y[i] = amin

    else:
        A2.append(amax - amin)
        x[i] = amax
        y[i] = amin

    

print(A2[0])

for i in range(N - 1):
    print(x[i], y[i])

Solution (Python):



N = int(input())
a = list(map(int,input().split()))
a.sort()
s = a[0]
l = a[-1]
log = []
for i in a[1:-1]:
    if i > 0:
        log += [(s, i)]
        s -= i
    else:
        log += [(l, i)]
        l -= i
log += [(l, s)]
print(l - s)
for i, j in log:
    print(i, j)

Solution (Python):

def main():
    n = int(input())
    lst = sorted(list(map(int, input().rstrip().split())))
    s = lst[0]
    l = lst[-1]
    log = []
    for i in lst[1:-1]:
        if i > 0:
            log += [(s, i)]
            s -= i
        else:
            log += [(l, i)]
            l -= i
    log += [(l, s)]
    print(l - s)
    for i, j in log:
        print(i, j)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect

N = int(input())
A = list(map(int, input().split()))
A.sort()

neg = [A[0]]
pos = [A[-1]]
if N >= 3:
    i = bisect.bisect_left(A, 0, lo=1, hi=N - 1)
    neg += A[1:i]
    pos += A[i:N - 1]

M = neg.pop()
Op = []
for i in range(len(pos) - 1):
    p = pos.pop()
    Op.append((M, p))
    M -= p
p = pos.pop()
Op.append((p, M))
M = p - M
for i in range(len(neg)):
    n = neg.pop()
    Op.append((M, n))
    M -= n

print(M)
for O in Op:
    print(O[0], O[1])

Solution (Python):
def c(n,a):
    a.sort()
    max = a.pop()
    min = a.pop(0)
    opr = []
    for i in a:
        if i >= 0:
            opr.append('{} {}'.format(min, i))
            min-=i
        else:
            opr.append('{} {}'.format(max, i))
            max-=i
    opr.append('{} {}'.format(max, min))
    return str(max - min) + '\n' + '\n'.join(opr)

n = int(input())
a = list(map(int, input().split()))
print(c(n, a))

Solution (Python):
import bisect
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
point = bisect.bisect_right(a, 0)
a_minus = a[:point]
a_plus = a[point:]
len_plus = len(a_plus)
if (len(a_minus) > 0) & (len(a_plus) > 0):
    ans = sum(a_plus) - sum(a_minus)
    print(ans)
    for i in range(len(a_plus)):
        if len_plus == 1:
            x = a_plus.pop(0)
            y = a_minus.pop(0)
            print(str(x) + " " + str(y))
        elif (i == 0):
            x = a_minus.pop(0)
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
        elif i == len_plus - 1:
            y = x - y
            x = a_plus.pop(0)
            print(str(x) + " " + str(y))
        else:
            x = x - y
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
    for j in range(len(a_minus)):
        x = x - y
        y = a_minus.pop(0)
        print(str(x) + ' ' + str(y))
elif len(a_plus) > 0:
    ans = sum(a_plus) - ((a[0]) * 2)
    print(ans)
    for i in range(len(a_plus) - 1):
        if i == 0:
            x = a_plus.pop(0)
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
        elif i == len_plus - 2:
            y = x - y
            x = a_plus.pop(0)
            print(str(x) + " " + str(y))
        else:
            x = x - y
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
elif len(a_minus) > 0:
    ans = (a_minus[-1] * 2) - sum(a_minus)
    print(ans)
    for i in range(len(a_minus) - 1):
        if i == 0:
            x = a_minus[-1]
            y = a_minus.pop(0)
            print(str(x) + " " + str(y))
        elif i == len_plus - 2:
            x = x - y
            y = a_minus.pop(0)
            print(str(x) + " " + str(y))
        else:
            x = x - y
            y = a_minus.pop(0)
            print(str(x) + " " + str(y))

Solution (Python):
n=int(input())
a=sorted(list(map(int,input().split())))
max=a[-1]
min=a[0]
if n==2:
    print(max-min)
    print(max,min)
    exit()
ans=0
if a[-1]<0:
    ans+=-sum(a)+max*2
elif a[0]>0:
    ans+=sum(a)-min*2
else:
    for x in a:
        ans+=abs(x)
print(ans)
X=max
Y=min
for i in range(1,n-1):
    if a[i]>=0:
        x=Y
        y=a[i]
        print(x,y)
        Y=x-y    
    else:
        x=X
        y=a[i]
        print(x,y)
        X=x-y
print(X,Y)

Solution (Python):
import sys
import collections as col
import bisect
import numpy as np


def main():
    n = int(input())
    aarray = [int(e) for e in input().split()]
    aarray.sort()
    record = []
    endpoint = max(1, bisect.bisect(aarray, 0))
    aqueue = col.deque(aarray)
    while len(aqueue) > endpoint + 1:
        amax = aqueue.pop()
        amin = aqueue.popleft()
        record.append((amin, amax))
        aqueue.appendleft(amin - amax)
    amax = aqueue.pop()
    while len(aqueue) > 0:
        amin = aqueue.popleft()
        record.append((amax, amin))
        amax -= amin
    print(amax)
    for r in record:
        print(r[0], r[1])


main()

Solution (Python):
from heapq import heappop,heappush
n = int(input())
A = sorted(list(map(int,input().split())))
P = []
M = []
if n == 2:
    print(A[1]-A[0])
    print(A[1],A[0])
    exit()
def move():
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
for a in A:
    if a>=0:
        P.append(a)
    else:
        M.append(a)

if A[-1]>=0 and A[0]<0:
    print(sum(abs(a) for a in A))
elif A[0]>=0:
    print(sum(A)-min(A)*2)
    mm = heappop(P)
    pp = heappop(P)
    print(mm,pp)
    heappush(M,mm-pp)
else:
    print(sum(abs(a) for a in A)+max(A)*2)
    mm = M[-1]
    pp = M[-2]
    print(mm,pp)
    M = M[:-2]
    heappush(P,mm-pp)
move()

Solution (Python):
from heapq import heappop,heappush
n = int(input())
A = sorted(list(map(int,input().split())))
P = []
M = []
if n == 2:
    print(A[1]-A[0])
    print(A[1],A[0])
    exit()
def move():
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
for a in A:
    if a>=0:
        P.append(a)
    else:
        M.append(a)

if A[-1]>=0 and A[0]<0:
    print(sum(abs(a) for a in A))
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
elif A[0]>=0:
    print(sum(A)-min(A)*2)
    mm = heappop(P)
    pp = heappop(P)
    print(mm,pp)
    heappush(M,mm-pp)
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
else:
    print(sum(abs(a) for a in A)+max(A)*2)
    mm = M[-1]
    pp = M[-2]
    print(mm,pp)
    M = M[:-2]
    heappush(P,mm-pp)
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A = sorted(A,reverse=True)
Y, X = [A[0]], [A[-1]]
for i in range(1, N-1):
    if A[i] >= 0:
        Y.append(A[i])
    else:
        X.append(A[i])
res = []

for i in range(len(Y)-1,0,-1) :
    res.append([X[-1],Y[i]])
    X[-1] -= Y[i]

for i in range(len(X)-1,-1,-1) :
    res.append([Y[0],X[i]])
    Y[0] -= X[i]

print(Y[0])
for r in res :
    print(*r)

Solution (Python):
import bisect

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

ans = []

A = sorted(A)

if N == 2 :
    ans.append([A[1],A[0]])
    calc = A[1] - A[0]

else :
    ind = bisect.bisect_left(A,0)

    X = A[:ind]
    Y = A[ind:]
    
    if len(X) == 0 :
        ans.append([Y[0],Y[1]])
        X.append(Y.pop(0)-Y.pop(0))
    
    elif len(Y) == 0 :
        ans.append([X[-1],X[-2]])
        Y.append(X.pop(-1)-X.pop(-1))
    
    while len(Y) != 1 :
        ans.append([X[-1],Y[0]])
        X[-1] -= Y.pop(0)
    
    while len(X) :
        ans.append([Y[0],X[-1]])
        Y[0] -= X.pop(-1)
    
    calc = Y.pop(0)

print(calc)
for a in ans :
    print(*a)

Solution (Python):
n = int(input())
ai = [int(x) for x in input().split()]

ai.sort()

temp = [abs(x) for x in ai[1:-1]]

o1 = -ai[0] + sum(temp) + ai[-1]

print(o1)

temp1 = ai.pop(-1)
num_p = sum(x >= 0 for x in ai)
if num_p == len(ai):
  num_p -= 1
for i in range(num_p):
  temp2 = ai.pop(-1)
  print(ai[0], temp2)
  ai[0] -= temp2

for i in range(len(ai)):
  temp2 = ai.pop(0)
  print(temp1, temp2)
  temp1 -= temp2

Solution (Python):
N = int(input())
a = list(map(int, input().split(' ')))
a.sort(reverse=True)

process = []
p_seed = a.pop(0)
n_seed = a.pop(-1)
for e in a:
    if e >= 0:
        process.append((n_seed, e))
        n_seed -= e
    else:
        process.append((p_seed, e))
        p_seed -= e
process.append((p_seed, n_seed))
m = p_seed - n_seed
print(m)
for x, y in process:
    print(str(x) + ' ' + str(y))

Solution (Python):
n = int(input())
a = input().split()
for i in range(len(a)):
    a[i]=int(a[i])

a.sort()


hashi1 = a[0]
hashi2 = a[-1]
a.remove(a[0])
a.remove(a[-1])

minus_list = []
plus_list = []
printlist = []
for i in range(len(a)):
    if a[i]>0:
        plus_list.append(a[i])
    else:
        minus_list.append(a[i])


for j in range(len(minus_list)):
    printlist.append(hashi2)
    printlist.append(minus_list[j])
    hashi2 = hashi2-minus_list[j]
    
for l in range(len(plus_list)):
    printlist.append(hashi1)
    printlist.append(plus_list[l])
    hashi1 = hashi1 - plus_list[l]
    
printlist.append(hashi2)
printlist.append(hashi1)
print(hashi2-hashi1)

for n in range(0,len(printlist),2):
    print(printlist[n],printlist[n+1])

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

a.sort()

now = 0

ans = []

for i in range(n-2):
    if a[i+1] > 0:
        ans.append([a[0], a[i+1]])
        a[0] = a[0] - a[i+1]
    else:
        ans.append([a[n-1], a[i+1]])
        a[n-1] = a[n-1] - a[i+1]

ans.append([a[n-1],a[0]])
print(a[n-1]-a[0])

for i in ans:
    print(i[0],i[1])

Solution (Python):
n,*a=map(int,open(0).read().split())
a.sort()
l=[]
if a[-1]>=0>a[0]:
  a,b=[t for t in a if t>=0],[t for t in a if t<0]
  c,y=a.pop(),b.pop()
  while b:
    t=b.pop()
    l.append((c,t))
    c-=t
  while a:
    t=a.pop()
    l.append((y,t))
    y-=t
  l.append((c,y))
  c-=y
elif a[0]>=0:
  s,*a,c=a
  while a:
    t=a.pop()
    l.append((s,t))
    s-=t
  l.append((c,s))
  c-=s
else:
  *a,c=a
  while a:
    t=a.pop()
    l.append((c,t))
    c-=t
print(c)
for t in l:print(*t)

Solution (Python):
n,*a=map(int,open(0).read().split())
a.sort()
l=[]
if a[-1]>=0>a[0]:
  a,b=[t for t in a if t>=0],[t for t in a if t<0]
  c,y=a.pop(),b.pop()
  while b:
    t=b.pop()
    l.append((c,t))
    c-=t
  while a:
    t=a.pop()
    l.append((y,t))
    y-=t
  l.append((c,y))
  c-=y
elif a[0]>=0:
  s,*a,c=a
  while a:
    t=a.pop()
    l.append((s,t))
    s-=t
  l.append((c,s))
  c-=s
else:
  *a,c=a
  while a:
    t=a.pop()
    l.append((c,t))
    c-=t
print(c)
for t in l:print(*t)

Solution (Python):
from collections import deque

N = int(input())
A = deque(maxlen=N)
x = [int(e) for e in input().split()]
x.sort()
for a in x:
    A.append(a)
l = []
for i in range(N - 2):
    (a, b, c) = (A[0], A[1], A[-1])
    if b < 0:
        l.append((c, a))
        A[-1] = c - a
    else:
        l.append((a, b))
        A[1] = a - b
    A.popleft()
print(A[1] - A[0])
l.append((A[1], A[0]))
for v in l:
    print(v[0], v[1])

Solution (Python):
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort()


cut = 0
while cut < N and A[cut] < 0:
    cut += 1

if cut == 0:
    M = sum(A[1:]) - A[0]
    print(M)
    pair = [A[0], A[N-1]]
    for i in range(N-2):
        print(' '.join(map(str, pair)))
        pair = [pair[0] - pair[1], A[N-i-2]]
    pair.reverse()
    print(' '.join(map(str, pair)))

elif cut == N:
    M = A[-1] - sum(A[:N-1])
    print(M)
    pair = [A[N-1], A[0]]
    for i in range(N-2):
        print(' '.join(map(str, pair)))
        pair = [pair[0] - pair[1], A[i+1]]
    print(' '.join(map(str, pair)))

else:
    M = sum(A[cut:]) - sum(A[:cut])
    print(M)

    B = deque(A[cut:]) 
    C = deque(A[:cut]) 

    while len(B) != 1:
        pair = [C[-1], B[-1]]
        print(' '.join(map(str, pair)))
        b = B.pop()
        C[-1] = C[-1] - b
    
    b = B.pop()

    while len(C) != 0:
        pair = [b, C[0]]
        print(' '.join(map(str, pair)))
        c = C.popleft()
        b = b - c

Solution (Python):
def successive_subtraction(N, A, i=1, l=[]):
    mx = max(A)
    mn = min(A)
    if i == N - 1:
        print(mx - mn)
        return l
    l.append((mn, mx))
    A.remove(mn)
    A.remove(mx)
    A.append(mn - mx)
    return successive_subtraction(N, A, i + 1, l)

N = int(input())
A = [int(e) for e in input().split()]
A.sort()
l = []
for i in range(N - 2):
    (a, b, c) = (A[0], A[1], A[-1])
    if b < 0:
        l.append((c, a))
        A[-1] = c - a
    else:
        l.append((a, b))
        A[1] = a - b
    A.pop(0)
print(A[1] - A[0])
l.append((A[1], A[0]))
for v in l:
    print(v[0], v[1])

Solution (Python):
import bisect
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
ans=[]
x=bisect.bisect_left(a, 0)
m=a[:x]
p=a[x:]

if n==2 and len(p)==2:
    print(p[1]-p[0])
    print(p[1],p[0])
    
    exit()
if m==[]:
    m+=[p[0]-p[-1]]
    ans+=[[p[0],p[-1]]]
    del p[0]
    del p[-1]

if p==[]:
    p+=[m[-1]-m[0]]
    ans+=[[m[-1],m[0]]]
    del m[-1]
    del m[0]

while len(p)!=1:

    m+=[m[0]-p[0]]
    ans+=[[m[0],p[0]]]
    del p[0]
    del m[0]
cnt=p[0]
for i in m:
    ans+=[[cnt,i]]
    cnt-=i
print(cnt)
for i,j in ans:print(i,j)

Solution (Python):
import sys
input=sys.stdin.readline
N=int(input())
A=list(map(int,input().split()))
A.sort()
ans=[]

if A[0]>0:
    num=A[0]
    for i in range(1,N-1):
        ans.append([num,A[i]])
        num-=A[i]
    ans.append([A[-1],num])
    print(A[-1]-num)
    for i in range(len(ans)):
        print(ans[i][0],ans[i][1])
elif A[-1]<0:
    A.sort(reverse=True)
    num=A[0]
    for i in range(1,N):
        ans.append([num,A[i]])
        num-=A[i]
    print(num)
    for i in range(len(ans)):
        print(ans[i][0],ans[i][1])
else:
    M=A[-1]
    m=A[0]
    for i in range(1,N-1):
        if A[i]<0:
            ans.append([M,A[i]])
            M-=A[i]
        else:
            ans.append([m,A[i]])
            m-=A[i]
    ans.append([M,m])
    print(M-m)
    for i in range(len(ans)):
        print(ans[i][0],ans[i][1])

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
A.sort()
ans=[]

if A[0]>0:
    num=A[0]
    for i in range(1,N-1):
        ans.append([num,A[i]])
        num-=A[i]
    ans.append([A[-1],num])
    print(A[-1]-num)
    for i in range(len(ans)):
        print(ans[i][0],ans[i][1])
elif A[-1]<0:
    A.sort(reverse=True)
    num=A[0]
    for i in range(1,N):
        ans.append([num,A[i]])
        num-=A[i]
    print(num)
    for i in range(len(ans)):
        print(ans[i][0],ans[i][1])
else:
    M=A[-1]
    m=A[0]
    for i in range(1,N-1):
        if A[i]<0:
            ans.append([M,A[i]])
            M-=A[i]
        else:
            ans.append([m,A[i]])
            m-=A[i]
    ans.append([M,m])
    print(M-m)
    for i in range(len(ans)):
        print(ans[i][0],ans[i][1])

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]

mxs = sorted(x for x in A if x < 0)
pxs = sorted(x for x in A if x >= 0)

if len(mxs) == 0:
    mxs = [pxs[0]]
    pxs = pxs[1:]

if len(pxs) == 0:
    pxs = [mxs[-1]]
    mxs = mxs[:-1]



result = sum(pxs) - sum(mxs)
print(result)

if len(pxs) == 1:
    for y in mxs:
        print(pxs[0], y)
        pxs[0] -= y
else:
    for y in pxs[:-1]:
        print(mxs[0], y)
        mxs[0] -= y
    print(pxs[-1], mxs[0])
    pxs[-1] -= mxs[0]
    for y in mxs[1:]:
        print(pxs[-1], y)
        pxs[-1] -= y

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
anslist = []

for i in range(1,n-1):
  if a[i] < 0:
    anslist.append([a[-1], a[i]])
    a[-1] -= a[i]
  else:
    anslist.append([a[0], a[i]])
    a[0] -= a[i]
anslist.append([a[-1], a[0]])
ans = a[-1]-a[0]
print(ans)
for i in range(len(anslist)):
  print(anslist[i][0], anslist[i][1])

Solution (Python):
N = int(input())
l = list(map(int,input().split()))
l.sort()
sousa = []
next_m = l[0]
next_p = l[N-1]
for i in range(1,N-1):
    if l[i] > 0:
        sousa.append([next_m,l[i]])
        next_m -= l[i]
    else:
        sousa.append([next_p,l[i]])
        next_p -= l[i]
ans = next_p - next_m
print(ans)
sousa.append([next_p,next_m])
for i in range(N-1):
    print(str(sousa[i][0])+" "+str(sousa[i][1]))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

a.sort()

now = 0

ans = []

for i in range(n-2):
    if a[i+1] > 0:
        ans.append([a[0], a[i+1]])
        a[0] = a[0] - a[i+1]
    else:
        ans.append([a[n-1], a[i+1]])
        a[n-1] = a[n-1] - a[i+1]

ans.append([a[n-1],a[0]])
print(a[n-1]-a[0])

for i in ans:
    print(i[0],i[1])

Solution (Python):
import bisect
N = int(input())
A = sorted([int(_) for _ in input().split()])
i = bisect.bisect_left(A, 0)
if i == N:
    i -= 2
elif i:
    i -= 1
now = A[i]
ans = []
for a in A[i + 1:N - 1]:
    ans += [[now, a]]
    now -= a
ans += [[A[N - 1], now]]
now = A[N - 1] - now
for a in A[:i]:
    ans += [[now, a]]
    now -= a
print(now)
[print(*xy) for xy in ans]

Solution (Python):
from collections import deque

N = int(input())
inpt = input()
A = [int(a) for a in inpt.split()]
A = deque(sorted(A))
outputs = []
negative = 0
positive = 0

for a in A:
    if a > 0:
        positive += 1
    elif a <= 0:
        negative += 1
for i in range(N-1):
    maxA = A[-1]
    minA = A[0]
    if minA>0:
        positive -= 1
    else:
        negative -= 1
    if maxA>0:
        positive -= 1
    else:
        negative -= 1
    A.pop()
    A.popleft()
    if positive > negative:
        newnum = minA-maxA
        A.appendleft(newnum)
        outputs.append(str(minA)+" "+str(maxA))
    else:
        newnum = maxA - minA
        A.append(newnum)
        outputs.append(str(maxA)+" "+str(minA))
    if newnum > 0:
        positive += 1
    else:
        negative += 1

print(A[0])
for output in outputs:
    print(output)

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

p,m = [],[]
p_total = 0
m_total = 0
p_max,p_min = 0,10**10
m_max,m_min = -10**10,0
p_count,p_min_idx,p_max_idx = 0,0,0
m_count,m_min_idx,m_max_idx = 0,0,0
for a in A:
    if a < 0:
        if m_min > a:
            m_min = a
            m_min_idx = m_count
        if m_max < a:
            m_max = a
            m_max_idx = m_count
        m_total += a
        m.append(a)
        m_count += 1
    else:
        if p_min > a:
            p_min = a
            p_min_idx = p_count
        if p_max < a:
            p_max = a
            p_max_idx = p_count
        p_total += a
        p.append(a)
        p_count += 1

if len(m) == 0:
    print(p_total-2*p_min)
    for i in range(len(p)-1):
        if i != p_min_idx:
            print(p[p_min_idx],p[i])
            p[p_min_idx] -= p[i]
    print(p[-1],p[p_min_idx])

elif len(p) == 0:
    print(2*m_max-m_total)
    for i in range(len(m)-1):
        if i != m_max_idx:
            print(m[m_max_idx],m[i])
            m[m_max_idx] -= m[i]
    print(m[m_max_idx],m[-1])

else:
    print(p_total-m_total)
    for i,v in enumerate(p):
        if i != p_max_idx:
            print(m[0], v)
            m[0] -= v
    for i in range(len(m)):
        print(p[p_max_idx],m[i])
        p[p_max_idx] -= m[i]

Solution (Python):
import sys
import itertools
import collections
sys.setrecursionlimit(10**7)
def lmi(): return list(map(int, input().split()))

n = int(input())
a = lmi()
a.sort()
b = [-1 for i in range(n)]
b[-1] = 1
for i in range(1, n-1):
    if a[i] > 0:
        b[i] = 1


print(sum([a[i]*b[i] for i in range(n)]))

x = a[0]
for y in [a[i] for i in range(n-1) if b[i] == 1]:
    print(x, y)
    x -= y

y = x
x = a[-1]
print(x, y)
x -= y
for y in [a[i] for i in range(1,n-1) if b[i] == -1]:
    print(x, y)
    x -= y

Solution (Python):
import sys
import itertools
import collections
sys.setrecursionlimit(10**7)
def lmi(): return list(map(int, input().split()))

n = int(input())
a = lmi()
a.sort()
b = [-1 for i in range(n)]
b[-1] = 1
for i in range(1, n-1):
    if a[i] > 0:
        b[i] = 1


print(sum([a[i]*b[i] for i in range(n)]))

x = a[0]
for y in [a[i] for i in range(n-1) if b[i] == 1]:
    print(x, y)
    x -= y

y = x
x = a[-1]
print(x, y)
x -= y
for y in [a[i] for i in range(1,n-1) if b[i] == -1]:
    print(x, y)
    x -= y