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
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
from collections import deque
l = []
r = []
for i in a:
  if i < 0:
    l.append(i)
  else:
    r.append(i)
    
ll = len(l)
rr = len(r)
r.sort()
l.sort()
ans = []
l = deque(l)
r = deque(r)

for i in range(n-1):
  if ll == 0:
    mi = r.popleft()
    ma = r.pop()
    ans.append((mi,ma))
    l.append(mi-ma)
    ll += 1
  else:
    if rr > 1:
      L = l.pop()
      R = r.pop()
      ans.append((L,R))
      l.append(L-R)
      rr -= 1
    elif rr == 1:
      L = l.pop()
      R = r.pop()
      ans.append((R,L))
      r.append(R-L)
    else:
      mi = l.popleft()
      ma = l.pop()
      ans.append((ma,mi))
      r.append(ma-mi)
      rr += 1
      
p,q = ans[-1]
print(abs(p-q))
if p < q:
  ans[-1] = (q,p)
for i,j in ans:
  print(i,j)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
A.sort()
ans=[0]*(N-1)
a=A[0]
z=A[-1]

for i in range(1,N-1):
    if A[i]>=0:
        ans[i-1]=(a,A[i])
        a-=A[i]
    else:
        ans[i-1]=(z,A[i])
        z-=A[i]
ans[N-2]=(z,a)
print(z-a)
for s in ans:
    print(*s)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
ans = [0]*(n-1)

k1, k2 = a[0], a[-1]
for i in range(1, n-1):
  if a[i] >= 0:
    ans[i-1] = (k1, a[i])
    k1 -= a[i]
  else:
    ans[i-1] = (k2, a[i])
    k2 -= a[i]
ans[n-2] = (k2, k1)
print(k2-k1)
for value in ans:
  print(*value)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
m,p=[],[]
for i in A:
    if i>=0:
        p.append(i)
    else:
        m.append(i)
if m and p:
    print(sum(p)-sum(m))
    t=m[0]
    for i in p[:-1]:
        print(t,i)
        t-=i
    print(p[-1],t)
    t=p[-1]-t
    for i in m[1:]:
        print(t,i)
        t-=i
elif p:
    p.sort()
    print(sum(p[1:])-p[0])
    t=p[0]
    for i in p[1:-1]:
        print(t,i)
        t-=i
    print(p[-1],t)
else:
    m.sort(reverse=1)
    print(m[0]-sum(m[1:]))
    t=m[0]
    for i in m[1:]:
        print(t,i)
        t-=i

Solution (Python):








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

if len(A) == 2:
  x = max(A)
  y = min(A)
  print(x-y)
  print(x,y)
  exit(0)

plusA = []
minusA = []
for a in A:
  if a >= 0:
    plusA += [a]
  else:
    minusA += [a]
    
method = [] 
if len(plusA) == 0:
  minusA = sorted(minusA) 

  x = minusA.pop()
  y = minusA.pop()

  method.append([x,y])
  plusA.append(x-y)

elif len(minusA) == 0:
  plusA = sorted(plusA)[::-1] 
  x = plusA.pop()
  y = plusA.pop()
  method.append([x,y])
  minusA.append(x-y)
  
while True:
  if len(minusA) + len(plusA) == 1:
    print(x-y)
    for m in method:
      print(*m)
    break
  if len(plusA) == 1:
    
    x = plusA.pop()
    y = minusA.pop()
    method.append([x,y])
    plusA.append(x-y)
  else:
    
    x = minusA.pop()
    y = plusA.pop()
    method.append([x,y])
    minusA.append(x-y)

Solution (Python):


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

    li = []
    mm1 = A[N-1]
    mm2 = A[0]
    i = 1
    while(i<N-1):
        if A[i]<=0:
            li.append([mm1,A[i]])
            mm1 -= A[i]
        else:
            li.append([mm2,A[i]])
            mm2 -= A[i]
        i += 1
    li.append([mm1,mm2])
    print(mm1-mm2)
    for i in range(N-1):
        print(*li[i])
    

        


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int , input().split()))
pos = sorted([i for i in a if i >= 0], reverse=True)
neg = sorted([i for i in a if i < 0], reverse=True)

ans = []
if len(neg) == 0 and len(pos) == 2:
    print(pos[0] - pos[1])
    print(pos[0], pos[1])
    exit()
