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

Solution (Python):











import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    minus = []
    plus = []
    M = I()
    A = LI()
    zero = []
    ans = []
    for a in A:
        if a < 0:
            minus.append(a)
        elif a > 0:
            plus.append(a)
        else:
            zero.append(0)
    minus.sort()
    plus.sort(reverse = True)
    if not minus:
        
        if zero and plus:
            a = zero.pop()
            b = plus.pop()
            minus.append(a-b)
            ans.append("{} {}".format(a, b))
        elif len(plus) >= 2:
            a = plus.pop()
            b = plus.pop()
            minus.append(a-b)
            ans.append("{} {}".format(a, b))
        
    if not plus:
        if zero and minus:
            a = zero.pop()
            b = minus.pop()
            plus.append(a-b)
            ans.append("{} {}".format(a, b))
        elif len(minus):
            a = minus.pop()
            b = minus.pop()
            plus.append(a-b)
            ans.append("{} {}".format(a, b))
        


    while minus and plus:
        if len(minus) == len(plus) == 1:
            a = plus.pop()
            b = minus.pop()
            ans.append("{} {}".format(a,b))
            plus.append(a - b)
            break
        a = minus.pop()
        b = plus.pop()
        if len(minus) > len(plus):
            ans.append("{} {}".format(b,a))
            plus.append(b-a)
        else:
            ans.append("{} {}".format(a,b))
            minus.append(a - b)

    while zero:
        if minus:
            a = zero.pop()
            b = minus.pop()
            
        elif plus:
            a = plus.pop()
            b = zero.pop()
            
        elif len(zero) >= 2:
            a = zero.pop()
            b = zero.pop()
            
        else:
            break
        plus.append(a - b)
        ans.append("{} {}".format(a,b))

    if zero:
        print(*zero)
    elif minus:
        print(*minus)
    else:
        print(*plus)
    print(*ans,sep="\n")

if __name__ == '__main__':
    main()

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

result = []

if a[0] > 0:
    if len(a) == 2:
        print(a[1]-a[0])
        print(a[1], a[0])
        exit()
    result.append((a[0], a[1]))
    t = a[0]-a[1]
    a.pop(0)
    a[0] = t

elif a[-1] < 0:
    if len(a) == 2:
        print(a[0]-a[1])
        print(a[0], a[1])
        exit()
    result.append((a[-1], a[-2]))
    t = a[-1]-a[-2]
    a.pop(-1)
    a[-1] = t


b = []
for i in range(len(a)-1):
    if a[i] > 0:
        result.append((b[0], a[i]))
        t = b[0]-a[i]
        b[0] = t
    else:
        b.append(a[i])
b.append(a[-1])

while len(b) != 1:
    result.append((b[-1], b[0]))
    b.append(b[-1]-b[0])
    b.pop(0)
    b.pop(-2)


print(b[0])
for r in result:
    print(r[0],r[1])

Solution (Python):
import bisect
N = int(input())
A = sorted(list(map(int, input().split())))
if N == 2:
    x, y = A[1], A[0]
    print(x-y)
    print(x, y)
    exit()
idx = bisect.bisect_left(A, 0)
if idx:
    if idx < N:
        Q = A[:idx]
        P = A[idx:]
    else:
        Q = A[:-1]
        P = [A[-1]]
else:
    Q = [A[0]]
    P = A[1:]


XY = []
LP = len(P)
for i in range(LP-1):
    XY.append((Q[0], P[i]))
    Q[0] -= P[i]
LQ = len(Q)
for i in range(LQ):
    XY.append((P[-1], Q[i]))
    P[-1] = P[-1] - Q[i]
ans = P[-1]
print(ans)
for x, y in XY:
    print(x, y)

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

A.sort()


t=A.pop(-1) 

b=A.pop(0)  



for i in A:
  
  
  if i>=0:
    r.append([b,i]) 
    b=b-i  
  
  else:
    r.append([t,i]) 
    t=t-i 
print(t-b)
r.append([t,b])
for i in r:
  
  print(i[0],i[1])

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

