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



Sample input:
3
1 -1 2


Sample output:
4
-1 1
2 -2

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n, k = LI()
    a = n - k
    if k == 1:
        print(0)
        return
    print(a)
    return

#B
def B():
    n = II()
    xy = LIR(n)
    pa = itertools.combinations_with_replacement(range(n), 2)
    ans = inf
    for xi, yi in pa:
        bf = 0
        x1,y1 = xy[xi]
        x2,y2 = xy[yi]
        p, q = x1 - x2, y1 - y2
        for xa, ya in pa:
            if not (xa == xi and yi == ya):
                x1,y1 = xy[xi]
                x2,y2 = xy[yi]
        
    return

#C
def C():
    m = II()
    a = LI()
    a.sort()
    check = [0] * m
    anslis = []
    ans = 0
    if m % 2:
        ans += a[m // 2]
        for i in range(m//2):
            anslis.append((a[i], a[-1 - i]))
            anslis.append((ans,-a[-1 - i]+a[i]))
            ans += a[-1 - i] - a[i]
    else:
        for i in range(m//2):
            ans += a[-1 - i] - a[i]
            anslis.append((a[i], a[-1 - i]))
            if i == 0:
                break
            anslis.append((a[-1 - i] - a[i], -1 * ans))
    print(ans)
    for a, b in anslis:
        print(a,b)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Wrong Answer

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

a.sort()
l=[]
x=a[0]
for i in range(n-1):
    if i!=n-2:
        l.append([x,a[n-1-i]])
        x-=a[n-1-i]
    else:
        l.append([a[n-1-i],x])
        x=a[n-1-i]-x
print(x)
for y in l:
    print(y[0],y[1])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
r=0
for i in input().split():
  a.append(int(i))
a.sort()
for i in range(n):
  if i<=n//2:
    r=r+a[-1-i]
  else:
    r=r-a[-1-i]
print(r)
while len(a)>1:
  b=a.pop(-1)
  c=a.pop(0)
  print(b,c)
  a.insert(0,c-b)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import math

def main():
    n = int(raw_input())
    an = map(int,raw_input().split())
    mi = min(an)
    mx = max(an)
    total = mx - mi
    an.remove(mi)
    an.remove(mx)
    for a in an:
        total += a
    print total
    miCheck = False
    mxCheck = False
    print mi,mx
    tmp = mi - mx
    cnt = 1
    for a in an:
        if cnt == len(an):
            print a,tmp
        else:
            print tmp, a
            tmp = tmp - a
        cnt += 1

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def operation(A):
    a_min = min(A)
    A.remove(a_min)
    a_max = max(A)
    A.remove(a_max)
    A += [a_min - a_max]
    return (A, (a_min, a_max))

N = int(input())
A = [int(s) for s in input().split(' ')]
result = []
while (len(A) > 2):
    A, op = operation(A)
    result += [op]
result += [(max(A), min(A))]
print(max(A) - min(A))
for op in result:
    print("{} {}".format(op[0], op[1]))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=[]
val=0
if(n>3):
    x=l[1]-l[0]
    ans.append([l[1],l[0]])
else:
    x=l[0]-l[1]
    ans.append([l[0],l[1]])
for i in range(2,n-1):
    if(i<n-2):
        val=l[i]-x
        ans.append([l[i],x])
        x=val
    else:
        val=x-l[i]
        ans.append([x,l[i]])
        x=val
val=x
x=l[-1]-x
ans.append([l[-1],val])
print(x)
for i in range(len(ans)):
    print(*ans[i])

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

cnt = bisect_left(a, 0)  # 正の開始点==負の個数

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

else:
    # cnt-1個は正最大に連結
    print(sum(map(abs, a)))
    t = a[-1]
    for i in range(cnt - 1):
        print(t, a[i])
        t -= a[i]
    s = a[cnt - 1]
    for i in range(cnt, N - 1):
        print(s, a[i])
        s -= a[i]
    print(t, s)

Submission status: Wrong Answer

Solution code:
n = int(input()) 
m = list(map(int, input().split()))

m.sort()
#print(m)

ans = []
if n % 2 == 0:
	y = m.pop(0)
	for j in range(n - 1):
		if j % 2 == 0:
			x = m.pop()
			ans.append(str(x) + " " + str(y))
			y = x - y
		else:
			x = m.pop(0)
			ans.append(str(x) + " " + str(y))
			y = x - y
	print(y)
else:
	y = m.pop()	
	for j in range(n - 1):
		if j % 2 == 0:
			x = m.pop(0)
			ans.append(str(x) + " " + str(y))
			y = x - y
		else:
			x = m.pop()
			ans.append(str(x) + " " + str(y))	
			y = x - y
	print(y)

for a in ans:
	print(a)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
n=int(input())
a=list(map(int,input().split()))
a.sort()
if(a[0]>0):
    print(sum(a[1:])-a[0])
    ans=a[0]
    for i in a[1:n-1]:
        print(ans,i)
        ans-=i
    print(a[-1],ans)
elif(a[-1]<0):
    print(a[-1]-sum(a[:n-1]))
    ans=a[-1]
    for i in a[:n-1]:
        print(ans,i)
        ans-=i
else:
    print(sum([abs(i) for i in a]))
    b=bisect_left(a,0)
    ans=a[0]
    tmp=a[-1]
    for i in a[b:n-1]:
        print(a[0],i)
        ans-=i
    for i in a[1:b]:
        print(tmp,i)
        tmp-=i
    print(tmp,ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n, k = LI()
    a = n - k
    if k == 1:
        print(0)
        return
    print(a)
    return

#B
def B():
    n = II()
    xy = LIR(n)
    pa = itertools.combinations_with_replacement(range(n), 2)
    ans = inf
    for xi, yi in pa:
        bf = 0
        x1,y1 = xy[xi]
        x2,y2 = xy[yi]
        p, q = x1 - x2, y1 - y2
        for xa, ya in pa:
            if not (xa == xi and yi == ya):
                x1,y1 = xy[xi]
                x2,y2 = xy[yi]
        
    return

#C
def C():
    m = II()
    a = LI()
    a.sort()
    check = [0] * m
    anslis = []
    ans = 0
    if m % 2:
        ans += a[m // 2]
        for i in range(m//2):
            anslis.append((a[i], a[-1 - i]))
            anslis.append((a[-1 - i]-a[i],-1*ans))
            ans += a[-1 - i] - a[i]
    else:
        for i in range(m//2):
            ans += a[-1 - i] - a[i]
            anslis.append((a[i], a[-1 - i]))
            if i == 0:
                break
            anslis.append((a[-1 - i] - a[i], -1 * ans))
    print(ans)
    for a, b in anslis:
        print(a,b)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Wrong Answer

Solution code:
n = int(input())
num = [int(i) for i in input().split()]
anslist = []
for i in range(n-2):
    anslist.append([min(num), max(num)])
    a = min(num) - max(num)
    num.remove(min(num))
    num.remove(max(num))
    num.append(a)
ans = max(num) - min(num)
print(ans)
for i in range(n-2):
    print(str(anslist[i][0]) + " " + str(anslist[i][1]))
print(str(max(num)) + " " + str(min(num)))

Submission status: Wrong Answer

Solution code:
def operation(A):
    a_min = min(A)
    A.remove(a_min)
    a_max = max(A)
    A.remove(a_max)
    A += [a_min - a_max]
    return (A, (a_min, a_max))

A = [int(s) for s in input().split(' ')]
result = []
while (len(A) > 2):
    A, op = operation(A)
    result += [op]
result += [(max(A), min(A))]
print(max(A) - min(A))
for op in result:
    print("{} {}".format(op[0], op[1]))

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import math

def main():
    n = int(raw_input())
    an = map(int,raw_input().split())
    mi = min(an)
    mx = max(an)
    total = mx - mi
    miCheck = False
    mxCheck = False
    for a in an:
        if miCheck == False and a == mi:
            miCheck == True
            continue
        if mxCheck == False and a == mx:
            mxCheck == True
            continue
        total += a
    print total
    miCheck = False
    mxCheck = False
    print mi,mx
    tmp = mi - mx
    cnt = 1
    an.remove(mi)
    an.remove(mx)
    for a in an:
        if cnt == len(an):
            print a,tmp
        else:
            print tmp, a
            tmp = tmp - a
        cnt += 1

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
M = int(input())
Alist = list(map(int, input().split()))

Alist.sort()

ans = sum(Alist) - 2 * min(Alist)

print(ans)

cou = Alist[0]

for i in range(1,M-1):
    print(cou, Alist[i])
    cou =cou - Alist[i]

print(Alist[-1], cou)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
B=[]
A=sorted(A)
M=0
for i in range(N):
	if i<N//2:
		M-=A[i]
	else:
		M+=A[i]
print(M)
for i in range(N//2):
	print(A[i],A[N-i-1])
	B.append(A[i]-A[N-i-1])
if N%2==1:
	B.append(A[N//2])
B=sorted(B)
for i in range(len(B)-1):
	print(B[-1],B[i])

Submission status: Wrong Answer

Solution code:
import bisect

N=int(input())
A=[int(i) for i in input().split(" ")]
A=sorted(A)
traceback=[]

for i in range(N-1):
    if i%2==0:
        x=A[0]
        y=A[-1]
    else:
        x=A[-1]
        y=A[0]
    A=A[1:-1:]
    bisect.insort_left(A,x-y)
    traceback.append((x,y))
print(abs(A[0]))
for i,t in enumerate(traceback):
    if i==len(traceback)-1:
        print(max(t[0],t[1]),min(t[0],t[1]))
    else:
        print(t[0],t[1])

Submission status: Wrong Answer

Solution code:
N = int(input())						# 5
A = sorted(list( map(int, input().split()) ))	# 1 2 3 4 5 ...(ソート付き)

ans=0
ANS=[]

for i in range(N-1):
	x=A[i]
	y=A[i+1]
	if i==N-2:
		x,y=y,x
		ans = x-y
	else:
		A[i+1]=x-y

	ANS.append((x,y))


print(ans)
for a in ANS:
	print(*a)

Submission status: Wrong Answer

Solution code:
from collections import deque


def m():
    M = int(input())
    A = deque(sorted([int(i) for i in input().split()]))
    steps = []
    # get last A
    maxA = A.pop()
    minA = A.popleft()
    while len(A) > 0:
        b = A.popleft()
        steps.append("{} {}".format(minA, b))
        minA -= b
    steps.append("{} {}".format(maxA, minA))
    print(maxA - minA)
    for s in steps:
        print(s)


m()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import numpy as np

n = int(input())
a = np.array(list(map(int, input().split())))
hist = []
for i in range(n - 1):
    x_idx = np.argmin(a)
    x = a[x_idx]
    a = np.delete(a, x_idx, 0)
    y_idx = np.argmin(a)
    y = a[y_idx]
    a = np.delete(a, y_idx, 0)
    if i < n - 2:
        hist.append([x, y])
        a = np.append(a, x - y)
    else:
        hist.append([y, x])
        a = np.append(a, y - x)
print(a[0])
for i in hist:
    print("{} {}".format(i[0], i[1]))

Submission status: Wrong Answer

Solution code:
import heapq
n=int(input())
A=[int(i) for i in input().split()]
Ans=[]
heapq.heapify(A)
for i in range(n-2):
  a1=heapq.heappop(A)
  a2=heapq.heappop(A)
  Ans.append([a1,a2])
  heapq.heappush(A,a1-a2)
a1=heapq.heappop(A)
a2=heapq.heappop(A)
Ans.append([a2,a1])
print(a2-a1)
for i in range(n-1):
  print(*Ans[i])

Submission status: Wrong Answer

Solution code:
m = int(input())
a = sorted(list(map(int, input().split())))
s, p = a[:len(a) // 2], a[len(a) // 2:]
print(sum(p) - sum(s))
if len(p) > len(s):
    c = p.pop(0)
    s, p = p, s
else:
    c = s.pop(0)
while len(p) > 0:
    b = p.pop(0)
    print(b, c)
    c = b - c
    s, p = p, s

Submission status: Wrong Answer

Solution code:
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:
    for i in range(1,n):
        ans_l += [(ans, a[i])]
        ans -= a[i]

print(ans)

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

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int,input().split()))
d = []
for i in range(a-1):
        if i < a-3:
                p,q = min(b),max(b)
                b.remove(p)
                b.remove(q)
                d.append([p,q])
                c = p - q
                b.append(c)
        else:
                p,q = max(b),min(b)
                d.append([p,q])
                c = p - q
                b.append(c)
print(max(b))
for j in range(len(d)):
        print(d[j][0],d[j][1])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
L1=[]
L2=[]
for i in range(n):
  if i<n//2:
    L1.append(-a[i])
  else:
    L2.append(a[i])
print(sum(L1)+sum(L2))
if n%2==0:
  for i in range(n-1):
    if i==0:
      print(L2[0],-L1[0])
      x=L2[0]+L1[0]
    elif i%2==1:
      print(-L1[(i+1)//2],x)
      x=-L1[(i+1)//2]-x
    else:
      print(L2[(i+1)//2],x)
      x=L2[(i+1)//2]-x
else:
  for i in range(n-1):
    if i==0:
      print(-L1[0],L2[0])
      x=-L1[0]-L2[0]
    elif i%2==0:
      print(-L1[(i+1)//2],x)
      x=-L1[(i+1)//2]-x
    else:
      print(L2[(i+1)//2],x)
      x=L2[(i+1)//2]-x

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = list(map(int,input().split()))
lst2 = []
while len(lst)>=3:
  lst2.append([min(lst), max(lst)])
  x = min(lst)-max(lst)
  lst.remove(max(lst))
  lst.remove(min(lst))
  lst.append(x)
lst2.append([max(lst), min(lst)])
x = max(lst)-min(lst)
print(x)
for i in lst2:
  print(i[0], i[1])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
print(sum(A)-2*A[0])

while N > 2:
    print(str(A[0]) + " " + str(A[1]))
    A = [A[0]-A[1]] + A[2:]
    N -= 1
print(str(A[1]) + " " + str(A[0]))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
data = []
if n > 2:
	for i in range(n-2):
		x = a.pop(0)
		y = a.pop(0)
		data.append([x,y])
		a.append(x-y)
		a.sort()
else:
	data.append([a[1],a[0]])	
print(a[1]-a[0])
for i in data:
	print("%d %d" % (i[0],i[1]))
print("%d %d" % (a[0],a[1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
if A[N-1] <= 0:
    print(2*A[N-1]-sum(A))
    x = A[N-1]
    for i in range(N-1):
        y = A[N-2-i]
        print(x,y)
        x = x-y

else:
    print(sum(A)-2*A[0])
    x = A[0]
    for i in range(N-2):
        y = A[i+1]
        print(x,y)
        x = x-y
    print(A[N-1],x)

Submission status: Wrong Answer

Solution code:
INF = 10 ** 100

n = int(raw_input())
ns = map(int, raw_input().split())

ns.sort(reverse=True)
pos = ns[:(n + 1) / 2]
neg = ns[(n + 1) / 2:]

while len(pos) > 1 and pos[-1] < 0:
    neg.append(pos.pop())

#print pos, neg

ans = sum(pos) - sum(neg)
print ans

res = INF
while pos and neg:
    if len(pos) >= 2 and len(neg) >= 2:
        a1, a2 = pos.pop(), pos.pop()
        b1, b2 = neg.pop(), neg.pop()

        c1 = a1 - b1
        print a1, b1

        c2 = b2 - a2
        print b2, a2

        pos.append(c1)
        neg.append(c2)
    elif len(pos) == 1 and len(neg) == 1:
        a1 = pos.pop()
        b1 = neg.pop()
        print a1, b1
        res = a1 - b1
    elif len(pos) == 2 and len(neg) == 1:
        a1, a2 = pos.pop(), pos.pop()
        b1 = neg.pop()

        c = b1 - a2
        print b1, a2
        print a1, c

        res = a1 - c
    elif len(pos) == 1:
        a1 = pos.pop()
        b1 = neg.pop()

        c = a1 - b1
        print a1, b1

        pos.append(c)
        if not pos and not neg:
            res = c
    else:
        assert False

assert res == ans
assert not pos
assert not neg

Submission status: Wrong Answer

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
q=[]
for i in range(n-1):
  a.sort()
  if i != n-2:
    x=a.pop(0)
    y=a.pop(-1)
    q.append([x,y])
    a.append(x-y)
  else:
    x=a.pop(-1)
    y=a.pop(0)
    q.append([x,y])
    m=x-y

print(m)
for i in range(len(q)):
  ans=" ".join(list(map(str,q[i])))
  print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

A.sort()

if N==2:
    print(str(A[1]-A[0])+'\n'+str(A[1])+' '+str(A[0]))
    
else:
    ans_li=[]
    before_result=A[0]
    for i in range(N-2):
        ans_li.append(str(before_result)+' '+str(A[i+1]))
        before_result = before_result - A[i+1]
        
    ans = A[-1]-before_result
    ans_li.append(str(A[-1])+' '+str(before_result))
    
    print(str(ans))
    for a in ans_li:
        print(a)

Submission status: Wrong Answer

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

min_idx = 0
max_idx = N-1

ans = []

if N % 2 == 0:
    last = A[max_idx] -A[min_idx]
    ans.append([A[max_idx], A[min_idx]])
    max_idx -= 1
    min_idx += 1
    N -= 1
else:
    last = A[max_idx]
    max_idx -= 1

for i in range(0, N-1, 2):
    ans.append([A[min_idx], last])
    last = A[min_idx] - last
    min_idx += 1
    ans.append([A[max_idx], last])
    last = A[max_idx] - last
    max_idx -= 1

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

Submission status: Wrong Answer

Solution code:
m = int(input())
lis = list(map(int,input().split()))
lis.sort(reverse = True)

if m % 2 == 0:
  print(abs(sum(lis[:m//2]))-abs(sum(lis[m//2:])))
  print(lis[0],lis[-1])
  num = lis[0]-lis[-1]
  for i in range(1,m//2):
    print(lis[-(i+1)],num)
    num = lis[-(i+1)]-num
    print(lis[i],num)
    num = lis[i]-num

else:
  print(sum(lis[:m//2])-sum(lis[m//2+1:])+abs(lis[m//2]))
  
  if lis[m//2] > 0:
    print(lis[-1],lis[0])
    num = lis[-1]-lis[0]
  
    for i in range(1,m//2):
      print(lis[i],num)
      num = lis[i]-num
      print(lis[-(i+1)],num)
      num = lis[-(i+1)]-num
      
    print(lis[m//2],num)
  else:
    print(lis[0],lis[-1])
    num = lis[0]-lis[-1]
    for i in range(1,m//2):
      print(lis[-(i+1)],num)
      num = lis[-(i+1)]-num
      print(lis[i],num)
      num = lis[i]-num
    print(num,lis[m//2])

Submission status: Wrong Answer

Solution code:
import bisect

N=int(input())
A=[int(i) for i in input().split(" ")]
A=sorted(A)
traceback=[]

for i in range(N-1):
    if i%2==0:
        x=A[0]
        y=A[-1]
    else:
        x=A[-1]
        y=A[0]
    A=A[1:-1:]
    bisect.insort_left(A,x-y)
    traceback.append((x,y))
print(A[0])
for t in traceback:
    print(t[0],t[1])

Submission status: Wrong Answer

Solution code:
import heapq
n=int(input())
A=[int(i) for i in input().split()]
heapq.heapify(A)
for i in range(n-2):
  a1=heapq.heappop(A)
  a2=heapq.heappop(A)
  print(a1,a2)
  heapq.heappush(A,a1-a2)
a1=heapq.heappop(A)
a2=heapq.heappop(A)
print(a2,a1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A_lis = list(map(int, input().split()))

A_lis.sort()

d = 0
seq = []

if N % 2 == 0:
  coeff = 1
else:
  coeff = -1

for j in range(N-1):
  if j == 0:
    if coeff==1:
      seq.append((A_lis[j+1], A_lis[j]))
    else:
      seq.append((A_lis[j], A_lis[j+1]))
    d = coeff * (A_lis[j+1]-A_lis[j])
  else:
    if coeff==1:
      seq.append((A_lis[j+1], d))
    else:
      seq.append((A_lis[j], d))
    d = coeff * (A_lis[j+1] - d)
  coeff *= -1
    
print(d)
for j in seq:
  print("%d %d" % j)

Submission status: Wrong Answer

Solution code:
n = int(input())
v = list(map(int, input().split()))
v.sort()
h = n // 2
print(sum(v[h:]) - sum(v[:h]))
s = 0
for i in range(n):
    if i % 2 == n % 2:
        x = v[i // 2]
    else:
        x = v[i // 2 + h]
    if i:
        print(x, s)
    s = x - s

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline

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

A.sort()

log = []
m = A[0]

for a in A[1:-1]:
    log.append((m, a))
    m = m - a

log.append((A[-1], m))
m = A[-1] - m

print(m)
for l, r in log:
    print('{} {}'.format(l, r))

Submission status: Wrong Answer

Solution code:
from collections import deque


def m():
    M = int(input())
    A = deque(sorted([int(i) for i in input().split()]))
    steps = []
    # get last A
    maxA = A.pop()
    minA = A.popleft()
    while len(A) > 0:
        b = A.pop()
        steps.append("{} {}".format(minA, b))
        minA -= b
    steps.append("{} {}".format(maxA, minA))
    print(maxA - minA)
    for s in steps:
        print(s)


m()

Submission status: Wrong Answer

Solution code:
def inpl(): return list(map(int, input().split()))
N = int(input())
A = sorted(inpl())
for i in range(N-2, 0, -1):
    print(A[0], A[i])
    A[0] -= A[i]
print(A[-1], A[0])

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,k = LI()
    print(n%k)
    return

#B
def B():

    n = I()
    po = LIR(n)
    return

#C
def C():
    n = I()
    a = LI()
    a.sort()
    if a[0] >= 0:
        print(sum(a)-2*a[0])
        for i in range(1,n-1):
            print(a[0],a[i])
            a[0] -= a[i]
        print(a[n-1],a[0])
    else:
        if a[-1] < 0:
            print(2*a[-1]-sum(a))
            for i in range(n-1):
                print(a[-1],a[i])
                a[-1] -= a[i]
        else:
            m = bisect.bisect_left(a,0)
            p = n-m
            ans = -sum(a[:m])+sum(a[m:])
            print(ans)
            if p%2:
                for i in range(m,n):
                    print(a[i],a[m-1])
                    a[m-1] = a[i]-a[m-1]
                for i in range(m-1):
                    print(a[m-1],a[i])
                    a[m-1] -= a[i]
            else:
                print(a[m-1],a[m])
                a[m-1] -= a[m]
                for i in range(m+1,n):
                    print(a[i],a[m-1])
                    a[m-1] = a[i]-a[m-1]
                for i in range(m-1):
                    print(a[m-1],a[i])
                    a[m-1] -= a[i]
    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#G
def G():
    n = I()

    return

#H
def H():
    n = I()

    return

#I
def I_():
    n = I()

    return

#J
def J():
    n = I()

    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
a=sorted(A)
print(a[-1]-a[0]+sum(a[1:-1]))
print(a[0],a[1])
tmp=a[0]-a[1]
if n>3:
  for i in range(2,n-1):
      print(tmp,a[i])
      tmp=tmp-a[i]
print(a[-1],tmp)

Submission status: Wrong Answer

Solution code:
N = int(input())
Alist = list(map(int,input().split()))
nAlist = sorted(Alist)
Sum = 0
outputList = []

for i in range(len(Alist)-1):
  if i!=len(Alist)-2:
    outputList.append([nAlist[0],nAlist[1]])
    nAlist[0] = nAlist[0]-nAlist[1]
    del nAlist[1]
  else:
    Sum = nAlist[1]-nAlist[0]
    outputList.append([nAlist[1],nAlist[0]])

print(Sum)
    
for i in range(len(outputList)):
  print(str(outputList[i][0]) + " " + str(outputList[i][1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = [int(i) for i in input().split()]
A.sort(reverse=True)
Q = deque(A)
pr = []
while(len(Q) > 1):
    if len(Q) % 2 == 0:
        x, y = Q.popleft(), Q.pop()
        pr.append([x, y])
        Q.appendleft(x-y)
    else:
        x, y = Q.pop(), Q.popleft()
        pr.append([x, y])
        Q.append(x-y)
print(Q[0])
for i in pr:
    print(i[0], i[1])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ansList = []
aPreMax = int(a.pop())

while len(a) > 1:
    a.sort()
    aMax = a.pop()
    aMin = a.pop(0)
    ansList.append([aMin, aMax])
    a.append(aMin - aMax)


ansList.append([aPreMax, a[0]])

print(aPreMax-a[0])
for ans in ansList:
    print(ans[0], ans[1])

Submission status: Wrong Answer

Solution code:
n=int(input())
newlist = list(map(int, input().split()))

switch=0
mazu=newlist[0]

for nl in newlist:
    if mazu!=nl:
        switch=1

if switch==1:
    wa=0
    for nl in newlist:
        wa+=abs(nl)
    print(wa)
else:
    print(mazu*(len(newlist)-2))

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
a=sorted(A)
print(a[-1]-a[0]+sum(a[1:-1]))
print(a[0],a[1])
tmp=a[0]-a[1]
if n>3:
  for i in range(2,n-2):
      print(tmp,a[i])
      tmp=tmp-a[i]
print(a[-1],tmp)

Submission status: Wrong Answer

Solution code:
from collections import deque


N = int(input())
A = list(map(int, input().split()))
A.sort()
ans = sum(A[N // 2:]) - sum(A[:N // 2])
print(ans)
queue = deque(A)

cnt = N
for i in range(N-1):
    if cnt % 2 == 0:
        x = queue.pop()
        y = queue.popleft()
        print(x, y)
        queue.append(x - y)
    else:
        y = queue.pop()
        x = queue.popleft()
        print(x, y)
        queue.appendleft(x - y)
    cnt -= 1

Submission status: Wrong Answer

Solution code:
N = int(input())

As = sorted(list(map(int,input().split())))

XYs = []
minus_one = N - 2
while True:
    if minus_one == 0:
        break
    x = As[0]
    y = As[minus_one]
    XYs.append((x,y))
    As[0] = x - y
    minus_one -= 1

XYs.append((As[-1],As[0]))
print(As[-1] - As[0])
for xy in XYs:
    print(xy[0],xy[1])

Submission status: Wrong Answer

Solution code:
INF = 10 ** 100

n = int(raw_input())
ns = map(int, raw_input().split())

ns.sort(reverse=True)
pos = ns[: n / 2]
neg = ns[(n + 1) / 2:]
#print pos, neg

if n % 2 == 1:
    if ns[n / 2] >= 0:
        pos.append(ns[n / 2])
    else:
        neg.append(ns[n / 2])

#print pos, neg

ans = sum(pos) - sum(neg)
print ans

res = INF
while pos and neg:
    if len(pos) >= 2 and len(neg) >= 2:
        a1, a2 = pos.pop(), pos.pop()
        b1, b2 = neg.pop(), neg.pop()

        c1 = a1 - b1
        print a1, b1

        c2 = b2 - a2
        print b2, a2

        pos.append(c1)
        neg.append(c2)
    elif len(pos) == 1 and len(neg) == 1:
        a1 = pos.pop()
        b1 = neg.pop()
        print a1, b1
        res = a1 - b1
    elif len(pos) == 2 and len(neg) == 1:
        a1, a2 = pos.pop(), pos.pop()
        b1 = neg.pop()

        c = b1 - a2
        print b1, a2
        print a1, c

        res = a1 - c
    elif len(pos) == 1 and len(neg) == 2:
        a1 = pos.pop()
        b1, b2 = neg.pop(), neg.pop()

        c = a1 - b1
        print a1, b1
        print c, b2

        res = c - b2
    else:
        assert False

assert res == ans
assert not pos
assert not neg

Submission status: Wrong Answer

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

print(sum(a)-2*a[0])

x = a[0]

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

print(a[n-1],x)