elif len(pos) == 0:
    tmp = neg[0]
    for i in range(1,len(neg)):
        ans.append([tmp, neg[i]])
        tmp -= neg[i]
    print(tmp)
    [print(*i) for i in ans]
    exit()
elif len(neg) == 0:
    neg.append(pos.pop())

tmp = neg.pop()
while len(pos) > 1:
    a = pos.pop()
    ans.append([tmp, a])
    tmp -= a
neg.append(tmp)

tmp = pos[0]
for i in neg:
    ans.append([tmp, i])
    tmp -= i

print(tmp)
[print(*i) for i in ans]

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
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, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
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 = 10 ** 9 + 7


n = I()
A = LI()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    minus_now = minus.pop()
    plus_now = plus.pop()
    while plus:
        nxt = plus.pop()
        print(minus_now, nxt)
        minus_now -= nxt
    while minus:
        nxt = minus.pop()
        print(plus_now, nxt)
        plus_now -= nxt
    print(plus_now, minus_now)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
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, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
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 = 10 ** 9 + 7


n = I()
A = LI()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    minus_now = minus.pop()
    plus_now = plus.pop()
    while plus:
        nxt = plus.pop()
        print(minus_now, nxt)
        minus_now -= nxt
    print(plus_now, minus_now)
    plus_now -= minus_now
    while minus:
        nxt = minus.pop()
        print(plus_now, nxt)
        plus_now -= nxt

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
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, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
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 = 10 ** 9 + 7


n = I()
A = LI()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    ret = minus.pop()
    plus_now = plus.pop()
    while plus:
        nxt = plus.pop()
        print(ret, nxt)
        ret -= nxt
    print(plus_now, ret)
    ret = plus_now - ret
    while minus:
        nxt = minus.pop()
        print(ret, nxt)
        ret -= nxt

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
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, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
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 = 10 ** 9 + 7


n = I()
A = LI()
A.sort()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    ret = minus.pop()
    for j in range(len(plus) - 1):
        print(ret, plus[j])
        ret -= plus[j]
    print(plus[-1], ret)
    ret = plus[-1] - ret
    for k in range(len(minus)):
        print(ret, minus[k])
        ret -= minus[k]

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
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, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
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 = 10 ** 9 + 7


n = I()
A = LI()
A.sort()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    ret = minus[-1]
    for j in range(len(plus) - 1):
        print(ret, plus[j])
        ret -= plus[j]
    print(plus[-1], ret)
    ret = plus[-1] - ret
    for k in range(len(minus) - 1):
        print(ret, minus[k])
        ret -= minus[k]

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
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, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
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 = 10 ** 9 + 7


n = I()
A = LI()
A.sort()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    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]

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

XY=[]
left,right=A[0],A[-1]
for i in range(1,n-1):
    num=A[i]
    if num<0:
        XY.append([right,num])
        right -=num
    else:
        XY.append([left,num])
        left -=num
XY.append([right,left])
print(right-left)
for x,y in XY:print(x,y)

Solution (Python):
n= int(input())
a = list(map(int,input().split()))
xy = []
xyap = xy.append
ap = max(a)
a.remove(ap)
am = min(a)
a.remove(am)
for i in a:
  if i>0:
    xyap((am,i))
    am -= i
  else:
    xyap((ap,i))
    ap -= i
xyap((ap,am))
print(ap-am)
for i in xy:
  print(*i)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    A = list(map(int, rl().split()))
    A.sort()
    
    ope = [[] for _ in range(N - 1)]
    for i in range(N - 2):
        if 0 <= A[i + 1]:
            ope[i] = [A[0], A[i + 1]]
            A[0] -= A[i + 1]
        else:
            ope[i] = [A[-1], A[i + 1]]
            A[-1] -= A[i + 1]
    ope[N - 2] = [A[-1], A[0]]
    
    ans = A[-1] - A[0]
    print(ans)
    for x, y in ope:
        print(x, y)


if __name__ == '__main__':
    solve()

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

A.sort()
from collections import deque
d = deque(A)
ans = []
r = d.pop()
for i in range(N-1):
  l = d.popleft()
  if i < N-2:
    nex = d.pop()
  else:
    nex = -1
  if nex>=0:
    ans.append((min(l,r),max(l,r)))
    d.appendleft((-1)*abs(l-r))
  else:
    ans.append((max(l,r),min(l,r)))
    d.appendleft(abs(l-r))
  r = nex
