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):
def examC():
    N = I()
    A = LI()
    negative = []; positive = []
    ans = 0; ansC = []
    for i in A:
        if i<0:
            negative.append(i)
        else:
            positive.append(i)
    if negative==[]:
        positive.sort()
        if N>2:
            cur = positive[0] - positive[1]
            ansC.append([positive[0], positive[1]])
            ans = sum(positive) - 2 * positive[0]
            for i in range(2, N - 1):
                ansC.append([cur, positive[i]])
                cur -= positive[i]
            ansC.append([positive[N - 1], cur])
        else:
            ansC.append([positive[1],positive[0]])
            ans = positive[1]-positive[0]

    elif positive==[]:
        negative.sort(reverse=True)
        cur = negative[0] - negative[1]
        ansC.append([negative[0],negative[1]])
        ans = -sum(negative)+2*negative[0]
        for i in range(2,N):
            ansC.append([cur,negative[i]])
            cur -=negative[i]
    else:
        positive.sort()
        negative.sort(reverse=True)
        ans = sum(positive) - sum(negative)
        curN = negative[0]
        curP = positive[0]
        for i in range(len(positive)-1):
            ansC.append([curN,positive[i+1]])
            curN -=positive[i+1]
        for i in range(len(negative)-1):
            ansC.append([curP,negative[i+1]])
            curP -=negative[i+1]
        ansC.append([curP,curN])

    print(ans)
    for v in ansC:
        print(" ".join(map(str,v)))

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

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

S = sum(map(abs,A))

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

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

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

Solution (Python):
import bisect
N = int(input())
A = list(map(int,input().split()))
if N == 2:
  print(max(A)-min(A))
  print(*A)
  exit()
A.sort()

plus = []
minus = []
for i in range(N):
  if A[i] < 0:
    minus.append(A[i])
  else:
    plus.append(A[i])
minus.sort()
plus.sort()
if len(plus) == 0:
  plus.append(minus.pop(-1))
elif len(minus) == 0:
  minus.append(plus.pop(0))


tmp = plus[-1]
tmp2 = minus[-1]
ans = []
cnt = 0
for i in range(len(plus)-1):
  ans.append([tmp2,plus[i]])
  tmp2 -= plus[i]
minus[-1] = tmp2
for i in range(len(minus)):
  ans.append([tmp,minus[i]])
  tmp -= minus[i]
print(tmp)
for i in range(N-1):
  print(*ans[i])

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
A.sort()
B=[A[i] for i in reversed(range(N)) if A[i]<0]
C=[A[i] for i in range(N) if A[i]>=0]
s=0
if len(B)>0 and len(C)>0:
  s=sum(B)*-1
  s+=sum(C)
else:
  if len(B)>0:
    s=B[0]-(sum(B)-B[0])
  else:
    s=sum(C)-C[0]*2
print(s)
tmp=0
if len(B)==0:
  tmp=C[0]
  for i in range(2,len(C)):
    print(tmp,C[i])
    tmp-=C[i]
  print(C[1],tmp)
  
  
elif len(C)==0:
  tmp=B[0]
  for i in range(1,len(B)):
    print(tmp,B[i])
    tmp-=B[i]
else:
  tmp=C[0]
  tmp2=B[0]
  for i in range(1,len(B)):
    print(tmp,B[i])
    tmp-=B[i]
  for i in range(1,len(C)):
    print(tmp2,C[i])
    tmp2-=C[i]
  print(tmp,tmp2)
  tmp-=tmp2

Solution (Python):

n = int(input())
a = list(map(int,input().split()))
plus = []
minus = []
for i in range(n):
  if a[i] >= 0:
    plus.append(a[i])
  else:
    minus.append(a[i])
plus.sort()
minus.sort(reverse=True)
lp = len(plus)
lm = len(minus)
if lp >= 1 and lm >= 1:
  ans = sum(plus) - sum(minus)
  print(ans)
  x = minus[0]
  lp = len(plus)
  for i in range(1,lp):
    print(x,plus[i])
    x = x - plus[i]
  minus[0] = x
  x = plus[0]
  for j in range(lm):
    print(x,minus[j])
    x = x - minus[j]
elif lp == 0:
  ans = - sum(minus) + 2 * max(minus)
  print(ans)
  x = minus[1]
  for j in range(2,lm):
    print(x,minus[j])
    x = x - minus[j]
  print(x,minus[0])
