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):
n = int(input())
a = list(map(int, input().split()))

pos = []
neg = []
zero = []
for i in range(n):
    if a[i] < 0:
        neg += [i]
    elif a[i] == 0:
        zero += [i]
    else:
        pos += [i]

if zero != [] or (pos != [] and neg != []):
    best = 0
    for i in range(n):
        best += abs(a[i])
    print(best)
else:
    best = 0
    for i in range(n):
        best += abs(a[i])
    if pos == []:
        best -= abs(max(a)) * 2
    else:
        best -= abs(min(a)) * 2
    print(best)

if len(pos) < len(neg):
    pos += zero
    pos.sort()
else:
    neg += zero
    neg.sort()

np = len(pos)
nn = len(neg)
if np == 0 or nn == 0:
    if np == 0: 
        index = a.index(max(a))
        x = a[index]
        for i in range(n):
            if i == index:
                continue
            y = a[i]
            print(x, y)
            x -= y
    else: 
        index = a.index(min(a))
        x = a[index]
        flag = 0
        if index == 0:
            flag = 1
            
        for i in range(n):
            if i == index or i == flag:
                continue
            y = a[i]
            print(x, y)
            x -= y
        x, y = a[flag], x
        print(x, y)
else:
    x = a[neg[0]]
    for k in range(np-1):
        y = a[pos[k]]
        print(x, y)
        x -= y
        
    y = x
    x = a[pos[np-1]]
    print(x, y)
    x -= y
    for k in range(1, nn):
        y = a[neg[k]]
        print(x, y)
        x -= y

Solution (Python):



from collections import defaultdict

N = int(input())
X = list(map(int, input().rstrip().split(' ')))
X = sorted(X)

count_minus = sum([1 if x < 0 else 0 for x in X])

if N == 2:
    print(X[1] - X[0])
    print(X[1], X[0])
else:
    ope = []
    min_value = X[0]
    max_value = X[-1]
    start = 1
    end = N-1

    for i in range(start, end):
        if X[i] < 0:
            ope.append([max_value, X[i]])
            max_value -= X[i]
        else:
            ope.append([min_value, X[i]])
            min_value -= X[i]
    ope.append([max_value, min_value])
    
    print(max_value - min_value)
    for i in range(len(ope)):
        a, b = ope[i]
        print(a, b)

