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( )))

a.sort()
from bisect import bisect_left

j = bisect_left(a,0)
if j==0:
    j+=1
elif j==n:
    j-=1
print(sum(a[j:])-sum(a[:j]))
for i in range(j,n-1):
    print(a[0],a[i])
    a[0]-=a[i]

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

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


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

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

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

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

ans = []
A.sort()
if A[0] >= 0:
    ans.append([A[0], A[1]])
    A[1] = A[0]-A[1]
    A.pop(0)
elif A[-1] <= 0:
    ans.append([A[-1], A[-2]])
    A[-2] = A[-1]-A[-2]
    A.pop(N-1)
for i in range(1, len(A)-1):
    if A[i] < 0:
        ans.append([A[-1], A[i]])
        A[-1] -= A[i]
    else:
        ans.append([A[0], A[i]])
        A[0] -= A[i]
if N > 2:
    ans.append([A[-1], A[0]])
print(A[-1]-A[0])
for a in ans:
    print(*a)

Solution (Python):
from sys import stdin
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
li.sort(reverse=True)
plus = []
minus = []
lin = []
for i in li:
    if i < 0:
        minus.append(i)
    else:
        plus.append(i)
if not plus:
    x = minus[0]
    for i in range(1,n):
        y = minus[i]
        lin.append([x,y])
        x -= y
    print(x)
    for i in lin:
        print(*i)
elif not minus:
    x = plus[-1]
    for i in range(1,n-1):
        y = plus[i]
        lin.append([x,y])
        x -= y
    lin.append([plus[0],x])
    print(plus[0]-x)
    for i in lin:
        print(*i)
else:
    x = minus[-1]
    y = plus[0]
    for i in range(len(minus)-1):
        lin.append([y,minus[i]])
        y -= minus[i]
    for i in range(1,len(plus)):
        lin.append([x,plus[i]])
        x -= plus[i]
    lin.append([y,x])
    print(y-x)
    for i in lin:
        print(*i)

Solution (Python):
N,*A=map(int, open(0).read().split())
A.sort()
m,M=A[0],A[-1]
B=A[1:-1]
ans=[]
for b in B:
    if b>=0:
        ans.append((m,b))
        m-=b
    else:
        ans.append((M,b))
        M-=b
ans.append((M,m))
print(M-m)
for x,y in ans:
    print(x,y)

Solution (Python):
from bisect import bisect_left

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

if A[0]<=0 and A[-1]<=0:
    ans = sum(map(abs, A[:-1]))+A[-1]
    A.reverse()

    r = 1
    a, b = A[0], A[1]
    t = []
    
    while 1:
        x, y = (a, b) if a>b else (b, a)
        t.append((x, y))
        r += 1
        if not r<N:
            break
        a, b = x-y, A[r]

elif A[0]>=0 and A[-1]>=0:
    ans = sum(A[1:])-A[0]

    r = 1
    a, b = A[0], A[1]
    t = []
    
    while 1:
        x, y = (a, b) if a<b else (b, a)
        t.append((x, y))
        
        r += 1
        if not r<N:
            break
        a, b = x-y, A[r]
    a, b = t[-1]
    t[-1] = (b, a)


else:
    ans = sum(map(abs, A))
    p = bisect_left(A, 0)
    t = []
    s = []

    a = A[p-1]
    for i in range(p, N-1):
        b = A[i]
        t.append((a, b))
        a = a - b
        
    c = A[N-1]
    for i in ([a]+A[:p-1]):
        t.append((c, i))
        c = c-i

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

Solution (Python):
n = int(input())
A = sorted(map(int, input().split()))
L = []
if A[0] >= 0:
  x = A[0]
  for i in range(1, n-1):
    L.append((x, A[i]))
    x -= A[i]
  L.append((A[-1], x))
  M = A[-1] - x
elif A[-1] < 0:
  x = A[-1]
  for i in range(1, n):
    L.append((x, A[n-1-i]))
    x -= A[n-1-i]
  M = x
