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

Solution (Python):


def readInts():
  return [int(s) for s in input().split(" ")]

def main():
  N = readInts()[0]
  A = readInts()

  A.sort()
  numMinus = 1
  for i in range(1, N):
    if A[i] < 0:
      numMinus += 1
    else:
      break

  if numMinus == N:
    numMinus = N - 1

  seq = []
  for i in range(numMinus, N-1):
    seq.append((A[0], A[i]))
    A[0] = A[0] - A[i]
  for i in range(numMinus):
    seq.append((A[N-1], A[i]))
    A[N-1] = A[N-1] - A[i]

  print(A[N-1])
  for x, y in seq:
    print("{} {}".format(x, y))

if __name__ == "__main__":
  main()

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

BB = A[:]

BB_P = []
BB_M = []


for b in BB:
    if b < 0:
        BB_M.append(b)
    else:
        BB_P.append(b)

        
ope = []
if not BB_M:
    BB_P = sorted(BB_P)
    p = BB_P[-1]
    m = BB_P[0]
    for b in BB_P[1:-1]:
        ope.append((m,b))
        m = m-b
    ope.append((p,m))
    ans = p - m
elif not BB_P:
    BB_M = sorted(BB_M)
    p = BB_M[-1]
    for b in BB_M[:-1]:
        ope.append((p, b))
        p = p-b
    ans = p
else:
    p = BB_P.pop()
    m = BB_M.pop()
    for b in BB_P:
        ope.append((m ,b))
        m = m-b
    for b in BB_M:
        ope.append((p ,b))
        p = p-b
    ope.append((p,m))
    ans = p-m
    
print(ans)
for x,y in ope:
    print(x, y)

Solution (Python):
def plus(pl):
    if len(pl) == 1:
        return pl[0]
    pl.sort()
    tmp = pl.pop(0)
    while len(pl) > 1:
        tmp2 = pl.pop(0)
        print(tmp, tmp2)
        tmp -= tmp2
    print(pl[0], tmp)
    return pl[0]-tmp


def minas(mi):
    if len(mi) == 1:
        return mi[0]
    mi.sort()
    tmp = mi.pop()
    while len(mi) > 1:
        tmp2 = mi.pop()
        print(tmp, tmp2)
        tmp -= tmp2
    print(tmp, mi[0])
    return tmp-mi[0]


n = int(input())

mi = []
pl = []

for i in [int(j) for j in input().split()]:
    if i < 0:
        mi.append(i)
    else:
        pl.append(i)

if len(pl) == 0:
    print(2*max(mi)-sum(mi))
    minas(mi)
elif len(mi) == 0:
    print(sum(pl)-2*min(pl))
    plus(pl)
else:
    print(abs(sum(mi))+sum(pl))
    mi.sort()
    pl.sort(reverse=True)
    while len(pl) > 1:
        tmp = pl.pop()
        print(mi[0], tmp)
        mi[0] -= tmp

    while len(mi) > 1:
        tmp = mi.pop()
        print(pl[0], tmp)
        pl[0] -= tmp

    print(pl[0], mi[0])

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

length = len(a_l)
k = 0
for i in range(length):
  if a_l[i] < 0:
    k = i
  else:
    break

b_l = a_l[:k+1]
if len(b_l) != 0:
  min_b = b_l[0]
a_l = a_l[k+1:]
if len(a_l) != 0:
  max_a = a_l[-1]
fu = len(b_l)
sei = len(a_l)
ans_l = list()
ans_n_l = list()
if len(b_l) != 0:
  if len(a_l) != 0:
    for i in range(1, fu):
      ans_l.append([max_a, b_l[i]])
      max_a -= b_l[i]
    for i in range(sei-1):
      ans_l.append([min_b, a_l[i]])
      min_b -= a_l[i]
    
    ans_l.append([max_a, min_b])
    max_a -= min_b
  else:
    max_a = b_l[-1]
    for i in range(1, fu-1):
      ans_l.append([max_a, b_l[i]])
      max_a -= b_l[i]
    ans_l.append([max_a, min_b])
    max_a -= min_b