elif lm == 0:
  ans = sum(plus) - 2 * min(plus)
  print(ans)
  x = plus[0]
  for i in range(2,lp):
    print(x,plus[i])
    x = x - plus[i]
  print(plus[1],x)

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

if A[0] < 0 and A[-1] > 0:
    ans = 0
    for a in A:
        ans += abs(a)
    print(ans)
    now = A[0]
    for a in A[1:N - 1]:
        if a >= 0:
            print(now, a)
            now -= a
    print(A[-1], now)
    now = A[-1] - now
    for a in A[1: -1]:
        if a < 0:
            print(now, a)
            now -= a
elif A[0] >= 0:
    ans = sum(A) - A[0] * 2
    print(ans)
    now = A[0]
    for a in A[1: -1]:
        print(now, a)
        now -= a
    print(A[-1], now)
else:
    ans = -sum(A) + A[-1] * 2
    print(ans)
    now = A[-1]
    for a in A[: -1]:
        print(now, a)
        now -= a

Solution (Python):
n=int(input())
L=list(map(int,input().split()))
L.sort()
ans=[]
for i in range(1,n-1):
  if L[i]>0:
    ans.append((L[0],L[i]))
    L[0]-=L[i]
for i in range(n):
  if L[i]>0 or len(ans) == n-1:
    break
  else:
    ans.append((L[n-1],L[i]))
    L[n-1]-=L[i]
print(L[n-1])
for a in ans:
  x,y=a
  print(x,y)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    n, k = LI()
    a = n - k
    if k == 1:
        print(0)
        return
    print(a)
    return


def B():
    n = II()
    xy = LIR(n)
    if n == 1:
        print(1)
        return
    ans = inf
    for i in range(n):
        for k in range(n):
            if i != k:
                b = 1
                check = []
                check.append((i,k))
                p = xy[i][0] - xy[k][0]
                q = xy[i][1] - xy[k][1] 
                for l in range(n):
                    for m in range(n):
                        if not((l,m) in check) and p == xy[l][0] - xy[m][0] and q == xy[l][1] - xy[m][1]:
                            b += 1
                            check.append((l, m))
                ans = min(ans, n-b)
    print(ans)

    return


def C():
    n = II()
    a = LI()
    a.sort(key=abs)
    ans = deque()
    positive = deque()
    negative = deque()
    for i in range(n):
        if a[i] >= 0:
            positive.append(a[i])
        else:
            negative.append(a[i])
    a = deque(a)
    lenp = len(positive)
    lenn = len(negative)
    if lenp == 0:
        for i in range(n - 1):
            b, c = a.popleft(), a.popleft()
            ans.append((b, c))
            b -= c
            a.appendleft(b)
        print(b)
    elif lenn == 0:
        for i in range(n - 2):
            b, c = a.popleft(), a.popleft()
            ans.append((b, c))
            b -= c
            a.appendleft(b)
        b, c = a.popleft(), a.popleft()
        ans.append((c, b))
        c -= b
        print(c)
    else:
        p = positive.popleft()
        negative.appendleft(p)
        for i in range(lenn - 1):
            b, c = negative.popleft(), negative.popleft()
            ans.append((b, c))
            b -= c
            negative.appendleft(b)
        p, c = negative.popleft(), negative.popleft()
        positive.appendleft(c)
        for i in range(lenp - 1):
            b, c = positive.popleft(), positive.popleft()
            ans.append((b, c))
            b -= c
            positive.appendleft(b)
        b = positive.popleft()
        ans.append((p, b))
        p -= b
        print(p)
    for an in ans:
        print(" ".join(map(str, an)))
    return


def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == '__main__':
    C()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    plus = []
    minus = []
    for i in a:
        if i > 0:
            plus.append(i)
        elif i < 0:
            minus.append(i)
        else:
            if len(minus) == 0:
                minus.append(i)
            else:
                plus.append(i)
    plus.sort(reverse=True)
    minus.sort()
    if len(minus) == 0:
        minus.append(plus.pop())
    elif len(plus) == 0:
        plus.append(minus.pop())
    print(sum(plus)-sum(minus))
    now = minus[-1]
    for i in range(len(plus) - 1):
        print(now, plus[i])
        now -= plus[i]
    print(plus[-1], now)
    now = plus[-1] - now
    for i in range(len(minus) - 1):
        print(now, minus[i])
        now -= minus[i]
main()