else:
  for i in range(n):
    if A[i] >= 0:
      border = i
      break
  for i in range(border, n-1):
    L.append((A[0], A[i]))
    A[0] -= A[i]
  x = A[-1]
  for i in range(border):
    L.append((x, A[i]))
    x -= A[i]
  M = x
  
print(M)
for x, y in L:
  print(x, y)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
plus=[a[n-1]]
minus=[a[0]]
for i in range(1,n-1):
	if a[i]>=0:
		plus.append(a[i])
	else:
		minus.append(a[i])
print(sum(plus)-sum(minus))
k=minus[-1]
for i in range(len(plus)-1):
	print(k,plus[i+1])
	k-=plus[i+1]
t=plus[0]
for i in range(len(minus)-1):
	print(t,minus[i])
	t-=minus[i]
print(t,k)

Solution (Python):

N = int(input())

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



A.sort()

keyz = 0
for a in A:
    if a<0:
        keyz += 1

if keyz==0:
    keyz = 1
elif keyz == N:
    keyz = N-1


M = 0
M += -sum(A[:keyz])
M += sum(A[keyz:])
print(M)


    



if keyz == N-1:
    cur = A[-1]
    for i in range(N-1):
        print(cur,A[i])
        cur -= A[i]
    
else:            
    cur = A[0]
    for i in range(keyz,N-1):
        print(cur,A[i])
        cur = cur - A[i]
    print(A[N-1],cur)
    cur = A[N-1] - cur
    for i in range(1,keyz):
        print(cur,A[i])
        cur -= A[i]

Solution (Python):
n = int(input())
L = list(map(int,input().split()))
L.sort()
T = []
plus = []
minus = []
for i in range(n):
  if L[i] >= 0:
    plus.append(L[i])
  else:
    minus.append(L[i])
if len(L) == 2:
  if len(plus) == len(minus):
    print(plus[0]-minus[0])
    print(plus[0],minus[0])
  elif len(plus) == 2:
    print(plus[1]-plus[0])
    print(plus[1],plus[0])
  else:
    print(minus[1]-minus[0])
    print(minus[1],minus[0])
else:
  if len(plus) != 0 and len(minus) != 0:
    if min(len(plus),len(minus)) >= 2:
      T.append([minus[0],plus[0]])
      cur = minus[0]-plus[0]
      for i in range(1,len(plus)-1):
        T.append([cur,plus[i]])
        cur = cur-plus[i]
      T.append([plus[len(plus)-1],cur])
      cur = plus[len(plus)-1]-cur
      for i in range(1,len(minus)):
        T.append([cur,minus[i]])
        cur = cur-minus[i]
      print(cur)
      for i in range(len(T)):
        print(T[i][0],T[i][1])
    else:
      if len(minus) == 1:
        cur = minus[0]
        T.append([cur,plus[0]])
        cur = cur-plus[0]
        for i in range(1,len(plus)-1):
          T.append([cur,plus[i]])
          cur = cur-plus[i]
        T.append([plus[len(plus)-1],cur])
        cur = plus[len(plus)-1]-cur
        print(cur)
        for i in range(len(T)):
          print(T[i][0],T[i][1])
      else:
        cur = plus[0]
        T.append([cur,minus[0]])
        cur = cur-minus[0]
        for i in range(1,len(minus)):
          T.append([cur,minus[i]])
          cur = cur-minus[i]
        print(cur)
        for i in range(len(T)):
          print(T[i][0],T[i][1])
  elif len(plus) == 0:
    cur = minus[len(minus)-1]
    for i in range(n-1):
      T.append([cur,minus[n-i-2]])
      cur = cur-minus[n-i-2]
    print(cur)
    for i in range(len(T)):
      print(T[i][0],T[i][1])
  else:
    cur = plus[0]
    for i in range(1,n-1):
      T.append([cur,plus[i]])
      cur = cur-plus[i]
    T.append([plus[n-1],cur])
    cur = plus[n-1]-cur
    print(cur)
    for i in range(len(T)):
      print(T[i][0],T[i][1])

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

a.sort()