print(d.pop())
for a in ans:
  print(*a,sep=' ')

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


if min(A)>=0:
    print(sum(A)-2*A[0])
    for i in range(n):
        if i==0:
            x = A[i]
        elif i==n-1:
            print(A[i], x)
        else:
            print(x, A[i])
            x -= A[i]


elif max(A)<=0:
    A = A[::-1]
    print(sum(A)*(-1)+2*A[0])
    for i in range(n):
        if i==0:
            x = A[i]
        else:
            print(x, A[i])
            x -= A[i]


else:
    m,p = [], []
    for a in A:
        if a<0:
            m.append(a)
        else:
            p.append(a)
    print(sum(p)-sum(m))
    m.sort()
    p.sort()
    x = m[0]
    for i in range(len(p)-1):
        print(x, p[i])
        x -= p[i]
    else:
        print(p[-1], x)
        x = p[-1] - x
        
    for j in range(1, len(m)):
        print(x, m[j])
        x -= m[j]

Solution (Python):

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

A.sort()

MIN=A[0]
MAX=A[-1]

if MIN>0:
    print(sum(A)-MIN*2)
elif MAX<0:
    print(-(sum(A)-MAX*2))
else:
    M=0
    for i in range(N):
        M+=abs(A[i])
    print(M)
    

for i in range(1,N-1):
    if A[i]<0:
        print(MAX,A[i])
        MAX-=A[i]
    else:
        print(MIN,A[i])
        MIN-=A[i]

print(MAX,MIN)

Solution (Python):
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
from bisect import *
input=lambda :sys.stdin.readline().rstrip()
N=int(input())
A=list(map(int,input().split()))
if N==2:
    A.sort()
    print(A[1]-A[0])
    print(A[1],A[0])
else:
    plus= [i for i in A if i>=0]
    minus=[i for i in A if i<0]

    plus.sort(reverse=True)
    minus.sort()
    lst=[]
    if not minus:
        a=plus.pop()
        b=plus.pop()
        minus.append(a-b)
        lst.append([a,b])
    if not plus:
        a=minus.pop()
        b=minus.pop()
        plus.append(a-b)
        lst.append([a,b])

    while len(plus)>1:
        a=plus.pop()
        lst.append([minus[0],a])
        minus[0]-=a

    while len(minus)>1:
        a=minus.pop()
        lst.append([plus[0],a])
        plus[0]-=a

    print(plus[0]-minus[0])
    lst.append([plus[0],minus[0]])
    for a,b in lst:
        print(a,b)

Solution (Python):
import sys
INF = 10 ** 11
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
from heapq import heapify,heappush,heappop   

def main():
    n = int(input())
    a = list(map(int,input().split()))
    m = []
    p = []
    z = 0
    for num in a:
        if num < 0:
            m.append(num)
        elif num > 0:
            p.append(num)
        else:
            z += 1
    
    ans = []
    
    if z == n:
        print(0)
        for i in range(n - 1):
            print(0,0)
        return

    if len(p) == 0:
        if z:
            num1 = 0
            z -= 1
        else:
            m.sort()
            num1 = m[-1]
            m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
    
    elif len(m) == 0:
        if z:
            num1 = 0
            z -= 1
            idx = 0
        else:
            p.sort()
            num1 = p[0]
            idx = 1
        for i in range(idx,len(p) - 1):
            ans.append((num1,p[i]))
            num1 -= p[i]
        ans.append((p[-1],num1))
        num1 = p[-1] - num1
    
    elif len(p) * len(m) > 0:
        num1 = p[-1]
        p.pop()
        num2 = m[-1]
        m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
        for i in range(len(p)):
            ans.append((num2,p[i]))
            num2 -= p[i]
        ans.append((num1,num2))
        num1 -= num2
    
    for i in range(z):
        ans.append((num1,0))
    print(num1)
    for i in range(n - 1):
        print(*ans[i])

if __name__=='__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
A.sort()
m=A[0]
if m>=0:
    M=sum(A)-2*m
    print(M)
    s=A[0]
    for i in range(0,N-1):
        if i!=N-2:
            print(s,A[i+1])
            s-=A[i+1]
        else:
            print(A[N-1],s)
