Problem:
 Score : 500 points    Problem Statement There are N integers, A_1, A_2, ..., A_N , written on a blackboard.  We will repeat the following operation N-1 times so that we have only one integer on the blackboard.   Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y .   Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.      Constraints  2 \leq N \leq 10^5  -10^4 \leq A_i \leq 10^4  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_N       Output Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.  Here x_i and y_i represent the integers x and y chosen in the i -th operation, respectively.  If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.  M  x_1  y_1  :  x_{N-1}  y_{N-1}         Sample Input 1 3
1 -1 2      Sample Output 1 4
-1 1
2 -2  If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2) .  Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4) .  In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4 .       Sample Input 2 3
1 1 1      Sample Output 2 1
1 1
1 0

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

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


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

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

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

Solution (Python):
import bisect
import os
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import re
import string
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))

plus = []
minus = []

for a in A:
    if a >= 0:
        plus.append(a)
    else:
        minus.append(a)
if not plus:
    mm = max(minus)
    plus.append(mm)
    minus.remove(mm)
if not minus:
    mp = min(plus)
    minus.append(mp)
    plus.remove(mp)

hist = []
x = minus.pop()
while len(plus) > 1:
    y = plus.pop()
    hist.append((x, y))
    x = x - y
minus.append(x)

x = plus.pop()
while len(minus) > 1:
    y = minus.pop()
    hist.append((x, y))
    x = x - y
y = minus.pop()
hist.append((x, y))

print(x - y)
for h in hist:
    print(*h)

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

leftest_val = A[0]
rightest_val = A[-1]
left_idx = 1
right_idx = N - 2
history = []
for i in range(N - 1):
    left_val = A[left_idx]
    right_val = A[right_idx]
    if i == N - 2:
        history.append("{} {}".format(rightest_val, leftest_val))
        print(rightest_val - leftest_val)
    elif left_val < 0:
        history.append("{} {}".format(rightest_val, left_val))
        rightest_val -= left_val
        left_idx += 1
    else:
        history.append("{} {}".format(leftest_val, right_val))
        leftest_val -= right_val
        right_idx -= 1

for h in history:
    print(h)

Solution (Python):
N = int(input())
A = sorted([int(a) for a in input().split()])
print(A[-1]-A[0]+sum([abs(a) for a in A[1:-1]]))

p = A[-1]
for i in range(N-1):
    a, b = max(p, A[i]), min(p, A[i])
    if i == N-2 or A[i+1] < 0:
        print(a, b)
        p = a - b
    else:
        print(b, a)
        p = b - a

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
a.sort(reverse=True)
add, sub = [a[0]], [a[-1]]
for i in range(1, n-1):
    if a[i] >= 0:
        add.append(a[i])
    else:
        sub.append(a[i])
res = []
it = len(add)-1
while it > 0:
    res.append([sub[-1],add[it]])
    sub[-1] -= add[it]
    it -= 1
it = len(sub)-1
while it >= 0:
    res.append([add[0],sub[it]])
    add[0] -= sub[it]
    it -= 1
print(add[0])
print("\n".join([" ".join(map(str,x)) for x in res]))

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

a.sort()
ope = []
max_num = a[-1]
min_num = a[0]
p = bisect.bisect_left(a, 0)
for i in range(1, min(p, n - 1)):
    ope.append([max_num, a[i]])
    max_num -= a[i]
for i in range(max(p, 1), n - 1):
    ope.append([min_num, a[i]])
    min_num -= a[i]
ope.append([max_num, min_num])
print(max_num - min_num)
for i in range(n - 1):
    print(" ".join(map(str, ope[i])))

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

from collections import deque

deq = deque(a_list)
res = []
while len(deq) > 2:
    min_num = deq.popleft()
    max_num = deq.pop()
    if min_num < 0 and max_num > 0:
        ne = deq.pop()
        if ne >= 0:
            res.append((min_num, ne))
            min_num -= ne
            deq.appendleft(min_num)
            deq.append(max_num)
        else:
            res.append((max_num, ne))
            max_num -= ne
            deq.append(max_num)
            deq.appendleft(min_num)
    else:
        if max_num <= 0:
            res.append((max_num, min_num))
            max_num = max_num - min_num
            deq.append(max_num)
        else:
            res.append((min_num, max_num))
            min_num = min_num - max_num
            deq.appendleft(min_num)

