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

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:
        dd = dd[::-1]
        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)

Submission status: Wrong Answer

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

neg = []
pos = []
zeros = []
for a in A:
    if a < 0:
        neg.append(a)
    elif a == 0:
        zeros.append(a)
    else:
        pos.append(a)

ans = []
for _ in range(len(zeros)-1):
    ans.append((0, 0))
if neg and pos:
    neg_base = neg.pop()
    pos_base = pos.pop()
elif pos:
    pos.sort(reverse=True)
    if zeros:
        x = 0
    else:
        x = pos.pop()
    y = pos.pop()
    ans.append((x, y))
    neg_base = x-y
    if pos:
        pos_base = pos.pop()
    else:
        print(y-x)
        for _ in range(len(zeros)-1):
            print(0, 0)
        print(y, x)
        if zeros:
            print(y-x, 0)
        exit()
elif neg:
    neg.sort()
    if zeros:
        x = 0
    else:
        x = neg.pop()
    y = neg.pop()
    ans.append((x, y))
    pos_base = x-y
    if neg:
        neg_base = neg.pop()
    else:
        print(x-y)
        for _ in range(len(zeros)-1):
            print(0, 0)
        print(x, y)
        if zeros:
            print(x-y, 0)
        exit()
else:
    print(0)
    for row in ans:
        print(*row)
    exit()


sum_ne = 0
for ne in neg:
    sum_ne += ne
    ans.append((pos_base, ne))
pos_base -= sum_ne
for po in pos:
    ans.append((neg_base, po))
    neg_base -= po
ans.append((pos_base, neg_base))
if zeros:
    ans.append((pos_base-neg_base, 0))

print(pos_base - neg_base)
for row in ans:
    print(*row)

Submission status: Wrong Answer

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

neg = []
pos = []
for a in A:
    if a < 0:
        neg.append(a)
    else:
        pos.append(a)

ans = []
if neg and pos:
    neg_base = neg.pop()
    pos_base = pos.pop()
elif pos:
    pos.sort(reverse=True)
    x = pos.pop()
    y = pos.pop()
    ans.append((x, y))
    neg_base = x-y
    if pos:
        pos_base = pos.pop()
    else:
        print(y-x)
        print(y, x)
        exit()
else:
    neg.sort()
    x = neg.pop()
    y = neg.pop()
    ans.append((x, y))
    pos_base = x-y
    if neg:
        neg_base = neg.pop()
    else:
        print(y-x)
        print(y, x)
        exit()


sum_ne = 0
for ne in neg:
    sum_ne += ne
    ans.append((pos_base, ne))
pos_base -= sum_ne
for po in pos:
    ans.append((neg_base, po))
    neg_base -= po
ans.append((pos_base, neg_base))

print(pos_base - neg_base)
for row in ans:
    print(*row)

Submission status: Wrong Answer

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

N=[]
head,num=A.popleft(),A.pop()
if num<0:
    N.append([num,head])
    num -=head
    for i in range(len(A)):
        y=A.popleft()
        N.append([num,y])
        num -=y
    print(num)
    for i in N:print(*i)
else:
    for i in range(len(A)):
        y=A.popleft()
        N.append([head,y])
        head -=y
    N.append([num,head])
    print(num-head)
    for i in N:print(*i)

Submission status: Wrong Answer

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

N=[]
head,num=A.popleft(),A.pop()
for i in range(len(A)):
    y=A.popleft()
    N.append([head,y])
    head -=y
N.append([num,head])
print(num-head)
for i in N:print(*i)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from collections import deque
N = int(input())
A = deque(sorted(list(map(int, input().split()))))
ans = deque()
while len(A) > 2:
  x = A.popleft()
  y = A.popleft()
  ans.append([x, y])
  A.insert(bisect_left(A, x-y), x-y)