else:
  min_b = a_l[0]
  for i in range(1, sei-1):
    ans_l.append([min_b, a_l[i]])
    min_b -= a_l[i]
  ans_l.append([max_a, min_b])
  max_a -= min_b
  
print(max_a)
for i in ans_l:
  print(i[0], i[1])

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
pos, neg = [], []
for num in a:
    if num >= 0:
        pos.append(num)
    else:
        neg.append(num)

def maximizeNeg():
    cur = neg[0]
    for i in range(1, len(neg)):
        ret.append((cur, neg[i]))
        cur -= neg[i]
    return cur

def maximizePos():
    cur = pos[0]
    for i in range(1, len(pos) - 1):
        ret.append((cur, pos[i]))
        cur -= pos[i]
    ret.append((pos[-1], cur))
    cur = pos[-1] - cur
    return cur

pos.sort()
neg.sort(reverse=True)
ret = []
if not neg:
    cur = maximizePos()
elif not pos:
    cur = maximizeNeg()
else:
    curNeg = neg[-1]
    for i in range(len(pos) - 1):
        ret.append((curNeg, pos[i]))
        curNeg -= pos[i]
    curPos = pos[-1]
    for i in range(len(neg) - 1):
        ret.append((curPos, neg[i]))
        curPos -= neg[i]
    ret.append((curPos, curNeg))
    cur = curPos - curNeg

print(cur)
print('\n'.join('%d %d' % (xy[0], xy[1]) for xy in ret))

Solution (Python):

n = int(input())
lis = list(map(int,input().split()))
lis.sort()
m = lis[0]
M = lis[-1]
ans_lis = []

for now in lis[1:-1]:
    if now > 0:
        ans_lis.append([m,now])
        m -= now
    else:
        ans_lis.append([M,now])
        M -= now
ans_lis.append([M,m])
print(M-m)
for x in ans_lis:
    print(x[0],x[1])

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
    return list(map(int,input().split()))
def m():
    return map(int,input().split())
def onem():
    return int(input())
def s(x):
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x):
    return " ".join(map(str,x))

n = onem()

a = l()
a.sort()
a = cl.deque(a)
aa = []
co = 0
up = a.pop()
do = a.popleft()

for i in range(len(a)):
    if a[i] >= 0:
        aa.append([do,a[i]])
        do -= a[i]
    else:
        aa.append([up,a[i]])
        up -= a[i]    
aa.append([up,do])
print(up-do)
for i in range(n-1):
    print(jo(aa[i]))

Solution (Python):
import bisect
N = int(input())
H = list(map(int, input().split()))
H.sort()
negative_count = bisect.bisect_left(H, 0)
positive_count = N - negative_count

def f():
    
    ans = ""

    if N == 2:
        return "{0} {1}\n".format(H[-1], H[0]), H[-1] - H[0]

    if positive_count == 0:
        ans += "{0} {1}\n".format(H[-1], H[0])
        sum_ans = H[-1] - H[0]
        for i in range(1, N - 1):
            v = H[i]
            ans += "{0} {1}\n".format(sum_ans, v)
            sum_ans = sum_ans - v
        return ans, sum_ans

    if negative_count == 0:
        ans += "{1} {0}\n".format(H[-1], H[0])
        sum_ans = - H[-1] + H[0]

        for i in range(1, N - 2):
            v = H[negative_count + i]
            ans += "{0} {1}\n".format(sum_ans, v)
            sum_ans = sum_ans - v

        v = H[-2]
        ans += "{1} {0}\n".format(sum_ans, v)
        sum_ans = v - sum_ans
        return ans, sum_ans


    if positive_count == 1:
        ans += "{0} {1}\n".format(H[-1], H[0])
        sum_ans = H[-1] - H[0]
    else:
        ans += "{1} {0}\n".format(H[-1], H[0])
        sum_ans = - H[-1] + H[0]

        for i in range(positive_count - 2):
            v = H[negative_count + i]
            ans += "{0} {1}\n".format(sum_ans, v)
            sum_ans = sum_ans - v

        v = H[-2]
        ans += "{1} {0}\n".format(sum_ans, v)
        sum_ans = v - sum_ans

    for i in range(1, negative_count):
        v = H[i]
        ans += "{0} {1}\n".format(sum_ans, v)
        sum_ans = sum_ans - v

    return ans, sum_ans