tmp1 = deq[0]
tmp2 = deq[1]
res.append((max(tmp1, tmp2), min(tmp2, tmp1)))
ans = max(tmp1, tmp2) - min(tmp2, tmp1)
print(ans)
for x,y in res:
    print(x, y)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split(' ')]
A.sort()
print(A[N - 1] + sum([abs(a) for a in A[1:N - 1]]) - A[0])
answer = ''
for i in range(1, len(A) - 1):
    if(A[i] > 0):
        answer += "{} {}\n".format(A[0], A[i])
        A[0] -= A[i]
    else:
        answer += "{} {}\n".format(A[N - 1], A[i])
        A[N - 1] -= A[i]
answer += "{} {}\n".format(A[N - 1], A[0])
print(answer)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
A=[a[0]]
x=0
sum=sum(a)
sumL=-9999999999
if n==2:
  print(max(a)-min(a))
  print(max(a),min(a))
else:
  for i in range(1,n):
    A.append(A[i-1]+a[i])
  for i in range(1,n):
    if sumL<-A[i-1]+sum-A[i-1]:
      sumL=-A[i-1]+sum-A[i-1]
      x=i
  print(sumL)
  L1=a[0:x]
  L2=a[x:n]

  if x!=1:
    print(L2[0],L1[0])
    z=L2[0]-L1[0]
    for i in range(1,x-1):
      print(z,L1[i])
      z=z-L1[i]
  else:
    z=L2[0]

  if x!=n-1:
    print(L1[x-1],L2[1])
    y=L1[x-1]-L2[1]
    for i in range(2,len(L2)):
      print(y,L2[i])
      y=y-L2[i]
  else:
    y=L1[len(L1)-1]

  print(z,y)

Solution (Python):

from collections import deque

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

positives = deque(sorted(a))
negatives = deque([positives.popleft()])
while len(positives) > 1:
    if positives[0] < 0:
        negatives.append(positives.popleft())
    else:
        break


print(sum(positives) - sum(negatives))

while len(positives) > 1:
    y = positives.popleft()
    x = negatives.pop()
    print(x, y)
    negatives.append(x - y)

x = positives.popleft()

while len(negatives) > 0:
    y = negatives.pop()
    print(x, y)
    x = x - y

Solution (Python):

import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())


N = int(input())
As = list(mi())

TL = [[], [], []]




for a in As:
  if a == 0: TL[0].append(a)
  elif a < 0: TL[1].append(a)
  else: TL[2].append(a)



ans = 0
printlist = []

if len(TL[0])==N: 
  print(0)
  for i in range(N-1):
    print(0, 0)
  exit()
elif len(TL[1])==N: 
  TL[1] = sorted(TL[1], reverse=True) 
  printlist.append((TL[1][0],TL[1][1])) 
  hand = TL[1][0]-TL[1][1] 
  idx = 2
  while idx < N:
    printlist.append((hand, TL[1][idx]))
    hand -= TL[1][idx]
    idx += 1
  ans = hand
  
  print(ans)
  for x, y in printlist:
    print(x, y)
  exit()
elif len(TL[2])==N: 
  TL[2] = sorted(TL[2]) 
  printlist.append((TL[2][0],TL[2][1])) 
  hand = TL[2][0]-TL[2][1] 
  idx = 2
  while idx < N-1:
    printlist.append((hand, TL[2][idx]))
    hand -= TL[2][idx]
    idx += 1
  
  printlist.append((TL[2][idx], hand))
  hand = TL[2][idx]-hand
  ans = hand
  
  print(ans)
  for x, y in printlist:
    print(x, y)
  exit()




if len(TL[0])==1 and len(TL[1])==1:
  
  left, right = TL[0].pop(), TL[1].pop()
  print(left-right)
  print(left, right)
  exit()
elif len(TL[0])==1 and len(TL[2])==1:
  
  left, right = TL[2].pop(), TL[0].pop()
  print(left-right)
  print(left, right)
  exit()