ans = []
a.sort()
low = a[0]
high = a[-1]
for i in a[1:-1]:
    if i>0:
        ans.append([low,i])
        low = low - i
    else:
        ans.append([high,i])
        high = high - i

ans.append([high,low])
print(high-low)
for x,y in ans:
    print('{0} {1}'.format(x,y))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
plus=[a[i] for i in range(n) if a[i]>=0]
l_plus=len(plus)
minus=[a[i] for i in range(n) if a[i]<0]
l_minus=len(minus)
x_cal=[]
y_cal=[]
if n==2:
  a.sort()
  print(a[1]-a[0])
  print(a[0],a[1])
else:
  if plus!=[] and minus!=[]:
    if l_plus>1:
      x_cal.append(minus[0])
    for j in range(1,l_plus-1):
      x_cal.append(x_cal[-1]-plus[j])
    for k in range(1,l_plus):
      y_cal.append(plus[k])
    if l_plus>1:
      y_last=x_cal[-1]-y_cal[-1]
    else:
      y_last=minus[0]
    x_cal.append(plus[0])
    for j in range(1,l_minus):
      x_cal.append(x_cal[-1]-minus[j])
    for k in range(1,l_minus):
      y_cal.append(minus[k])
    y_cal.append(y_last)
    print(x_cal[-1]-y_cal[-1])
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])
  elif minus==[]:
    a.sort()
    print(sum(a)-2*a[0])
    x_cal.append(a[0])
    for ij in range(1,n-2):
      x_cal.append(x_cal[-1]-a[ij])
    for jj in range(0,n-2):
      y_cal.append(a[jj+1])
    y_cal.append(x_cal[-1]-y_cal[-1])
    x_cal.append(a[-1])
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])
  else:
    a.sort()
    print(a[-1]*2-sum(a))
    x_cal.append(a[-1])
    for ij in range(1,n-2):
      x_cal.append(x_cal[-1]-a[ij])
    for jj in range(0,n-2):
      y_cal.append(a[jj+1])
    x_cal.append(x_cal[-1]-y_cal[-1])
    y_cal.append(a[0])
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])

Solution (Python):
import sys

def bigsmall(x,y):
    return (x,y) if x>y else (y,x)

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

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[-1])
    if a[1] <= 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    del a[-1]
    a.append(-1) 

    for i in range(1, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] <= 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

Solution (Python):


from collections import deque
import itertools as ite
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

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

pos1 = pos2 = 0
flag1 = flag2 = False
for i in range(N):
	if A[i] > 0:
		flag1 = True
		pos1 = i
	elif A[i] < 0:
		flag2 = True
		pos2 = i

if flag1 and flag2:
	print sum(abs(num) for num in A)
	n1 = A[pos1]
	n2 = A[pos2]
	for i in range(N):
		if i == pos1 or i == pos2:
			continue
		if A[i] > 0:
			print n2, A[i]
			n2 -= A[i]
		else:
			print n1, A[i]
			n1 -= A[i]
	print n1, n2
elif flag1:
	pos = 0
	for i in range(N):
		if A[pos] > A[i]:
			pos = i
	S = 0
	if pos == 0:
		S = 1
	n1 = A[pos]
	print sum(A) - n1 * 2
	for i in range(N):
		if i == pos or i == S:
			continue
		print n1, A[i]
		n1 -= A[i]
	print A[S], n1
else:
	pos = 0
	for i in range(N):
		if A[pos] < A[i]:
			pos = i
	n1 = A[pos]
	print -sum(A) + n1 * 2
	for i in range(N):
		if i == pos:
			continue
		print n1, A[i]
		n1 -= A[i]

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main():
    N = II()
    A = LI()

    A.sort()
    from bisect import bisect
    slice_ = bisect(A, 0)
    from collections import deque
    minus = deque(A[:slice_])
    plus = deque(A[slice_:])
    

    if minus and plus:
        first_plus = plus.popleft()
        first_minus = minus.popleft()
    elif minus and not plus:
        first_plus = minus.pop()  
        first_minus = minus.popleft()
    elif plus and not minus:
        first_minus = plus.popleft()  
        first_plus = plus.popleft()

    
    tmp = first_minus
    ans_li = []
    while len(plus) > 0:
        p = plus.popleft()
        ans_li.append((tmp, p))
        tmp = tmp - p
    ans_li.append((first_plus, tmp))
    tmp = first_plus - tmp
    while len(minus) > 0:
        m = minus.popleft()
        ans_li.append((tmp, m))
        tmp = tmp - m
    print(tmp)
    for i in ans_li: print('{} {}'.format(*i))