absum=0
minabs=10**9
for num in a:
        abnum=abs(num)
        absum+=abnum
        if minabs > abnum:
                minabs=abnum

if a[0] <= 0 and a[-1] >=0:
        print(absum)
else:
        print(absum - 2*minabs)
        num1=a.pop(0)
        num2=a.pop(-1)
        if abs(num1) > abs(num2):
                print(num2,num1)
                a.append(num2-num1)
        else:
                print(num1,num2)
                a.append(num1-num2)
a.sort()


now = a[0]
for i in range(len(a)-2,-1,-1):
        if a[i] > 0:
                print(now, a[i])
                now-=a[i]

a[0]=now

now = a[-1]

for i in range(len(a)-1):
        if a[i] >0:
                break
        print(now,a[i])
        now-=a[i]

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

a = sorted(a)

min = a[0]
max = a[n-1]

ans = []

for i in range(1,n-1):
    temp = a[i]
    if(temp < 0):
        ans.append('{} {}'.format(max, temp))
        max -= temp
    else:
        ans.append('{} {}'.format(min, temp))
        min -= temp

ans.append('{} {}'.format(max, min))
max -= min

print(max)

for i in range(n-1):
    print(ans[i])

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
plus, minus = [], []
for x in a:
    if x < 0: minus.append(x)
    else: plus.append(x)

if plus and minus:
    print(sum(map(abs, a)))
    p = plus[0]
    for x in minus[1:]:
        print(p, x)
        p -= x
    
    m = minus[0]
    for x in plus[1:]:
        print(m, x)
        m -= x
    
    print(p, m)

if not plus:
    minus.sort(reverse=True)
    m = minus[0]
    print(-(sum(minus) - m * 2))
    for x in minus[1:]:
        print(m, x)
        m -= x

if not minus:
    plus.sort()
    p = plus[0]
    print(sum(plus) - p * 2)
    for x in plus[2:]:
        print(p, x)
        p -= x
    print(plus[1], p)

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=10**9+7

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

lst=[]

flag=0
for i in range(n-1):
    if a[i+1]>0:
        flag=i+1
        break
if flag==0:
    flag=n-1
tmp=a[0]
for i in range(flag,n-1):
    lst.append([tmp,a[i]])
    tmp-=a[i]
a[0]=tmp
tmp=a[-1]
for i in range(flag):
    lst.append([tmp,a[i]])
    tmp-=a[i]

print(tmp)
for i in range(n-1):
    print(*lst[i])

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
xy=[]
for i in range(n-2,0,-1):
  y=a[i]
  if y>0:
    x=a[0]
    a[0]-=y
  else:
    x=a[n-1]
    a[n-1]-=y
  xy.append((x,y))
print(a[n-1]-a[0])
xy.append((a[n-1],a[0]))
print('\n'.join([' '.join(map(str,t)) for t in xy]))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
l=[a[0]]
r=[a[-1]]
for i in range(1,n-1):
    if a[i]<=0:
        r.append(a[i])
    else:
        l.append(a[i])
r.sort()
l.sort()
if len(r)==1:
    X=r[0]
    p=[]
else:
    p=[[r[-1],r[-2]]]
    for i in range(len(r)-3,-1,-1):
        p.append([p[-1][0]-p[-1][1],r[i]])
    X=p[-1][0]-p[-1][1]
if len(l)==1:
    Y=l[0]
    q=[]
else:
    q=[[l[0],l[1]]]
    for i in range(2,len(l)):
        q.append([q[-1][0]-q[-1][1],l[i]])
    Y=q[-1][0]-q[-1][1]
print(X-Y)
if p and q:
    for i in range(len(p)):
        print(*p[i])
    for i in range(len(q)):
        print(*q[i])
    print(X,Y)
elif p:
    for i in range(len(p)):
        print(*p[i])
    print(X,Y)
elif q:
    for i in range(len(q)):
        print(*q[i])
    print(X,Y)
else:
    print(X,Y)

Solution (Python):
import sys
from bisect import bisect_left

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
A.sort()

minus = bisect_left(A, 0)

m = min(minus, N-1)
if m == 0: m += 1