ans, sum_ans = f()
ans = "{}\n".format(sum_ans) + ans
print(ans)

Solution (Python):

N = int(input())
A = sorted(list(map(int, input().split())))
mn = A[0]
mx = A[-1]
ls_xy = []
for a in A[1:-1]:
    if a > 0:
        ls_xy.append([mn,a])
        mn -= a
    else:
        ls_xy.append([mx,a])
        mx -= a
ls_xy.append([mx,mn])
print (mx -mn)
[print (x,y) for x,y in ls_xy]

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


A.sort()
M = A.pop()
heapq.heapify(A)
m = heapq.heappop(A)
res = []
while A:
  a = heapq.heappop(A)
  if a < 0:
    res.append((M, a))
    M -= a
  else:
    res.append((m, a))
    m -= a
print(M-m)
for x, y in res:
  print(x, y)
print(M, m)

Solution (Python):
def simulateBoard(posNums, negNums):
  P, N = len(posNums), len(negNums)
  out_str = ""
  
  if P == 0 and N == 2:
    num1 = max(negNums)
    num2 = min(negNums)
    print(num1 - num2)
    print(num1, ' ', num2)
  elif N==0 and P==2:
    num1, num2 = max(posNums), min(posNums)
    print(num1 - num2)
    print(num1, ' ', num2)
  elif P == 0:
    negNums.sort(reverse=True)
    
    num1 = negNums[0]
    num2 = negNums.pop()
    
    last_pos = num1 - num2
    negNums = negNums[1:]
    print(abs(sum(negNums)) + last_pos)
    print(num1, ' ', num2) 
    last_neg = negNums.pop() 
    
    while len(negNums) != 0:
      cur_neg = negNums.pop()
      cur_diff = last_pos - cur_neg
      print(last_pos, ' ', cur_neg, ' ')
      last_pos = cur_diff
   	
    cur_diff = last_pos - last_neg
    print(last_pos, ' ', last_neg, ' ')
    
  
  elif N == 0:
    posNums.sort()
    
    num1 = posNums[0]
    num2 = posNums.pop()
    
    last_neg = num1 - num2
    posNums = posNums[1:]
    print(abs(last_neg) + sum(posNums))
    print(num1, ' ', num2)
    last_pos = posNums.pop()

    while len(posNums) != 0:
      cur_pos = posNums.pop()
      cur_diff = last_neg - cur_pos
      print(last_neg, ' ', cur_pos, ' ')
      last_neg = cur_diff
   	
    cur_diff = last_pos - last_neg
    print(last_pos, ' ', last_neg, ' ')
    
    
    
  
  
    
    
    
      
      
      
      
    
  
  elif P>=1 and N>=1:
    print(abs(sum(negNums)) + sum(posNums)) 
    
    last_pos = posNums.pop()
    last_neg = negNums.pop()
    
    while len(negNums) != 0:
      cur_neg = negNums.pop()
      cur_diff = last_pos - cur_neg
      print(last_pos, ' ', cur_neg, ' ')
      last_pos = cur_diff
    
    while len(posNums) != 0:
      cur_pos = posNums.pop()
      cur_diff = last_neg - cur_pos
      print(last_neg, ' ', cur_pos, ' ')
      last_neg = cur_diff
   	
    cur_diff = last_pos - last_neg
    print(last_pos, ' ', last_neg, ' ')

N = int(input())
nums = input().split()