main()

Solution (Python):
l = input().split()
n = int(l[0])

l = input().split()
A = [int(x) for x in l]



A.sort()
P = []
N = []
for a in A:
    if a >= 0:
        P.append(a)
    else:
        N.append(a)

def print_ans(ans, pairs):
    print(ans)
    for p in pairs:
        print(p[0], p[1])

def solve_p(A):
    ans = A[0]
    pairs = []
    for i in range(1, n - 1):
        pairs.append((ans, A[i]))
        ans -= A[i]

    pairs.append((A[n - 1], ans))
    ans = A[n - 1] - ans
    print_ans(ans, pairs)

def solve_n(A):
    A.sort(key=lambda x: -x)
    ans = A[0]
    pairs = []
    for i in range(1, n - 1):
        pairs.append((ans, A[i]))
        ans -= A[i]

    pairs.append((ans, A[n - 1]))
    ans -= A[-1]
    print_ans(ans, pairs)


def solve1(P, N):
    ans = P[0]
    pairs = []
    for n in N:
        pairs.append((ans, n))
        ans -= n
    print_ans(ans, pairs)


def solve2(P, N):
    ans = N[0]
    pairs = []
    for i in range(len(P) - 1):
        pairs.append((ans, P[i]))
        ans -= P[i]
    pairs.append((P[-1], ans))
    ans = P[-1] - ans
    print_ans(ans, pairs)


def solve3(P, N):
    num_p = len(P)
    num_n = len(N)
    ans = P[0]
    pairs = []
    for i in range(num_n - 1):
        pairs.append((ans, N[i]))
        ans -= N[i]
    pairs.append((N[-1], ans))
    ans = N[-1] - ans
    for i in range(1, num_p - 1):
        pairs.append((ans, P[i]))
        ans -= P[i]
    pairs.append((P[-1], ans))
    ans = P[-1] - ans
    print_ans(ans, pairs)

num_p = len(P)
num_n = len(N)
if num_p == 0:
    solve_n(A)
elif num_n == 0:
    solve_p(A)
elif num_p == 1:
    solve1(P, N)
elif num_n == 1:
    solve2(P, N)
else:
    solve3(P, N)

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

neg = [n for n in A if n < 0]
pos = [n for n in A if n >= 0]

if len(neg) == 0:  
    x = A.pop(0)
    print(sum(A) - x)
    pivot = A.pop()
    for y in A:
        print(x, y)
        x -= y
    print(pivot, x)
elif len(pos) == 0:  
    x = A.pop()
    print(x - sum(A))
    for y in A:
        print(x, y)
        x -= y
else:
    print(sum(pos) - sum(neg))
    pivot = pos.pop()
    x = neg.pop()
    for y in pos:
        print(x, y)
        x -= y
    x, pivot = pivot, x
    for y in neg:
        print(x, y)
        x -= y
    print(x, pivot)

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

log = []


if A[-1] > 0 :
    a = A.pop()
    b = A.pop()
    log.append([a, b])
    A.append(a - b)

elif A[0] < 0 :
    a = A.pop(0)
    b = A.pop()
    log.append([a, b])
    A = [a-b] + A
    
i = 1
while A[i] > 0 :
    a = A.pop()
    log.append([a, A[i]])
    A.append(a-A[i])
    i += 1
    
while i < len(A) :
    log.append([A[0], A[i]])
    A[0] -= A[i]
    i += 1
    
print(A[0])
for l in log :
    print(l[0], l[1])

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

m = arr[0]
M = arr[-1]
S = []
for a in arr[1:-1]:
  if a > 0:
    S.append(str(m) + ' ' + str(a))
    m -= a
  else:
    S.append(str(M) + ' ' + str(a))
    M -= a