y = A.popleft()
x = A.popleft()
ans.append([x, y])
M = x-y
print(M)
print("\n".join(["%d %d"%(a[0], a[1]) for a in ans]))

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = list(map(int,input().split()))
a.sort()
d = deque(a)
print(sum(a)-2*sum(a[:n//2]))
if n % 2 == 1:
    now = d.pop()
    for i in range(n-1):
        if i % 2 == 0:
            nex = d.popleft()
        elif i % 2 == 1:
            nex = d.pop()
        print(str(nex) + " " + str(now))
        now = nex - now
elif n % 2 == 0:
    now = d.popleft()
    for i in range(n-1):
        if i % 2 == 0:
            nex = d.pop()
        elif i % 2 == 1:
            nex = d.popleft()
        print(str(nex) + " " + str(now))
        now = nex - now

Submission status: Wrong Answer

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

res = []
up = a[-1]
low = a[0]
for i in range(1, n-1):
    if a[i] < 0:
        res.append([up, low])
        up = up - a[i]
    else:
        res.append([low, up])
        low = low - a[i]

print(up-low)
res.append([up, low])

for v in res:
    print(*v)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

res = []
while a:
    low = a.popleft()
    up = a.popleft()
    if len(a) == 0:
        res.append([up, low])
        print(up - low)
    else:
        a.appendleft(low - up)
        res.append([low, up])

for v in res:
    print(*v)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

res = []
while a:
    low = a.popleft()
    up = a.pop()
    if len(a) == 0:
        res.append([up, low])
        print(up - low)
    else:
        a.appendleft(low - up)
        res.append([low, up])

for v in res:
    print(*v)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = sorted(list(map(int, input().split())))

    que = deque(A)
    res = []
    while len(que) > 2:
        ma = que.pop()
        mi = que.popleft()
        res.append([mi, ma])
        que.appendleft(mi - ma)

    ma = que.pop()
    mi = que.popleft()
    res.append([ma, mi])
    que.appendleft(ma - mi)
    print(*que)
    for i in res:
        print(*i)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

if n==2:
    print(al[1]-al[0])
    print(al[1],al[0])
    exit()

res = []
neg = []
pos = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

if len(pos)==0:
    res.append((neg[0],neg[1]))
    pos.append(neg[0]-neg[1])
    neg = neg[2:]
if len(neg)==0:
    res.append((pos[0],pos[1]))
    neg.append(pos[0]-pos[1])
    pos = pos[2:]


# len(pos)>0 and len(neg)>0
n = neg[0]
for i in range(1,len(pos)):
    res.append((n,pos[i]))
    n = n - pos[i]
p = pos[0]
for i in range(1,len(neg)):
    res.append((p,neg[i]))
    p = p - neg[i]
res.append((p,n))
print(p-n)
for x,y in res:
    print(x,y)

Submission status: Wrong Answer

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

x = al[-1]
m = al[0]
res = []
for i in range(1,n-1):
    res.append((m,al[i]))
    m = m - al[i]
res.append((x,m))
m = x - m
print(m)
for x,y in res:
    print(x,y)

Submission status: Wrong Answer

Solution code:
def f():
    import sys
    input=sys.stdin.buffer.readlines
    sys.setrecursionlimit(10**9)
    from collections import defaultdict
    n,*l=input()
    l=sorted(map(int,l[0].split()))
    ret=[]
    n=int(n)
    c=n%2
    if c:
        ret.append((l[0],l[1]))
        now=l[0]-l[1]
    else:
        ret.append((l[1],l[0]))
        now=l[1]-l[0]
    for i in range(2,n):
        if i%2!=c:
            ret.append((l[i],now))
            now=l[i]-now
        else:
            ret.append((now,l[i]))
            now-=l[i]
    print(now)
    for x in ret:print(*x)
if __name__ == "__main__":
    f()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
arrays = []
a.sort()

b = []
c = []

for i in a:
    if i < 0:
        c.append(i)
    else:
        b.append(i)

print(b)
print(c)

if len(c) >= 1 and len(b) >= 1:
    while len(b) > 1:
        x = b.pop(0)
        array = [c[0], x]
        c[0] -= x
        arrays.append(array)
    while len(c) > 1:
        y = c.pop(0)
        array = [b[0], y]
        b[0] -= y
        arrays.append(array)

    array = [b[0], c[0]]
    arrays.append(array)

    print(b[0] - c[0])

elif len(b) == 0:
    mi = c.pop(-1)
    while len(c) > 1:
        x = c.pop(0)
        array = [mi, x]
        mi -= x
        arrays.append(array)
    array = [mi, c[0]]
    arrays.append(array)
    print(mi - c[0])


else:
    mi = b.pop(0)
    while len(b) > 1:
        x = b.pop(0)
        array = [mi, x]
        mi -= x
        arrays.append(array)
    array = [b[0], mi]
    arrays.append(array)
    print(b[0]-mi)


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

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
n = int(input())
A = list(map(int,input().split()))
A.sort()
index = bisect_left(A,0)
if index == n or index == n-1 or index == n-2:
    ans = A[n-1]
    for i in range(n-1):
        ans -= A[i]
    print(ans)
    now = A[n-1]
    for i in range(n-1):
        x = now
        y = A[i]
        print(x,y)
        now -= y
else:
    ans = -A[index]
    for i in range(index+1,n):
        ans += A[i]
    for i in range(index):
        ans -= A[i]
    print(ans)
    now = A[index]
    for i in range(index+1,n):
        x = now
        y = A[i]
        if i != n-1:
            print(x,y)
            now -= y
        else:
            print(y,x)
            now = y-x
    for i in range(index):
        x = now
        y = A[i]
        print(x,y)
        now -= y

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
n = int(input())
A = list(map(int,input().split()))
A.sort()
ans = -A[0]
for i in range(1,n):
    ans += A[i]
print(ans)
now = A[0]
for i in range(1,n):
    x = now
    y = A[i]
    if i != n-1:
        print(x,y)
    else:
        print(y,x)
    now -= y

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
n = int(input())
A = list(map(int,input().split()))
A.sort()
ans = -A[0]
for i in range(1,n):
    ans += A[i]
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
while(len(A) != 1):
    A = sorted(A)
    a = A.pop(0)
    b = A.pop(0)
    if (len(A) == 0):
        print(max(a, b), min(a,b))
        A.insert(0, max(a, b) - min(a,b))
    else:
        print(min(a, b), max(a, b))
        A.insert(0, min(a, b) - max(a, b))

print(A[0])

Submission status: Wrong Answer

Solution code:
def solve():
    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)
    
    neg.sort()
    pos.sort()
    
    op = []
    if len(neg) == N:
        pos.append(neg[-1])
        neg.pop()
    
    if len(pos) == N:
        neg.append(pos[-1])
        pos.pop()
    
    for i in range(len(pos)-1):
        op.append("{0} {1}".format(neg[0], pos[i]))
        neg[0] -= pos[i]
    
    for ne in neg:
        op.append("{0} {1}".format(pos[-1], ne))
        pos[-1] -= ne
    
    printAns(pos[-1], op)

def printAns(M, op):
    print(M)
    print(*op, sep='\n')
    
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
def solve():
    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)
    
    neg.sort()
    pos.sort()
    
    op = []
    if len(neg) == N:
        for i in range(len(neg)-1):
            op.append("{0} {1}".format(neg[-1], neg[i]))
            neg[-1] -= neg[i]
        
        printAns(neg[-1], op)
        return
    
    if len(pos) == N:
        for i in range(len(pos)-2):
            op.append("{0} {1}".format(pos[i+1], pos[i]))
            pos[i+1] = pos[i] - pos[i+1]
        
        op.append("{0} {1}".format(pos[-1], pos[-2]))
        
        printAns(pos[-1] - pos[-2],op)
        return
    
    for i in range(len(pos)-1):
        op.append("{0} {1}".format(neg[0], pos[i]))
        neg[0] -= pos[i]
    
    for ne in neg:
        op.append("{0} {1}".format(pos[-1], ne))
        pos[-1] -= ne
    
    printAns(pos[-1], op)

def printAns(M, op):
    print(M)
    print(*op, sep='\n')
    
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
def solve():
    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)
    
    neg.sort()
    pos.sort()
    
    op = []
    if len(neg) == N:
        for i in range(len(neg)-1):
            op.append("{0} {1}".format(neg[-1], neg[i]))
            neg[-1] -= neg[i]
        
        printAns(neg[-1], op)
        return
    
    if len(pos) == N:
        for i in range(len(pos)-1):
            op.append("{0} {1}".format(pos[i+1], pos[i]))
            pos[i+1] -= pos[i]
        
        printAns(pos[-1],op)
        return
    
    for i in range(len(pos)-1):
        op.append("{0} {1}".format(neg[0], pos[i]))
        neg[0] -= pos[i]
    
    for ne in neg:
        op.append("{0} {1}".format(pos[-1], ne))
        pos[-1] -= ne
    
    printAns(pos[-1], op)