nums = [int(i) for i in nums]
pos_nums = []
neg_nums = []

for num in nums:
  if num >= 0: pos_nums.append(num)
  else: neg_nums.append(num)
    
simulateBoard(pos_nums, neg_nums)

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

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

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

else:
    i = 0
    while A[i] < 0:
        i += 1
    print(sum(A[i:]) - sum(A[:i]))
    a,b = A[i-1],A[i]

    if i != N-1:
        for j in range(N-i-1):
            print(a,A[i+j+1])
            a -= A[i+j+1]
    if i != 1:
        for j in range(i-1):
            print(b,A[j])
            b -= A[j]

    print(b,a)

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

xy = [0]*(N-1)
idx = 0

n = A.pop()

for i in range(N-1):
    a = A.pop()
    if a > 0:
        xy[idx] = [A[0], a]
        A[0] -= a
    else:
        xy[idx] = [n, a]
        n -= a
        idx += 1
        for j in A:
            xy[idx] = [n, j]
            n -= j
            idx += 1
        break

    idx += 1

print(n)
for x, y in xy:
    print(x,y)

Solution (Python):
n = int(input())
alist = list(map(int,input().split()))
ans = []

if len(alist) == 2:
    alist.sort(reverse = True)
    print(alist[0]-alist[1])
    print(' '.join(map(str,alist)))
else:
    plus =[w for w in alist if w>=0]
    minus =[x for x in alist if x<0]
    if minus == []:
        plus.sort()
        for y in range(len(plus)-2):
            ans.append(' '.join(map(str,plus[y:y+2])))
            plus[y+1] = plus[y]-plus[y+1]
        pplus = plus[::-1]
        ans.append(' '.join(map(str,pplus[0:2])))
        print(pplus[0]-pplus[1])
        for y in ans:
            print(y)
    elif plus == []:
        minus.sort(reverse = True)
        for y in range(len(minus)-1):
            ans.append(' '.join(map(str,minus[y:y+2])))
            minus[y+1] = minus[y]-minus[y+1]
        print(minus[-1])
        for y in ans:
            print(y)
    else:
        for z in range(len(plus)-1):
            ans.append(' '.join(map(str,[minus[0],plus[z]])))
            minus[0] = minus[0]-plus[z]
        for q in minus:
            ans.append(' '.join(map(str,[plus[-1],q])))
            plus[-1] = plus[-1]-q
        print(plus[-1])
        for t in ans:
            print(t)

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

if a[0] >= 0:
    print([sum(a_2) -2, sum(a_2)][a[0] == 0])
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(aa, a[i + 1])
            aa = aa - a[i + 1]
        elif i == n - 2:
            print(a[i + 1], aa)
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

elif a[-1] <= 0:

    print([sum(a_2) -2, sum(a_2)][a[-1] == 0])
    a.reverse()
    if a[-1] == 0:
        a = [a[-1]] + a[0:]
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(aa, a[i + 1])
            aa = aa - a[i + 1]
        elif i == n - 2:
            print(aa, a[i + 1])
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

else:
    print(sum(a_2))
    minus = []
    plus = []
    for i in range(n):
        if a[i] < 0:
            minus.append(a[i])
        else:
            plus.append(a[i])
    minus2 = minus.pop()
    plus2 = plus.pop()
    for i in range(len(minus)):
        print(plus2 ,minus[i])
        plus2 -= minus[i]
    for i in range(len(plus)):
        print(minus2, plus[i])
        minus2 -= plus[i]
    print(plus2, minus2)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    A.sort()
    M = A.pop()
    heapq.heapify(A)
    m = heapq.heappop(A)

    ans = []
    while A:
        a = heapq.heappop(A)
        if a < 0:
            ans.append((M, a))
            M -= a
        else:
            ans.append((m, a))
            m -= a

    print(M-m)
    for x, y in ans:
        print(x, y)
    print(M, m)


def main():
    N = read_int()
    A = read_int_n()
    (slv(N, A))