Solution (Python):
import sys
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls.sort()
flg = True
if a_ls[-1] < 0:
    score = - sum(a_ls) + a_ls[-1] * 2
    flg = False
elif a_ls[1] < 0:
    score = sum([abs(a) for a in a_ls])
else:
    score = sum(a_ls) - a_ls[0] * 2
cur = a_ls[0]
last = a_ls[-1]
print(score)
if flg:
    for i in range(1,n):
        a = a_ls[i]
        if a < 0:
            print(last, a)
            last -= a
            continue
        if i < n - 1:
            print(cur, a_ls[i])
            cur -= a_ls[i]
        else:
            print(last, cur)
else:
    for i in range(n-1):
        print(last, a_ls[i])
        last -= a_ls[i]

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

if data[0] > 0:
    print(sum(data) - data[0] * 2)
    k = data[0]
    for i in range(1,n - 1):
        print("{} {}".format(k, data[i]))
        k -= data[i]
    print("{} {}".format(data[-1], k))

elif data[-1] <= 0:
    print(-sum(data) + (data[-1] * 2))
    k = data[-1]
    for i in range(n - 1):
        print("{} {}".format(k, data[i]))
        k -= data[i]

else: 
    print(sum(list(map(lambda x:abs(x), data))))
    max_data = data[-1]
    min_data = data[0]
    i = 1
    while data[i] <= 0:
        print("{} {}".format(max_data,data[i]))
        max_data -= data[i]
        i += 1
    while i < n - 1:
        print("{} {}".format(min_data, data[i]))
        min_data -= data[i]
        i += 1
    print("{} {}".format(max_data, min_data))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = []
A.sort()
a = A.pop(0)
b = A.pop()
N -= 2
k = 1
for i in range(N):
    y = A.pop()
    if y<0:k=0
    if k:
        ans.append([a,y])
        a -= y
    else:
        ans.append([b,y])
        b -= y
ans.append([b,a])
print(b-a)
for a in ans: print(*a)

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
A.sort()
ans = []
p = A[0]
q = A[-1]
for i in A[1:-1]:
    if i < 0:
        ans.append((q,i))
        q -= i
    else:
        ans.append((p,i))
        p -= i
ans.append((q,p))
print(q-p)
for i,j in ans:
    print(i,j)

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

Solution (Python):
n=int(input())
a=[int(j) for j in input().split()]
a.sort()
ans=[]
p=a[0]
q=a[-1]
for i in a[1:-1]:
    if i<0:
        ans.append([q,i])
        q-=i
    else:
        ans.append([p,i])
        p-=i
ans.append([q,p])
print(q-p)
for i,j in ans:
    print(i,j)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
hu=0
for b in a:
    if b<0:
        hu+=1
    else:
        break
if hu==0:
    hu=1
if hu==n:
    hu=n-1
ansl=[]
nowl=a[0]
for i in range(hu,n-1):
    ansl.append((nowl,a[i]))
    nowl-=a[i]

nowr=a[n-1]
for i in range(1,hu):
    ansl.append((nowr,a[i]))
    nowr-=a[i]

ansl.append((nowr,nowl))
print(nowr-nowl)
for i in range(n-1):
    x,y=ansl[i]
    print(x,y)

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

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

A.sort()

npl = A[-1]
nmi = A[0]

del A[0]
del A[-1]
lis = []

for i in A:

    if i < 0:
        lis.append([npl,i])
        npl -= i

    else:
        lis.append([nmi,i])
        nmi -= i

lis.append([npl,nmi])
print (npl-nmi)

for i in lis:

    print (i[0],i[1])

Solution (Python):

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

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

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

Solution (Python):
n = int(input())
arr = sorted(list(map(int,input().split())))
ans = 0
ok1 = False
ok2 = False
for i in arr:
    if i > 0:
        ok1 = True
    else:
        ok2 = True
if ok1 and ok2:
    for i in arr:
        ans += i if i > 0 else -i
    print(ans)
    ope1 = arr.pop()
    ope2 = arr.pop(0)
    for a in arr:
        if a > 0:
            print(ope2,a)
            ope2 -= a
        else:
            print(ope1,a)
            ope1 -= a
    print(ope1,ope2)
else:
    if ok1:
        ans = sum(arr) - 2*arr[0]
        print(ans)
        ope1 = arr.pop(0)
        for i in range(n-2):
            print(ope1,arr[i])
            ope1 -= arr[i]
        print(arr[-1],ope1)
    else:
        ans = -sum(arr) + 2*arr[-1]
        print(ans)
        ope2 = arr.pop(-1)
        for i in range(n-1):
            print(ope2,arr[i])
            ope2 -= arr[i]