else:
    if A[N-1]>0:
        start=0
        end=N-1
        while end-start>1:
            test=(end+start)//2
            if 0>A[test]:
                start=test
            else:
                end=test
        if 0>A[end]:
            mid=end
        else:
            mid=start
        M=sum(abs(A[i]) for i in range(0,N))
        print(M)
        s=A[0]
        for i in range(mid+1,N):
            if i!=N-1:
                print(s,A[i])
                s-=A[i]
            else:
                print(A[N-1],s)
                s=A[N-1]-s
        for i in range(1,mid+1):
            print(s,A[i])
            s-=A[i]
    else:
        M=-sum(A)+2*A[N-1]
        print(M)
        s=A[N-1]
        for i in range(0,N-1):
            print(s,A[i])
            s-=A[i]

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    A.sort()

    if all([a <= 0 for a in A]):
        print(A[-1]-(sum(A)-A[-1]))

        for i in range(N-1):
            print(A[-1],A[i])
            A[-1] -= A[i]
        return
    if all([a >= 0 for a in A]):
        print(sum(A)-A[0]-A[0])
        for i in range(1,N-1):
            print(A[0],A[i])
            A[0] -= A[i]
        print(A[-1],A[0])
        return
    
    answer = 0
    plus = []
    minus = []
    for i in range(N):
        if A[i] >= 0:
            plus.append(A[i])
        else:
            minus.append(A[i])
        answer += abs(A[i])
    print(answer)
    
    while len(plus)>1:
        p = plus.pop()
        m = minus.pop()
        minus.append(m-p)
        print(m,p)
    
    while minus:
        m = minus.pop()
        print(plus[0],m)
        plus[0] -= m
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

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

    a = list(sorted(a))
    min_a = a.pop(0)
    max_a = a.pop(-1)

    pos_a = filter(lambda x: x >= 0, a)
    neg_a = filter(lambda x: x < 0, a)
    procedure = []
    
    state = min_a
    for _a in pos_a:
        procedure.append((state, _a))
        state -= _a

    procedure.append((max_a, state))
    state = max_a - state

    for _a in neg_a:
        procedure.append((state, _a))
        state -= _a

    print(state)
    for t in procedure:
        print(t[0], t[1])

if __name__ == "__main__":
    solve()

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

if n==2:
    print(max(al)-min(al))
    print(max(al),min(al))
    exit()

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

pos.sort()
neg.sort(reverse=True)
res = []

if len(pos)==0:
    x = neg[0]
    for a in neg[1:]:
        res.append((x,a))
        x -= a
elif len(neg)==0:
    x = pos[0]
    for a in pos[1:-1]:
        res.append((x,a))
        x -= a
    res.append((pos[-1],x))
    x = pos[-1]-x
else:
    p = pos[0]
    pos = pos[1:]

    x = neg[0]
    neg = neg[1:]

    for a in pos:
        res.append((x,a))
        x -= a
    res.append((p,x))
    x = p - x
    for a in neg:
        res.append((x,a))
        x -= a


print(x)
for a,b in res:
    print(a,b)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
A.sort()
P=sum([1 for a in A if a >= 0])
Q=N-P
if P==0:
  P=1
  Q=N-1
elif Q==0:
  P=N-1
  Q=1
ans=[]
for y in range(Q, N-1):
  ans.append((A[0], A[y]))
  A[0]=A[0]-A[y]

for y in range(Q):
  ans.append((A[N-1],A[y]))
  A[N-1]=A[N-1]-A[y]

print(A[N-1])
for x in ans:
  print(*x)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    neg = []
    pos = []
    for v in a:
        if v < 0:
            neg.append(v)
        else:
            pos.append(v)
    ans = []
    neg.sort()
    x, y = 0, len(neg)-1
    if len(pos) == 0:
        ans.append([neg[y], neg[x]])
        p = neg[y] - neg[x]
        pos.append(p)
        x += 1
        y -= 1
    for i in range(x, y):
        ans.append([pos[0], neg[i]])
        pos[0] -= neg[i]
    if y-x+1 > 0:
        pos.append(neg[y])
    pos.sort()
    for i in range(len(pos)-2):
        ans.append([pos[i], pos[i+1]])
        pos[i+1] = pos[i] - pos[i+1]
    if len(pos) > 1:
        ans.append([pos[len(pos)-1], pos[len(pos)-2]])
        pos[len(pos)-1] = pos[len(pos)-1] - pos[len(pos)-2]
    print(pos[len(pos)-1])
    for p, q in ans:
        print(p, q)

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort()
a = A[0]
b = A[-1]
xy = []
for i in range(1, N-1):
    if A[i] > 0:
        xy.append((a, A[i]))
        a -= A[i]
    else:
        xy.append((b, A[i]))
        b -= A[i]