while not (len(TL[0])==0 and len(TL[1])==1 and len(TL[2])==1):
  left = 0
  right = 0
  if len(TL[0])>=1:
    
    if len(TL[1])==0: 
      left, right = TL[0].pop(), TL[2].pop()
      printlist.append((left, right))
      TL[1].append(left-right)
    elif len(TL[2])==0: 
      left, right = TL[0].pop(), TL[1].pop()
      printlist.append((left, right))
      TL[2].append(left-right)
    else: 
      left, right = TL[1].pop(), TL[0].pop()
      printlist.append((left, right))
      TL[1].append(left-right)
    continue
  
  
  
  if len(TL[1])>=len(TL[2]): 
    left, right = TL[2].pop(), TL[1].pop()
    printlist.append((left, right))
    TL[2].append(left-right)
  else: 
    left, right = TL[1].pop(), TL[2].pop()
    printlist.append((left, right))
    TL[1].append(left-right)


left, right = TL[2].pop(), TL[1].pop()
printlist.append((left, right))
ans = left-right



print(ans)
for x, y in printlist:
  print(x, y)

Solution (Python):
def c(N, A):
    A.sort()
    maximum = A.pop()
    minimum = A.pop(0)
    operation = []
    for a in A:
        if a >= 0:
            operation.append('{} {}'.format(minimum, a))
            minimum -= a
        else:
            operation.append('{} {}'.format(maximum, a))
            maximum -= a
    operation.append('{} {}'.format(maximum, minimum))
    return str(maximum - minimum) + '\n' + '\n'.join(operation)

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

Solution (Python):
N = int(input())
A = [int(s) for s in input().split(' ')]
A.sort()
print(A[N - 1] + sum([abs(a) for a in A[1:N - 1]]) - A[0])
answer = ''
for i in range(1, len(A) - 1):
    if(A[i] > 0):
        answer += "{} {}\n".format(A[0], A[i])
        A[0] -= A[i]
    else:
        answer += "{} {}\n".format(A[N - 1], A[i])
        A[N - 1] -= A[i]
answer += "{} {}\n".format(A[N - 1], A[0])
print(answer)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = sorted(LI())
    i = 0
    if a[0] >= 0:
        i = 0
    elif a[-1] <= 0:
        i = n - 2
    else:
        i = bisect.bisect_right(a,0) - 1

    r = []
    j = i + 1
    
    for k in range(i-1,-1,-1):
        r.append('{} {}'.format(a[j], a[k]))
        a[j] -= a[k]
    for k in range(j+1,n):
        r.append('{} {}'.format(a[i], a[k]))
        a[i] -= a[k]
    r.append('{} {}'.format(a[j],a[i]))

    return '{}\n{}'.format(a[j]-a[i], JA(r, '\n'))


print(main())

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


if N == 2:
  A.sort()
  print(A[-1] - A[0])
  print(A[-1], A[0])
  exit()


if sum(a < 0 for a in A) == 0:
  A.sort()
  solver = []
  var = A[0]
  for i in range(1,N-1):
    solver.append((var, A[i]))
    var = var - A[i]
  else:
    solver.append((A[-1], var))
    var = A[-1] - var

  print(var)
  for i in range(len(solver)):
    print(solver[i][0], solver[i][1])
  exit()
  

if sum(a > 0 for a in A) == 0:
  A.sort(reverse=True)
  solver = []
  var = A[0]
  for i in range(1,N):
    solver.append((var, A[i]))
    var = var - A[i]

  print(var)
  for i in range(len(solver)):
    print(solver[i][0], solver[i][1])
  exit()
  

A.sort()
n_nega = sum(a < 0 for a in A)
n_posi = sum(a > 0 for a in A)
solver = []
var = A[n_nega-1]
if n_posi > 1:
  for i in range(n_nega,N-1):
    solver.append((var, A[i]))
    var = var - A[i]
solver.append((A[-1], var))
var = A[-1] - var
if n_nega > 1:
  for i in range(n_nega-1):
    solver.append((var, A[i]))
    var = var - A[i]

print(var)
for i in range(len(solver)):
  print(solver[i][0], solver[i][1])

Solution (Python):
from collections import deque
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
p = sum(1 for a in A if a > 0)
if A[-1] > 0:
    print(sum(A[:-1]) - A[-1])
    k = A[-1]
    for a in A[:-2]:
        print(k, a)
        k -= a
    print(A[-2], k)

elif A[0] < 0:
    print(A[0] - sum(A[1:]))
    k = A[0]
    for a in A[1:]:
        print(k, a)
        k -= a