def printAns(M, op):
    print(M)
    print(*op, sep='\n')
    
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
def solve():
    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)
    
    op = []
    if len(neg) == N:
        for i in range(len(neg)-1):
            op.append("{0} {1}".format(neg[-1], neg[i]))
            neg[-1] -= neg[i]
        
        printAns(neg[-1], op)
        return
    
    if len(pos) == N:
        for i in range(len(pos)-1):
            op.append("{0} {1}".format(pos[i+1], pos[i]))
            pos[i+1] -= pos[i]
        
        printAns(pos[-1],op)
        return
    
    for i in range(len(pos)-1):
        op.append("{0} {1}".format(neg[0], pos[i]))
        neg[0] -= pos[i]
    
    for ne in neg:
        op.append("{0} {1}".format(pos[-1], ne))
        pos[-1] -= ne
    
    printAns(pos[-1], op)

def printAns(M, op):
    print(M)
    print(*op, sep='\n')
    
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
from collections import deque

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

a.sort()

if a[0] >= 0:
    m = sum(a[1:]) - a[0]
    print(m)
    p = a.pop(0)
    while len(a) > 1:
        b = a.pop(0)
        print(*[p, b])
        p -= b
    b = a.pop()
    print(*[b, p])
elif a[-1] <= 0:
    m = -sum(a[0:n-1])+a[n-1]
    print(m)
    p = a.pop(-1)
    while len(a) > 0:
        b = a.pop(0)
        print(*[p, b])
        p -= b