if __name__ == '__main__':
    main()

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

import copy

List.sort()

result_list = []
tmp_list = copy.deepcopy(List)

if List[0] > 0:
    if N == 2:
        x = tmp_list[1]
        y = tmp_list[0]
        result_list.append([x,y])
    else:
        x = tmp_list[0]
        y = tmp_list[1]
        result_list.append([x,y])
        del tmp_list[0]
        tmp_list[0] = x - y
        if N >= 4:
            for i in range(N-3):
                x = tmp_list[0]
                y = tmp_list[1]
                result_list.append([x,y])
                del tmp_list[0]
                tmp_list[0] = x - y
        x = tmp_list[1]
        y = tmp_list[0]
        result_list.append([x,y])
        del tmp_list[0]
        tmp_list[0] = x - y                
        
elif List[N-1] <= 0:
    for i in range(N-1):
        x = tmp_list[N-i-1]
        y = tmp_list[N-i-2]
        result_list.append([x,y])
        del tmp_list[N-i-1]
        tmp_list[N-i-2] = x - y
        
else:
    i = 0
    while tmp_list[N-i-2] >= 0:
        x = tmp_list[0]
        y = tmp_list[N-i-1]
        result_list.append([x,y])
        del tmp_list[N-i-1]
        tmp_list[0] = x - y
        i += 1
    for j in range(N-i-1):
        x = tmp_list[N-i-j-1]
        y = tmp_list[0]
        result_list.append([x,y])
        del tmp_list[0]
        tmp_list[N-i-j-2] = x - y

result = x - y

print(result)

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

Solution (Python):
N = int(input())
l = [int(i) for i in input().split(" ")]
l.sort()
M = l[-1]
m = l[0]
ans_l = []
for i in l[1:-1]:
    if i>0:
        ans_l.append(str(m)+" "+str(i))
        m -= i
    else:
        ans_l.append(str(M)+" "+str(i))
        M -= i
ans_l.append(str(M)+" "+str(m))
ans = M - m
print(ans)
[print(j) for j in ans_l]

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

Solution (Python):
import numpy as np


N = int(input())

poss, negs = [], []
npos, nneg = 0, 0
for a in input().split():
    ia = int(a)
    if ia < 0:
        negs.append(ia)
        nneg += 1
    else:
        poss.append(ia)
        npos += 1












if N == 2:
    a1, a2 = (poss + negs)
    if a1 < a2:
        print(a2 - a1)
        print(a2, a1)
    else:
        print(a1 - a2)
        print(a1, a2)
    exit(0)

if nneg == 0:
    print(sum(poss) - min(poss) * 2)
    argmin = np.array(poss).argmin()
    minpos = poss.pop(argmin)
    pos = poss.pop()
    print(minpos, pos)
    negs.append(minpos - pos)
    npos -= 2
    nneg += 1
elif npos == 0:
    print(-sum(negs) + max(negs) * 2)
    argmax = np.array(negs).argmax()
    maxneg = negs.pop(argmax)  
    neg = negs.pop()  
    print(maxneg, neg)
    poss.append(maxneg - neg)
    nneg -= 2
    npos += 1
else:
    print(sum(poss) - sum(negs))

if npos > nneg:
    for _ in range(npos - nneg):
        pos, neg = poss.pop(), negs.pop()
        print(neg, pos)
        negs.append(neg - pos)
elif npos < nneg:
    for _ in range(nneg - npos):
        pos, neg = poss.pop(), negs.pop()
        print(pos, neg)
        poss.append(pos - neg)

for i in range(min(nneg, npos) - 1):
    pos, neg = poss.pop(), negs.pop()
    print(neg, pos)
    negs.append(neg - pos)
    pos, neg = poss.pop(), negs.pop()
    print(pos, neg)
    poss.append(pos - neg)

pos, neg = poss.pop(), negs.pop()
print(pos, neg)

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