Solution (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


def neg_pos(neg: list, pos: list):
    cur = neg[0]
    path = []
    for posi in pos[1:]:
        path.append((cur, posi))
        cur -= posi

    neg[0] = cur
    cur = pos[0]

    for negi in neg:
        path.append((cur, negi))
        cur -= negi

    print(cur)
    for pathi in path:
        print(*pathi)

    return


def neg_only(neg: list):
    cur = neg[0]
    path = []
    for negi in neg[1:]:
        path.append((cur, negi))
        cur -= negi

    print(cur)
    for pathi in path:
        print(*pathi)

    return

def pos_only(pos: list):
    cur = pos[0]
    path = []
    for posi in pos[1:-1]:
        path.append((cur, posi))
        cur -= posi

    path.append((pos[-1], cur))
    cur = pos[-1] - cur

    print(cur)
    for pathi in path:
        print(*pathi)

    return


n = ni()
a = list(li())

neg = []
pos = []

for ai in a:
    if ai < 0:
        neg.append(ai)
    else:
        pos.append(ai)

neg.sort(reverse=True)
pos.sort()


if len(neg) == 0:
    pos_only(pos)

elif len(pos) == 0:
    neg_only(neg)

else:
    neg_pos(neg, pos)

Solution (Python):
import bisect

N = int(input())
A = [int(_) for _ in input().split()]
plusflag = minusflag = False

for i in A:
    if not plusflag and i > 0:
        plusflag = True
    elif not minusflag and i < 0:
        minusflag = True
    
    if plusflag and minusflag:
        break

if not plusflag and not minusflag:
    print(0)
    for i in range(N-1):
        print(0, 0)
elif not plusflag and minusflag:
    A = sorted(A)[::-1]
    print(A[0]-sum(A[1:]))
    tmp = A[0]
    for i in range(N-1):
        x = tmp
        y = A[i+1]
        print(x, y)
        tmp -= y
elif plusflag and not minusflag:
    A = sorted(A)
    print(-A[0]+sum(A[1:]))
    tmp = A[0]
    for i in range(N-2):
        x = tmp
        y = A[i+1]
        print(x, y)
        tmp -= y
    print(A[-1], tmp)
else:
    A = sorted(A)
    zeroindex = bisect.bisect_left(A, 0)
    print(-sum(A[:zeroindex])+sum(A[zeroindex:]))
    if zeroindex != N-1:
        print(A[zeroindex-1], A[zeroindex])
        tmp = A[zeroindex-1] - A[zeroindex]
        for i in range(zeroindex+1, N-1):
            x = tmp
            y = A[i]
            print(x, y)
            tmp -= y
        print(A[-1], tmp)
        tmp = A[-1] - tmp
        for i in range(zeroindex-1):
            x = tmp
            y = A[i]
            print(x, y)
            tmp -= y
    else:
        tmp = A[-1]
        for i in range(N-1):
            x = tmp
            y = A[i]
            print(x, y)
            tmp -= y

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

i_plus = 0

plusflg = True
for i,A in enumerate(As):
    if A < 0:
        i_plus = i
        plusflg = False
        break

if plusflg:
    i_plus = N-1

ans = sum(As)
for i,A in enumerate(As[::-1]):
    if i == N-1:
        continue
    elif i == 0 or A < 0:
        ans -= 2*A
print(ans)

now = As[-1]
for i,A in enumerate(As[:-1]):
    if i+1 < i_plus:
        print(now,A)
        now -= A
    elif i+1 == i_plus:
        print(A, now)
        now = (A-now)
    else:
        print(max(A,now),min(A,now))        
        now = max(A,now)-min(A,now)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
p = []
c = []
    

a_max = a.pop(a.index(max(a)))
a_min = a.pop(a.index(min(a)))
for v in a:
    if v >= 0:
        p.append(v)
    else:
        c.append(v)
        
s = a_min
if n == 2:
    print(a_max - a_min)
    print(a_max, a_min)
    
else:
    t = [a_min]
    if p != []:
        for v in p:
            s -= v
            t.append(s)
    ans = a_max - s
    t.append(ans)
    if c != []:
        for v in c:
            ans -= v
            t.append(ans)
    print(ans)
    tmp = len(p)
    if p != []:
        for i in range(0, tmp):
            print(t[i], p[i])
    print(a_max, t[tmp])
    if c != []:
        for i in range(0, len(c)):
            print(t[tmp+i+1], c[i])

Solution (Python):

def solve():
    N = int(input())
    As = [int(i) for i in input().split()]
    As.sort()
    maxA = As.pop()
    minA = As.pop(0)
    upper0A = [i for i in As if i >= 0]
    lower0A = [i for i in As if i < 0]
    
    
    
    
    result_load = []
    for uA in upper0A:
        result_load.append([minA, uA])
        minA = minA - uA
    for lA in lower0A:
        result_load.append([maxA, lA])
        maxA = maxA - lA
    result_load.append([maxA, minA])
    resultA = maxA - minA

    
    print(resultA)
    for x, y in result_load:
        print(x, y)


if __name__ == '__main__':
    solve()

Solution (Python):

from itertools import product
from heapq import heappop, heappush

def solve():
    N = int(input())
    A0, A1 = [], []
    for a in map(int, input().split()):
        if a >= 0:
            heappush(A0,a)
        else:
            heappush(A1,-a)
    if A0 and A1:
        S = sum(A0) + sum(A1)
        res_l = [str(S)]
        b = heappop(A0)
        while len(A1) > 1:
            a = -heappop(A1)
            res_l.append('{} {}'.format(b, a))
            b -= a
        c = b
        b = -heappop(A1)
        while A0:
            a = heappop(A0)
            res_l.append('{} {}'.format(b, a))
            b -= a
        res_l.append('{} {}'.format(c, b))                
    else:
        if A0:
            S = sum(A0)
            b = heappop(A0)
            S -= 2*b
            res_l = [str(S)]
            for _ in range(N-2):
                a = heappop(A0)
                res_l.append('{} {}'.format(b, a))
                b -= a
            a = heappop(A0)
            res_l.append('{} {}'.format(a, b))
        else:
            S = sum(A1)
            b = -heappop(A1)
            S = S+2*b
            res_l = [str(S)]
            for _ in range(N-1):
                a = -heappop(A1)
                res_l.append('{} {}'.format(b, a))
                b -= a
    res =  '\n'.join(res_l)

    return res
if __name__ == '__main__':
    print(solve())

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    pos = []
    neg = []
    for i in range(N):
        if A[i] < 0:
            neg.append(A[i])
        else:
            pos.append(A[i])
    pos.sort()
    neg.sort(reverse=True)
    ans = sum(map(abs, A))
    if len(pos) == N:
        x = pos.pop(0)
        ans -= x * 2
        neg.append(x)
    elif len(neg) == N:
        x = neg.pop(0)
        ans += x * 2
        pos.append(x)
    print(ans)
    n = 1
    p = 1
    x = pos[0]
    y = neg[0]
    while p < len(pos):
        print(y, pos[p])
        y -= pos[p]
        p += 1
    while n < len(neg):
        print(x, neg[n])
        x -= neg[n]
        n += 1
    print(x, y)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
min_ = a[0]
max_ = a[-1]
if min_ == max_:
    if n == 2:
        print(0)
        print(max_, max_)
        exit()
    if min_ < 0:
        print(-min_ * (n - 2))
        print(min_, min_)
        for i in range(n - 2):
            print(i * min_, min_)
    else:
        print(min_ * (n - 2))
        print(max_, max_)
        for i in range(n - 3):
            print(i * min_, min_)
        print(max_, -(n - 3) * max_)
else:
    ans = ''
    if min_ >= 0:
        for i in range(1, n - 1):
            ans += str(min_) + ' ' + str(a[i]) + '\n'
            min_ -= a[i]
        ans += str(a[-1]) + ' ' + str(min_) + '\n'
        print(a[-1] - min_)
        print(ans, end='')
    elif max_ <= 0:
        for i in range(n - 1):
            ans += str(max_) + ' ' + str(a[i]) + '\n'
            max_ -= a[i]
        print(max_)
        print(ans, end='')
    else:
        for i in range(1, n - 1):
            if a[i] < 0:
                continue
            else:
                ans += str(min_) + ' ' + str(a[i]) + '\n'
                min_ -= a[i]
        ans += str(max_) + ' ' + str(min_) + '\n'
        max_ -= min_
        for i in range(1, n - 1):
            if a[i] >= 0:
                break
            else:
                ans += str(max_) + ' ' + str(a[i]) + '\n'
                max_ -= a[i]
        print(max_)
        print(ans, end='')

Solution (Python):
inpl = lambda: list(map(int,input().split()))

def output(plus,minus):
    print(sum(plus)-sum(minus))
    p = plus[0]
    for z in minus[1:]:
        print(p,z)
        p -= z
    q = minus[0]
    for w in plus[1:]:
        print(q,w)
        q -= w
    print(p,q)

N = int(input())
A = inpl()
plus = list(filter(lambda x: x>0, A))
minus = list(filter(lambda x: x<0, A))
Np = len(plus)
Nm = len(minus)
Nz = N-Np-Nm

if Np == N:
    plus.sort(reverse=True)
    minus = [plus.pop()]
elif Nm == N:
    minus.sort()
    plus = [minus.pop()]
elif Nz == N:
    plus = [0]*(Nz-1)
    minus = [0]
else:
    if Nm == 0:
        minus = [0]*Nz
    else:
        plus.extend([0]*Nz)

output(plus,minus)

Solution (Python):
import numpy as np
N = int(input())
A = sorted(list(map(int, input().split())))
B = np.array(A)
tmp = set(B)

if len(tmp) > 1:
    t = sum(np.abs(B))
    if A[0] > 0:
        print(t-A[0]*2)
    elif A[-1] < 0:
        print(t+A[-1]*2)
    else:
        print(t)
else:
    print((N - 2) * abs(A[0]))

for i in range(N-2):
    x = A.pop(0)
    y = A.pop(-1)
    if x > y:
        tmp = x
        x = y
        y = tmp
    if A[0] > 0:
        print(x, y)
        A.append(x-y)
    else:
        print(y, x)
        A.append(y-x)
x = A[0]
y = A[1]
if x > y:
    print(x, y)
else:
    print(y, x)

Solution (Python):
N = int(input())


As = []
minus_As = []
plus_As = []
minus_count = 0
plus_count = 0
for elm in input().split(" "):
    A = int(elm)
    if A >= 0:
        plus_As.append(A)
        plus_count += 1
    else:
        minus_As.append(A)
        minus_count += 1
    As.append(A)

cond1 = plus_count > 0
cond2 = minus_count > 0

case1 = cond1 and cond2

case2 = cond1 and (not cond2)

case3 = (not cond1) and cond2

out_txts = []
if case1:
    max_val = max(plus_As)
    max_i = plus_As.index(max_val)

    mA = minus_As[0]
    for i, pA in enumerate(plus_As):
        if i == max_i:
            continue
        out_txts.append("%d %d" % (mA, pA))
        mA -= pA
    minus_As[0] = mA

    out = max_val
    for mA in minus_As:
        out_txt = "%d %d" % (out, mA)
        out_txts.append(out_txt)
        out -= mA
elif case2:
    max_val = max(plus_As)
    max_i = plus_As.index(max_val)
    del plus_As[max_i]

    min_val = min(plus_As)
    min_i = plus_As.index(min_val)
    del plus_As[min_i]

    minA = min_val
    for i, pA in enumerate(plus_As):
        
        
        out_txt = "%d %d" % (minA, pA)
        out_txts.append(out_txt)
        minA -= pA
    
    out_txt = "%d %d" % (max_val, minA)
    out_txts.append(out_txt)
    out = max_val - minA

else:
    max_val = max(minus_As)
    max_i = minus_As.index(max_val)
    del minus_As[max_i]

    out = max_val
    for i, mA in enumerate(minus_As):
        
        
        out_txt = "%d %d" % (out, mA)
        out_txts.append(out_txt)
        out -= mA


print(out)
for out_txt in out_txts:
    print(out_txt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a.sort()
anslist = []
for i in range(1, n - 1):
    if a[i] >= 0:
        anslist.append([a[0], a[i]])
        a[0] -= a[i]
    else:
        anslist.append([a[-1], a[i]])
        a[-1] -= a[i]

anslist.append([a[-1], a[0]])
ans = a[-1] - a[0]
print(ans)
for i, j in anslist:
    print(i, j)

Solution (Python):

 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 
import time
import sys
from pprint import pprint

N = int(input())
A = [ int(x) for x in input().split() ]

min_heap = []
max_heap = []

for a in A:
    heapq.heappush(min_heap, a)
    heapq.heappush(max_heap, -a)

ans = []
for i in range(N-2):
    max = -heapq.heappop(max_heap)
    min = heapq.heappop(min_heap)
    diff = max - min
    new_val = 0
    if -max_heap[0] - (-diff) > diff - min_heap[0]:
        ans.append("{} {}".format(min,max))
        new_val = -diff
    else:
        ans.append("{} {}".format(max,min))
        new_val = diff
    heapq.heappush(max_heap, -new_val)
    heapq.heappush(min_heap, new_val)

print( -max_heap[0] - min_heap[0]  )
for a in ans:
    print(a)
print("{} {}".format(-max_heap[0], min_heap[0]))

Solution (Python):
from collections import deque
N = int(input())
A = list(map(int, input().split()))
A.sort()
q = deque(A)
ansq = deque()
i = 0
while i != N - 2:
    """if i % 2 == 0:
        l, r = q.popleft(), q.pop()
        q.appendleft(l - r)
        ansq.append((l,  r))
    else:
        l, r = q.popleft(), q.pop()
        q.append(r - l)
        ansq.append((r,  l))"""
    l, r = q.popleft(), q.pop()
    if l < 0 and r < 0: 
        if l < r:
            q.append(r - l)
            ansq.append((r, l))
        else:
            q.append(l - r)
            ansq.append((l, r))
    elif l >= 0 and r >= 0: 
        if l < r:
            q.appendleft(l - r)
            ansq.append((l, r))
        else:
            q.appendleft(r - l)
            ansq.append((r, l))
    else: 
        if abs(r - l - q[-1]) > abs(q[0] - l + r):
            q.append(r - l)
            ansq.append((r, l))
        else:
            q.appendleft(l - r)
            ansq.append((l, r))
    i += 1
print(q[1] - q[0])
for ans in ansq:
    print("{} {}".format(ans[0], ans[1]))
print(q[1], q[0])

Solution (Python):
import heapq
n=int(input())
Ans=[]
A=[int(i) for i in input().split()]
A.sort()
if A[0]>=0:
  Ans=[]
  heapq.heapify(A)
  for i in range(n-2):
    a1=heapq.heappop(A)
    a2=heapq.heappop(A)
    Ans.append([a1,a2])
    heapq.heappush(A,a1-a2)
  a1=heapq.heappop(A)
  a2=heapq.heappop(A)
  Ans.append([a2,a1])
  print(a2-a1)
  for i in range(n-1):
    print(*Ans[i])

elif A[-1]<=0:
  Ans=[]
  AA=[]
  for i in range(n):
    AA.append(-A[i])
  heapq.heapify(AA)
  for i in range(n-2):
    a1=heapq.heappop(AA)
    a2=heapq.heappop(AA)
    Ans.append([-a1,-a2])
    heapq.heappush(AA,-(a2-a1))
  a1=heapq.heappop(AA)
  a2=heapq.heappop(AA)
  Ans.append([max(-a2,-a1),min(-a2,-a1)])
  print(max(-a2,-a1)-min(-a2,-a1))
  for i in range(n-1):
    print(*Ans[i])
else:    
  Ap,Az,Am=[],[],[]
  for i in range(n):
    if A[i]>0:
      Ap.append(A[i])
    elif A[i]==0:
      Az.append(A[i])
    else:
      Am.append(-A[i])
  Ap.sort()
  Am.sort()
  pp,mm=Ap.pop(),Am.pop()
  heapq.heapify(Ap)
  heapq.heapify(Am)

  while len(Am)>=1:
    am=heapq.heappop(Am)
    Ans.append([pp,-am])
    pp+=am
  while len(Ap)>=1:
    ap=heapq.heappop(Ap)
    Ans.append([-mm,ap])
    mm+=ap
  for i in range(len(Az)):
    Ans.append([pp,0])
  Ans.append([pp,-mm])
  print(pp+mm)
  for i in range(len(Ans)):
    print(*Ans[i])

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr)
cnt1=0
cnt2=0
for i in range(n):
  if arr[i]>=0:
    cnt1+=1
  if arr[i]<=0:
    cnt2+=1
if cnt1==n:
  ans=-arr[0]+sum(arr[1:])
  print(ans)
  tmp=arr[0]
  for i in range(2,n):
    print(tmp,arr[i])
    tmp=tmp-arr[i]
  print(arr[1],tmp)
elif cnt2==n:
  ans=arr[-1]
  for i in range(n-1):
    ans+=abs(arr[i])
  print(ans)
  tmp=arr[-1]
  for i in range(n-1):
    print(tmp,arr[i])
    tmp=tmp-arr[i]
else:
  pos=0
  for i in range(n-1):
    if arr[i]*arr[i+1]<=0:
      pos=i
      break
  pos=pos+1
  ans=0
  for i in range(n):
    ans+=abs(arr[i])
  print(ans)
  for i in range(pos+1,n):
    print(arr[0],arr[i])
    arr[0]=arr[0]-arr[i]
  for i in range(pos):
    print(arr[pos],arr[i])
    arr[pos]=arr[pos]-arr[i]

Solution (Python):
N=int(input())
A=sorted([int(i) for i in input().split()])
Plus=[]
Noplus=[]
for i in A:
    if i>0:
        Plus.append(i)
    else:
        Noplus.append(i)
X=len(Plus)
Y=len(Noplus)
if Y==0:
    
    
    M=sum(A)-2*min(A)
    print(M)
    tmp=A[0]
    for i in range(1,N):
        if i!=N-1:
            print(tmp,A[i])
            tmp-=A[i]
        else:
            print(A[i],tmp)
            tmp=A[i]-tmp
elif X==0:
    
    
    M=-sum(A)+2*max(A)
    print(M)
    tmp=A[N-1]
    for i in range(N-1):
        print(tmp,A[i])
        tmp-=A[i]
elif X<Y:
    print(sum([abs(i) for i in A]))
    D=Y-X
    
    
    tmp=Plus.pop()
    for i in range(D):
        tmp2=Noplus.pop()
        print(tmp,tmp2)
        tmp-=tmp2
    
    
    tmp2=Noplus.pop()
    print(tmp,tmp2)
    tmp-=tmp2
    for i in range(X-1):
        tmp2=Noplus.pop()
        print(tmp2,tmp)
        tmp=tmp2-tmp
        tmp2=Plus.pop()
        print(tmp2,tmp)
        tmp=tmp2-tmp
else:
    print(sum([abs(i) for i in A]))
    
    D=X-Y
    
    
    tmp=Noplus.pop()
    for i in range(D):
        tmp2=Plus.pop()
        print(tmp,tmp2)
        tmp-=tmp2
    
    
    tmp2=Plus.pop()
    print(tmp2,tmp)
    tmp=tmp2-tmp
    for i in range(Y-1):
        tmp2=Noplus.pop()
        print(tmp2,tmp)
        tmp=tmp2-tmp
        tmp2=Plus.pop()
        print(tmp2,tmp)
        tmp=tmp2-tmp

Solution (Python):
n=int(input())
a=sorted(list(map(int,input().split())))
if a[-1]<=0:
  print(2*a[-1]-sum(a))
  s=a[-1]
  for i in a[:-1]:
    print(s,i)
    s-=i
elif a[0]>=0:
  print(sum(a)-2*a[0])
  s=a[0]
  for i in a[1:-1]:
    print(s,i)
    s-=i
  print(a[-1],s)
else:
  m=0
  for i in range(n):
    if a[i]>=0:
      k=i
      break
    m-=a[i]
  print(m+sum(a[k:]))
  s=a[0]
  for i in a[k:-1]:
    print(s,i)
    s-=i
  print(a[-1],s)
  s=a[-1]-s
  for i in a[1:k]:
    print(s,i)
    s-=i

Solution (Python):
import bisect

N=int(input())
A=[int(i) for i in input().split(" ")]
A=sorted(A)
traceback=[]

if A[0]>=0:
    minus=[A[0]]
    plus=A[1::]
elif A[-1]<=0:
    minus=A[:-1:]
    plus=[A[-1]]
else:
    index=bisect.bisect_left(A,0)
    minus=A[:index:]
    plus=A[index::]

print(sum(plus)-sum(minus))
m=minus.pop(0)
for p in plus[:-1:]:
    print(m,p)
    m=m-p
print(plus[-1],m)
p=plus[-1]-m
for m in minus:
    print(p,m)
    p=p-m

Solution (Python):

"""
Created on Sat Jun 15 20:54:34 2019

@author: Yamazaki Kenichi
"""

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

A.sort()

for near0 in range(N):
    if A[near0] > 0:
        break

if near0 != 0:
    near0 -= 1

tmp = A[near0]
T = []
for i in range(near0+1,N-1):
    T.append([tmp,A[i]])
    tmp = tmp - A[i]

T.append([A[N-1],tmp])
tmp = A[N-1] - tmp
for i in range(near0):
    T.append([tmp,A[i]])
    tmp = tmp - A[i]

print(tmp)
for t in T:
    print(' '.join(map(str,t)))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a.sort()
if a[0] >= 0:
    print(sum(a) - 2 * min(a))
    if n == 2:
        print(a[1], a[0])
    else:
        print(a[0], a[1])
        now = a[0] - a[1]
        for i in range(2, n-1):
            print(now, a[i])
            now = now - a[i]
        print(a[-1], now)
elif a[-1] <= 0:
    print(sum(map(abs, a)) + 2 * max(a))
    b = a[::-1]
    print(b[0], b[1])
    now = b[0] - b[1]
    for i in range(2, n):
        print(now, b[i])
        now -= b[i]
else:
    print(sum(map(abs, a)))
    if n == 2:
        print(a[1], a[0])
        exit()
    hu = list(filter(lambda x: x<0, a))
    se = list(filter(lambda x: x>=0, a))
    if len(se) == 1:
        print(a[-1], a[0])
        now = a[-1] - a[0]
        for i in range(1, n-1):
            print(now, a[i])
            now = now - a[i]
    else:
        print(hu[0], se[0])
        now = hu[0] - se[0]
        for i in range(1, len(se)-1):
            print(now, se[i])
            now -= se[i]
        print(se[-1], now)
        now = se[-1] - now
        for i in range(1, len(hu)):
            print(now, hu[i])
            now -= hu[i]

Solution (Python):
import bisect
import sys

N = int(input())
A = list(map(int,input().split()))
A.sort()
ans = 0
if N == 2:
    print(str(A[1]-A[0]))
    print(str(A[1]) + " " + str(A[0]))
else:
    if A[0] <= 0 and A[-1] >= 0:
        for i in range(N):
            ans += abs(A[i])
        print(str(ans))
        boarder = bisect.bisect_left(A, 0)
        if boarder != 0 and boarder != N-1:
            print(str(A[boarder-1]) + " " + str(A[boarder]))
            x = A[boarder-1] - A[boarder]
        else:
            if boarder == N-1:
                x = A[-1]
                for i in range(N-2, -1, -1):
                    print(str(x) + " " + str(A[i]))
                    x -= A[i]
                sys.exit()
            x = 0
        for i in range(boarder+1, N-1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
        print(str(A[-1]) + " " + str(x))
        x = A[-1] - x
        for i in range(boarder-2, -1, -1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
    elif A[0] <= 0:
        print(str(abs(sum(A))+A[-1]*2))
        
        print(str(A[N-1]) + " " + str(A[N-2]))
        x = A[N-1] - A[N-2]
        for i in range(N-3, -1, -1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
        
    else:
        print(str(sum(A)-A[0]*2))

        print(str(A[0]) + " " + str(A[1]))
        x = A[0] - A[1]
        for i in range(2, N-1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
        print(str(A[-1]) + " " + str(x))

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

a.sort()

if a[-1] <= 0:
    print(2*a[-1]-sum(a))
    p = a[-1]
    for i in range(1,n):
        print(p,a[n-1-i])
        p -= a[n-1-i]
    exit()


k = bisect.bisect_right(a,0)
if k == 0:
    print(sum(a)-2*a[0])
    p = a[0]
    for i in range(1,n-1):
        print(p,a[i])
        p -= a[i]
    print(a[-1],p)
    exit()

res = 0
for i in range(k):
    res -= a[i]
for i in range(k,n):
    res += a[i]
print(res)
p = a[0]
for i in range(k,n-1):
    print(p,a[i])
    p -= a[i]
print(a[-1],p)
p = a[-1]-p

for i in range(1,k):
    print(p,a[i])
    p -= a[i]

Solution (Python):

N = int(input())
A = list(map(int,input().split()))
A.sort()
AMIN = A[0]
AMAX = A[-1]
anslist = []
if AMIN*AMAX < 0:
    for i in range(N-1):
        if i == 0:
            pass
        elif A[i] < 0:
            anslist.append([AMAX,A[i]])
            AMAX-=A[i]
        elif A[i] == 0:
            anslist.append([AMAX,A[i]])
        elif A[i] > 0:
            anslist.append([AMIN,A[i]])
            AMIN-=A[i]
    anslist.append([AMAX,AMIN])
    ans = (AMAX-AMIN)
elif AMIN*AMAX >= 0:
    if AMAX > 0:
        for i in range(N-1):
            if i == 0:
                pass
            elif i == 1:
                anslist.append([A[0],A[1]])
                AMIN = A[0]-A[1]
            else:
                anslist.append([AMIN,A[i]])
                AMIN-=A[i]
        anslist.append([AMAX,AMIN])
        ans = (AMAX-AMIN)
    else:
        B = []
        for i in range(N):
            B.append(-A[N-i-1])
        BMIN = B[0]
        BMAX = B[-1]
        for i in range(N-1):
            if i == 0:
                pass
            elif i == 1:
                anslist.append([-B[0],-B[1]])
                BMIN = B[0]-B[1]
            else:
                anslist.append([-BMIN,-B[i]])
                BMIN-=B[i]
        anslist.append([-BMIN,-BMAX])
        ans = (BMAX-BMIN)
        
print(ans)
for cell in anslist:
    print(cell[0],cell[1])

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


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

    ko = a[-1]
    for i in range(0,n-1):
        print(ko,a[i])
        ko = ko-a[i]

elif a[0] < 0:
    ans = 0
    negn = 0
    for i in range(n):
        ans += abs(a[i])
        if a[i]<0:
            negn += 1
    print(ans)
    s = a[0]
    for i in range(negn , n-1):
        print(s,a[i])
        s = s-a[i]
    k = a[-1]
    for i in range(1,negn):
        print(k,a[i])
        k = k-a[i]
    print(k,s)

else:
    ans = 0
    for i in range(n):
        ans += abs(a[i])
    print(ans-2*a[0])
    m = a[0]
    for i in range(1,n-1):
        print(m,a[i])
        m = m-a[i]
    print(a[-1],m)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
AM = A.pop(0)
xy = []
if AM > 0 and A[-1] >= 0:
    p = 1
elif AM <= 0 and A[-1] < 0:
    p = 2
else:
    p = 3
if p == 1:
    while(len(A)>1):
        x = A.pop(-1)
        y = A.pop(0)
        xy.append([x, y])
        A.append(x-y)
    xy.append([AM, A[0]])
    ans = AM - A[0]
if p == 2:
    while(len(A)>1):
        x = AM
        y = A.pop(-1)
        xy.append([x, y])
        AM -= y
    xy.append([AM, A[0]])
    ans = AM - A[0]
if p == 3:
    while(len(A)>1):
        y = A.pop(0)
        if y >= 0:
            x = A.pop(-1)
            xy.append([x, y])
            A.append(x-y)
        else:
            x = AM
            xy.append([x, y])
            AM -= y
    xy.append([AM, A[0]])
    ans = AM - A[0]
print(ans)
for out in xy:
    print(*out)

Solution (Python):
import numpy as np
 
N = int(input())
*A, = map(int, input().split())

A.sort()
A = A[::-1]

log = []

a = A[0] 
b = A[-1] 
for i in range(1,N-1):
  if (A[i] < 0):
    log.append((a,A[i]))
    a -= A[i]
  else:
    log.append((b,A[i]))
    b -= A[i]
log.append((a,b))
print(a-b)
for l in log:
  print(l[0], l[1])

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


plusq =deque([a for a in A if a >=0])
minusq = deque([a for a in A if a < 0])

if not plusq:
    plusq.append(minusq.pop())
elif not minusq:
    minusq.appendleft(plusq.popleft())

ans = []

x = minusq.pop()
tmp_plus = plusq.pop()
while plusq:
    y = plusq.pop()
    ans.append((x,y))
    x = x - y

ans.append((tmp_plus,x))
x = tmp_plus  - x

while minusq:
    y = minusq.pop()
    ans.append((x,y))
    x = x - y
print(x)
for x,y in ans:
    print(x,y)

Solution (Python):
import bisect 

n=int(input())
l=list(map(int,input().split()))
l.sort()
if l[0]>=0:
    
    a=l[0]
    ans=0
    for i in range(n):
        ans+=l[i]
    print(ans-2*l[0])
    for i in range(1,n-1):
        print(a,l[i])
        a-=l[i]
    print(l[-1],a)
elif l[-1]<=0:
    
    a=l[-1]
    ans=l[-1]
    for i in range(n-1):
        ans-=l[i]
    print(ans)
    for i in range(n-1):
        print(a,l[i])
        a-=l[i]
else:
    
    idx=bisect.bisect(l,0)
    ans=0
    for i in range(n):
        if l[i]>=0:
            ans+=l[i]
        else:
            ans-=l[i]
    print(ans)
    a=l[0]
    for i in range(idx,n-1):
        print(a,l[i])
        a-=l[i]
    b=l[-1]
    for i in range(1,idx):
        print(b,l[i])
        b-=l[i]
        
    print(b,a)