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:
N = int(input())
A = list(map(int,input().split()))

pos = []
neg = []
zero = 0
for a in A:
    if a > 0:
        pos.append(a)
    elif a < 0:
        neg.append(a)
    else:
        zero += 1

if zero == N:
    print(0)
    for i in range(N-1):
        print(0,0)
    exit()

pos.sort()
neg.sort()
arr = []

if len(neg)==0:
    if len(pos)==1:
        print(pos[0])
        for i in range(zero):
            print(pos[0],0)
    elif len(pos)==2:
        print(pos[1] - pos[0])
        for i in range(zero):
            print(pos[1], 0)
        print(pos[1], pos[0])
    else:
        mx = pos[-1]
        mn = pos[0] - pos[1]
        arr.append((pos[0], pos[1]))
        for p in pos[2:-1]:
            arr.append((mn, p))
            mn -= p
        for i in range(zero):
            arr.append((mx, 0))
        arr.append((mx, mn))
        print(mx - mn)
        for a,b in arr:
            print(a,b)
elif len(pos)==0:
    if len(neg)==1:
        print(-neg[0])
        print(0,neg[0])
        for i in range(zero-1):
            print(-neg[0],0)
    else:
        if zero:
            mn = neg[0]
            mx = -neg[1]
            arr.append((0,neg[1]))
            for n in neg[2:]:
                arr.append((mx, n))
                mx -= n
            for i in range(zero-1):
                arr.append((mx, 0))
            arr.append((mx, mn))
            print(mx - mn)
            for a,b in arr:
                print(a,b)
        else:
            if len(neg)==2:
                assert False #mijissou
            neg.reverse()
            mn = neg[0]
            mx = neg[1]-neg[2]
            arr.append((neg[1],neg[2]))
            for n in neg[3:]:
                arr.append((mx, n))
                mx -= n
            arr.append((mx, mn))
            print(mx - mn)
            for a,b in arr:
                print(a,b)
else:
    mx = pos[-1]
    mn = neg[0]
    for p in pos[:-1]:
        arr.append((mn, p))
        mn -= p
    for n in neg[1:]:
        arr.append((mx, n))
        mx -= n
    arr.append((mx, mn))
    for i in range(zero):
        arr.append((mx,0))
    print(mx - mn)
    for a,b in arr:
        print(a,b)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def main():
    N = int(input())
    A = list(map(int, input().split()))
    pos = []
    neg = []
    for i in range(N):
        if A[i] >= 0:
            pos.append(A[i])
        else:
            neg.append(A[i])
    pos.sort()
    neg.sort()
    
    # N個のうち、最低1つは符号を変える＆最低１つは符号をかえない
    ans = []
    if len(pos) == 0:# 負だけ
        print(-sum(A)+2*A[N-1])
    elif len(neg) == 0:# 正だけ
        print(sum(A)-2*A[0])
        now = A[0]
        for i in range(1,N-1):
            print(now,A[i])
            now -= A[i]
        print(A[N-1],now)
    else:
        p = 0
        n = 1
        ans = []
        now = pos[p]
        while n < len(neg):
            ans.append((now,neg[n]))
            now -= neg[n]
            n += 1
        
        p += 1
        now2 = neg[0]
        while p < len(pos):
            ans.append((now2,pos[p]))
            now2 -= pos[p]
            p += 1
        
        ans.append((now,now2))
        print(now-now2)
        for a in ans:
            print(a[0],a[1])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())
from heapq import heappush, heappop
Q = []
for a in As:
    heappush(Q, a)

ans = []
for i in range(N-2):
    x = heappop(Q)
    y = heappop(Q)
    ans.append((x, y))
    heappush(Q, x-y)

x = heappop(Q)
y = heappop(Q)
ans.append((y, x))
print(y-x)
for a in ans:
    print(*a)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())
from heapq import heappush, heappop
Q = []
for a in As:
    heappush(Q, a)

for i in range(N-2):
    x = heappop(Q)
    y = heappop(Q)
    print(x, y)
    heappush(Q, x-y)