ans = ''
stack_neg = []
stack_pos_neg = []
pos_neg_flag = True
ans_flag = False
answer_list = []

A = sorted(A)

while A:
    tmp = A.pop()
    
    if not ans_flag:
        ans = tmp
        ans_flag = True
        continue
        
    if ans_flag and ans < 0 and tmp >= 0:
        tmp2 = ans
        ans = tmp
        tmp = tmp2
    
    if tmp >= 0 and pos_neg_flag and ans_flag:
        stack_pos_neg.append(tmp)

    elif tmp < 0 and pos_neg_flag:
        stack_pos_neg.append(tmp)

        while stack_pos_neg:
            a = stack_pos_neg.pop()
            if not stack_pos_neg:
                stack_pos_neg.append(a)
                pos_neg_flag = False
                break
            b = stack_pos_neg.pop()
            answer_list.append((a, b))
            stack_pos_neg.append(a - b)

    elif tmp >= 0 and not pos_neg_flag:
        a = stack_pos_neg.pop()
        answer_list.append((a, tmp))
        stack_pos_neg.append(a - tmp)

    elif tmp < 0 and not pos_neg_flag:
        stack_neg.append(tmp)
        
if pos_neg_flag:
    stack_pos_neg.append(ans)
    stack_pos = sorted(stack_pos_neg)
    
    ans = stack_pos.pop()
    
    if stack_pos:
        target = stack_pos.pop(0)
    while stack_pos:
        tmp = stack_pos.pop()
        answer_list.append((target, tmp))
        target = target - tmp
    answer_list.append((ans, target))
    ans = ans - target

else:
    tmp = stack_pos_neg.pop()
    answer_list.append((ans, tmp))
    ans = ans - tmp
    
    while stack_neg:
        tmp = stack_neg.pop()
        answer_list.append((ans, tmp))
        ans = ans - tmp

print(ans)

if N != 1:
    for answer in answer_list:
        i, j = answer
        print(i, j)

Solution (Python):
from collections import deque
N = int(input())
A = list(map(int, input().split()))
plus = 0
minus = 0
for i in range(N):
    if A[i] >= 0:
        plus += 1
    else:
        minus += 1
bb = deque(sorted(A))
x = [0]*(N-1)
y = [0]*(N-1)
for i in range(N-1):
    if len(bb) == 2:
        l = bb.popleft()
        r = bb.pop()
        if l > r:
            ret = l-r
            x[i] = l
            y[i] = r
        else:
            ret = r - l
            x[i] = r
            y[i] = l
    else:
        l = bb.popleft()
        r = bb.pop()

        if plus > minus:
            ret = l - r
            x[i] = l
            y[i] = r
            plus -= 1
        else:
            ret = r - l
            x[i] = r
            y[i] = l
            minus -= 1
    if ret > 0:
        bb.append(ret)
    else:
        bb.appendleft(ret)
print(bb.pop())
for i in range(N-1):
    print(x[i], y[i])

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=[]
R=a.pop()
L=a.pop(0)
s=L
if a:
    while a[-1]>0:
        r=a.pop()
        ans.append([s,r])
        s=s-r
        if not a:break
ans.append([R,s])
s=R-s
while a:
    L=a.pop()
    ans.append([s,L])
    s=s-L
print(s)
for x in ans:
    print(*x)

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A = sorted(A)
    A_M = sorted(A)
    A_M[0] *= -1
    start = 0
    if N == 2:
        print(max(A) - min(A))
        print('{} {}'.format(max(A), min(A)))
        return
    for i in range(1, N-1):
        if A_M[i] < 0:
            A_M[i] *= -1
            start = i
    M = sum(A_M)
    print(M)
    if start == 0:
        for i in range(1, N):
            if i < N-1:
                if i == 1:
                    x = A[0]
                    y = A[1]
                else:
                    x = tmp
                    y = A[i]
            else:
                x = A[-1]
                y = tmp
            tmp = x - y
            print('{} {}'.format(x, y))
    elif start == N - 2:
        for i in range(1, N):
            if i == 1:
                x = A[-1]
                y = A[start]
            else:
                x = tmp
                y = A[start-i+1]
            tmp = x - y
            print('{} {}'.format(x, y))
    else:
        for i in range(1, N):
            if i == 1:
                x = A[start]
                y = A[start+1]
            elif 1 < i < N - 1 - start:
                x = tmp
                y = A[start+i]
            elif i == N - 1 - start:
                x = A[-1]
                y = tmp
            else:
                x = tmp
                y = A[start-(i-(N-1-start))]
            tmp = x - y
            print('{} {}'.format(x, y))
    return