Solution (Python):



def main():
    n = int(input())
    a = sorted(list(map(int, input().split())))
    plus = [a[-1]]
    minus = [a[0]]

    
    
    
    
    
    
    for ai in a[1:-1]:
        if ai >= 0:
            plus.append(ai)
        else:
            minus.append(ai)

    print(sum(plus) - sum(minus))

    
    for p in plus[1:]:
        print(minus[-1], p)
        minus[-1] -= p

    for m in minus:
        print(plus[0], m)
        plus[0] -= m


if __name__ == '__main__':
    main()

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

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=[]
C=[]
D=[]

for i in range(N):
  if A[i]<0:
    B.append(A[i])
  elif A[i]==0:
    C.append(A[i])
  else:
    D.append(A[i])

ans=[]
anss=0

if len(B)>0 and len(D)>0:
  for i in C:
    ans.append((B[0],0))
  for i in range(len(D)-1):
    ans.append((B[0],D[i]))
    B[0]-=D[i]
  for i in range(len(B)):
    ans.append((D[-1],B[i]))
    D[-1]-=B[i]
  anss=D[-1]

elif len(C)>0:
  for i in range(len(C)-1):
    ans.append((0,0))
  for i in B:
    ans.append((anss,i))
    anss-=i
  if(len(D)>0):
    for i in range(len(D)-1):
      ans.append((anss,D[i]))
      anss-=D[i]
    ans.append((D[-1],anss))
    anss=D[-1]-anss

else:
  E=sorted(B,reverse=True)+sorted(D)
  anss=E[0]
  for i in range(len(E)-2):
    ans.append((anss,E[i+1]))
    anss-=E[i+1]
  if anss<=0:
    ans.append((E[-1],anss))
    anss=E[-1]-anss
  else:
    ans.append((anss,E[-1]))
    anss-=E[-1]

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=[]
C=[]
D=[]

for i in range(N):
  if A[i]<0:
    B.append(A[i])
  elif A[i]==0:
    C.append(A[i])
  else:
    D.append(A[i])

ans=[]
anss=0

if len(B)>0 and len(D)>0:
  for i in C:
    ans.append((B[0],0))
  for i in range(len(D)-1):
    ans.append((B[0],D[i]))
    B[0]-=D[i]
  for i in range(len(B)):
    ans.append((D[-1],B[i]))
    D[-1]-=B[i]
  anss=D[-1]

elif len(C)>0:
  for i in range(len(C)-1):
    ans.append((0,0))
  for i in B:
    ans.append((anss,i))
    anss-=i
  if(len(D)>0):
    for i in range(len(D)-1):
      ans.append((anss,D[i]))
      anss-=D[i]
    ans.append((D[-1],anss))
    anss=D[-1]-anss

else:
  E=sorted(B,reverse=True)+sorted(D)
  anss=E[0]
  for i in range(len(E)-2):
    ans.append((anss,E[i+1]))
    anss-=E[i+1]
  if anss<=0:
    ans.append((E[-1],anss))
    anss=E[-1]-anss
  else:
    ans.append((anss,E[-1]))
    anss-=E[-1]

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

Solution (Python):
import bisect

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

i0 = bisect.bisect_left(A,0)
if i0 >= N-1:
  print(-sum(A[:N-1])+A[N-1])
  a = A[N-1]
  for i in range(N-1):
    print(a,A[i])
    a = a-A[i]
elif i0 <= 1:
  print(-A[0]+sum(A[1:]))
  a = A[0]
  for i in range(1,N-1):
    print(a,A[i])
    a = a-A[i]
  print(A[N-1],a)
else:
  print(-sum(A[:i0])+sum(A[i0:]))
  a = A[i0-1]
  for i in range(i0,N-1):
    print(a,A[i])
    a = a-A[i]
  print(A[N-1],a)
  a = A[N-1]-a
  for i in range(i0-1):
    print(a,A[i])
    a = a-A[i]

Solution (Python):
import bisect

N = int(input())
A = list(map(int,input().split()))
A.sort()
if N == 2:
  print(A[1]-A[0])
  print(A[1],A[0])
  exit()
i0 = bisect.bisect_left(A,0)
if i0 >= N-1:
  print(-sum(A[:N-1])+A[N-1])
  a = A[N-1]
  for i in range(N-1):
    print(a,A[i])
    a = a-A[i]