xy.append((b, a))
print(b-a)
for x, y in xy:
    print(x, y)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter

def main():
    N = int(input())
    a = list(map(int,input().split()))
    posi,nega = [],[]
    for num in a:
        if num >= 0:
            posi.append(num)
        else:
            nega.append(num)
    
    posi.sort()
    nega.sort(reverse=True)
    ans = []
    if len(posi) == 0:
        a = nega[0]
        for i in range(N-1):
            ans.append((a,nega[i+1]))
            a -= nega[i+1]
    elif len(nega) == 0:
        if len(posi) == 2:
            ans.append((max(posi),min(posi)))
        else:
            a = posi[0]
            for i in range(N-2):
                ans.append((a,posi[i+1]))
                a -= posi[i+1]
            ans.append((posi[-1],a))
    else:
        pl,nl = len(posi),len(nega)
        a = posi[0]
        for i in range(pl-1):
            ans.append((nega[0],posi[i+1]))
            nega[0] -= posi[i+1]
        for i in range(nl):
            ans.append((a,nega[i]))
            a -= nega[i]
            
    print(ans[-1][0]-ans[-1][1])
    for x,y in ans:
        print(x,y)

if __name__ == "__main__":
    main()

Solution (Python):
from heapq import heapify,heappop,heappush
n = int(input())
a = list(map(int,input().split()))
if max(a) > 0 and min(a) < 0:
  hp = []
  hn = []
  heapify(hp)
  heapify(hn)
  ans = 0
  for i in range(n):
    if a[i] >= 0:
      heappush(hp,a[i])
      ans += a[i]
    else:
      ans -= a[i]
      heappush(hn,a[i])
  print(ans)
  while hp and hn:
    p = heappop(hp)
    q = heappop(hn)
    if hn:
      print(p,q)
      heappush(hp,p-q)
    elif hp:
      print(q,p)
      heappush(hn,q-p)
    else:
      print(p,q)
elif min(a) >= 0:
  ans = sum(a)-2*min(a)
  print(ans)
  heapify(a)
  while a:
    p = heappop(a)
    if a:
      q = heappop(a)
      if a:
        print(p,q)
        heappush(a,p-q)
      else:
        print(q,p)
    else:
      break
elif max(a) <= 0:
  ans = -sum(a)+2*max(a)
  a = [-a[i] for i in range(n)]
  heapify(a)
  print(ans)
  while a:
    p = heappop(a)
    if a:
      q = heappop(a)
      if a:
        print(-p,-q)
        heappush(a,p-q)
      else:
        print(-p,-q)
    else:
      break

Solution (Python):
import heapq
from collections import defaultdict
N = int(input())
nums  = list(map(int, input().split(" ")))

p_count = 0
m_count = 0
pos = []
minus = []
for num in nums:
    if num < 0:
        m_count += 1
        minus.append(num)
    else:
        p_count += 1
        pos.append(num)
pos.sort()
minus.sort() 
途中式 = []
if N == 2:
    x = max(nums) 
    y = min(nums)
    print(max(nums) - min(nums))
    print(x, y)
else:
    
    if m_count == 0: 
        x = min(pos)
        y = max(pos)
        途中式.append((x, y))
        minus.append(x - y)
        pos.remove(x)
        pos.remove(y)
        p_count -=2
        m_count += 1

    if p_count > 1: 
        for i in range(p_count - 1):
            
            x = minus.pop()
            y = pos.pop()
            途中式.append((x,y))
            minus.append(x- y)
    elif p_count == 0 : 
        x = minus.pop()
        y = minus.pop()
        途中式.append((x, y))
        pos.append(x - y)

    x = pos.pop()
    for i in minus:
        y = i
        途中式.append((x, y))
        x -=y
    print(x)
    for temp in 途中式:
        x,y = temp
        print(x,y)