S.append(str(M) + ' ' + str(m))
print(M-m)
[print(s) for s in S]

Solution (Python):
N=int(input())
listA=list(map(int,input().split()))
listB=[abs(i) for i in listA]
MAX=max(listA)
MIN=min(listA)
ans=sum(listB)
if MAX<=0:
  print(ans+MAX*2)
elif MIN<=0:
  print(ans)   
else:
  print(ans-MIN*2)
listC=sorted(listA)  
for i in range(len(listA)-2):
  if listC[i+1]>=0:
    print(MIN,listC[i+1])
    MIN=MIN-listC[i+1]
  else:
    print(MAX,listC[i+1])
    MAX=MAX-listC[i+1]
print(MAX,MIN)

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

ANS=[]

a=A[0]
z=A[-1]

s=0
t=0

for i in A[1:-1]:
	if i>=0:
		ANS.append((a,i))
		a = a-i
	else:
		ANS.append((z,i))
		z = z-i


ANS.append((z,a))

print(z-a)
for a in ANS:
	print(*a)

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

ans = a[0]
ans_l = []
ans_e = a[-1]

if ans_e >= 0:
    for i in range(1,n-1):
        if a[i] < 0:
            ans_l += [(ans_e, a[i])]
            ans_e -= a[i]
        else:
            ans_l += [(ans, a[i])]
            ans -= a[i]

    ans_l += [(ans_e,ans)]
    ans = ans_e - ans

else:
    ans = ans_e
    for i in range(0,n-1):
        ans_l += [(ans, a[i])]
        ans -= a[i]

print(ans)

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

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

ans = a[0]
ans_l = []
ans_e = a[-1]

if ans_e >= 0:
    for i in range(1,n-1):
        if a[i] < 0:
            ans_l += [(ans_e, a[i])]
            ans_e -= a[i]
        if a[i] >= 0:
            ans_l += [(ans, a[i])]
            ans -= a[i]

    ans_l += [(ans_e,ans)]
    ans = ans_e - ans

else:
    ans = ans_e
    for i in range(0,n-1):
        ans_l += [(ans, a[i])]
        ans -= a[i]

print(ans)

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

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

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

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

A_sorted = sorted(A_list)

pos_count = len([a for a in A_sorted if a >= 0])
neg_count = len([a for a in A_sorted if a < 0])

if pos_count == 0:
    pos_count = 1
    neg_count = N - 1
if neg_count == 0:
    pos_count = N - 1
    neg_count = 1

output_list = [0] * (N - 1)
count = 0
for q in range(neg_count, N - 1):
    output_list[count] = [A_sorted[0], A_sorted[q]]
    count += 1
    A_sorted[0] -= A_sorted[q]


for i in range(neg_count):
    output_list[count] = [A_sorted[N - 1], A_sorted[i]]
    count += 1
    A_sorted[N - 1] -= A_sorted[i]

print(A_sorted[N - 1])
for item in output_list:
    print(item[0], item[1])

Solution (Python):
n=int(input())
a=[]
r=[]
for i in input().split():
  a.append(int(i))
a.sort()
t=a.pop(-1)
b=a.pop(0)
for i in a:
  if i>=0:
    r.append([b,i])
    b=b-i
  else:
    r.append([t,i])
    t=t-i
print(t-b)
r.append([t,b])
for i in r:
  print(i[0],i[1])

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A.sort()
final_list = []
plus = sum(x >= 0 for x in A)

def pop(length, flag):
  val = A[flag]
  A.pop(flag)
  for i in range(length):
    next_add = A[-1 - flag]
    final_list.append((val, next_add))
    val = val - next_add
    A.pop(-1 - flag)
  return val

if plus == N: 
  ans = pop(N - 2, 0)
  final_list.append((max(ans, A[0]), min(ans, A[0])))
  ans = abs(ans - A[0])
elif plus == 0: 
  ans = pop(N - 2, -1)
  final_list.append((max(ans, A[0]), min(ans, A[0])))
  ans = abs(ans - A[0])