answer = -sum(A[:m]) + sum(A[m:])
print(answer)
cur = A[0]
for i in range(m, N-1):
    print(cur, A[i])
    cur -= A[i]

A[0] = cur
cur = A[N-1]
for i in range(m):
    print(cur, A[i])
    cur -= A[i]

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) 
def inpm(): return map(int,input().split()) 
def inpl(): return list(map(int, input().split())) 
def inpls(): return list(input().split())  
def inplm(n): return list(int(input()) for _ in range(n)) 
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def main():
    n = inp()
    a = inpl()
    a.sort()
    ans = []
    cnt = 0
    if a[0] >= 0:
        x = a[0]
        for i in range(n-2):
            ans.append((x,a[i+1]))
            x -= a[i+1]
        ans.append((a[n-1],x))
        cnt = a[n-1]-x        
    elif a[n-1] <= 0:
        x = a[n-1]
        for i in range(n-1):
            ans.append((x,a[i]))
            x -= a[i]
        cnt = x
    else:
        for i in range(n):
            if a[i] >= 0:
                x = a[i-1]
                index = i
                break
        for i in range(index+1,n):
            ans.append((x,a[i]))
            x -= a[i]
        a[index-1] = x
        x = a[index]
        for i in range(index):
            ans.append((x,a[i]))
            x -= a[i]
        cnt = x        
    print(cnt)
    for i in range(n-1):
        print(*ans[i])

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort()
M = A[0]
X = [A[0]]
pos = N
for i in range(N):
  if A[i] >= 0:
    pos = i
    break
if pos == 0:
  for i in range(1, N - 1):
    M -= A[i]
    X.append(M)
  S = M
  M = A[N-1] - M
  print(M)
  for i in range(N - 2):
    print(X[i], A[i+1])
  print(A[N - 1], S)
elif 1 <= pos <= N - 1:
  M = sum(abs(_) for _ in A)
  print(M)
  L = A[0]
  for j in range(pos, N - 1):
    print(L, A[j])
    L -= A[j]
  R = A[N - 1]
  print(R, L)
  R -= L
  for j in range(1, pos):
    print(R, A[j])
    R -= A[j]
elif pos == N:
  print(A[N-1] - sum(A[i] for i in range(N-1)))
  R = A[N - 1]
  for i in range(N-1):
    print(R, A[i])
    R -= A[i]

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

Solution (Python):
def main():
    from bisect import bisect_left

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

    a.sort()

    i0 = bisect_left(a, 0)
    
    
    

    sum_ = 0
    if i0 == 0:
        
        
        
        
        ret = []
        s = a[0]
        e = a[-1]
        for x in a[1:-1]:
            ret.append((s, x))
            s -= x
        ret.append((e, s))
        e -= s
        sum_ = e

    elif i0 == n:
        
        
        
        
        ret = []
        s = a[-1]
        for x in a[:-1]:
            ret.append((s, x))
            s -= x
        sum_ = s

    else:
        
        
        
        
        ret = []
        m = a[0]
        for x in a[i0:-1]:
            ret.append((m, x))
            m -= x

        p = a[-1]
        for x in a[1:i0]:
            ret.append((p, x))
            p -= x

        ret.append((p, m))
        sum_ = p - m

    print(sum_)
    for row in ret:
        print(*row)


if __name__ == '__main__':
    main()

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

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

print(A[N-1]-A[0])
xy.append((A[N-1],A[0]))
print('\n'.join([' '.join(map(str,t)) for t in xy]))

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

A.sort()

m = A[0]
M = A[-1]
ans = list()
for a in A[1: -1]:
    if a < 0:
        ans.append((M, a))
        M = M - a
    else:
        ans.append((m, a))
        m = m - a
ans.append((M, m))
print(M - m)
for x, y in ans:
    print(x, y)

Solution (Python):
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor


"""
if has negative

if all positive

"""