else:
    print(sum(abs(a) for a in A))
    A = deque(A)
    k = A.pop()
    for _ in range(p-1):
        t = A.popleft()
        print(k, t)
        k -= t
    t = A.popleft()
    print(t, k)
    t -= k
    for a in A:
        print(t, a)
        t -= a

Solution (Python):
N = int(input())
*A, = map(int, input().split())
INF = 10**9
p = q = 0
for i, a in enumerate(A):
    if a > 0:
        p += 1
    elif a < 0:
        q += 1

def solve(s, t):
    R = []
    i = A.index(s)
    if s == t:
        j = A.index(t, i+1)
    else:
        j = A.index(t)
    for k, a in enumerate(A):
        if i == k or j == k:
            continue
        if a >= 0:
            R.append("%d %d\n" % (s, a))
            s -= a
    for k, a in enumerate(A):
        if i == k or j == k:
            continue
        if a < 0:
            R.append("%d %d\n" % (t, a))
            t -= a
    R.append("%d %d\n" % (t, s))
    t -= s
    return t, R
ans = 0
if p > 0 and q > 0:
    s = t = 0
    for a in A:
        if a > 0:
            t = a
        elif a < 0:
            s = a
    ans, R = solve(s, t)
elif p > 0:
    A.sort()
    ans, R = solve(A[0], A[1])
elif q > 0:
    A.sort()
    ans, R = solve(A[-2], A[-1])
else:
    R = ["0 0\n"]*(N-1)
print(ans)
print("".join(R))

Solution (Python):
import sys
from collections import deque  
from collections import defaultdict  
from heapq import heapify, heappush, heappop, heappushpop  
from bisect import bisect_left, bisect_right  



s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    A = i2nn()
    A.sort()
    a0 = A[0]
    a3 = A[N-1]
    A12 = A[1:N-1]
    iz = bisect_left(A12, 0)
    A1 = A12[:iz]
    A2 = A12[iz:]

    logs = []
    def logging(x, y):
        logs.append('%d %d' % (x, y))
        return x - y
    x = a0
    for y in A2:
        x = logging(x, y)
    y = x
    x = a3
    x = logging(x, y)
    for y in A1:
        x = logging(x, y)
    print(x)
    for log in logs:
        print(log)

main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a.sort()
ps = []
ms = []

cnt = 0
ans = 0
anss = []
for e in a:
  if e<0:
    cnt += 1
    ms.append(e)
  else:
    ps.append(e)
  ans += abs(e)
if cnt==n:
  ans += a[n-1]*2
elif cnt==0:
  ans -= a[0]*2
print(ans)

if cnt==0:
  cur = a[0]
  a.remove(cur)
  for i in range(n-2):
    print(cur, a[i])
    cur = cur-a[i]
  print(a[n-2], cur)
elif cnt==n:
  cur = a[n-1]
  a.remove(cur)
  for i in range(n-1):
    print(cur, a[i])
    cur = cur-a[i]
else:
  while len(ps)>1:
    t = ps.pop()
    print(ms[0], t)
    ms[0] -= t
  while len(ms):
    t = ms.pop()
    print(ps[0], t)
    ps[0] -= t

for e in anss:
  print(*e)

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

else:
    num = 0
    ans = 0
    for i in range(n):
        if a[i] < 0:
            ans -=a[i]
        else:
            if a[i-1] < 0 and a[i] >= 0:
                num = i
            ans += a[i]
    print(ans)
    p = a[0]
    for i in range(num,n-1):
        print(p,a[i])
        p -= a[i]
    q = a[n-1]
    print(q,p)
    q -= p
    for i in range(1,num):
        print(q,a[i])
        q -= a[i]

Solution (Python):
N, *A = map(int, open(0).read().split())

x = sorted([v for v in A if v >= 0])
y = sorted([v for v in A if v < 0], reverse=True)

ans = []
if len(y) == 0:
    cnt = x[0]
    for v in x[1:-1]:
        ans.append((cnt, v))
        cnt -= v
    ans.append((x[-1], cnt))
    print(x[-1] - cnt)
elif len(x) == 0:
    cnt = y[0]
    for v in y[1:]:
        ans.append((cnt, v))
        cnt -= v
    print(cnt)
