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:
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort()
d = deque(A)
if A[0] <= 0 and A[-1] > 0:
    print(sum(map(abs, A)))
    while len(d) > 2:
        while d[1] < 0:
            x = d.pop()
            y = d.popleft()
            print(x, y)
            d.append(x - y)
        x = d.popleft()
        y = d.pop()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])

elif A[0] > 0:
    print(sum(A) - 2 * A[0])
    x = d.popleft()
    y = d.pop()
    print(x, y)
    d.appendleft(x - y)
    while len(d) > 2:
        x = d.popleft()
        y = d.pop()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])
else:
    print(-sum(A) + 2 * A[-1])
    x = d.pop()
    y = d.popleft()
    print(x, y)
    d.append(x - y)
    while len(d) > 2:
        while d[1] < 0 and len(d) > 2:
            x = d.pop()
            y = d.popleft()
            print(x, y)
            d.append(x - y)
        x = d.popleft()
        y = d.pop()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
A.sort()

M = A[-1] - (A[0] - sum(A[1:-1]))
print(M)
tmp = A[0]
for i in range(1, N - 1):
    print(tmp, A[i])
    tmp -= A[i]
print(A[-1], 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):
        if i % 2 == 0:
            al.append([a[-(1+i)],a[i]])
            aa.append(a[-(1+i)]-a[i])
        else:
            al.append([a[i],a[-(1+i)]])
            aa.append(a[i]-a[-(1+i)])
    su = sum(map(abs,aa))
    while True:
        if len(aa) == 1:
            break
        if len(aa) == 2:
            al.append([max(aa),min(aa)])
            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:
    if a[n//2] <= 0:
        for i in range(n//2):
            if i % 2 == 0:
                al.append([a[-(1+i)],a[i]])
                aa.append(a[-(1+i)]-a[i])
            else:
                al.append([a[i],a[-(1+i)]])
                aa.append(a[i]-a[-(1+i)])
        aa.append(a[n//2])
        su = sum(map(abs,aa))
        while True:
            if len(aa) == 1:
                break
            if len(aa) == 2:
                al.append([max(aa),min(aa)])
                break
            else:
                z = aa.pop()
                zz = aa.pop()
                al.append([z,zz])
                aa.append(z-zz)
    else:
        for i in range(n//2):
            if i % 2 == 0:
                al.append([a[i],a[-(1+i)]])
                aa.append(a[i]-a[-(1+i)])
            else:
                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
            if len(aa) == 2:
                al.append([max(aa),min(aa)])
                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:
import bisect 

n=int(input())
l=list(map(int,input().split()))
l.sort()
#dq=deque(l)
if l[0]>=0:
    print('hello')
    a=l[0]
    ans=0
    for i in range(n):
        ans+=l[i]
    print(ans-2*l[0])
    for i in range(1,n-1):
        print(a,l[i])
        a-=l[i]
    print(l[-1],a)
elif l[-1]<=0:
    #print('hello')
    a=l[-1]
    ans=l[-1]
    for i in range(n-1):
        ans-=l[i]
    print(ans)
    for i in range(n-1):
        print(a,l[i])
        a-=l[i]
else:
    #print('hello')
    idx=bisect.bisect(l,0)
    ans=0
    for i in range(n):
        if l[i]>=0:
            ans+=l[i]
        else:
            ans-=l[i]
    print(ans)
    a=l[0]
    for i in range(idx,n-1):
        print(a,l[i])
        a-=l[i]
    for i in range(1,a):
        print(l[-1],l[i])
    print(l[-1],a)

Submission status: Wrong Answer

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

high_ls=ls[int(n/2):]
low_ls=ls[:int(n/2)]

print(sum(high_ls)-sum(low_ls))

if n%2==0:
    tmp=low_ls.pop(0)
    left_ls=high_ls
    for i in range(n-1):
        lefter=left_ls.pop(0)
        print(lefter,tmp)
        tmp=lefter-tmp
        if left_ls==high_ls:
            left_ls=low_ls
        else:
            left_ls=high_ls
else:
    tmp=high_ls.pop(0)
    left_ls=low_ls
    for i in range(n-1):
        lefter=left_ls.pop(0)
        print(lefter,tmp)
        tmp=lefter-tmp
        if left_ls==high_ls:
            left_ls=low_ls
        else:
            left_ls=high_ls

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
#print(a)
s=[]
ans=[]
i=2
while i!=n:
    z=min(a)-max(a)
    s.append(min(a))
    s.append(max(a))
    ans.append(s)
    a.remove(min(a))
    a.remove(max(a))
    a.append(z)
    s=[]
    i+=1
#print(z)
#print(a)
print(max(a)-min(a))
s.append(max(a))
s.append(min(a))
ans.append(s)
i=0
while i!=len(ans):
    print(ans[i][0]," ",ans[i][0])
    i+=1

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
ans = []
while len(arr) != 1:
  x = arr.pop()
  y = arr.pop()
  ans.append([x, y])
  arr.append((x-y))
  arr.sort()
print(abs(arr[0]))  
[print(x, y) for x, y in ans]

Submission status: Wrong Answer

Solution code:
# div2019-2C - Successive Subtraction
n = int(input())
lst = sorted(list(map(int, input().rstrip().split())))
ans = lst[0]
log = []
for i in lst[1:-1]:
    log += [(ans, i)]
    if i > 0:
        ans -= i
    else:
        ans += i
log += [(lst[-1], ans)]
print(lst[-1] - ans)
for i, j in log:
    print(i, j)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

from collections import defaultdict

N = int(input())
X = list(map(int, input().rstrip().split(' ')))
X = sorted(X)

count_minus = sum([1 if x < 0 else 0 for x in X])

if N == 2:
    print(X[1] - X[0])
    print(X[1], X[0])
else:
    ope = []
    if count_minus == N:
        max_value = X[-1] - X[0]
        min_value = X[2]
        ope.append([X[-1], X[0]])
        start = 2
        end = N - 2
    elif count_minus == 0:
        min_value = X[0] - X[-1]
        max_value = X[-2]
        ope.append([X[0], X[-1]])
        start = 2
        end = N - 2
    else:
        min_value = X[0]
        max_value = X[-1]
        start = 1
        end = N-1
     
    for i in range(start, end):
        if X[i] < 0:
            ope.append([max_value, X[i]])
            max_value -= X[i]
        else:
            ope.append([min_value, X[i]])
            min_value -= X[i]
    ope.append([max_value, min_value])
    
    print(max_value - min_value)
    for i in range(len(ope)):
        a, b = ope[i]
        print(a, b)

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):
  print(answer,A[i])
  answer = answer - A[i]
print(A[N-1],answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

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

queue = sorted(a)

x_list = list()
y_list = list()

while len(queue) != 1:
    if len(queue) > 2:
        head3 = queue[:3]
        head3.sort()
        x = head3[1]
        y = head3[0]
        x_list.append(x)
        y_list.append(y)
        queue = [y - x, head3[2]]
        queue.extend(queue[3:])
    else:
        queue.sort()
        x = queue[1]
        y = queue[0]
        x_list.append(x)
        y_list.append(y)
        queue = [x - y]

print(queue[0])
for x, y in zip(x_list, y_list):
    print(x, y)

Submission status: Wrong Answer

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

if N%2==1:
	for i in range(N//2):
		print(A[i],A[N-i-2])
		B.append(A[i]-A[N-2-i])
	B=sorted(B)
	B.append(A[N-1])
else:
	for i in range(1,N//2):
		print(A[i],A[N-i-1])
		B.append(A[i]-A[N-1-i])
	print(A[-1],A[0])
	B=sorted(B)
	B.append(A[-1]-A[0])

for i in range(len(B)-1):
	print(B[-1],B[i])
	B[-1]=B[-1]-B[i]

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
AM = A.pop(0)
xy = []
while(len(A)>1):
    x = A.pop(-1)
    y = A.pop(0)
    xy.append([x, y])
    A.append(x-y)
xy.append([AM, A[0]])
ans = AM - A[0]
print(ans)
for out in xy:
    print(*out)

Submission status: Wrong Answer

Solution code:
import sys, math, collections, heapq, itertools
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if A[1] >= 0 or A[N-2] < 0:
        ans = A[0] - A[1]
        out.append([A[0], A[1]])
        for i in range(2, N - 1):
            out.append([ans, A[i]])
            ans -= A[i]
        out.append([A[N-1], ans])
        ans = A[N - 1] - ans
    else:
        for i in range(1, N):
            if A[i] >= 0: 
                start = i - 1
                break
        ans = A[0] - A[start + 1]
        out.append([A[0], A[start + 1]])
        for i in range(start + 2, N):
            out.append([ans, A[i]])
            ans -= A[i]
        out.append([A[start], ans])
        ans = A[start] - ans
        for i in range(1, start):
            out.append([ans, A[i]])
            ans -= A[i]


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

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
li=[]
for i in range(n-1):
    if i!=n-2:
        mi=a[0]
        ma=a[n-1-i]
        ans=mi-ma
        a.pop(n-1-i)
        a.pop(0)
        a=[ans]+a
        li+=[[mi,ma]]
    else:
        li+=[[a[1],a[0]]]
        ans=a[1]-a[0]
print(ans)
for i in range(n-1):
    print(li[i][0],li[i][1])

Submission status: Wrong Answer

Solution code:
import collections

def solve():
  n = int(input())
  a = collections.deque(sorted(list(int(i) for i in input().split())))
  big = a.pop()
  mini = a.popleft()
  queue = collections.deque([])
  #print(big,mini)
  while (a):
    if a[0] <= 0:
      tmp = a.popleft()
      queue.append((big,tmp))
      big -= tmp
    else:
      tmp = a.pop()
      queue.append((tmp,mini))
      mini -= tmp
  print(big-mini)
  queue.append((big,mini))
  for i in range(len(queue)):
    print(*queue[i])
  
solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
if a[0] >= 0:
    print(sum(a) - min(a))
    if n == 2:
        print(a[1], a[0])
    else:
        print(a[0], a[1])
        now = a[0] - a[1]
        for i in range(2, n-1):
            print(now, a[i])
            now = now - a[i]
        print(a[-1], now)
elif a[-1] <= 0:
    print(sum(map(abs, a)) + max(a))
    b = a[::-1]
    print(a[0], a[1])
    now = a[0] - a[1]
    for i in range(2, n):
        print(now, a[i])
        now -= a[i]
else:
    print(sum(map(abs, a)))
    if n == 2:
        print(a[1], a[0])
        exit()
    hu = list(filter(lambda x: x<0, a))
    se = list(filter(lambda x: x>=0, a))
    if len(se) == 1:
        print(a[-1], a[0])
        now = a[-1] - a[0]
        for i in range(1, n-1):
            print(now, a[i])
            now = now - a[i]
    else:
        print(hu[0], se[0])
        now = hu[0] - se[0]
        for i in range(1, len(se)-1):
            print(now, se[i])
            now -= se[i]
        print(se[-1], now)
        now = se[-1] - now
        for i in range(1, len(hu)):
            print(now, hu[i])
            now -= hu[i]

Submission status: Wrong Answer

Solution code:
N = int( input())
A = list( map( int, input().split()))
A.sort(reverse=True)
if N%2 == 0:
    print( sum(A[:N//2]) - sum(A[N//2:]))
    now = A[0]
    print(now, A[N-1])
    now -= A[N-1]
    for i in range(2,N):
        if i%2 == 0:
            print(A[N-1-i//2], now)
            now = A[N-1-i//2] - now
        else:
            print(A[i//2], now)
            now = A[i//2] - now

else:
    k = N//2
    t = 1
    if A[k] < 0:
        t = 0
    print( sum(A[:k+t]) - sum(A[k+t:]))
    if t == 1:
        now = A[N-1]
        print(now, A[0])
        now = now - A[0]
        for i in range(2, N):
            if i%2 == 0:
                print(A[i//2], now)
                now = A[i//2] - now
            else:
                print(A[N-1-i//2], now)
                now = A[N-1-i//2] - now
    else:
        now = A[0]
        print(now, A[N-1])
        now -= A[N-1]
        for i in range(2, N-1):
            if i%2 == 0:
                print(A[N-1-i//2], now)
                now = A[N-1-i//2] - now
            else:
                print(A[i//2], now)
                now = A[i//2] - now
        print(now, A[k])

Submission status: Wrong Answer

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

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

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

    if ans is None:
        ans = ans_e
    else:
        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:
N = int(input())
A = [int(s) for s in input().split(' ')]
A.sort()
print(sum(A[1:]) - A[0])
n = len(A) - 1
while(n > 1):
    op = (A.pop(0), A.pop(n - 1))
    A = [op[0] - op[1]] + A
    print("{} {}".format(op[0], op[1]))
    n -= 1
print(A[1], A[0])

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = sorted(list(map(int, input().split())))
B = np.array(A)
tmp = set(B)
if len(tmp) > 1:
    print(sum(np.abs(B)))
else:
    print(3 * A[0])

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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

# for i in range(n-1):

tmp = 0
history = []
for i in reversed(range(n - 1)):
    if i == n-2:
        minNum = x.pop(x.index(min(x)))
        maxNum = x.pop(x.index(max(x)))
        tmp += minNum - maxNum
        history.append([minNum, maxNum])
    elif i != 0:
        maxNum = x.pop(x.index(max(x)))
        tmp += tmp - maxNum
        history.append([tmp, maxNum])
    else:
        print(min(x) - tmp)
        for i in history:
            print(i[0], i[1])
        print(min(x), tmp)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

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

queue = sorted(a)

x_list = list()
y_list = list()

while len(queue) != 1:
    if len(queue) > 2:
        head3 = queue[:3]
        head3.sort()
        x = head3[1]
        y = head3[0]
        x_list.append(x)
        y_list.append(y)
        queue = [y - x, head3[2]]
        queue.extend(queue[3:])
    else:
        queue.sort(reverse=True)
        x = queue[1]
        y = queue[0]
        x_list.append(x)
        y_list.append(y)
        queue = [y - x]

print(queue[0])
for x, y in zip(x_list, y_list):
    print(x, y)

Submission status: Wrong Answer

Solution code:
M = int(input())
A = [int(i) for i in input().split()]
A.sort()
ans = abs(A[0] - sum(A[1:]))
print(ans)
tmp = A[0]
for i in range(M-1):
    if i < M-2:
        print(tmp, A[i+1])
        tmp -= A[i+1]
    else:
        print(A[-1], tmp)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n = int(input())
a = list(map(int, input().split()))
list.sort(a, reverse=True)
hist = []
for i in range(n - 1):
    max_a = a.pop(0)
    min_a = a.pop()
    if i < n - 2:
        hist.append([min_a, max_a])
        a.append(min_a - max_a)
    else:
        hist.append([max_a, min_a])
        a.append(max_a - min_a)
print(a[0])
for i in hist:
    print("{} {}".format(i[0], i[1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [0]*N
A = list(map(int, input().split()))
ans = int(0)
x = int(0)
y = int(0)

As = sorted(A)

for i in range(N):
    ans += As[i]

ans -=As[0]*2
print(ans)

x = As[0]
for i in range(N-1):
    y = As[i+1]
    print(x,y)
    x = x - y

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
plus=[a for a in A if a>0]
mainus=[a for a in A if a<=0]
plus.sort()
if len(mainus)>0:
    print(sum(plus)-sum(mainus))
    now=mainus[0]
    for i in range(len(plus)-1):
        print(str(now)+" "+str(plus[i]))
        now-=plus[i]
    if len(plus)>0:
        print(str(plus[-1])+" "+str(now))
        now=plus[-1]-now
    for i in range(1,len(mainus)):
        print(str(now)+" "+str(mainus[i]))
        now-=mainus[i]
else:
    print(sum(plus)-2*plus[0])
    now=plus[0]
    for i in range(1,len(plus)-1):
        print(str(now)+" "+str(plus[i]))
        now-=plus[i]
    print(str(plus[-1])+" "+str(now))

Submission status: Wrong Answer

Solution code:
import bisect

N = int(input())
A = list(map(int,input().split()))
A.sort()
ans = 0
if N == 2:
    print(str(A[1]-A[0]))
    print(str(A[1]) + " " + str(A[0]))
else:
    if A[0] <= 0 and A[-1] >= 0:
        for i in range(N):
            ans += abs(A[i])
        print(str(ans))
        boarder = bisect.bisect_left(A, 0)
        if boarder != 0:
            print(str(A[boarder-1]) + " " + str(A[boarder]))
            x = A[boarder-1] - A[boarder]
        else:
            x = 0
        for i in range(boarder+1, N-1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
        print(str(A[-1]) + " " + str(x))
        x = A[-1] - x
        for i in range(boarder-2, -1, -1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
    elif A[0] <= 0:
        print(str(abs(sum(A))+A[-1]*2))
        
        print(str(A[N-1]) + " " + str(A[N-2]))
        x = A[N-1] - A[N-2]
        for i in range(N-3, -1, -1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
        
    else:
        print(str(sum(A)-A[0]*2))

        print(str(A[0]) + " " + str(A[1]))
        x = A[0] - A[1]
        for i in range(2, N-1):
            print(str(x) + " " + str(A[i]))
            x -= A[i]
        print(str(A[-1]) + " " + str(x))

Submission status: Wrong Answer

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

switch=0
mazu=newlist[0]
if newlist[0]>=0:
    seihu=1
else:
    seihu=-1
konzai=0

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

if switch==1:
    wa=0
    list2=[]
    for nl in range(n):
        list2.append([newlist[nl], abs(newlist[nl])])
        if seihu*newlist[nl]<0:
            konzai=1
            numb=nl
    if konzai==1:
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        print(wa)
        now=newlist[numb]
        print(now,newlist[0])
        now=abs(now)+abs(newlist[0])
        for i in range(n-1):
            if i+1!=numb:
                if newlist[i+1]<0:
                    print(now,newlist[i+1])
                    now=abs(now)+abs(newlist[i+1])
                else:
                    print(now*(-1),newlist[i+1])
                    now=abs(now)+abs(newlist[i+1])
    elif seihu==1:
        newlist=sorted(newlist)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa-=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            print(now, newlist[ll+2])
            now=-(abs(now)+abs(newlist[ll+2]))
    elif seihu==-1:
        newlist=sorted(newlist, reverse=True)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa+=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            print(now, newlist[ll+2])
            now=(abs(now)+abs(newlist[ll+2]))
    
else:
    print(abs(mazu*(len(newlist)-2)))
    for i in range(len(newlist)-1):
        print(mazu*(1-i), mazu)

Submission status: Wrong Answer

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

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

ans = []
while len(B) > 1:
    x = B.pop()
    y = B.pop()
    if y >= 0:
        idx = bisect(B, x - y)
        B = B[:idx] + [x - y] + B[idx:]
        ans.append([x, y])
    else:
        idx = bisect(B, y - x)
        B = B[:idx] + [y - x] + B[idx:]
        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:
n = int(input())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
ans = []
while len(arr) != 1:
  x = arr.pop()
  y = arr.pop()
  ans.append([x, y])
  arr.append((x-y))
  arr.sort(reverse=True)
print(abs(arr[0]))  
[print(x, y) for x, y in ans]

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
q = deque(A)
max_tmp = q.pop()
min_tmp = q.popleft()
steps = list()

while len(q) > 1:
    max_tmp2 = q.pop()
    min_tmp2 = q.popleft()
    steps.append((min_tmp2, max_tmp2))
    min_tmp2 -= max_tmp2
    q.appendleft(min_tmp2)

if q:
    max_tmp2 = q.pop()
    steps.append((min_tmp, max_tmp2))
    min_tmp -= max_tmp2

steps.append((min_tmp, max_tmp))
m = max_tmp - min_tmp
print(m)
for x, y in steps:
    print('{} {}'.format(x, y))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())))
ans = sum(A[(N//2):]) +sum(list(map(lambda x: x * (-1), A[:(N//2)])))
print(ans)
if N % 2 == 0:
    temp = A[0]
    m = 1
    p = -1
    for i in range(N-1):
        if i % 2 == 0:
            print(A[p], temp)
            temp = A[p] - temp
            p -= 1
        else:
            print(A[m], temp)
            temp = A[m] - temp
            m += 1
else:
    temp = A[-1]
    m = 0
    p = -2
    for i in range(N-1):
        if i % 2 == 0:
            print(A[m], temp)
            temp = A[m] - temp
            m += 1
        else:
            print(A[p], temp)
            temp = A[p] - temp
            p -= 1

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import defaultdict
from functools import reduce

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

k = max(N//2, len(list(filter(lambda x: x < 0, A)))) # マイナスをつける個数
t = N-k
s = set(range(N))
res = []

if N%2 == 0:
    M = A[0]-A[N-1]
    res.append((A[0], A[N-1]))
    t -= 1
    s.remove(0)
    s.remove(N-1)
else:
    M = A[0]
    t -= 1
    s.remove(0)

B = []

for i in range(t):
    if N%2 == 0:
        j = N-1-i-1
    else:
        j = N-1-i
    B.append(A[j]-A[i+1])
    res.append((A[j], A[i+1]))
    s.remove(j)
    s.remove(i+1)

for b in B:
    res.append((M,b))
    M = M - b

for i in s:
    res.append((M,A[i]))
    M = M - A[i]

print(M)
for r in res:
    print(r[0], r[1])

Submission status: Wrong Answer

Solution code:
n=int(input())
l=sorted(list(map(int,input().split())))
if n==2:
  print(l[-1]-l[0])
  print(l[-1],l[0])
elif n%2==0:
  l1=[] 
  l2=[]
  for i in range(1,n//2):
    l1.append([l[i],l[-i-1]])
  c=l[-1]-l[0]
  for i in l1:
    l2.append([c,i[0]-i[1]])
    c-=i[0]-i[1]
  print(c)
  print(l[-1],l[0])
  for i in l1:
    print(i[0],i[1])
  for i in l2:
    print(i[0],i[1])
elif n%2==1:
  l1=[]
  l2=[]
  c=l[-1]
  for i in range(1,(n//2)+1):
    l1.append([l[i-1],l[-1-i]])
  for i in l1:
    l2.append([c,i[0]-i[1]])
    c-=i[0]-i[1]   
  print(c)
  for i in l1:
    print(i[0],i[1])
  for i in l2:
    print(i[0],i[1])

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)
    #print(id)
    s = -A[0]
    for i in range(1,N):
        if i < id:
            s -= A[i]
        else:
            s += A[i]
    print(s)
    if id == 0:
        x = A[0]
        for i in range(1,N-1):
            y = A[i]
            print(x,y)
            x = x-y
        print(A[N-1],x)
    else:
        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 = sorted(a)

ans = None
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]

    if ans is None:
        ans = ans_e
    else:
        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:
import sys
# input = sys.stdin.readline
inf = 10 ** 18

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

from collections import deque

A = deque(sorted(A))

pm = N % 2
ans = []
for i in range(N - 1):
    # print("#", list(A))
    A_min = A.popleft()
    A_max = A.pop()
    if (i + pm) % 2:
        ans.append((A_min, A_max))
        A.appendleft(A_min - A_max)
    else:
        ans.append((A_max, A_min))
        A.append(A_max - A_min)

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

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
plus=[a for a in A if a>0]
mainus=[a for a in A if a<=0]
if len(mainus)>0:
    print(sum(plus)-sum(mainus))
    now=mainus[0]
    for i in range(len(plus)-1):
        print(str(now)+" "+str(plus[i]))
        now-=plus[i]
    if len(plus)>0:
        print(str(plus[-1])+" "+str(now))
        now=plus[-1]-now
    for i in range(1,len(mainus)):
        print(str(now)+" "+str(mainus[i]))
        now-=mainus[i]
else:
    print(sum(plus)-2*plus[0])
    now=plus[0]
    for i in range(1,len(plus)-1):
        print(str(now)+" "+str(plus[i]))
        now-=plus[i]
    print(str(plus[-1])+" "+str(now))

Submission status: Wrong Answer

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

saidainosuuji = A[-1]
A = A[:-1]

shuturyoku = []
while len(A) > 1:
    shuturyoku.append((A[0], A[-1]))
    A[0] = A[0] - A[-1]
    A = A[:-1]

shuturyoku.append((saidainosuuji, A[0]))
print(saidainosuuji - A[0])
for sh in shuturyoku:
    print(sh[0], sh[1])

Submission status: Wrong Answer

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