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 itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

def reverse_insort(a, x, lo=0, hi=None):
    """Insert item x in list a, and keep it reverse-sorted assuming a
    is reverse-sorted.

    If x is already in a, insert it to the right of the rightmost x.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if x > a[mid]: hi = mid
        else: lo = mid+1
    a.insert(lo, x)

def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    index = bisect.bisect_left(A, 0)
    minus = A[:index]
    minus.reverse()
    plus = A[index:]
    
    
    progres = []
    ans = 0
    while True:
        if len(plus) == 1 and len(minus) == 1:
            p = plus.pop()
            m = minus.pop()
            progres.append((p, m))
            ans = p - m
            
            break

        if len(plus) == 1 and len(minus) == 0:
            ans = plus.pop()
            break
        if len(plus) == 0 and len(minus) == 1:
            ans = minus.pop()
            break

        
        if len(plus) == 0:
            val1 = minus.pop()  
            val2 = minus.pop(0)
            new_val = val2 - val1
            plus.append(new_val)
            progres.append((val2, val1))
            
            continue
        if len(minus) == 0:
            val1 = plus.pop(0)
            val2 = plus.pop()
            new_val = val1 - val2
            minus.append(new_val)
            progres.append((val1, val2))
            
            continue

        if len(plus) > len(minus):
            val1 = plus.pop()
            val2 = minus.pop()
            new_val = val2 - val1
            reverse_insort(minus, new_val)
            progres.append((val2, val1))
            
        else:
            val1 = plus.pop()
            val2 = minus.pop()
            new_val = val1 - val2
            index = bisect.bisect_left(plus, new_val)
            plus.insert(index, new_val)
            progres.append((val1, val2))
            

    print(ans)
    for p, q in progres:
        print('{} {}'.format(p, q))


if __name__ == '__main__':
    main()

Solution (Python):
inp = input()
large_n = int(inp)
inp = input()
split_input = inp.split()
a_list = [int(i) for i in split_input]
plus_list = []
minus_list = []
zero_count = 0
for n in a_list:
    if n > 0:
        plus_list.append(n)
    elif n < 0:
        minus_list.append(n)
    else:
        zero_count += 1
if zero_count == large_n:
    print(0)
    for _ in range(large_n-1):
        print("0 0")
    exit()
print_list = []
m = 0
if zero_count == 0:
    if len(plus_list) == 0:
        max_number = max(minus_list)
        hoge = minus_list.pop(minus_list.index(max_number))
        present_number = max_number
        for number in minus_list:
            print_list.append(str(present_number) + " " + str(number))
            present_number = present_number - number
        m = present_number
    elif len(minus_list) == 0:
        mini_number = plus_list.pop(plus_list.index(min(plus_list)))
        present_number = mini_number
        for number in plus_list[:-1]:
            print_list.append(str(present_number) + " " + str(number))
            present_number = present_number - number
        print_list.append(str(plus_list[-1]) + " " + str(present_number))
        m = plus_list[-1] - present_number
    else:
        present_number = minus_list.pop()
        for number in plus_list[:-1]:
            print_list.append(str(present_number) + " " + str(number))
            present_number -= number
        print_list.append(str(plus_list[-1]) + " " + str(present_number))
        present_number = plus_list[-1] - present_number
        for number in minus_list:
            print_list.append(str(present_number) + " " + str(number))
            present_number -= number
        m = present_number
else:
    for _ in range(zero_count-1):
        print_list.append("0 0")
    
    plus_number = 0
    if len(plus_list) > 0:
        plus_number = plus_list.pop()
    present_number = 0
    
    for number in plus_list:
        print_list.append(str(present_number) + " " + str(number))
        present_number -= number
    if plus_number != 0:
        print_list.append(str(plus_number) + " " + str(present_number))
        present_number = plus_number - present_number
    for number in minus_list:
        print_list.append(str(present_number) + " " + str(number))
        present_number -= number
    m = present_number


print(m)
for p in print_list:
    print(p)

Solution (Python):
n = int(input())
a = [int(_) for _ in input().split()]
ans = []
a.append(0)
a.sort()

if a.index(0) == 0:
    x = a.pop(1)
    y = a.pop()
    tmp = x-y
    ans.append([x,y])
    a.append(tmp)
    a.sort()
elif a.index(0) == n:
    x = a.pop(-2)
    y = a.pop(0)
    tmp = x-y
    ans.append([x,y])
    a.append(tmp)
    a.sort()

a.remove(0)
if len(a) == 1:
    print(a[0])
    print(ans[0][0], ans[0][1])
else:
    maximum = a.pop()
    minimum = a.pop(0)
    while len(a) > 0:
        x = a.pop()
        if x > 0:
            ans.append([minimum, x])
            minimum -= x
        else:
            ans.append([maximum, x])
            maximum -= x

    m = maximum-minimum
    print(m)
    ans.append([maximum, minimum])
    for i in ans:
        print(i[0], i[1])

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
p = []
m = []
for a in A:
    if a < 0:
        m.append(a)
    else:
        p.append(a)
was = []
if N == 2:
    print(max(A)-min(A))
    print(max(A), min(A))
    quit()
if len(m) == 0:
    p.sort()
    m.append(p[0] - p[-1])
    was.append((p[0], p[-1]))
    p = p[1:-1]
elif len(p) == 0:
    m.sort()
    p.append(m[-1] - m[0])
    was.append((m[-1], m[0]))
    m = m[1:-1]

mm = m[0]
m = m[1:]
pp = p[0]
p = p[1:]
for x in p:
    was.append((mm,x))
    mm -= x
for x in m:
    was.append((pp,x))
    pp -= x
was.append((pp, mm))
print(pp-mm)
for p,m in was:
    print(p,m)

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
lipos = [i for i in li if i>0]
lineg = [i for i in li if i<0]
li0 = [i for i in li if i==0]
np = len(lipos)
ng = len(lineg)
n0 = len(li0)

if ng == 0:
  lipos = lipos + li0
  np = len(lipos)
  print(sum(lipos)-2*min(lipos))
  lipos.sort()
  tmp = lipos[0]
  for i in range(1,np-1):
    print("{} {}".format(tmp,lipos[i]))
    tmp -= lipos[i]
  print("{} {}".format(lipos[np-1],tmp))
elif np == 0:
  lineg = lineg + li0
  ng = len(lineg)
  print(2*max(lineg)-sum(lineg))
  lineg.sort()
  lineg.reverse()
  tmp = lineg[0]
  for i in range(1,ng):
    print("{} {}".format(tmp,lineg[i]))
    tmp -= lineg[i]
else:
  lipos = lipos + li0
  np = len(lipos)
  print(sum(list(map(abs,lineg))) + sum(lipos))
  lipos.sort()
  lineg.sort()
  lineg.reverse()
  tmp = lineg[0]
  for i in range(1,np):
    print("{} {}".format(tmp,lipos[i]))
    tmp -= lipos[i]
  print("{} {}".format(lipos[0],tmp))
  tmp = lipos[0] - tmp
  for i in range(1,ng):
    print("{} {}".format(tmp,lineg[i]))
    tmp -= lineg[i]

Solution (Python):
from collections import deque
N = int(input())
A = (map(int, input().split()))
A = sorted(A)
A = deque(A)
l = []
for i in range(N - 1):
    if i == N - 2:
        ans = A[1] - A[0]
        l.append("{} {}".format(A[1], A[0]))
    elif i == N - 3:
        left = A.popleft()
        right = A.pop()
        center = A.pop()

        temp_left = abs(right - (left - center))
        temp_right = abs(left - (right - center))

        if temp_left >= temp_right:
            A.appendleft(left - center)
            A.append(right)
            l.append("{} {}".format(left, center))
        else:
            A.append(right - center)
            A.appendleft(left)
            l.append("{} {}".format(right, center))


    else:
        left = A.popleft()
        left2 = A.popleft()
        right = A.pop()
        right2 = A.pop()
        
        temp_left = abs(right - (left - right2))
        temp_right = abs(left - (right - left2))
        if temp_left >= temp_right:
            A.appendleft(left2)
            A.appendleft(left - right2)
            A.append(right)
            l.append("{} {}".format(left, right2))
        else:
            A.append(right2)
            A.append(right - left2)
            A.appendleft(left)
            l.append("{} {}".format(right, left2))

print(ans)
for line in l:
    print(line)

Solution (Python):
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort()
negative = deque([])
positive = deque([])

for n in A:
    if n < 0:
        negative.append(n)
    else:
        positive.append(n)

output = N-1

if negative and positive:
    print(abs(sum(negative)) + sum(positive))
elif not negative:
    print(sum(positive)-positive[0]*2)
    x = positive.popleft()
    y = positive.popleft()
    print(x, y)
    negative.append(x-y)
    output -= 1
else:
    print(abs(sum(negative)-negative[-1]*2))
    x = negative.pop()
    y = negative.pop()
    print(x, y)
    positive.append(x-y)
    output -= 1

for i in range(output):
    if len(negative) >= 2:
        y = negative.pop()
        x = positive.pop()
        print(x, y)
        positive.append(x-y)
    elif len(positive) >= 2:
        x = negative.pop()
        y = positive.pop()
        print(x, y)
        negative.append(x-y)
    else:
        print(positive.pop(), negative.pop())

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



n = int(input())
a = list(map(int, input().split()))
pt = []
nt = []
for x in a:
    if x >= 0:
        pt.append(x)
    elif x < 0:
        nt.append(x)

res = []

s = 0
if len(pt) == n:
    a.sort()
    s = a[0]
    for i in range(1, n-1):
        res.append((s, a[i]))
        s -= a[i]

    res.append((a[-1], s))
    s = a[-1] - s

elif len(nt) == n:
    a.sort(reverse=True)
    s = a[0]
    for i in range(1, n):
        res.append((s, a[i]))
        s -= a[i]

else:
    s = nt[0]
    for i in range(1, len(pt)):
        res.append((s, pt[i]))
        s -= pt[i]

    res.append((pt[0], s))
    s = pt[0] - s

    for i in range(1, len(nt)):
        res.append((s, nt[i]))
        s -= nt[i]

print(s)
for r in res:
    print(r[0], r[1])

Solution (Python):
import heapq

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

max_heap = []
plus_min = 100000
min_heap = []
minus_max = -100000
ope = []

for i in range(N):
    if A[i] >= 0:
        plus_min = min(plus_min, A[i])
        heapq.heappush(max_heap,(-A[i], A[i]))
    else:
        minus_max = max(minus_max, A[i])
        heapq.heappush(min_heap,(A[i], A[i]))

k = 0
for i in range(N-1):
    if len(min_heap)==0:
        x, y = plus_min, heapq.heappop(max_heap)[1]
        m = x - y
        ope.append("" + str(x) + " " + str(y))
        k+=1
        heapq.heappush(min_heap,(m, m))
    elif len(max_heap)==0:
        x, y = heapq.heappop(min_heap)[1], minus_max
        m = y - x
        ope.append("" + str(y) + " " + str(x))
        heapq.heappush(max_heap,(-m, m))
    elif len(max_heap)>1+k:
        x, y = heapq.heappop(min_heap)[1], heapq.heappop(max_heap)[1]
        m = x - y
        ope.append("" + str(x) + " " + str(y))
        heapq.heappush(min_heap,(m, m))
    else:
        x, y = heapq.heappop(min_heap)[1], heapq.heappop(max_heap)[1]
        m = y - x
        ope.append("" + str(y) + " " + str(x))
        heapq.heappush(max_heap,(-m, m))

print(heapq.heappop(max_heap)[1])
print("\n".join(ope))

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

Solution (Python):
from sys import stdin

if __name__ == '__main__':
    N = int(stdin.readline())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    A.sort(reverse=True)
    A_max = A.pop(0)

    A_minus = [i for i in A if i < 0]
    A_plus = [j for j in A if j >= 0]

    if len(A_minus) > 0:
        print(A_max - sum(A_minus) + sum(A_plus))

        A_minus.sort(reverse=False)
        tmp = A_minus.pop(0)

        for j in range(len(A_plus)):
            print(str(tmp) + " " + str(A_plus[j]))
            tmp = tmp - A_plus[j]

        print(str(A_max) + " " + str(tmp))
        tmp = A_max - tmp

        for i in range(len(A_minus)):
            print(str(tmp) + " " + str(A_minus[i]))
            tmp = tmp - A_minus[i]

    else:
        A_plus.sort(reverse=False)
        tmp = A_plus.pop(0)
        print(A_max - tmp + sum(A_plus))
        for i in range(len(A_plus)):
            print(str(tmp) + " " + str(A_plus[i]))
            tmp = tmp - A_plus[i]

        print(str(A_max) + ' ' + str(tmp))

Solution (Python):
n=int(input())
a_list=list(map(int,input().split()))
a_list.sort()
import bisect
x=bisect.bisect_left(a_list,0)
y=bisect.bisect_right(a_list,0)
neg=a_list[:x]
pos=a_list[y:]
zero=y-x
if zero==n:
    print(0)
    for i in range(n-1):
        print(0,0)
else:
    if neg==[]:
        if zero==0:
            m=sum(pos[1:])-pos[0]
            print(m)
            now=pos[0]
            for i in pos[1:-1]:
                print(now,i)
                now=now-i
            print(pos[-1],now)
        else:
            m=sum(pos)
            print(m)
            for i in range(zero-1):
                print(0,0)
            now=0
            for i in pos[:-1]:
                print(now,i)
                now=now-i
            print(pos[-1],now)
    elif pos==[]:
        if zero==0:
            m=abs(sum(neg[:-1])-neg[-1])
            print(m)
            now=neg[-1]
            for i in neg[:-1]:
                print(now,i)
                now=now-i
        else:
            m=abs(sum(neg))
            print(m)
            for i in range(zero-1):
                print(0,0)
            now=0
            for i in neg:
                print(now,i)
                now=now-i
    else:
        if zero==0:
            m=-sum(neg)+sum(pos)
            print(m)
            now=neg[0]
            for i in pos[1:]:
                print(now,i)
                now=now-i
            print(pos[0],now)
            now=pos[0]-now
            for i in neg[1:]:
                print(now,i)
                now=now-i
        else:
            m=-sum(neg)+sum(pos)
            print(m)
            for i in range(zero-1):
                print(0,0)
            now=neg[0]
            for i in pos:
                print(now,i)
                now=now-i
            print(0,now)
            now=-now
            for i in neg[1:]:
                print(now,i)
                now=now-i

Solution (Python):
from collections import deque
N=int(input())
A=list(map(int,input().split()))
A.sort()
A=deque(A)
L=deque([])
for i in range(N):
    if len(A)>=3:
        a=A.popleft()
        b=A.pop()
        if (b-a)-A[0] >= A[-1]-(a-b):
            A.append(b-a)
            L.append([b,a])
        else:
            A.appendleft(a-b)
            L.append([a,b])
    elif len(A)==2:
        a = A.popleft()
        b = A.pop()
        A.append(b-a)
        L.append([b,a])
        break
    else:
        break
print(A[0])
for j in L:
    print(j[0],j[1],sep=' ')

Solution (Python):
def main():
    num = int(input())
    data = list(map(int, input().split()))
    data.sort()

    if data[0] <= 0 and data[num - 1] >= 0:
        now_sei = data[num - 1]
        now_fu = data[0]

        ans = 0
        for i in range(num):
            ans += abs(data[i])
        print(ans)

        for i in range(1, num - 1):
            if data[i] > 0:
                print(now_fu, data[i])
                now_fu -= data[i]
            else:
                print(now_sei, data[i])
                now_sei -= data[i]
        print(now_sei, now_fu)

    elif data[num - 1] < 0:
        ans = data[-1]
        for i in range(num - 1):
            ans += abs(data[i])
        print(ans)

        now_moji = data[-1]
        for i in range(num - 1):
            print(now_moji,  data[i])
            now_moji -= data[i]

    elif data[0] > 0:
        ans = -1 * data[0]
        for i in range(1, num):
            ans += abs(data[i])
        print(ans)

        now_moji = data[0]
        for i in range(1, num - 1):
            print(now_moji,  data[i])
            now_moji -= data[i]
        print(data[num - 1], now_moji)






if __name__ == '__main__':
    main()

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

ans = sum([abs(i) for i in a[1:]]) - a[0]
for i in range(n):
    if a[i] >= 0:
        b = a[:i]
        c = a[i:]
        break
else:
    b = a[:]
    c = []



if b == []:
    l = c[0]
    c.pop(0)
else:
    l = b[0]
if c == []:
    
    r = b[-1]
    b.pop(-1)
    ans = sum([abs(i) for i in a[:-1]]) + a[-1]
else:
    r = c[-1]

print(ans)


for i in range(len(c)-1):
    print(l,c[i])
    l -= c[i]
for i in range(len(b)-1):
    print(r,b[-i-1])
    r -= b[-i-1]
print(r, l)

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

def printAns(array1,array2=[]):
    curr1 = array1[0]
    for i in range(1, len(array1)):
        print(curr1, array1[i])
        curr1 -= array1[i]

    if len(array2)>0:
        curr2 = array2[0]
        for i in range(1, len(array2)):
            print(curr2, array2[i])
            curr2 -= array2[i]
            
        print(curr1, curr2)
        curr1 -= curr2
        
    return curr1

plus = []
minus = []
zero = []
for it in a:
    if it == 0:
        zero.append(it)
    elif it > 0:
        plus.append(it)
    else:
        minus.append(it)


if len(plus) == 0:
    minus += zero
    minus.sort(key=lambda x:abs(x))
    max_value = abs(sum(minus[1:]))-abs(minus[0])
    print(max_value)
    printAns(minus)
elif len(minus) == 0:
    plus += zero
    plus.sort(key=lambda x:abs(x))  
    max_value = sum(plus[1:])-plus[0]
    print(max_value)
    printAns([plus[-1]], plus[:-1])
else:
    plus += zero
    array1 = [plus.pop()]
    array2 = []
    for i, it in enumerate(minus):
        if i==0:
            array2.append(it)
        else:
            array1.append(it)
    
    array2+=plus
    
    max_value = sum(map(abs, a))
    print(max_value)
    tmp = printAns(array1, array2)

Solution (Python):
n = int(input())
a = sorted(map(int, input().split()), reverse=True)
x, y = a[0], a[-1]
v = []
for i in a[1:-1]:
    if i > 0:
        v.append((y, i))
        y -= i
    else:
        v.append((x, i))
        x -= i
v.append((x, y))
print(x - y)
for i in v:
    print(i[0], i[1])

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

L=max(A)
S=min(A)
A.remove(L)
A.remove(S)
for x in A:
    if x>0:
        log += "\n"+str(S)+" "+str(x)
        S = S-x
    else:
        log += "\n"+str(L)+" "+str(x)
        L = L-x
log += "\n"+str(L)+" "+str(S)
L = L-S
print(str(L)+log)

Solution (Python):







n = int(input())
lists = list(map(int,input().split()))
lists.sort()
plus = []
minus = []
total = 0
for a in lists:
    if a < 0:
        minus.append(a)
    else:
        plus.append(a)
mini = min(len(minus), len(plus))
flag = (mini == len(plus))              
eq_flag = (len(plus) == len(minus))     
zero_flag = (mini == 0)                 
minus.reverse()


sum_list = 0

total = 0
if len(minus) == 0:
    print(sum(plus)-plus[0]*2)
    total = plus[0]
    for i in range(1,len(plus)-1):
        print(total, plus[i])
        total = total - plus[i]
    print(plus[-1], total)
elif len(plus) == 0:
    print(abs(sum(minus))-abs(minus[0]*2))
    total = minus[0]
    for i in range(1,len(minus)-1):
        print(total, minus[i])
        total = total - minus[i]
    print(total, minus[-1])
else:
    print(abs(sum(plus))+abs(sum(minus)))
    total = minus[-1]
    for i in range(len(plus)-1):
        print(total, plus[i])
        total = total - plus[i]
    print(plus[-1], total)
    total = plus[-1] - total
    for i in range(len(minus)-1):
        print(total, minus[i])
        total = total - minus[i]

Solution (Python):
from collections import deque

N = int(input())

pos = deque()
neg = deque()
zeros = 0

for a in map(int, input().split()):
    if a == 0:
        zeros += 1
    else:
        if a > 0:
            pos.append(a)
        else:
            neg.append(a)

for _ in range(zeros):
    if len(pos) == 0:
        pos.append(0)
    else:
        neg.append(0)

pos = sorted(pos)
neg = sorted(neg)
ops = deque()


if len(pos) > 0:
    p0 = pos.pop()
else:
    p0 = neg.pop()

if len(neg) > 0:
    n0 = neg.pop()
else:
    n0 = pos.pop(0)

plast = p0
for n in neg:
    ops.append((plast, n))
    plast = plast - n

nlast = n0
for p in pos:
    ops.append((nlast, p))
    nlast = nlast - p

ops.append((plast, nlast))
m = plast - nlast

print(m)
for x, y in ops:
    print(str(x) + " " + str(y))

Solution (Python):

from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip

import random
import collections
import math
import itertools

def real_main():
    N = read_int()
    nums = read_int_array()
    x, actions = solve(N, nums)
    print(x)
    for a, b in actions:
        print(a, b)


def solve1(n, nums):
    next_small = n // 2 - 1
    next_big = next_small + 1
    next_op = n % 2

    actions = []
    if next_op:
        last = nums[next_small] - nums[next_big]
        actions.append((nums[next_small], nums[next_big]))
    else:
        last = nums[next_big] - nums[next_small]
        actions.append((nums[next_big], nums[next_small]))

    next_big += 1
    next_small -= 1

    next_op = 1 - next_op
    while next_small >= 0 or next_big < n:
        if next_op:
            actions.append((nums[next_small], last))
            last = nums[next_small] - last
            next_small -= 1
        else:
            actions.append((nums[next_big], last))
            last = nums[next_big] - last
            next_big += 1
        next_op = 1 - next_op

    return last, actions

def solve2(n, nums):
    next_small = (n+1) // 2 - 1
    next_big = next_small + 1
    next_op = n % 2

    actions = []
    if next_op: 
        last = nums[next_big] - nums[next_small]
        
    else: 
        last = nums[next_small] - nums[next_big]
        

    next_big += 1
    next_small -= 1

    next_op = 1 - next_op
    while next_small >= 0 or next_big < n:
        if next_op:
            actions.append((nums[next_big], last))
            last = last - nums[next_big]
            next_big += 1
        else:
            actions.append((nums[next_small], last))
            last = last - nums[next_small]
            next_small -= 1
        next_op = 1 - next_op

    return last, actions

def solve(n, nums):
    nums.sort()
    actions = []
    if nums[0] <= 0 <= nums[-1]:
        ans = nums[0]
        ans2 = nums[-1]
        for i in range(1, n-1):
            if nums[i] > 0:
                actions.append((ans, nums[i]))
                ans -= nums[i]
            else:
                actions.append((ans2, nums[i]))
                ans2 -= nums[i]
        actions.append((ans2, ans))
        ans = ans2 - ans
    elif nums[-1] < 0:
        ans = nums[-1]
        for i in range(n-1):
            actions.append((ans, nums[i]))
            ans -= nums[i]
    else:
        ans = nums[0]
        for i in range(1, n-1):
            actions.append((ans, nums[i]))
            ans -= nums[i]
        actions.append((nums[-1], ans))
        ans = nums[-1] - ans
    return ans, actions


def solveSlow(n, nums):

    def maximize(nums):
        if len(nums) == 2:
            return max(nums[0] - nums[1], nums[1] - nums[0])
        nums.sort()
        return max(nums[-1] - minimize(nums[:-1]), maximize(nums[1:]) - nums[0])

    def minimize(nums):
        if len(nums) == 2:
            return min(nums[0] - nums[1], nums[1] - nums[0])
        nums.sort()
        return min(minimize(nums[:-1]) - nums[-1], nums[0] - maximize(nums[1:]))

    return maximize(nums)


def main():
    if 'PYCHARM_HOSTED' in os.environ:
        main_pycharm()
    else:
        real_main()


def generate_nums(n, min, max, check_if_good=None):
    while True:
        nums = [random.randint(min, max) for _ in range(n)]
        if check_if_good is None or check_if_good(nums):
            return nums


from copy import deepcopy
def main_pycharm():
    solution = solve

    test_inputs = []
    for _ in range(100):
        n = random.randint(2, 10)
        test_inputs.append([n, generate_nums(n, -100, 100)])
        test_inputs[-1][1].sort()


    test_outputs = None
    judge = None
    slow_solution = solveSlow
    if solution is not None:
        if test_outputs is not None:
            test_with_answers(solution, test_inputs, test_outputs)
        if judge is not None:
            test_with_judge(solution, test_inputs, judge)
        if slow_solution is not None:
            test_with_slow_solution(solution, test_inputs, slow_solution)


def test_with_answers(solution, inputs_answers):
    total, wrong = 0, 0
    for args, test_ans in inputs_answers:
        ans = solution(*args.copy())
        if ans != test_ans:
            print('WRONG! ans=%s, test_ans=%s, args=%s' % (ans, test_ans, args))
            wrong += 1
        else:
            print('GOOD')
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_judge(solution, inputs_gen, judge):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        if not judge(deepcopy(ans), *deepcopy(args)):
            print('WRONG! ans=%s, args=%s' % (ans, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_slow_solution(solution, inputs_gen, solution_slow):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))[0]
        slow = solution_slow(*deepcopy(args))
        if ans != slow:
            print('WRONG! ans=%s, slow=%s, args=%s' % (ans, slow, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

read = input
read_int = lambda: int(input())
read_array = lambda sep=None, maxsplit=-1: input().split(sep, maxsplit)
read_int_array = lambda: [int(a) for a in read_array()]



if __name__ == "__main__":
    main()

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

l.sort()

if l[0] >= 0:
    
    m = sum(l[1:])-l[0]
    print(m)
    curr = l[0]
    for i in range(n-2):
        print(curr,l[i+1])
        curr -= l[i+1]
    print(l[-1],curr)
elif l[-1]<=0:
    m = l[-1] - sum(l[:-1])
    print(m)
    curr = l[-1]
    for i in range(n-1):
        print(curr,l[n-2-i])
        curr -= l[n-2-i]
else:
    m = sum([abs(item) for item in l])
    print(m)
    idx = 0
    for i in range(n):
        if l[i]>=0:
            idx=i
            break
    plus = l[idx:]
    minus = l[:idx]
    curr = minus[0] 
    for i in range(len(plus)-1):
        print(curr,plus[i])
        curr -= plus[i]
    print(plus[-1],curr)
    curr = plus[-1] - curr
    for i in range(len(minus)-1):
        print(curr,minus[i+1])
        curr -= minus[i+1]