elif i0 <= 1:
  print(-A[0]+sum(A[1:]))
  a = A[0]
  for i in range(1,N-1):
    print(a,A[i])
    a = a-A[i]
  print(A[N-1],a)
else:
  print(-sum(A[:i0])+sum(A[i0:]))
  a = A[i0-1]
  for i in range(i0,N-1):
    print(a,A[i])
    a = a-A[i]
  print(A[N-1],a)
  a = A[N-1]-a
  for i in range(i0-1):
    print(a,A[i])
    a = a-A[i]

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



N = I()
A = III()
A.sort(reverse=True)
plus = 0
for a in A:
    if a>0:
        plus += 1
if plus==N:
    plus -= 1

ans = []
val = A[plus]

if plus:
    for i in range(1,plus)[::-1]:
        ans.append((val,A[i]))
        val -= A[i]
    ans.append((A[0],val))
    val = A[0]-val

for i in range(plus+1,N):
    ans.append((val,A[i]))
    val -= A[i]

print(val)
for a in ans:
    print(*a, sep=' ')

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

Solution (Python):
from bisect import bisect_left as bi
n = int(input())
a = [int(i) for i in input().split()]
a = sorted(a)
b,num = bi(a,0),a[0]
if b==n:
    q = n-1
    print(a[-1]-sum(a[:n-1]))
elif b:
    q = b
    print(sum(a[b:])-sum(a[:b]))
else:
    q = 1
    print(sum(a[1:])-a[0])
for i in range(q,n-1):
    print(num,a[i])
    num-=a[i]
print(a[-1],num)
num = a[-1]-num
for i in range(1,q):
    print(num,a[i])
    num-=a[i]

Solution (Python):
from collections import deque
N = int(input())
A = [int(i) for i in input().split()]
A.sort()
A = deque(A)
m = A.popleft()
ans = []
while len(A)>1:
  if A[-2]<=0:
    break
  x = A.pop()
  ans += [(m,x)]
  m = m-x
for i in range(len(A)-1):
  ans += [(A[-1],A[i])]
  A[-1] = A[-1]-A[i]

ans += [(A[-1],m)]
print(A[-1]-m)
for a in ans:
  print(*a)

Solution (Python):
import bisect

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

zp = bisect.bisect_left(A, 0)
ans = []

M = A[0]
for i in range(max(zp, 1), N-1):
    ans.append((M, A[i]))
    M -= A[i]

ans.append((A[N-1], M))
M = A[N-1]-M

for i in range(1, min(zp, N-1)):
    ans.append((M, A[i]))
    M -= A[i]


print(M)
for a, b in ans:
    print(a, b)

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

A.sort()
c = 0
for a in A:
    if a <= 0:
        c += 1

if c == N or c == N-1:
    ans = A[-1] - sum(A[:-1])
    print(ans)
    a, b = A[-1], A[0]
    for i in range(1, N):
        print(a, b)
        a, b = a-b, A[i]
elif c == 0 or c == 1:
    ans = sum(A[1:]) - A[0]
    print(ans)
    a, b = A[0], 0
    for i in range(1, N-1):
        a, b = a-b, A[i]
        print(a, b)
    a, b = A[-1], a-b
    print(a, b)
else:
    ans = sum(A[c:]) - sum(A[:c])
    print(ans)
    a, b = A[0], 0
    for i in range(N-c-1):
        a, b = a-b, A[c+i]
        print(a, b)
    a, b = A[-1], a-b
    print(a, b)
    for i in range(c-1):
        a, b = a-b, A[i+1]
        print(a, b)

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

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

A.sort()

minimum, maximum = A[0], A[-1]
xy = []

for i in range(1,N-1):
    num = A[i]
    if num >= 0:
        xy.append((minimum, num))
        minimum -= num
    else:
        xy.append((maximum, num))
        maximum -= num

xy.append((maximum, minimum))
print(maximum-minimum)
for x, y in xy:
    print(x,y)

Solution (Python):
n,*l=map(int,open(0).read().split())
l.sort()
i=0
ans=[]
while l[i]<0:
  i+=1
  if i==n:
    break
minus=l[:i]
plus=l[i:]
if minus and plus:
  x=minus[0]
  for y in plus[:-1]:
    ans.append((x,y))
    x-=y
  minus[0]=x
  x=plus[-1]
  for y in minus:
    ans.append((x,y))
    x-=y