else:
    mi = list(filter(lambda x: x < 0, a))
    pl = list(filter(lambda x: x > 0, a))
    m = -sum(mi) + sum(pl)
    print(m)
    lp = pl.pop(0)
    mi = deque(mi)
    while len(pl) > 0:
        b = pl.pop()
        c = mi.pop()
        print(*[c, b])
        c -= b
        mi.append(c)
    while len(mi) > 0:
        b = mi.pop()
        print(*[lp, b])
        lp -= b

Submission status: Wrong Answer

Solution code:
def all_pos(lst):
    for e in lst:
        if e < 0:
            return False
    return True

def all_neg(lst):
    for e in lst:
        if e > 0:
            return False
    return True

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

ans = []
M = 0
if all_pos(A):
    A.sort()
    res = A[0]
    for i in range(1, N - 1):
        ans.append((res, A[i]))
        res -= A[i]
    ans.append((A[-1], res))
    M = A[-1] - res
elif all_neg(A):
    A.sort()
    res = A[0]
    for i in range(1, N):
        ans.append((res, A[i]))
        res -= A[i]
    M = res
else:
    A.sort()
    A.reverse()
    tmp_neg = A[-1]
    tmp_pos = A[0]
    for i in range(1, len(A) - 1):
        if A[i] >= 0:
            ans.append((tmp_neg, A[i]))
            tmp_neg -= A[i]
        else:
            ans.append((tmp_pos, A[i]))
            tmp_pos -= A[i]
    ans.append((tmp_pos, tmp_neg))
    M = tmp_pos - tmp_neg
    
print(M)
for e in ans:
    print(e[0], e[1])

Submission status: Wrong Answer

Solution code:
import sys
#import numpy as np
import math
#from fractions import Fraction
#import itertools
#from collections import deque
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
M=max(a)
m=min(a)
resm=m
resM=M
c_M=False
c_m=False
ans=[]
for i in range(n):
    if a[i]==M and c_M==False:
        c_M=True
        continue
    elif a[i]==m and c_m==False:
        c_m=True
        continue
    if a[i]>0:
        ans.append((resm,a[i]))
        resm=resm-a[i]
    else:
        ans.append((resM,a[i]))
        resM=resM-a[i]
print(resM-resm)
for i in ans:
    print(*i)
    print(resM,resm)

Submission status: Wrong Answer

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
from collections import deque
inf = 10**18
mod = 10**9+7