else:
  ans_neg = pop(plus - 1, 0)
  ans_pos = pop(N - plus - 1, -1)
  final_list.append((ans_pos, ans_neg))
  ans = ans_pos - ans_neg
  


print(ans)
for i in range(len(final_list)):
  print(final_list[i][0], " ", final_list[i][1])

Solution (Python):
from collections import deque

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

A_pos = [x for x in A if x >= 0]  
A_minus = [x for x in A if x < 0]  
if len(A_pos) == 0:
    max_tmp = A_minus[-1]
    A_minus = A_minus[:-1]
    A_pos.append(max_tmp)

if len(A_minus) == 0:
    min_tmp = A_pos[0]
    A_pos = A_pos[1:]
    A_minus.append(min_tmp)

A_pos = deque(A_pos)
A_minus = deque(A_minus)


while True:
    if len(A_pos) == 1 and len(A_minus) == 1:
        break
    if len(A_pos) == 1:
        pos = A_pos.pop()
        minus = A_minus.pop()
        steps.append((pos, minus))
        pos -= minus
        A_pos.append(pos)
        continue
    if len(A_minus) == 1:
        
        pos = A_pos.pop()
        minus = A_minus.pop()
        steps.append((minus, pos))
        minus -= pos
        A_minus.append(minus)
        continue
    pos = A_pos.pop()
    minus = A_minus.pop()
    steps.append((pos, minus))
    pos -= minus
    A_pos.append(pos)

pos = A_pos.pop()
minus = A_minus.pop()

steps.append((pos, minus))
print(pos - minus)
for x, y in steps:
    print('{} {}'.format(x, y))

Solution (Python):







import sys
sys.setrecursionlimit(10**5+1)

inf     =  int(10 ** 20)
max_val =  inf
min_val = -inf

RW  = lambda : sys.stdin.readline().strip()
RI  = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]


nb_elem = RI()
elements = sorted(RMI())
mins = elements[0]
maxs = elements[-1]
ans = []
for e in elements[1:-1]:
    if e > 0:
        ans.append([mins, e])
        mins -= e
    else:
        ans.append([maxs, e])
        maxs -= e
ans.append([maxs, mins])
print(maxs - mins)
for i in ans:
    print(*i)

Solution (Python):
n = int(input())
s = n % 2
a = list(map(int, input().split()))
a.sort()
mi = a[0]
ma = a[n - 1]
l = []
for i in range(1, n - 1):
    if a[i] < 0:
        l.append((ma, a[i]))
        ma -= a[i]
    else:
        l.append((mi, a[i]))
        mi -= a[i]
l.append((ma, mi))
print(ma - mi)
for x, y in l:
    print(x, y)

Solution (Python):
N = int(input())
a = sorted(list(map(int, input().split())))
nyu = []
ma = a[-1]
mi = a[0]
i=0
while i!=N-2:
    if a[i+1]>=0:
        nyu.append([mi, a[i+1]])
        mi -= a[i+1]
    else:
        nyu.append([ma, a[i+1]])
        ma -= a[i+1]
    i += 1
nyu.append([ma, mi])
print(nyu[N-2][0]-nyu[N-2][1])
for i in range(N-1):
    print(nyu[i][0], nyu[i][1])

Solution (Python):
N = int(input())
a = tuple(sorted(map(int, input().split())))

ans = []
res = 0
m = a[0]  
p = a[-1]  
for aa in a[1:N - 1]:
    if aa < 0:
        res -= aa
        ans.append((p, aa))
        p -= aa
    else:
        res += aa
        ans.append((m, aa))
        m -= aa
ans.append((p, m))

res += a[-1] - a[0]
print(res)
for aa in ans:
    print(*aa)

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

mn = A[0]
mx = A[-1]
if N ==2:
    print(A[1] - A[0])
    print(A[1],A[0])
    sys.exit()
ope = []
for a in A[1:-1]:
    if a <=0:
        ope.append([mx, a])
        mx -= a
    else:
        ope.append([mn, a])
        mn -= a
ope.append([mx,mn])
print(mx-mn)
for o in ope:
    print(o[0],o[1])

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

a.sort()

posi = [a[-1]]
nega = [a[0]]