if __name__ == '__main__':
    main()

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

tasug=[]
hikug=[]
for i in range(n):
  if a[i]<0:
    hikug.append(a[i])
  else:
    tasug.append(a[i])

if len(hikug)==0:
  tasug.sort()
  ans=sum(tasug)-2*tasug[0]
  print(ans)
  for i in range(len(tasug)-1):
    if i ==0:
      print(tasug[0],tasug[1])
      a=tasug[0]-tasug[i]
    elif i==1:
      continue
    else:
      print(a,tasug[i])
      a=a-tasug[i]
  if not n==2:
    print(tasug[len(tasug)-1],a)
  
  
elif len(tasug)==0:
  hikug.sort()
  ans=-sum(hikug)+2*hikug[n-1]
  print(ans)
  for i in range(n-1):
    if i ==0:
      a=hikug[n-1]

    print(a,hikug[i])
    a=a-hikug[i]

else:
  ans=sum(tasug)-sum(hikug)
  print(ans)
  if len(tasug)==1:
    for i in range(len(hikug)):
      if i ==0:
        print(tasug[0],hikug[i])
        a=tasug[0]-hikug[i]
      else:
        print(a,hikug[i])
        a=a-hikug[i]
  else:
    for i in range(len(tasug)-1):
      if i ==0:
        print(hikug[0],tasug[i])
        a=hikug[0]-tasug[i]
      else:
        print(a,tasug[i])
        a=a-tasug[i]

    print(tasug[len(tasug)-1],a)
    a=tasug[len(tasug)-1]-a
  
    for i in range(1,len(hikug)):
      print(a,hikug[i])
      a=a-hikug[i]

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

ans = []
f = 0
l = N-1
s = 0

if N == 2:
    ans.append([A[l], A[f]])
    s = A[l] - A[f]
    f += 1
    l -= 1
else:
    hasPlus = True if A[N-2] >= 0 else False
    if hasPlus:
        
        ans.append([A[f], A[l]])
        s = A[f] - A[l]
        f += 1
        l -= 1
        while f <= l:
            if f == l or A[l-1] < 0:
                
                ans.append([A[l], s])
                s = A[l] - s
                l -= 1
                break
            
            ans.append([s, A[l]])
            s = s - A[l]
            l -= 1
    else:
        
        ans.append([A[l], A[f]])
        s = A[l] - A[f]
        f += 1
        l -= 1


while f <= l:
    ans.append([s, A[l]])
    s = s - A[l]
    l -= 1

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

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

n_pos = len([a for a in A if a > 0])
operations = []
if n_pos == 0:
    pos = [A[-1]]
    neg = A[:-1]
elif n_pos == N:
    pos = A[1:]
    neg = A[:1]
else:
    pos = [a for a in A if a > 0]
    neg = [a for a in A if a <= 0]