n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
fir = a.pop()
sec = a.pop()
ans = []
ansnum = sec-fir
for i in range(n-2):
    tmp = a.pop()
    ans.append([fir, tmp])
    ansnum += tmp
    fir = fir-tmp
ans.append([sec, fir])
print(ansnum)
for p in ans:
    print(*p, sep=' ')

Submission status: Wrong Answer

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
from collections import deque
inf = 10**18
mod = 10**9+7

n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
fir = a.pop()
sec = a.pop()
ans = []
ansnum = -fir
for i in range(n-2):
    tmp = a.pop()
    ans.append([fir, tmp])
    fir = fir-tmp
    ansnum+=tmp
ans.append([sec,fir])
ansnum+=sec
print(ansnum)
for p in ans:
    print(*p, sep=' ')

Submission status: Wrong Answer

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

now = A[0] - A[1]
ans = [[A[0], A[1]]]
for i in range(2,N):
    ans.append([A[i], now])
    now = A[i] - now
    
print(now)
for a in ans:
    print(*a, sep=" ")

Submission status: Wrong Answer

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

if n == 2:
    print(li[0]-li[1])
    print(li[0],li[1])
    exit()

ans = []
for _ in range(n-2):
    a = li.pop(-1)
    b = li.pop(-1)
    ans.append((a,b))
    tmp = a - b
    li.append(tmp)
#print(li)
print(li[0]-li[1])
for a in ans:
    print(a[0],a[1])
print(li[0],li[1])

Submission status: Wrong Answer

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

if n == 2:
    print(li[0]-li[1])
    exit()

ans = []
for _ in range(n-2):
    a = li.pop(-1)
    b = li.pop(-1)
    ans.append((a,b))
    tmp = a - b
    li.append(tmp)
#print(li)
print(li[0]-li[1])
for a in ans:
    print(a[0],a[1])
print(li[0],li[1])

Submission status: Wrong Answer

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

a.sort()
if a[0] * a[-1] <= 0:
    total = 0
    for i in a:
        total += abs(i)
    print(total)
    now = a[-1]
    for i in range(0, n - 2):
        if a[i + 1] > 0:
            print(a[i], now)
            now = a[i] - now
        else:
            print(now, a[i])
            now -= a[i]
    if a[-2] > 0:
        print(a[-2], now)
    else:
        print(now, a[-2])

elif a[0] > 0:
    total = sum(a) - 2 * a[0]
    print(total)
    now = a[0]
    for i in range(1, n - 1):
        print(now, a[i])
        now -= a[i]
    print(a[-1], now)

else:
    total = -1 * sum(a) + 2 * a[-1]
    print(total)
    now = a[n - 1]
    for i in range(n - 2, -1, -1):
        print(now, a[i])
        now -= a[i]

Submission status: Wrong Answer

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

a.sort()
if a[0] * a[-1] <= 0:
    total = 0
    for i in a:
        total += abs(i)
    print(total)
    now = a[-1]
    for i in range(0, n - 2):
        if a[i + 1] >= 0:
            print(a[i], now)
            now = a[i] - now
        else:
            print(now, a[i])
            now -= a[i]
    if a[-2] >= 0:
        print(a[-2], now)
    else:
        print(now, a[-2])

elif a[0] > 0:
    total = sum(a) - 2 * a[0]
    print(total)
    now = a[0]
    for i in range(1, n - 1):
        print(now, a[i])
        now -= a[i]
    print(a[-1], now)

else:
    total = -1 * sum(a) + 2 * a[-1]
    print(total)
    now = a[n - 1]
    for i in range(n - 2, -1, -1):
        print(now, a[i])
        now -= a[i]

Submission status: Wrong Answer

Solution code:
import heapq
N = int(input())
tmp = list(map(int, input().split()))
q = []
for _ in range(N):
    heapq.heappush(q, -tmp.pop())

orig = -heapq.heappop(q)

MAX_TAS = (N - 1) // 2
TAS = []
for _ in range(MAX_TAS):
    if -q[0] > 0:
        TAS.append(-heapq.heappop(q))
    else:
        break

print(orig + sum(TAS) + sum(q))

HIK = []

while TAS:
    a = -q.pop()
    b = TAS.pop()
    print(a, b)
    HIK.append(a-b)