for n in a[1:-1]:
  if n < 0:
    nega.append(n)
  else:
    posi.append(n)

selected = []
ans = nega[0]

for n in posi[1:]:
  (x, y) = ans, n
  ans = x - y
  selected.append((x, y))

nega.append(ans)

ans = posi[0]
for n in nega[1:]:
  (x, y) = ans, n
  ans = x - y
  selected.append((x, y))

print(ans)

for (x, y) in selected:
  print(str(x) + ' ' + str(y))

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

ans = lst[n-1] - lst[0]
for i in range(1,n-1):
    ans += abs(lst[i])

print(ans)

for i in range(1,n-1):
    if lst[i] >= 0:
        print("%s %d" % (lst[0],lst[i]))
        lst[0] = lst[0] - lst[i]
    else:
        print("%s %d" % (lst[n-1],lst[i]))
        lst[n-1] = lst[n-1] - lst[i]
        

print("%s %d" % (lst[n-1],lst[0]))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A.sort()
ans = 0
hoge = sum(A)
p = 0
for i in range(N-1):
    hoge -= A[i]*2
    if hoge > ans:
        ans = hoge
        p = i
print(ans)
hoge = A[0]
for i in range(p+1,N-1):
    print(hoge,A[i])
    hoge -= A[i]
print(A[N-1],hoge)
hoge = A[N-1] - hoge
for i in range(1,p+1):
    print(hoge,A[i])
    hoge -= A[i]

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

A = sorted(A)
plus = []
minus = []

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

print(sum(plus)-sum(minus))
tmp = minus[0]
for i in range(len(plus)-1):
    print(tmp,plus[i])
    tmp -= plus[i]

minus[0] = tmp
tmp2 = plus[-1]
for i in range(len(minus)):
    print(tmp2,minus[i])
    tmp2 -= minus[i]

Solution (Python):
import sys
sys.setrecursionlimit(10000000)

N = None
As = None
used_plus = 0
used_minus = 0

def make_leaf():
    return [True, None, None, None]

def make_branch(left, right):
    return [False, None, left, right]

def count(nminus):
    global N, As
    res = 0
    for i in range(N):
        if i < nminus:
            res -= As[i]
        else:
            res += As[i]
    return res

def make_tree(a, b):
  if a + b == 1:
      if a == 1:
        return make_leaf()
      else:
          raise Exception("tukurenai")
  else:
      x = 1
      z = 1
      y = b - z
      w = a - x
      return make_branch(make_tree(x, y), make_tree(z, w))


def f(node, minus):
    global N, As, used_plus, used_minus
    [leaf, value, left, right] = node
    if leaf:
        if minus:
            node[1] = As[used_minus]
            used_minus += 1
        else:
            node[1] = As[N - 1 - used_plus]
            used_plus += 1
    else:
        f(left, minus)
        f(right, not minus)

def g(node):
    [leaf, value, left, right] = node
    if leaf:
        return value;
    else:
        x = g(left);
        y = g(right);
        print(str(x) + " " + str(y))
        return x - y

def show(nminus):
    node = make_tree(N - nminus, nminus)
    f(node, False)
    g(node)

def main():
    global N, As
    N = int(input())
    As = list(map(int,input().split()))
    As.sort()
    best = 0
    for i in range(N):
        if As[i] < 0: best = i
    m = -1
    argmax = 0
    for i in range(-1, 2):
        j = min(max(best + i, 1), N - 1)
        c = count(j)
        if c > m:
            m = c
            argmax = j
    print(m)
    show(argmax)

main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
minus = len([i for i in a if i < 0])
if minus == 0:
  minus = 1
elif minus == len(a):
  minus = len(a)-1
  
print(sum(a[minus:])- sum(a[:minus]))
for e in a[minus:-1]:
  print(str(a[0])+" "+str(e))
  a[0] -= e
for e in a[:minus]:
  print(str(a[-1])+" "+str(e))
  a[-1] -= e

Solution (Python):
N = int( input())
A = list( map( int, input().split()))
A.sort(reverse = True)
plus = 0
zero = 0
for i in range(N):
    if A[i] > 0:
        plus += 1
    elif A[i] == 0:
        zero += 1