if len(pos) >= 2:
    tmp = neg[0]
    for i in range(len(pos)-1):
        operations.append((tmp, pos[i]))
        tmp = tmp - pos[i]
    operations.append((pos[-1], tmp))
    tmp = pos[-1] - tmp
    for i in range(1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
else:
    tmp = pos[0]
    for i in range(len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
print(tmp)
for o in operations:
    print("{} {}".format(o[0], o[1]))

Solution (Python):
import sys
N = int(input())
A = [int(i) for i in input().split()]
A.sort()
a = A[0]
b = A[-1]
if N == 2:
    print(A[1]-A[0])
    print(A[1],A[0])
    sys.exit()
ans = []
for x in A[1:-1]:
    if x <= 0:
        ans.append([b, x])
        b = b - x
    else:
        ans.append([a, x])
        a = a - x
ans.append([b,a])
M = b-a
print(M)
for x in ans:
    print(x[0], x[1])

Solution (Python):
import numpy as np

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

A = np.array(A)

A = np.sort(A)

B = np.copy(A)
if n == 2:
    print(A[1] - A[0])
    print(A[1], A[0])

elif A[0] <= 0 and A[n-1] >= 0:
    for i in range(n-1):
        A[i] = min(A[i], -A[i])

    print(np.sum(A[:-1]) * -1 + A[n-1])

    k = n - 2
    a = B[0]
    while B[k] >= 0:
        print(a, B[k])
        a = a - B[k]
        k -= 1
        if k == 0:
            break

    b = B[n-1]

    while k != 0:
        print(b, B[k])
        b = b - B[k]
        k -= 1

    print(b, a)

elif A[n-1] < 0:
    A[n-1] = A[n-1] - A[n-2]
    A[n-2] = 0
    for i in range(n-1):
        A[i] = min(A[i], -A[i])

    print(np.sum(A[:-1]) * -1 + A[n-1])

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

else:
    A[0] = A[0] - A[1]
    A[1] = 0
    for i in range(n-1):
        A[i] = min(A[i], -A[i])

    print(np.sum(A[:-1]) * -1 + A[n-1])

    k = n - 2
    a = B[0]
    while B[k] >= 0:
        print(a, B[k])
        a = a - B[k]
        k -= 1
        if k == 0:
            break

    b = B[n-1]

    while k != 0:
        print(b, B[k])
        b = b - B[k]
        k -= 1

    print(b, a)

Solution (Python):
from collections import Counter
import sys

def debug(*a):
    print(*a, file=sys.stderr)

def solve(N, A):
    A = sorted(A)
    debug("A =", A)
    assert len(A) == N
    s = []
    if all(a < 0 for a in A):
        x = A[-1]
        for i in range(0, N-1):
            s.append((x, A[i]))
            x -= A[i]
        print(x)
        for x in s:
            print(*x)
        return
    if all(a >= 0 for a in A):
        x = A[0]
        for i in range(2, N):
            s.append((x, A[i]))
            x -= A[i]
        s.append((A[1], x))
        print(A[1] -x)
        for x in s:
            print(*x)
        return 

    pos = 0
    for i in range(N):
        if A[i] >= 0:
            pos =i
            break
    
    x = A[pos] 
    for i in range(pos-1):
        s.append(("+", x, A[i]))
        x -= A[i]
    
    y = A[pos-1] 
    for i in range(pos+1, N):
        s.append(("-", y, A[i]))
        y -= A[i] 
    s.append(("=", x, y))
    
    print(x-y)
    assert(len(s) == N-1)
    for x in s:
        debug(*x)
        print(*x[1:])

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

    solve(N, A)

main()

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

A.sort()

L = [a for a in A if a <= 0]
R = [a for a in A if a > 0]

ans = []

if L != [] and R != []:
    low = L[0]
    for r in R[:-1]:
        ans.append([low, r])
        low -= r

    up = R[-1]
    for l in L[1:]:
        ans.append([up, l])
        up -= l

    ans.append([up, low])
    ans.append([up - low])

elif L == []:
    low, up = R[0], R[-1]
    for r in R[1:-1]:
        ans.append([low, r])
        low -= r
    ans.append([up, low])
    ans.append([up - low])

elif R == []:
    low, up = L[0], L[-1]
    for l in L[1:-1]:
        ans.append([up, l])
        up -= l
    ans.append([up, low])
    ans.append([up - low])

print(*ans[-1])
for a in ans[:-1]:
    print(*a)