while HIK:
    tmp = HIK.pop()
    print(orig, tmp)
    orig -= tmp

while q:
    tmp = -q.pop()
    print(orig, tmp)
    orig -= tmp

Submission status: Wrong Answer

Solution code:
import heapq
N = int(input())
tmp = list(map(int, input().split()))
q = []
for _ in range(N):
    heapq.heappush(q, -tmp.pop())

tas = [-heapq.heappop(q)]

for _ in range(-(-N//2) - 1):
    if -q[0] > 0:
        tas.append(-heapq.heappop(q))
    else:
        break

print(sum(tas) + sum(q))

orig = tas.pop()
hiku = []
while tas:
    a = tas.pop()
    b = -q.pop()
    print(b,a)
    hiku.append(b - a)

for _ in range(len(hiku)):
    c = hiku.pop()
    print(orig, c)
    orig -= c

for _ in range(len(q)):
    c = -q.pop()
    print(orig, c)
    orig -= c

Submission status: Wrong Answer

Solution code:
N=int(input())
plus=[]
other=[]
d=list(map(int,input().split()))
for num in d:
    if num>0:
        plus.append(num)
    else:
        other.append(num)
if len(plus)>0:
    K=len(plus)
    if K==1:
        data=[]
        ans=plus[0]
        for q in other:
            data.append((ans,q))
            ans+=abs(q)
        print(ans)
        for some in data:
            x,y=some
            print(x,y)

    else:
        data=[]
        if len(other)>=1:
            ans=other[-1]
            for some in plus[1:]:
                data.append((ans,some))
                ans-=some
            other[-1]=ans
            ans=plus[0]
            for q in other:
                data.append((ans,q))
                ans+=abs(q)
            print(ans)
            for some in data:
                x,y=some
                print(x,y)
        else:
            plus.sort(reverse=True)
            ans=plus[0]
            for some in plus[1:-1]:
                data.append((ans,some))
                ans-=some
            data.append((plus[-1],ans))
            ans=plus[-1]-ans
            print(ans)
            for some in data:
                x,y=some
                print(x,y)
            
elif len(plus)==0:
    other.sort(reverse=True)
    data=[]
    ans=other[0]
    for q in other[1:]:
        data.append((ans,q))
        ans+=abs(q)
    print(ans)
    for some in data:
        x,y=some
        print(x,y)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    A = LI()

    A.sort(reverse=True)
    x = A[-1]
    p = []
    for i in range(1,len(A)-1):
        p.append((x,A[i]))
        x -= A[i]
    p.append((A[0],x))
    print(A[0]-x)
    for a in p:
        print(a[0],a[1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

print(sum(A) - 2*A[-1])

for _ in range(N-2):
    x = A.pop()
    y = A.pop()
    print(x, y)
    A.append(x-y)

print(A[0], A[1])

Submission status: Wrong Answer

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

xy = []
for i in range(N-1):
    if len(A)%2 == 1:
        x = A.pop()
        y = A.pop()
        xy.append([x,y])
        A.append(x-y)
    else:
        y = A.pop()
        x = A.pop()
        xy.append([x,y])
        A.append(x-y)

print(A[0])
for ans in xy:
    print(*ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import deque
def main():
    n=int(input())
    A=list(map(int,input().split()))
    A.sort(reverse=True)
    Large=deque([a for a in A if a>=0])
    Small=deque([a for a in A if a<0])
    if not Large:Large.append(Small.pop())
    if not Small:Small.append(Large.pop())
    ans=[]
    while len(Small)>=2:
        a=Large.pop()
        b=Small.pop()
        ans.append((a,b))
        Large.append(a-b)
    small=Small.pop()
    while len(Large)>=2:
        a=Large.pop()
        ans.append((small,a))
        small-=a
    large=Large.pop()
    ans.append((large,small))
    print(large-small)
    for a,b in ans:
        print(a,b)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import *

n = int(input())
a = list(map(int, input().split()))
heapify(a)
r = []
while len(a)> 1:
    x = heappop(a)
    y = a.pop()
    r.append([y, x])
    heappush(a, x-y)

print(-a[0])
for i in r[:-1]:
    print(*i)
if min(r[-1]) >= 0:
    print(r[-1][1], r[-1][0])
else:
    print(*r[-1])