minus = N - plus - zero
if plus + zero== N:
    print(sum(A)-A[-1]*2)
    now = A[-1]
    for i in range(N-2):
        print(now, A[i])
        now -= A[i]
    print(A[N-2], now)
elif minus + zero== N:
    print(A[0]*2 - sum(A))
    now = A[0]
    for i in range(1,N):
        print(now, A[i])
        now -= A[i]
elif plus+zero == 1:
    print( sum( list( map( abs, A))))
    now = A[0]
    for i in range(1,N):
        print(now, A[i])
        now -= A[i]
else:
    print( sum( list( map( abs, A))))
    now = A[N-1]
    k = N-1
    for i in range(1,N-1):
        if A[i] <= 0:
            k = i
            break
        print(now, A[i])
        now -= A[i]
    print(A[0], now)
    now = A[0] - now
    for i in range(k,N-1):
        if A[i] > 0:
            pass
        print(now, A[i])
        now -= A[i]

Solution (Python):
import sys, math, collections, heapq, itertools
from bisect import bisect_left
  
def solve():
    file = sys.stdin.readline 
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [[A[1], A[0]]]
    else:
        plus = bisect_left(A, 0)
        if plus == 0:
            ans = A[0]
            for i in range(1, N-1):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[N-1], ans])
            ans = A[N-1] - ans
        elif plus < N:
            ans = A[0]
            for i in range(plus, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[N-1], ans])
            ans = A[N-1] - ans
            for i in range(1, plus):
                out.append([ans, A[i]])
                ans -= A[i]
        else:
            ans = A[N-1]
            for i in range(N-1):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))


    return 0
  
if __name__ == "__main__":
    solve()

Solution (Python):
import sys

N=input()
A=map(int, sys.stdin.readline().split())
A.sort()
min_A=A[0]
max_A=A[-1]

L=[]
for i in range(1,N-1):
	v=A[i]
	if v<0:
		L.append( (max_A, v) )
		max_A-=v
	else:
		L.append( (min_A, v) )
		min_A-=v
else:
	L.append( (max_A,min_A) )
	print max_A-min_A
	for x,y in L:
		print x,y

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()
    m = A.pop(0)

    ans = []
    while A:
        a = A.pop()
        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):
import sys
N=int(input())
A=list(map(int,input().split()))

plus=[]
minus=[]

if min(A)>=0:
    tmp=min(A)
    print(sum(A)-tmp*2)
    if len(A)==2:
        print(max(A),min(A))
        sys.exit()
    tmpind=A.index(tmp)
    if tmpind==len(A)-1:
        tmpind=0
        aa=A[0]
        A[0]=A[-1]
        A[-1]=aa
    for i in range(len(A)-1):
        if i==tmpind:continue
        print(tmp,A[i])
        tmp-=A[i]
    print(A[-1],tmp)
    
elif max(A)<=0:
    tmp=max(A)
    print(-(sum(A)-tmp*2))
    if len(A)==2:
        print(max(A),min(A))
        sys.exit()
    tmpind=A.index(tmp)
    if tmpind==len(A)-1:
        tmpind=0
        aa=A[0]
        A[0]=A[-1]
        A[-1]=aa
    for i in range(len(A)-1):
        if i==tmpind:continue
        print(tmp,A[i])
        tmp-=A[i]
    print(tmp,A[-1])
    
else:
    print(sum(map(abs,A)))
    for i in range(len(A)):
        if A[i]>0:
            plus.append(A[i])
        else:
            minus.append(A[i])
    if len(plus)==1 and len(minus)==1:
        print(plus[0],minus[0])
    elif len(minus)==1:
        tmp=minus[0]
        for i in range(len(plus)-1):
            print(tmp,plus[i])
            tmp-=plus[i]
        print(plus[-1],tmp)
    else:
        for i in range(1,len(minus)):
            print(plus[-1],minus[i])
            plus[-1]-=minus[i]
        tmp=minus[0]
        for i in range(len(plus)-1):
            print(tmp,plus[i])
            tmp-=plus[i]
        print(plus[-1],tmp)

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