else:
    for v in x[:-1]:
        ans.append((y[0], v))
        y[0] -= v
        
    cnt = x[-1]
    for v in y:
        ans.append((cnt, v))
        cnt -= v
    print(cnt)
    
for v in ans:
    print(*v)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
a_left, a_right = a[0], a.pop()
ans_list = []
for i in range(1, n - 1):
    ans_list.append([[a_right, a_left][a[i] >= 0], a[i]])
    a_right, a_left = a_right - [a[i], 0][a[i] >= 0], a_left - [0, a[i]][a[i] >= 0]
ans_list.append([a_right, a_left])
print(a_right - a_left)
[print(*ans_list[i]) for i in range(n-1)]

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
l.sort()
if l[0]>=0 :
  print(sum(l)-2*l[0])
  for i in range(1,n-1):
    print(l[0],l[i])
    l[0]-=l[i]
  print(l[-1],l[0])
elif l[-1]<0:
  print(abs(sum(l)-2*l[-1]))
  for i in range(n-1):
    print(l[-1],l[i])
    l[-1]-=l[i]
else:
  cnt=0
  i=0
  while l[i]<0:
    cnt+=1
    if l[i+1]>=0:
      break
    i+=1
  ans=0
  for j in range(n):
    ans+=abs(l[j])
  print(ans)
  for j in range(n-2-i):
    print(l[0],l[i+j+1])
    l[0]-=l[i+j+1]
  for j in range(i+1):
    print(l[-1],l[j])
    l[-1]-=l[j]