x = heappop(Q)
y = heappop(Q)
print(y, x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()
    a[0] *= -1
    m = sum(a)
    print(m if m > 0 else -m)
    a[0] *= -1
    tmp = a[0]
    for i in range(1, n):
        if i == 1:
            print(tmp, a[i])
        else:
            print(a[i], tmp)
        tmp -= a[i]
        # print(tmp)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

plus, minus = a[0], a[-1]
for ai in a[1:-1]:
    if ai < 0:
        print(minus, ai)
        minus -= ai
    elif ai >= 0:
        print(plus, ai)
        plus -= ai
else:
    print(minus, plus)

Submission status: Wrong Answer

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

A.sort()
S = sum(A)

if A[0] >= 0:
    print(S - A[0] * 2)
    tmp = A[0]
    for i in range(1, N - 1):
        print(tmp, A[i])
        tmp -= A[i]
    print(A[-1], tmp)

elif A[-1] <= 0:
    print(-S + A[-1] * 2)
    tmp = A[-1]
    for i in range(1, N):
        print(tmp, A[~i])
        tmp -= A[~i]

else:
    op = []
    tmp1 = A[-1]
    i = 1
    while A[i] < 0:
        op.append((tmp1, A[i]))
        tmp1 -= A[i]
        i += 1
    tmp2 = A[0]
    i = ~1
    while A[i] >= 0:
        op.append((tmp2, A[~i]))
        tmp2 -= A[~i]
        i -= 1
    print(tmp1 - tmp2)
    for x, y in op:
        print(x, y)
    print(tmp1, tmp2)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
a.sort()
print(sum(abs(i)for i in a)-2*abs(a[0])*(a[0]*a[-1]>0))
if a[0]>0:
    print(a[0],a[-1])
    a[0]-=a[-1]
    for i in a[1:-2]:
        print(a[0],i)
        a[0]-=i
    print(a[-2],a[0])
elif a[-1]<0:
    for i in a[:-1]:
        print(a[-1],i)
        a[-1]-=i
else:
    for i in a[1:-1]:
        if i<0:
            print(a[-1],i)
            a[-1]-=i
        else:
            print(a[0],i)
            a[0]-=i
    print(a[-1],a[0])

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
a.sort()
print(sum(abs(i)for i in a)-2*abs(a[0])*(a[0]*a[-1]>0))
if a[0]>0:
    print(a[0],a[-1])
    a[0]-=a[-1]
    for i in a[1:-2]:
        print(a[0],i)
        a[0]-=i
    print(a[-2],a[0])
elif a[-1]<=0:
    for i in a[:-1]:
        print(a[-1],i)
        a[-1]-=i
else:
    for i in a[1:-1]:
        if i<0:
            print(a[-1],i)
            a[-1]-=i
        else:
            print(a[0],i)
            a[0]-=i
    print(a[-1],a[0])

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
a.sort()
print(sum(abs(i)for i in a)-2*abs(a[0])*(a[0]*a[-1]>0))
if a[0]>=0:
    print(a[0],a[-1])
    a[0]-=a[-1]
    for i in a[1:-2]:
        print(a[0],i)
        a[0]-=i
    print(a[-2],a[0])
elif a[-1]<=0:
    for i in a[:-1]:
        print(a[-1],i)
        a[-1]-=i
else:
    for i in a[1:-1]:
        if i<0:
            print(a[-1],i)
            a[-1]-=i
        else:
            print(a[0],i)
            a[0]-=i
    print(a[-1],a[0])

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
a.sort()
print(sum(abs(i)for i in a)-2*abs(a[0])*(a[0]*a[-1]>0))
if n==2:
    if a[1]<0:a=a[::-1]
    print(a[1]-a[0])
    print(a[1],a[0])
elif a[0]>=0:
    print(a[0],a[-1])
    a[0]-=a[-1]
    for i in a[1:-2]:
        print(a[0],i)
        a[0]-=i
    print(a[-2],a[0])
elif a[-1]<=0:
    for i in a[:-1]:
        print(a[-1],i)
        a[-1]-=i
else:
    for i in a[1:-1]:
        if i<0:
            print(a[-1],i)
            a[-1]-=i
        else:
            print(a[0],i)
            a[0]-=i
    print(a[-1],a[0])

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
a.sort()
print(a)
print(sum(abs(i)for i in a)-2*abs(a[0])*(a[0]*a[-1]>0))
if n==2:
    if a[1]<0:a=a[::-1]
    print(a[1]-a[0])
    print(a[1],a[0])
elif a[0]>=0:
    print(a[0],a[-1])
    a[0]-=a[-1]
    for i in a[1:-2]:
        print(a[0],i)
        a[0]-=i
    print(a[-2],a[0])
elif a[-1]<=0:
    for i in a[:-1]:
        print(a[-1],i)
        a[-1]-=i
else:
    for i in a[1:-1]:
        if i<0:
            print(a[-1],i)
            a[-1]-=i
        else:
            print(a[0],i)
            a[0]-=i
    print(a[-1],a[0])

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
a.sort()
print(sum(abs(i)for i in a)-2*abs(a[0]))
if n==2:
    if a[1]<0:a=a[::-1]
    print(a[1]-a[0])
    print(a[1],a[0])
elif a[0]>=0:
    print(a[0],a[-1])
    a[0]-=a[-1]
    for i in a[1:-2]:
        print(a[0],i)
        a[0]-=i
    print(a[-2],a[0])
elif a[-1]<=0:
    for i in a[:-1]:
        print(a[-1],i)
        a[-1]-=i
else:
    for i in a[1:-1]:
        if i<0:
            print(a[-1],i)
            a[-1]-=i
        else:
            print(a[0],i)
            a[0]-=i
    print(a[-1],a[0])

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
a.sort()
p,z,m=[],[],[]
for i in a:
    if i<0:m+=[i]
    elif i>0:p+=[i]
    else:z+=[i]
print(sum(abs(i)for i in a)-2*abs(a[0]))
if p and m:
    p=z+p
    for i in p[:-1]:
        print(m[0],i)
        m[0]-=i
    for i in m:
        print(p[-1],i)
        p[-1]-=i
elif p:
    p=z+p
    for i in p[:-2]:
        print(p[0],i)
        p[0]-=i
    print(p[-2],p[0])    
elif m:
    m=m+z
    for i in p[:-1]:
        print(p[-1],i)
        p[-1]-=i

Submission status: Wrong Answer

Solution code:
from collections import deque
import bisect

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

A.sort()

A = deque(A)
ans = []
for i in range(N-1):
    x = A.popleft()
    if A[0] < 0:
        y = A.pop()
        z = y - x
    else:
        y = A.popleft()
        z = x - y
    ans.append([x, y])
    bisect.insort(A, z)

print(abs(A[0]))
for i in range(len(ans)):
    if i == len(ans)-1:
        if A[0] < 0:
            x, y = ans[i]
            print(y, x)
            break
    print(*ans[i])

Submission status: Wrong Answer

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

a.sort()

def answer():
    p = sum(1 for i in a if i < 0)
    q = sum(1 for i in a if i >= 0)

    if p == 0:
        p += 1
        q -= 1
    if q == 0:
        p -= 1
        q += 1

    for i in range(q - 1):
        print(a[0], a[p + i])
        a[0] -= a[p + i]
    for i in range(p):
        print(a[-1], a[i])
        a[-1] -= a[i]
    # print(a[-1])


answer()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
x, y = a[0], a[-1]
l = []
for i in a[1:-1]:
    if i < 0:
        l.append([y, i])
        y -= i
    else:
        l.append([x, i])
        x -= i
print(y - x)
for i in l:
    print(*i)
print(y, x)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
A = LIST()

A.sort()

ans = []
if N-1 <= bisect(A, 0):
	tmp = A[-1]
	for i in range(2, N+1):
		ans.append((tmp, A[-i]))
		tmp -= A[-i]
else:
	idx = max(0, bisect(A, 0)-1)
	tmp = A[idx]
	for i in range(idx+1, N-1):
		ans.append((tmp, A[i]))
		tmp -= A[i]

	ans.append((A[-1], tmp))
	tmp = A[-1] - tmp

	for i in range(idx-1, -1, -1):
		print(i)
		ans.append((tmp, A[i]))
		tmp -= A[i]

print(tmp)
for x in ans:
	print(*x)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
A = deque(sorted(list(map(int,input().split()))))
ans = []
while n!=1:
  if n%2==0:
    a1 = A.pop()
    a2 = A.popleft()
    ans.append([a1,a2])
    a = a1-a2
    A.append(a)
    n -= 1
  else:
    a1 = A.pop()
    a2 = A.popleft()
    a = a2-a1
    ans.append([a2,a1])
    A.appendleft(a)
    n -= 1
print(A[0])
for j,k in ans:
  print(j,k)

Submission status: Wrong Answer

Solution code:
n=int(input())

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

a.sort()

query = []

high = a[-1]

low = a[0]

for i in range(1,n-1):
    if a[i] > 0:
        query.append([low, a[i]])
        low -= a[i]
    else:
        query.append([high, a[i]])
        high += a[i]

query.append([high, low])

print(high-low)

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
print(sum(a)-2*a[0])
b=a[0]
for i in range(1,n-1):
  print(b,a[i])
  b-=a[i]
print(a[-1],b)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N = in_n()
    A = in_nl()

    pa, ma = [], []
    for i in range(N):
        if A[i] >= 0:
            pa.append(A[i])
        else:
            ma.append(A[i])

    pa = deque(sorted(pa))
    ma = deque(sorted(ma))

    max_value = 0
    ans = []
    while pa or ma:

        if len(pa) >= 1 and len(ma) >= 1:
            if len(pa) == 1 and len(ma) == 1:
                p, m = pa.popleft(), ma.popleft()
                ans.append((p, m))
                max_value = p - m
            elif len(pa) >= 2 and len(ma) >= 1:
                p, m = pa.popleft(), ma[0]
                ans.append((m, p))
                ma[0] -= p
            elif len(pa) >= 1 and len(ma) >= 2:
                p, m = pa[0], ma.popleft()
                ans.append((p, m))
                pa[0] -= m
        elif len(pa) >= 2:
            if len(pa) == 2:
                p1, p2 = pa.popleft(), pa.popleft()
                p1, p2 = max(p1, p2), min(p1, p2)
                ans.append((p1, p2))
                max_value = p1 - p2
            else:
                p1, p2 = pa.popleft(), pa.popleft()
                ans.append((p1, p2))
                if (tp := p1 - p2) >= 0:
                    pa.appendleft(tp)
                else:
                    ma.append(tp)
        elif len(ma) >= 2:
            if len(ma) == 2:
                m1, m2 = ma.popleft(), ma.popleft()
                m1, m2 = max(m1, m2), min(m1, m2)
                ans.append((m1, m2))
                max_value = m1 - m2
            else:
                m1, m2 = ma.popleft(), ma.popleft()
                ans.append((m1, m2))
                pa.appendleft(m1 - m2)

    print(max_value)
    print('\n'.join(map(lambda a: '{} {}'.format(a[0], a[1]), ans)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# プラス　マイナス引いていく
plus=[]
minus=[]
zeros=[]
for a in A:
    if a==0:
        zeros.append(a)
    elif a>=1:
        plus.append(a)
    else:
        minus.append(a)
plus=sorted(plus)
minus=sorted(minus)
zeros=sorted(zeros)
ans=[]
if plus and minus:
    while len(plus)>1:
        now=plus.pop()
        ans.append((minus[0],now))
        minus[0]-=now
    while len(zeros)>0:
        now=zeros.pop()
        ans.append((plus[0],now))
    while len(minus)>1:
        now=minus.pop()
        ans.append((plus[0],now))
        plus[0]-=now
    ans.append((plus[0],minus[0]))
    ansnum=plus[0]-minus[0]
else:
    ansnum=0
    if len(plus)==0:
        if zeros:
            core=zeros.pop()
        else:
            core=minus.pop()
        while len(minus)>0:
            now=minus.pop()
            ans.append((core,now))
            core-=now
        ansnum=core
    else:
        if zeros:
            core=zeros.pop()
        else:
            plus=deque(plus)
            core=plus.popleft()
        while len(plus)>1:
            now=plus.pop()
            ans.append((core,now))
            core-=now
        ans.append((plus[0],core))
        ansnum=plus[0]-core
    while len(zeros)>0:
        now=zeros.pop()
        ans.append((core,now))
    
    

print(ansnum)
for t in ans:
    x,y=t
    print(x,y)

Submission status: Wrong Answer

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

# プラス　マイナス引いていく
plus=[]
minus=[]
zeros=[]
for a in A:
    if a==0:
        zeros.append(a)
    elif a>=1:
        plus.append(a)
    else:
        minus.append(a)
plus=sorted(plus)
minus=sorted(minus)
zeros=sorted(zeros)
ans=[]
if plus and minus:
    while len(plus)>1:
        now=plus.pop()
        ans.append((minus[0],now))
        minus[0]-=now
    while len(zeros)>0:
        now=zeros.pop()
        ans.append((plus[0],now))
    while len(minus)>1:
        now=minus.pop()
        ans.append((plus[0],now))
        plus[0]-=now
    ans.append((plus[0],minus[0]))
    ansnum=plus[0]-minus[0]
else:
    if len(plus)==0:
        if zeros:
            core=zeros.pop()
        else:
            core=minus.pop()
        while len(minus)>0:
            now=minus.pop()
            ans.append((core,now))
            core-=now
        ansnum=core
    else:
        if zeros:
            core=zeros.pop()
        else:
            plus=deque(plus)
            core=plus.popleft()
        while len(plus)>1:
            now=plus.pop()
            ans.append((core,now))
            core-=now
        ans.append((plus[0],core))
        ansnum=plus[0]-core
    while len(zeros)>0:
        now=zeros.pop()
        ans.append((core,now))
    
    

print(ansnum)
for t in ans:
    x,y=t
    print(x,y)

Submission status: Wrong Answer

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

def main():
    N = int(input())
    A = list(map(int, input().split()))
    neg = []
    pos = []
    for a in A:
        if a < 0:
            neg.append(a)
        else:
            pos.append(a)
    pos.sort()
    neg.sort(reverse = True)


    if len(neg)==0:
        print(sum(pos[1:])-pos[0])
        ret = pos[0]
        for i in range(1,len(pos)-1):
            print(ret, pos[i])
            ret -= pos[i]
        print(pos[-1],ret)
    elif len(pos)==0:
        print(-sum(neg[1:]) + neg[0])
        ret = neg[0]
        for i in range(1,len(neg)):
            print(ret, neg[i])
            ret -= neg[i]
    else:
        print(sum(pos)-sum(neg))
        ret = neg[0]
        for i in range(len(pos)-1):
            print(ret, pos[i])
            ret -= pos[i]
        print(pos[-1],ret)
        for i in range(1,len(neg)):
            print(ret, neg[i])
            ret -= neg[i]

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

# 最後の計算で(max)-(min)が必要
# max: 最大値 - ∑負の数
# min: 最小値 - ∑正の数

ans = []
low = A[0]
high = A[-1]

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

ans.append((high, low))

print(high - low)
for (x, y) in ans:
    print(x, y)

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort()
# Aneg = [a for a in A if a < 0]
# Apos = [a for a in A if a >= 0]
# Aneg.sort()
# Apos.sort()


# 方針
# greedyで負の数を最小化する
# greedyで正の数を最大化する
# 2つを引けばよい
# 最大だけ残せば良いかな

amax = A[-1]
# print(A, amax)
now = A[0]
ans = []
for i in range(1, N - 1):
    ans.append((now, A[i]))
    now = now - A[i]

print(amax - now)
for (a, b) in ans:
    print(a, b)
print(amax, now)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    if all([a > 0 for a in A]) or all([a < 0 for a in A]):
        pre = A[0]
        M = sum(A) - 2 * min(A)
        print(M)
        for i in range(1,N):
            if i != N - 1:
                print(pre, A[i])
                pre -= A[i]
            else:
                print(A[i], pre)
    else:
        cnt = 0
        M = 0
        for a in A:
            if a > 0:
                cnt += 1
            M += abs(a)
        print(M)
        if cnt == 1:
            pre = A[-1]
            for i in range(N-1):
                print(pre, A[i])
                pre -= A[i]
        else:
            pre = A[0]
            for i in range(1,N-1):
                if A[i] >= 0:
                    print(pre, A[i])
                    pre -= A[i]

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    if all([a > 0 for a in A]) or all([a < 0 for a in A]):
        pre = A[0]
        M = sum(A) - 2 * min(A)
        print(M)
        for i in range(1,N):
            if i != N - 1:
                print(pre, A[i])
                pre -= A[i]
            else:
                print(A[i], pre)
    else:
        cnt = 0
        M = 0
        for a in A:
            if a > 0:
                cnt += 1
            M += abs(a)
        print(M)
        if cnt == 1:
            pre = A[-1]
            for i in range(N-1):
                print(pre, A[i])
                pre -= A[i]
        else:
            pre = A[0]
            for i in range(N-1):
                if A[i] >= 0:
                    print(pre, A[i])
                    pre -= A[i]

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
A.sort()
P=0
Q=[]
if A[0]<=0 and A[-1]>=0:
  B,C=[],[]
  for i in range(N):
    if A[i]==0:
      if len(B)==0:
        B.append(0)
      else:
        C.append(0)
    else:
      if A[i]<0:
        B.append(A[i])
      else:
        C.append(A[i])
  for i in range(len(C)-1):
    Q.append([B[0],C[-1]])
    B[0]-=C[-1]
    del C[-1]
  for i in range(len(B)):
    Q.append([C[0],B[-1]])
    del B[-1]
  P=C[0]
else:
  if A[0]>0:
    for i in range(N-2,0,-1):
      Q.append([A[0],A[i]])
      A[0]-=A[i]
    Q.append([A[-1],A[0]])
    P=A[-1]-A[0]
  else:
    for i in range(N-1):
      Q.append([A[-1],A[i]])
      A[-1]-=A[i]
    P=A[-1]
print(P)
for i in range(N-1):
  print(*Q[i])

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()
A.sort()


def solve():
    small = A[0]
    res = []
    for i in range(1, N - 1):
        res.append((small, A[i]))
        small -= A[i]
    res.append((A[-1], small))
    print(A[-1] - small)
    for x, y in res:
        print(x, y)


solve()

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

a.sort()
mc = sum(item<0 for item in a)
ans = []
if mc==1 or mc==0:
    val = a[0]
    for i in range(n-2):
        ans.append((val, a[i+1]))
        val = val - a[i+1]
    ans.append((a[n-1], val))
    value = a[n-1] - val
elif mc<n:
    for i in range(mc-1):
        val = a[-1]
        ans.append((val, a[i]))
        val -= a[i]
    val2 = a[mc-1]
    for i in range(mc, n-1):
        ans.append((val2, a[i]))
        val2 -= a[i]
    ans.append((val, val2))
    value = val-val2
else:
    val = a[-1]
    for i in range(n-1):
        ans.append((val, a[i]))
        val -= a[i]
    value = val
        
print(value)
write("\n".join(" ".join(map(str, item)) for item in ans))

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

a.sort()
ans = []
val = a[0]
for i in range(n-2):
    ans.append((val, a[i+1]))
    val = val - a[i+1]
ans.append((a[n-1], val))
print(a[n-1] - val)
write("\n".join(" ".join(map(str, item)) for item in ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
mi = []
pl = []

l = list(map(int,input().split()))
for i in l:
    if i >= 0:
        pl.append(i)
    else:
        mi.append(i)
pl.sort()
mi.sort()
if n == 2:
    print(max(l)-min(l))
    print(max(l),min(l))
    exit()

if pl and mi:
    ans = []
    np = pl[-1]
    for i in range(1,len(mi)):
        ans.append([np,mi[i]])
        np -= mi[i]
    nm = mi[0]
    for i in range(len(pl)-1):
        ans.append([nm,pl[i]])
        nm -= pl[i]
    ans.append([np,nm])
    print(np-nm)
    for i in ans:
        print(*i)
elif pl:
    ans = []
    ans.append([pl[0],pl[1]])
    nm = pl[0]-pl[i]
    for i in range(2,len(pl)-1):
        ans.append([nm,pl[i]])
        nm -= pl[i]
    ans.append([pl[-1],nm])
    print(pl[-1]-nm)
    for i in ans:
        print(*i)
else:
    ans = []
    ans.append([mi[-1],mi[-2]])
    np = mi[-1]-mi[-2]
    for i in range(1,len(mi)-2):
        ans.append([np,mi[i]])
        np -= mi[i]
    ans.append([np,mi[0]])
    print(np-mi[0])
    for i in ans:
        print(*i)

Submission status: Wrong Answer

Solution code:
n = int(input())
mi = []
pl = []
l = list(map(int,input().split()))
for i in l:
    if i >= 0:
        pl.append(i)
    else:
        mi.append(i)
pl.sort()
mi.sort()
if n == 2:
    print(max(l)-min(l))
    exit()

if pl and mi:
    ans = []
    np = pl[-1]
    for i in range(1,len(mi)):
        ans.append([np,mi[i]])
        np -= mi[i]
    nm = mi[0]
    for i in range(len(pl)-1):
        ans.append([nm,pl[i]])
        nm -= pl[i]
    ans.append([np,nm])
    print(np-nm)
    for i in ans:
        print(*i)
elif pl:
    ans = []
    ans.append([pl[0],pl[1]])
    nm = pl[0]-pl[i]
    for i in range(2,len(pl)-1):
        ans.append([nm,pl[i]])
        nm -= pl[i]
    ans.append([pl[-1],nm])
    print(pl[-1]-nm)
    for i in ans:
        print(*i)
else:
    ans = []
    ans.append([mi[-1],mi[-2]])
    np = mi[-1]-mi[-2]
    for i in range(1,len(mi)-2):
        ans.append([np,mi[i]])
        np -= mi[i]
    ans.append([np,mi[0]])
    print(np-mi[0])
    for i in ans:
        print(*i)

Submission status: Wrong Answer

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

a.sort()

u,d,z = 0,0,0
uu,dd = [],[]
for i in range(n):
    if a[i] > 0:
        u += 1
        uu.append(a[i])
    elif a[i] < 0:
        d += 1
        dd.append(a[i])
    else:
        z += 1
        
dd = dd[::-1]
        
ans = []
if u == 0 and d == 0:
    for i in range(z):
        ans.append((0,0))
    cur = 0
    
elif d == 0:
    if z != 0:
        for i in range(z-1):
            ans.append((0,0))
        cur = 0
        for i in range(u-1):
            ans.append((cur,uu[i]))
            cur -= uu[i]
        ans.append((uu[u-1],cur))
        cur = uu[u-1] - cur
    else:
        cur = uu[0]
        for i in range(u-2):
            ans.append((cur,uu[i+1]))
            cur -= uu[i+1]
        ans.append((uu[u-1],cur))
        cur = uu[u-1] - cur
        
elif u == 0:
    if z != 0:
        for i in range(z-1):
            ans.append((0,0))
        cur = 0
        for i in range(d):
            ans.append((cur,dd[i]))
            cur -= dd[i]
    
    else:
        cur = dd[0]
        for i in range(d-1):
            ans.append((cur,dd[i+1]))
            cur -= dd[i+1]
            
else:
    for i in range(z):
        ans.append((uu[0],0))
    cur1 = uu[0]
    cur2 = dd[0]
    for i in range(d-1):
        ans.append((cur1,dd[i+1]))
        cur1 -= dd[i+1]
    for i in range(u-1):
        ans.append((cur2,uu[i+1]))
        cur2 -= uu[i+1]
        
    ans.append((cur1,cur2))
    cur = cur1 - cur2
    
print(cur)
for i in ans:
    print(*i)