N = gi()
A = [0] * N
ans = 0
max_vi = -1
max_v = -1e4 - 1
min_vi = -1
min_v = 1e4 + 1
for i in range(N):
    a = gi()
    A[i] = a
    if (a >= max_v):
        max_vi = i
        max_v = a
    if (a < min_v):
        min_vi = i
        min_v = a
    ans += abs(a)

seq = []
for i in range(N):
    if i == max_vi or i == min_vi:
        continue
    a = A[i]
    if a >= 0:
        seq.append((min_v, a))
        min_v -= a
    else:
        seq.append((max_v, a))
        max_v -= a
print(max_v - min_v)
for p in seq:
    print("%d %d" % (p[0], p[1]))
print(max_v, min_v)

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

A.sort(reverse=True)
P=[]
M=[]

for i in A:
    if i<0:
        M.append(i)
    else:
        P.append(i)

ans=[]
if len(M)==0:
    if P[0]==0:
        print(0)
        for i in range(N-1):
            print(0,0)
        exit()
    else:
        x=P.pop()
        y=P.pop(0)
        ans.append([x,y])
        M.append(x-y)
if len(P)==0:
    x=M.pop(0)
    y=M.pop()
    ans.append([x,y])
    P.append(x-y)


for i in range(len(P)-1):
    y=P.pop()
    x=M.pop()
    ans.append([x,y])
    M.append(x-y)

for i in range(len(M)):
    y=M.pop()
    x=P.pop()
    ans.append([x,y])
    P.append(x-y)

print(P[0])

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

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

A = []
count = 0
for i in range(N):
    if AA[i] != 0:
        A.append(AA[i])
    else:
        count += 1

NN = N - count
B = [abs(A[i]) for i in range(NN)]