elif minus:
  x=minus[-1]
  for y in minus[:-1]:
    ans.append((x,y))
    x-=y
else:
  x=plus[0]
  for y in plus[1:-1]:
    ans.append((x,y))
    x-=y
  ans.append((plus[-1],x))
  x=plus[-1]-x
print(x)
for i,j in ans:
  print(i,j)

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

positive = 0
negative = 0

for a in A:
    if a > 0:
        positive += a
    else:
        negative += a

if positive == 0:  
    
    A.sort(reverse=True)
    now = A[0]
    ans = []
    for a in A[1:]:
        ans.append((now, a))
        now -= a
    print(now)
    for a in ans:
        print(*a)
elif negative == 0:  
    
    A.sort()
    now = A[0]
    ans = []
    for a in A[1: N - 1]:
        ans.append((now, a))
        now -= a
    ans.append((A[-1], now))
    now = A[-1] - now
    print(now)
    for a in ans:
        print(*a)
else:
    A.sort()
    neg = A[0]
    pos = A[-1]
    ans = []
    for a in A[1: N - 1]:
        if a > 0:
            ans.append((neg, a))
            neg -= a
        else:
            ans.append((pos, a))
            pos -= a
    ans.append((pos, neg))
    print(pos - neg)
    for a in ans:
        print(*a)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
a.sort()
ans = []
if a[0] > 0:
    x = a[0]
    for i in range(1, N - 1):
        ans.append((x, a[i]))
        x -= a[i]
    ans.append((a[-1], x))
    x = a[-1] - x
elif a[-1] < 0:
    x = a[-1]
    for i in range(N - 2, -1, -1):
        ans.append((x, a[i]))
        x -= a[i]
else:
    x = a[0]
    for i in range(1, N - 1):
        if a[i] >= 0:
            ans.append((x, a[i]))
            x -= a[i]
    ans.append((a[-1], x))
    x = a[-1] - x
    for i in range(1, N - 1):
        if a[i] < 0:
            ans.append((x, a[i]))
            x -= a[i]

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

Solution (Python):
def main():

    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    
    if A[-1] < 0:
        maxv = -sum(A) + 2 * A[-1]
        cur = A[-1]
        ops = []
        for i in range(N-1):
            ops.append([cur, A[i]])
            cur -= A[i]
    elif A[0] >= 0:
        maxv = sum(A)-2*A[0]
        ops = []
        cur = A[0]
        for i in range(1, N-1):
            ops.append([cur, A[i]])
            cur -= A[i]
        ops.append([A[-1], cur])
    else:
        pos = []
        neg = []
        for a in A:
            if a < 0: neg.append(a)
            else: pos.append(a)
        maxv = sum(pos) - sum(neg)
        ops = []
        cur = neg[0]
        for i in range(len(pos)-1):
            ops.append([cur, pos[i]])
            cur -= pos[i]
        ops.append([pos[-1], cur])
        cur = pos[-1] - cur
        for i in range(1, len(neg)):
            ops.append([cur, neg[i]])
            cur -= neg[i]

    return maxv, ops

if __name__ == '__main__':
    output = main()
    print(output[0])
    for a, b in output[1]:
        print(" ".join(map(str, [a, b])))

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

n = i()
a = wi()

minus = 0
plus = 0

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

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


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

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

Solution (Python):
N=int(input())
A=sorted(map(int,input().split()))
m,M=A[0],A[-1]
L=[]

for i in range(1,N-1):
    v=A[i]
    if v>=0:
        L.append((m,v))
        m-=v
    else:
        L.append((M,v))
        M-=v
L.append((M,m))
ans=M-m

print(ans)
for a,b in L:
    print(a,b)

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

A.sort()

for i,a in enumerate(A):
    if a>=0:break
B = A[:i]
C = A[i:]

ans = []
if not B:
    x = C[0]
    for a in C[1:-1]:
        ans.append((x,a))
        x -= a
    ans.append((C[-1],x))
    x = C[-1] - x
elif not C:
    x = B[-1]
    for a in B[:-1]:
        ans.append((x,a))
        x -= a
else:
    x = B[0]
    for a in C[1:]:
        ans.append((x,a))
        x -= a
    ans.append((C[0],x))
    x = C[0]-x
    for a in B[1:]:
        ans.append((x,a))
        x -= a

print(x)
for out in ans:
    print(*out)