Solution (Python):
from collections import Counter
from collections import deque
from functools import reduce
from pprint import pprint
import bisect
import copy
import fractions
import itertools
import math
import queue
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers): return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers): return reduce(LCM, numbers)
def LCM(m, n): return (m * n // fractions.gcd(m, n))


n = II()
a = sorted(LI(), reverse=True)
ans = []
s = 0
if all([i < 0 for i in a]):
    tmp = a[0]
    for i in range(1, n):
        ans.append((tmp, a[i]))
        tmp -= a[i]
    print(tmp)
    for i in ans:
        print(i[0], i[1])
else:
    tmp = a[-1]
    for i in range(1, n - 1):
        if a[i] < 0:
            break
        ans.append((tmp, a[i]))
        tmp -= a[i]

    ans.append((max(a), tmp))
    tmp = max(a) - tmp

    for i in range(1, n - 1):
        if a[i] >= 0:
            continue
        ans.append((tmp, a[i]))
        tmp -= a[i]
    print(tmp)
    for i in ans:
        print(i[0], i[1])

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
ans = 0
a.sort()
a_left, a_right = a[0], a.pop()
minus, plus = [], []
for i in range(1, n - 1):
    if a[i] < 0:
        minus.append(a[i])
    else:
        plus.append(a[i])

ans_list = []
for i in range(len(minus)):
    ans_list.append([a_right ,minus[i]])
    a_right -= minus[i]
for i in range(len(plus)):
    ans_list.append([a_left ,plus[i]])
    a_left -= plus[i]
ans_list.append([a_right, a_left])

print(a_right - a_left)
[print(*ans_list[i]) for i in range(n-1)]

Solution (Python):
from collections import deque
plusQ = deque([]) 
minsQ = deque([])
 
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
 
for itr, val in enumerate(A):
  if itr == 0:
    plusQ.append(val)
  elif itr != len(A) - 1 and val > 0:
    plusQ.append(val)
  else:
    minsQ.append(val)
    
p1 = plusQ.popleft()
 
ans = deque([])
while len(minsQ) > 0:
  if len(plusQ) > 0:
    p, m = plusQ.popleft(), minsQ.popleft()
    ans.append([m, p])
    minsQ.append(m - p)    
  else:
    m = minsQ.popleft()
    ans.append([p1, m])
    p1 = p1 - m

print(p1)
while len(ans) > 0:
  hoge = ans.popleft()
  print(hoge[0],hoge[1])

Solution (Python):
from bisect import bisect

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

ans = []

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

else :
    ind = bisect(A,0)

    X = A[:ind]
    Y = A[ind:]
    
    if len(X) == 0 :
        ans.append([Y[0],Y[1]])
        X.append(Y[0]-Y[1])
        Y.pop(0)
        Y.pop(0)
    elif len(Y) == 0 :
        ans.append([X[-1],X[-2]])
        Y.append(X[-1]-X[-2])
        X.pop(-1)
        X.pop(-1)
    
    while True :
        if len(X) >= 2 :
            ans.append([Y[0],X[-1]])
            Y[0] -= X.pop(-1)
        elif len(Y) >= 2 :
            ans.append([X[-1],Y[0]])
            X[-1] -= Y.pop(0)
        else :
            ans.append([Y[0],X[0]])
            calc = Y.pop(0) - X.pop(0)
            break
    
print(calc)
for i in range(len(ans)) :
    print(*ans[i])

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

print(a[-1] - a[0] + sum(map(abs, a[1:-1])))

pos = [a[-1]]
neg = [a[0]]
for x in a[1:-1]:
    if x > 0:
        pos.append(x)
    else:
        neg.append(x)
now = neg[0]
for p in pos[1:]:
    print(now, p)
    now -= p
print(pos[0], now)
now = pos[0] - now
for n in neg[1:]:
    print(now, n)
    now -= n

Solution (Python):
from collections import deque

N = int(input())
a = deque(sorted(map(int, input().split())))


max_a = a.pop()
min_a = a.popleft()

ans_s = ""
while True:
    if len(a) <= 0:
        ans_s += "{} {}\n".format(max_a, min_a)
        ans = max_a - min_a
        break

    if max_a <= 0:
        ans_s += "{} {}\n".format(max_a, min_a)
        max_a -= min_a

        min_a = a.popleft()
    elif min_a >= 0:
        ans_s += "{} {}\n".format(min_a, max_a)
        min_a -= max_a

        max_a = a.pop()
    else:
        w = a.pop()
        if w < 0:
            ans_s += "{} {}\n".format(max_a, w)
            max_a -= w
        else:
            ans_s += "{} {}\n".format(min_a, w)
            min_a -= w

print(ans)
print(ans_s, end="")

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
plus = [x for x in A if x > 0]
minus = [x for x in A if x <= 0]
plus.sort()
minus.sort()
minA = minus.pop() if minus else plus.pop(0)
M = sum(plus) - sum(minus)
M += -minA if plus else minA
print(M)
xy = minA
for i in range(N - 1):
  if len(plus) > 1:
    y = plus.pop()
    print('{} {}'.format(xy, y))
    xy -= y
  elif len(plus) == 1:
    x = plus.pop()
    print('{} {}'.format(x, xy))
    xy = x - xy
  else:
    y = minus.pop()
    print('{} {}'.format(xy, y))
    xy -= y

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

out = ""

minus = [a[0]]
plus = [a[-1]]

for v in a[1:-1]:
    if v < 0:
        minus.append(v)
    else:
        plus.append(v)


base_minus = minus.pop(0)
base_plus = plus.pop(0)
for i in range(len(plus)):
    x = base_minus
    y = plus[i]
    out += "{} {}\n".format(x, y)
    base_minus = x - y

out += "{} {}\n".format(base_plus, base_minus)
base_plus -= base_minus

for i in range(len(minus)):
    x = base_plus
    y = minus[i]
    out += "{} {}\n".format(x, y)
    base_plus = x - y
print(base_plus)
print(out)

Solution (Python):
import bisect

def main():
	n = int(input())
	a = list(map(int,input().split()))
	a.sort()
	idx = bisect.bisect_left(a,0)
	if idx == 0:
		idx = 1
	elif idx == n:
		idx = -1
	cur_min = a[0]
	cur_max = a[-1]
	res = []
	for n in a[1:idx]:
		res.append('{} {}'.format(cur_max,n))
		cur_max -= n
	for n in a[idx:-1]:
		res.append('{} {}'.format(cur_min,n))
		cur_min -= n
	res.append('{} {}'.format(cur_max,cur_min))
	print(cur_max-cur_min)
	for r in res:
		print(r)

if __name__ == '__main__':
	main()

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
a.sort()
t = 0
for m in range(N):
    if a[m] < 0:
        t += 1
if N > 2:
    if t == 0:
        c = a[0]
        for i in range(N - 2):
            c = c - a[N - 1 - i]
        d = a[1] - c
        print(d)
        e = a[0]
        f = a[N - 1]
        for j in range(N - 2):
            print(e, end = ' ')
            print(f)
            e = e - f
            f = a[N - 2 - j]
        print(a[1], end = ' ')
        print(e)
    elif t > 0 and t < N - 1:
        c = 0
        for i in range(t):
            c += a[i] 
        for j in range(N - t -1):
            c = c - a[t + 1 + j]
        d = a[t] - c
        print(d)
        e = a[0]
        f = a[N - 1]
        for k in range(N - t - 1):
            print(e, end = ' ')
            print(f)
            e = e - f
            f = a[N - 2 - k]
        a[0] = e
        g = a[t]
        for l in range(t):
            print(g, end = ' ')
            print(a[l])
            g = g - a[l]
    elif t == N - 1 or t == N:
        c = a[N - 1]
        for i in range(N - 1):
            c = c - a[i]
        print(c)
        d = a[N - 1]
        for j in range(N - 1):
            print(d, end = ' ')
            print(a[j])
            d -= a[j]
else:
    print(a[1] - a[0])
    print(a[1], end = ' ')
    print(a[0])

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

p = A[-1]
n = A[0]

ans_list = []

for a in A[1:-1]:
    if a >= 0:
        ans_list.append((n, a))
        n -= a
    else:
        ans_list.append((p, a))
        p -= a

M = p - n
ans_list.append((p, n))

print(M)
for x, y in ans_list:
    print(x, y)

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

ans_list = []
p = []
n = []


p.append(A[-1])
n.append(A[0])


for a in A[1:-1]:
    if a >= 0:
        p.append(a)
    elif a < 0:
        n.append(a)


res = n[0]
for ep in p[1:]:
    ans_list.append((res, ep))
    res -= ep
n[0] = res
p = [p[0]]


res = p[0]
for en in n:
    ans_list.append((res, en))
    res -= en


M = res
print(M)
for x, y in ans_list:
    print(x, y)

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    print(A[-1], tmp)

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)

else:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    if count_p != 0:
        for i in range(count_p-1):
            print(tmp_2, A[i+cmz])
            tmp_2 -= A[i+cmz]
        print(tmp, tmp_2)

Solution (Python):
from collections import deque

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

ans_list = []
p = deque()
n = deque()


p.append(A[-1])
n.append(A[0])


for a in A[1:-1]:
    if a >= 0:
        p.append(a)
    elif a < 0:
        n.append(a)


while len(p) != 1:
    x = n.pop()
    y = p.pop()
    n.append(x-y)
    ans_list.append((x, y))


while len(n) != 0:
    x = p.pop()
    y = n.pop()
    p.append(x-y)
    ans_list.append((x,y))


M = p[0]
print(M)
for x, y in ans_list:
    print(x, y)

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

ans_list = []
p = []
n = []


p.append(A[-1])
n.append(A[0])


for a in A[1:-1]:
    if a >= 0:
        p.append(a)
    elif a < 0:
        n.append(a)


while len(p) != 1:
    x = n.pop()
    y = p.pop()
    n.append(x-y)
    ans_list.append((x, y))


while len(n) != 0:
    x = p.pop()
    y = n.pop()
    p.append(x-y)
    ans_list.append((x,y))


M = p[0]
print(M)
for x, y in ans_list:
    print(x, y)

Solution (Python):
def c_successive_subtraction(N, A):
    A.sort()
    maximum = A.pop()
    minimum = A.pop(0)

    
    
    
    
    
    operation = []
    for a in A:
        if a >= 0:
            operation.append('{} {}'.format(minimum, a))
            minimum -= a
        else:
            operation.append('{} {}'.format(maximum, a))
            maximum -= a
    operation.append('{} {}'.format(maximum, minimum))
    return str(maximum - minimum) + '\n' + '\n'.join(operation)

N = int(input())
A = [int(i) for i in input().split()]
print(c_successive_subtraction(N, A))

Solution (Python):

import bisect
n = int(input())
a = sorted(map(int, input().split()))
if n == 2:
    print(a[1] - a[0])
    print(a[1], a[0])
    exit()

p = bisect.bisect_left(a, 0)
p = min(max(1, p), n - 1)
print(sum(a[p:]) - sum(a[:p]))
b = a[p - 1]
for i in range(p, n - 1):
    print(b, a[i])
    b -= a[i]
c = a[n - 1]
for i in range(p - 1):
    print(c, a[i])
    c -= a[i]
print(c, b)