if count == 0:
    det = 1
    s = A[0]//B[0]
    for i in range(1,NN):
        if s * A[i] < 0:
            det = -1
            break

    if det == 1:
        m = min(B)

        M = sum(B) - 2 * m
        print(M)

        for i in range(NN):
            if m == B[i]:
                p = i
                break

        C = [A[i]//B[i] for i in range(NN)]

        C[p] *= -1

    else:
        M = sum(B)
        print(M)
        C = [A[i]//B[i] for i in range(NN)]

    fp = -1 
    fm = -1 
    for i in range(NN):
        if fp == -1 and C[i] == 1:
            fp = i
        elif fm == -1 and C[i] == -1:
            fm = i
        elif fp != -1 and fm != -1:
            break

    X = []
    Y = []

    for i in range(NN):
        if i == fp or i == fm:
            continue
        if C[i] == -1:
            X.append(i)
        else:
            Y.append(i)

    x = A[fp]
    for i in X:
        print(x,A[i])
        x -= A[i]

    y = A[fm]
    for i in Y:
        print(y,A[i])
        y -= A[i]

    print(x,y)

elif A == []:
    print(0)
    for i in range(count-1):
        print(0,0)

else:

    det = 1
    s = A[0]//B[0]
    for i in range(1,NN):
        if s * A[i] < 0:
            det = -1
            break

    M = sum(B)
    print(M)

    if det == 1:
        
        if s < 0:
            for i in range(count-1):
                print(0,0)
            x = 0
            for i in range(NN):
                print(x,A[i])
                x -= A[i]
        else:
            for i in range(count-1):
                print(0,0)
            x = 0
            for i in range(NN-1):
                print(x,A[i])
                x -= A[i]
            print(A[NN-1],x)
            




    else:
        
        
        C = [A[i]//B[i] for i in range(NN)]

        fp = -1 
        fm = -1 
        for i in range(NN):
            if fp == -1 and C[i] == 1:
                fp = i
            elif fm == -1 and C[i] == -1:
                fm = i
            elif fp != -1 and fm != -1:
               break

        X = []
        Y = []

        for i in range(NN):
            if i == fp or i == fm:
                continue
            if C[i] == -1:
                X.append(i)
            else:
                Y.append(i)

        x = A[fp]
        for i in X:
            print(x,A[i])
            x -= A[i]

        y = A[fm]
        for i in Y:
            print(y,A[i])
            y -= A[i]

        print(x,y)

        for i in range(count):
            print(x-y,0)

Solution (Python):
import bisect
n=int(input())
a=list(map(int,input().split()))
a.sort()
f = bisect.bisect_left(a,0)
res = []
if f == 0:
    t = a[0]
    for i in range(1,n-1):
        res.append([t,a[i]])
        t = t - a[i]
    res.append([a[n-1],t])
    t = a[n-1] - t
elif f == n:
    t = a[n-1]
    for i in range(n-1):
        res.append([t,a[i]])
        t = t - a[i]
else:
    t = a[f-1]
    for i in range(f+1,n):
        res.append([t,a[i]])
        t = t - a[i]
    res.append([a[f],t])
    t = a[f] - t
    for i in range(f-1):
        res.append([t,a[i]])
        t = t - a[i]
    

print(t)
for sres in res:
    print(" ".join(map(str, sres)))

Solution (Python):
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
sys.setrecursionlimit(200000)
input = sys.stdin.readline









N, = map(int, input().split())
L = [int(v) for v in input().split()]
L.sort()




c = 0
for v in L:
    if v < 0:
        c = c | 1
    elif v > 0:
        c = c | 2

if c == 0:
    c = 2

if c == 2:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    print(L[-2] - p)
    for x, y in ans:
        print(x, y)

if c == 1:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    ans = []
    p = L[-1] - L[0]
    ans.append((L[-1], L[0]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((p, L[-2]))
    print(p - L[-2])
    for x, y in ans:
        print(x, y)

if c == 3:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    mc = 0
    for v in L:
        if v < 0:
            mc += 1

    ans = []
    if N - mc == 1:
        p = L[-1] - L[0]
        ans.append((L[-1], L[0]))
        for v in L[1:-1]:
            ans.append((p, v))
            p -= v
        print(p)
        for x, y in ans:
            print(x, y)

        exit()

    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[mc:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    p = L[-2] - p
    for v in L[1:mc]:
        ans.append((p, v))
        p -= v
    print(p)
    for x, y in ans:
        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()


if A[0] >= 0:  
    now = A[0]
    ansls = []
    for a in A[1:-1]:
        ansls.append((now, a))
        now = now - a

    ansls.append((A[-1], now))
    ans = A[-1] - now
    print(ans)
    for ans in ansls:
        print(*ans)
elif A[-1] <= 0:  
    A = A[::-1]
    now = A[0]
    ansls = []
    for a in A[1:]:
        ansls.append((now, a))
        now = now - a

    print(now)
    for ans in ansls:
        print(*ans)
else:  
    ans = sum([abs(a) for a in A])
    print(ans)
    from bisect import bisect_right
    idx = bisect_right(A, 0) - 1  

    now = A[idx]
    ansls = []
    
    for a in A[idx + 1:-1]:
        ansls.append((now, a))
        now = now - a
    ansls.append((A[-1], now))
    now = A[-1] - now  

    
    
    for a in A[:idx]:
        ansls.append((now, a))
        now = now - a
    assert now == ans

    for ans in ansls:
        print(*ans)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int,input().split()))

a, b = max(A), min(A)
for t in [a, b]:
    for i in range(len(A)):
        if A[i] == t:
            A.pop(i)
            break

ans = a-b
for i in range(n-2):
    ans += abs(A[i])
print(ans)

for i in range(n-2):
    if A[i] > 0:
        print(b, A[i])
        b -= A[i]
    else:
        print(a, A[i])
        a -= A[i]
print(a, b)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(lambda x:abs(x),A))

A.sort()
for i in range(N):
    if A[i]>0:
        break
hu=A[:i]
sei=A[i:]

l=[]
if sei and hu:
    ans=sei.pop(-1)
    for item in sei:
        l.append([hu[0],item])
        hu[0]-=item
    for item in hu:
        l.append([ans,item])
        ans-=item

elif sei:
    ans=sei.pop(-1)
    for i in range(len(sei)-1):
        l.append([sei[0],sei[i+1]])
        sei[0]-=sei[i+1]
    l.append([ans,sei[0]])
    ans-=sei[0]
elif hu:
    for i in range(len(hu)-1):
        l.append([hu[-1],hu[-i-2]])
        hu[-1]-=hu[-i-2]
    ans=hu[-1]

print(ans)
for item in l:
    print(*item)

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

answer = []
if len(set(A)) == 1: 
  if A[0] >= 0:
    for i in range(2, N):
      answer.append((A[1], A[i]))
      A[1] -= A[i]
    answer.append((A[0], A[1]))
    answer_A = A[0] - A[1]
  else:
    for i in range(1, N):
      answer.append((A[0], A[i]))
      A[0] -= A[i]
    answer_A = A[0]
else:
  max_A = max(A)
  min_A = min(A)
  for i in range(N):
    if A[i] == max_A:
      max_i = i
    if A[i] == min_A:
      min_i = i
  for i in range(N):
    if i != max_i and i != min_i:
      if A[i] >= 0:
        answer.append((A[min_i], A[i]))
        A[min_i] -= A[i]
      else:
        answer.append((A[max_i], A[i]))
        A[max_i] -= A[i]
  answer.append((A[max_i], A[min_i]))
  answer_A = A[max_i] - A[min_i]

print(answer_A)
for a in answer:
  print(*a)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
A.sort()
flag=[0]*N
ans=[]
for i in range(1,N-1):
    if A[i]<0:
        ans.append([A[-1],A[i]])
        A[-1]-=A[i]
    else:
        ans.append([A[0],A[i]])
        A[0]-=A[i]
ans.append([A[-1],A[0]])
M=A[-1]-A[0]
print(M)
for u in ans:
    print(*u)

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

if max(A) < 0:
    print(max(A)-sum(A[:-1]))
    c = max(A)
    for i in range(N-1):
        print(c, A[i])
        c -= A[i]

elif min(A) >= 0:
    print(sum(A[1:])-min(A))
    c = min(A)
    for i in range(1, N-1):
        print(c, A[i])
        c -= A[i]
    print(max(A), c)

else:
    print(sum(abs(x) for x in A))
    c = min(A)
    d = max(A)
    for i in range(1, N-1):
        if A[i] < 0:
            print(d, A[i])
            d -= A[i]
        else:
            print(c, A[i])
            c -= A[i]
    print(d, c)

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    import collections
    D = collections.deque(A)
    ans = []
    for i in range(N-1):
        a, b = D.popleft(), D.pop()
        if not D or D[-1] < 0:
            D.append(b - a)
            ans.append((b, a))
        else:
            D.appendleft(a - b)
            ans.append((a, b))
    print(D.pop())
    for x, y in ans:
        print(x, y)


if __name__ == '__main__':
    main()

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

left = a.popleft()
right = a.pop()

ans = []
for i in a:
    if i > 0:
        ans.append((left, i))
        left -= i
    else:
        ans.append((right, i))
        right -= i
else:
    ans.append((right, left))

print(right-left)
for x, y in ans:
    print(x, y)

Solution (Python):
import sys
from bisect import bisect_left
read = sys.stdin.read

N, *A = map(int, read().split())
A.sort()
zero = bisect_left(A, 0)
xy = []
minimum = A[0]
maximum = A[-1]
for i in range(1, N - 1):
    if i < zero:
        xy.append(str(maximum) + ' ' + str(A[i]))
        maximum -= A[i]
    else:
        xy.append((str(minimum) + ' ' + str(A[i])))
        minimum -= A[i]
else:
    xy.append(str(maximum) + ' ' + str(minimum))

print(maximum - minimum)
print('\n'.join(xy))

Solution (Python):
import sys
from bisect import bisect_left
read = sys.stdin.read

N, *A = map(int, read().split())
A.sort()
zero = bisect_left(A, 0)
xy = []
for i in range(1, N - 1):
    if i < zero:
        xy.append((A[-1], A[i]))
        A[-1] -= A[i]
    else:
        xy.append((A[0], A[i]))
        A[0] -= A[i]

print(A[-1] - A[0])
for x, y in xy:
    print(x, y)
print(A[-1], A[0])