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:
M=int(input())
A= list(map(int, input().split()))
while(len(A)>3):
    A.sort()
    C=A[1]-A[0]
    A.remove(A[1])
    A.remove(A[0])
    A.append(C)
print(max(A)-min(A))
print(max(A),min(A))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().strip().split(' ')))
A=sorted(A)[::-1]
change=len(A)-1
for i in range(len(A)):
    if A[i]<0:
        change=i
        break

if change==0:
    change=1
MAX=sum(A[:change])+sum([-A[i] for i in range(change,len(A))])


prefix=[]
pre=0
for i in range(len(A)):
    if i<change:
        pre+=A[i]
        prefix+=[pre]
    else:
        pre-=A[i]
        prefix+=[pre]

queries=[]

if change>=2:
    pre=A[change]
    for i in range(change-1):
        queries+=[(pre,A[i])]
        pre-=A[i]
    queries+=[(A[change-1],pre)]
    for i in range(change+1,len(A)):
        queries+=[(pre,A[i])]
        pre-=A[i]
else:
    pre=A[0]
    for i in range(1,len(A)):
        queries+=[(pre,A[i])]
        pre-=A[i]

print(MAX)
for (a,b) in queries:
    print(a,b)

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:
        ans += a[m//2] - a[m//2-1]
        for i in range(m//2-1):
            anslis.append((a[i], a[-1 - i]))
            anslis.append((ans,-a[-1 - i]+a[i]))
            ans += a[-1 - i] - a[i]
    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:
import bisect

n = int(input())
a = list(map(int, input().split()))
a.sort()
max_a = max(a)
min_a = min(a)

if max(a) <= 0:
    for i in range(n - 1):
        print(max_a, a[i])
        max_a -= a[i]
elif min(a) >= 0:
    for i in range(n - 2):
        print(min_a, a[i + 1])
        min_a -= a[i + 1]
    print(max_a, min_a)
else:
    num = bisect.bisect(a, 0)
    for i in range(num + 1, n):
        print(min_a, a[i])
        min_a -= a[i]
    a_num = a[num]
    a[0] = min_a
    for i in range(num):
        print(a_num, a[i])
        a_num -= a[i]

Submission status: Wrong Answer

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

if a[0] <= 0 and 0 <= a[-1]:
    import bisect
    print(sum(map(lambda x: abs(x), a)))
    x0 = bisect.bisect_left(a,0)

    print("{} {}".format(a[0], a[x0]))
    res = a[0] - a[x0]
    for x in a[x0+1:-2]:
        print("{} {}".format(res, x))
        res -= x
    
    print("{} {}".format(a[-1], res))
    res = a[-1] - res
    for x in a[1:x0]:
        print("{} {}".format(res,x))
        res -= x

elif a[0] > 0:
    print(sum(a) - 2*a[0])

    print("{} {}".format(a[0],a[1]))
    res = a[0] - a[1]
    for x in a[2:-2]:
        print("{} {}".format(res, x))
        res -= x
    
    print("{} {}".format(a[-1], res))
else:
    print(-sum(a) + 2*a[-1])

    print("{} {}".format(a[-1],a[-2]))
    res = a[-1] - a[-2]
    for x in a[:-2]:
        print("{} {}".format(res, x))
        res -= x

Submission status: Wrong Answer

Solution code:
N = int(input())

numbers = list(map(int, input().split()))
xs = []
ys = []

numbers.sort(reverse=True)
for _ in range(N-2):
    _min = numbers.pop()
    _max = numbers.pop(0)
    new_number = _min - _max
    numbers.append(new_number)
    xs.append(_min)
    ys.append(_max)

_min = numbers.pop(0)
_max = numbers.pop()
result = _min - _max
xs.append(_min)
ys.append(_max)

print(result)

[print(xs[i], ys[i]) for i in range(N-1)]

Submission status: Wrong Answer

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

A.sort(reverse = True)

Ans = []
temp = A[-1]
for a in A[1:-1]:
    Ans.append([temp,a])
    temp -= a
ans = [A[0],temp]
Ans.append(ans)
print(ans[0] - ans[1])
for answ in Ans:
    print(*answ, sep = " ")

Submission status: Wrong Answer

Solution code:
def bs_left(l, target): # l=sortedのリスト, target以上の値をもつ要素のうち一番左のindexを返す
    low, high = 0, len(l)
    while low < high:
        mid = (low+high)//2
        if l[mid] < target:
            low = mid+1
        else:
            high = mid
    if low == len(l):
        return -1
    return low
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:
    id = bs_left(A,0)
    s = 0
    for i in range(N):
        if i < id:
            s -= A[i]
        else:
            s += A[i]
    print(s)
    x = A[0]
    for i in range(id,N-1):
        y = A[i]
        print(x,y)
        x = x-y
    print(A[N-1],x)
    x = A[N-1]-x
    for i in range(1,id):
        y = A[i]
        print(x,y)
        y = x-y

Submission status: Wrong Answer

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

A.sort()

B = [0] * N
C = [0] * N
if N%2==1:
    for i in range(N//2):
        B[i*2+1]=A[i]

    for i in range(N//2,N):
        B[(i-N//2)*2]=A[i]

    for i in range(N//2+1):
        C[i*2]=A[i]

    for i in range(N//2+1,N):
        C[(i-N//2-1)*2+1]=A[i]


else:
    for i in range(N//2):
        B[i*2]=A[i]

    for i in range(N//2,N):
        B[(i-N//2)*2+1]=A[i]


if N%2==0:
    temp = B[0]
    ans = []
    for i in range(N-1):
        ans.append(str(B[i+1])+" "+str(temp))
        temp = B[i+1]-temp
else:
    temp = B[0]
    ans = []
    for i in range(N-1):
        ans.append(str(B[i+1])+" "+str(temp))
        temp = B[i+1]-temp
    temp2 = C[0]
    ans2 = []
    for i in range(N-3):
        ans2.append(str(C[i+1])+" "+str(temp2))
        temp2 = C[i+1]-temp2
    ans2.append(str(C[-2])+" "+str(C[-1]))
    temp3 = C[-2]-C[-1]
    ans2.append(str(temp3)+" "+str(temp2))
    temp2 = temp3-temp2
    if temp<temp2:
        temp=temp2
        ans = ans2


print(temp)
for a in ans:
    print(a)

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:
    ans_frg = 0
    ans_l = []
    ans_e = a[-1]
    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 and ans_frg == 0:
            ans = a[0]
            ans_frg = 1
        if a[i] >= 0 and ans_frg == 1:
            ans_l += [(ans, a[i])]
            ans -= a[i]

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

else:
    ans = a[0]
    ans_l = []
    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:
from collections import deque
N = int(input())
A = list(map(int, input().split()))
A.sort()
q = deque(A)
ansq = deque()
i = 0
while i != N - 2:
    if i % 2 == 0:
        l, r = q.popleft(), q.pop()
        q.appendleft(l - r)
        ansq.append((l,  r))
    else:
        l, r = q.popleft(), q.pop()
        q.append(r - l)
        ansq.append((r,  l))
    i += 1
print(q[1] - q[0])
for ans in ansq:
    print("{} {}".format(ans[0], ans[1]))
print(q[1], q[0])

Submission status: Wrong Answer

Solution code:
import sys
N=int(input())
A=[int(i) for i in input().split()]
Aabs = [abs(a) for a in A]
zeroarray = []
plusarray = []
minusarray =[]
for n,a in enumerate(A):
    if a==0:
        zeroarray.append(a)
    elif a>0:
        plusarray.append(a)
    else:
        minusarray.append(a)
zeros =len(zeroarray)
plus = len(plusarray)
minus = N-zeros-plus
if zeros :#どんな正負も実現可能
    firstzero = 0
    print(sum(Aabs))
    if plus:#マイナスならfirstplusから、プラスならfirstzeroから、最後にfirsrtplusからfirstzeroを引く
        firstplus = plusarray[0]
        for i in range(1,plus):
            print(firstzero, plusarray[i])
            firstzero-=plusarray[i]
        for m in minusarray:
            print(firstplus,m)
            firstplus-=m
        for z in zeroarray:
            print(firstplus,z)
            firstplus-=z
    else:#全部firstzeroから引く
        if zeros>1:
            for z in range(zeros-1):
                print(0,0)
        for m in minusarray:
            print(firstzero,m)
            firstzero-=m
    sys.exit()
    
if (plus and minus):#どんな正負も実現可能
    firstminus = minusarray[0]
    print(sum(Aabs))
    firstplus = plusarray[0]
    firstminus = minusarray[0]
    for i in range(1,plus):
        print(firstminus, plusarray[i])
        firstminus-=plusarray[i]
    for i in range(1,minus):
        print(firstplus,minusarray[i])
        firstplus-=minusarray[i]
    print(firstplus,firstminus)
    sys.exit()
    
    
if plus:#全部プラスなのでminplusから一個以外引いた上でfirstplusから引く
    plusarray.sort()
    minplus = plusarray[0]
    firstplus = plusarray[1]
    print(sum(Aabs)-2*minplus)
    for i in range(2,plus):
        print(minplus, plusarray[i])
        minplus-=plusarray[i]
    print(firstplus,minplus)
    sys.exit()
   
else:#全部マイナスなのでmaxminusから一個以外引く
    minusarray.sort(reverse = True)
    maxminus = minusarray[0]
    print(sum(Aabs)+2*maxminus)
    for i in range(1,minus):
        print(maxminus, minusarray[i])
        maxminus-=minusarray[i]
    sys.exit()

Submission status: Wrong Answer

Solution code:
import bisect
m = int(input())
a = list(map(int,input().split()))
a.sort()

if a[-1] < 0:
    a1 = -(sum(a)) + 2 * a[-1]
    print(a1)
    p = a[-1]
    q = a[-2]
    for i in range(-1,-(len(a)),-1):
        print(str(p)+ " " + str(q))
        if i == -(len(a) -1):
            break
        p = p - q
        q = a[i-2]
elif a[0] >= 0:
    print(sum(a[1:]) - a[0])
    p = a[0]
    q = a[1]
    for i in range(len(a)-1):
        print(str(p)+" "+str(q))
        p = p - q
        if i + 2 > len(a) - 2:
            break 
        q = a[i+2]
    print(str(a[-1])+" "+str(p))
else:
    c = bisect.bisect_left(a,0)
    c1 = 0
    for x in a:
        c1 += abs(x)
    print(c1)
    p = a[c-1]
    q = a[c]
    for i in range(c,m-1):
        print(str(p)+" "+str(q))
        p = p - q
        q = a[i+1]
    p1 = a[-1]
    q1 = a[0]
    for i in range(c-1):
        print(str(p1)+" "+str(q1))
        p1 = p1 - q1
        q1 = a[i+1]
    print(str(p1)+ " "+str(p))

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):
        if(l[i]>x):
            val=l[i]-x
            ans.append([l[i],x])
            x=val
        else:
            val=x-l[i]
            ans.append([x,l[i]])
            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:
#!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):
            anslis.append((a[i], a[-1 - i]))
            if i == 0:
                ans += a[-1 - i] - a[i]
                continue
            anslis.append((ans,-a[-1 - i]+a[i]))
            ans += a[-1 - i] - a[i]
    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()))
# print(a)
a.sort()
a.reverse()
# print(a)
l1=[]
if n==2:
	print(a[0]-a[1])
	print(a[0],a[1])
else:
	for i in range(n-2):
		l1.append((a[n-1-i],a[n-2-i]))
		a[n-2-i] = a[n-1-i]-a[n-2-i]
	ans = a[0]-a[1]
	print(ans)
	for i in range(n-2):
		print(l1[i][0],l1[i][1])
	print(a[0],a[1])

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = list(map(int, input().split()))
a.sort()
a = deque(a)
ans = []
anss = 0
for i in range(n-2):
  mi = a.popleft()
  ma = a.pop()
  ans.append((mi, ma))
  t = mi-ma
  a.appendleft(t)
mi = a.popleft()
ma = a.pop()
ans.append((ma, mi))
anss = ma-mi
print(anss)
for e in ans:
  print(*e)

Submission status: Wrong Answer

Solution code:
m = int(input())

a = list(map(int, input().split(' ')))

a.sort()

p = []

for i in range(m - 1):
    a1 = a.pop(1)
    a2 = a.pop(0)
    if len(a) != 0:
        a.insert(a2 - a1, 0)
        p.append((a2, a1))
    else:
        ans_m = a1 - a2
        p.append((a1, a2))

print(ans_m)

for i in p:
    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])
i = 1
tmp = A[0]
while N > 2:
    print(str(tmp) + " " + str(A[i]))
    tmp -= A[i]
    i += 1
    N -= 1
print(str(A[-1]) + " " + str(tmp))

Submission status: Wrong Answer

Solution code:
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()
aa = []
al = cl.deque()
if n % 2 == 0:
    for i in range(n//2):
        al.append([a[-(1+i)],a[i]])
        aa.append(a[-(1+i)]-a[i])
    su = sum(map(abs,aa))
    while True:
        if len(aa) == 1:
            break
        else:
            z = aa.pop()
            zz = aa.pop()
            al.append([z,zz])
            aa.append(z-zz)
    print(su)
    for i in range(len(al)):
        print(jo(al[i]))

else:
    for i in range(n//2):
        al.append([a[-(1+i)],a[i]])
        aa.append(a[-(1+i)]-a[i])
    aa.append(a[n//2])
    su = sum(map(abs,aa))
    while True:
        if len(aa) == 1:
            break
        else:
            z = aa.pop()
            zz = aa.pop()
            al.append([z,zz])
            aa.append(z-zz)
    print(su)
    for i in range(len(al)):
        print(jo(al[i]))

Submission status: Wrong Answer

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

Alist.sort()

ans = sum(Alist) - 2 * Alist[0]

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

numbers = list(map(int, input().split()))
xs = []
ys = []

numbers.sort(reverse=True)
for _ in range(N-1):
    _min = numbers.pop()
    _max = numbers.pop(0)
    new_number = _min - _max
    numbers.append(new_number)
    xs.append(_min)
    ys.append(_max)

print(numbers[0])

[print(xs[i], ys[i]) for i in range(N-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)
  a.insert(0,c-b)
  if len(a)>1:
    print(c,b)
  elif len(a)==1:
    print(b,c)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=sorted(map(int, input().split()))
a1=[i for i in a if i>=0]
a2=[i for i in a if i<0]
if a1 and a2:
    print(sum(a1)-sum(a2))
    mx,mn=a1[-1], a2[0]
    for i in a1[:-1]:
        print(i,mn)
        mn-=i
    for i in a2[1:]:
        print(mx,i)
        mx-=i
    print(mx,mn)
elif not a2:
    print(sum(a1[1:])-a1[0])
    mx,mn=a1[-1], a1[0]
    for i in a1[1:-1]:
        print(i,mn)
        mn-=i
    print(mx,mn)
else:
    print(a2[-1]-sum(a2[:-1]))
    mx,mn=a2[-1], a2[0]
    for i in a2[1:-1]:
        print(mx,i)
        mx-=i
    print(mx,mn)

Submission status: Wrong Answer

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

A.sort()
MAX = A.pop(-1)
ans = MAX
MIN = A.pop(0)
if MIN <= 0:
    if N == 2:
        ans -= MIN
        print("{0} {1}".format(MAX, MIN))
    else:
        
        for a in A:
            if a > 0:
                print("{0} {1}".format(MIN, a))
                MIN -= a
            else:
                print("{0} {1}".format(MAX, a))
                MAX -= a
        print("{0} {1}".format(MAX, MIN))
else:
    if N == 2:
        ans -= MIN
        print("{0} {1}".format(MAX, MIN))
    else:
        b = A.pop(-1)
        print("{0} {1}".format(MIN, b))
        MIN -= b
        for a in A:
            if a > 0:
                print("{0} {1}".format(MIN, a))
                MIN -= a
            else:
                print("{0} {1}".format(MAX, a))
                MAX -= a
        print("{0} {1}".format(MAX, MIN))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = input().split()
A = [int(x) for x in A]
A.sort()
answer = A[0] - A[1]
for i in range(2 , N - 2):
  answer = answer - A[i]
answer = A[N-1] - answer
print(answer)
answer = 0
print(A[0],A[1])
answer = A[0] - A[1]
for i in range(2 , N - 2):
  answer = answer - A[i]
  print(answer,A[i])
print(A[N-1],answer)

Submission status: Wrong Answer

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

for i in range(N):
    if A[i] >= 0:
        po = i
        break
else:
    po = -1
for i in range(N):
    if A[i] <= 0:
        ne = i
        break
else:
    ne = -1
mi = min(B)
for i in range(N):
    if B[i] == mi:
        mii = i
        break
else:
    mii = -1

if po >= 0 and ne >= 0:
    print(sum(B))
    t1, t2 = A[po], A[ne]
    if po > ne:
        po, ne = ne, po
    # print("pone =", po, ne)
    if po == ne:
        del A[ne]
        A = [0] + A
    else:
        del A[ne]
        del A[po]
        A = [t1, t2] + A
else:
    print(sum(B) - 2 * mi)
    del A[mii]
    A = [mi] + A
# print(A)
prev = A[0]
for i in range(N-1):
    if i == N-2 or A[i+2] < 0:
        print(max(prev, A[i+1]), min(prev, A[i+1]))
        prev = max(prev, A[i+1]) - min(prev, A[i+1])
    else:
        print(min(prev, A[i+1]), max(prev, A[i+1]))
        prev = min(prev, A[i+1]) - max(prev, A[i+1])

Submission status: Wrong Answer

Solution code:
import bisect
m = int(input())
a = list(map(int,input().split()))
a.sort()

if a[-1] < 0:
    a1 = -(sum(a)) + 2 * a[-1]
    print(a1)
    p = a[-1]
    q = a[-2]
    for i in range(-1,-(len(a)),-1):
        print(str(p)+ " " + str(q))
        if i == -(len(a) -1):
            break
        p = p - q
        q = a[i-2]
elif a[0] >= 0:
    print(sum(a[1:]) - a[0])
    p = a[0]
    q = a[1]
    for i in range(len(a)-1):
        print(str(p)+" "+str(q))
        p = p - q
        if i + 2 > len(a) - 2:
            break 
        q = a[i+2]
    print(str(a[-1])+" "+str(p))
else:
    c = bisect.bisect_left(a,0)
    c1 = 0
    for x in a:
        c1 += abs(x)
    print(c1)
    p = a[c-1]
    q = a[c]
    for i in range(c,m-1):
        print(str(p)+" "+str(q))
        p = p - q
        q = a[i+1]
    p1 = a[-1]
    q1 = a[0]
    for i in range(c-1):
        print(str(p1)+" "+str(q1))
        p1 = p1 - q1
        q1 = a[i+1]
    print(str(p)+ " "+str(p1))

Submission status: Wrong Answer

Solution code:
import bisect
N=int(input())
A=list(map(int,input().split()))
A.sort()
k=bisect.bisect_left(A,0)
if k<=1 and N>=3:
  a=sum(A)-2*(A[0]+A[1])
elif k==N:
  a=-sum(A)+2*A[-1]
else:
  a=-sum(A[:k])+sum(A[k:])
print(a)
if N==2:
  print(A[1],A[0])
elif k>=N-1:
  x=A[N-1]
  for i in range(N-1):
    print(x,A[i])
    x-=A[i]
else:
  
    x=A[0]
    for i in range(max(k,2),N):
      print(x,A[i])
      x-=A[i]
    print(A[1],x)
    x=A[1]-x
    for i in range(max(k,2),k):
      print(x,A[i])
      x-=A[i]

Submission status: Wrong Answer

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

a.sort()
ans = a[0]
for i in range(1, n - 1):
    ans += a[i] * (-1)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
print(sum(A)-2*A[0])
i = 1
tmp = A[0]
while N > 2:
    print(str(tmp) + " " + str(A[i]))
    tmp -= A[i]
    N -= 1
print(str(A[-1]) + " " + str(tmp))

Submission status: Wrong Answer

Solution code:
N, *A = map(int, open(0).read().split())

B = A[:]
A_MAX = max(B)
B.remove(A_MAX)
ans = []

while len(B) > 1:
    B.sort()
    x = B.pop()
    y = B.pop()
    if y >= 0:
        B.append(x - y)
        ans.append([x, y])
    else:
        B.append(y - x)
        ans.append([y, x])
    
if B[0] >= 0:
    print(A_MAX - B[0])
    ans.append([A_MAX, B[0]])
else:
    print(B[0] - A_MAX)
    ans.append([B[0], A_MAX])
for i in range(N - 1):
    print(*ans[i])

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline

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

A.sort()

amax = A.pop(-1)
amin = A.pop(0)
neg = [a for a in A if a < 0]
pos = [a for a in A if a >= 0]

log = []

m = amin
for a in pos:
    log.append((m, a))
    m = m - a

M = amax
for a in neg:
    log.append((m, a))
    M = M - a

log.append((M, m))
ans = M - m

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

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
from collections import deque
from heapq import heappush,heappop
import re

def int_raw():
    return int(input())
 
def ss_raw():
    return input().split()
 
def ints_raw():
    return tuple(map(int, ss_raw()))


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


DIV = 10**9+7

def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret


def main():
    ans = 0
    N = int_raw()
    As = list(ints_raw())
    As.sort()
    buf = As[0]
    S = ""
    for idx in range(1,N):
        if (N-idx) % 2 == 1:
            S += str(As[idx])+" "+str(buf)+"\n"
            buf = As[idx]-buf
        else:
            S+=str(buf)+" "+str(As[idx])+"\n"
            buf = buf-As[idx]
    print (buf)
    print (S)


main()

Submission status: Wrong Answer

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

select = []

def maxi(ai):
    if len(ai) == 2:
        select.append("{} {}".format(ai[0], ai[1]))
        return ai[0] - ai[1]
    else:
        minin = mini(ai[1:])
        select.append("{} {}".format(ai[0], minin))
        return ai[0] - minin

def mini(ai):
    if len(ai) == 2:
        select.append("{} {}".format(ai[1], ai[0]))
        return ai[1] - ai[0]
    else:
        maxin = max(ai[1:])
        select.append("{} {}".format(ai[0], maxin))
        return ai[len(ai)-1] - maxin

print(maxi(sorted(ai, reverse=True)))
for s in select:
    print(s)

Submission status: Wrong Answer

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

A.sort()

M = 0
for i in range(N):
    if i == 0:
        M -= A[i]
    else:
        M += abs(A[i])
print (M)

if N == 2:
    print (A[1], A[0])
    sys.exit()

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

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = list(map(int, input().split()))
a.sort()
a = deque(a)
ans = []
anss = 0

for i in range(n-2):
  mi = a.popleft()
  ma = a.popleft()
  ans.append((mi, ma))
  t = mi-ma
  a.appendleft(t)
mi = a.popleft()
ma = a.pop()
ans.append((ma, mi))
anss = ma-mi
print(anss)
for e in ans:
  print(*e)

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(ans,i)
        ans-=i
    for i in a[1:b]:
        print(tmp,i)
        tmp-=i
    print(tmp,ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(map(int, input().split()))
mi = A[0]
ma = A[-1]
Ans = []
for i, a in enumerate(A[1:-1], 1):
    if a < 0:
        ma -= a
    else:
        Ans.append([mi, a])
        mi -= a

print(ma - mi)
for x, y in Ans:
    print(x, y)
print(ma, mi)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
# print(a)
a.sort()
a.reverse()
# print(a)
l1=[]
for i in range(n-2):
	l1.append((a[n-1-i],a[n-2-i]))
	a[n-2-i] = a[n-1-i]-a[n-2-i]
ans = a[0]-a[1]
print(ans)
for i in range(n-2):
	print(l1[i][0],l1[i][1])
print(a[0],a[1])

Submission status: Wrong Answer

Solution code:
m = int(input())

a = list(map(int, input().split(' ')))

a.sort()

p = []

for i in range(m - 1):
    a1 = a.pop(1)
    a2 = a.pop(0)
    if len(a) != 0:
        a.append(a2 - a1)
        p.append((a2, a1))
    else:
        ans_m = a1 - a2
        p.append((a1, a2))
    a.sort()

print(ans_m)

for i in p:
    print(i[0], i[1])

Submission status: Wrong Answer

Solution code:
import heapq
n=int(input())
a=list(map(int,input().split()))
heapq.heapify(a)
stack=[]
for i in range(n-2):
    b=heapq.heappop(a)
    c=heapq.heappop(a)
    stack.append([b,c])
    heapq.heappush(a,b-c)
if len(a)==2:
    b=heapq.heappop(a)
    c=heapq.heappop(a)
    stack.append([c,b])
print(c-b)
for i in range(n-1):
    print(stack[i][0],stack[i][1])