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()
sei = []
fu = []
if n % 2 == 0:
    for i in range(n):
        if i <= n//2-1:
            fu.append(a[i])
        else:
            sei.append(a[i])
else:
    for i in range(n):
        if i < n//2:
            fu.append(a[i])
        else:
            sei.append(a[i])
print(sum(sei)-sum(fu))
p = 0
q = 0
for i in range(n-1):
    if i == 0:
        if n % 2 == 0:
            print(sei[p],fu[q])
            count = sei[p] - fu[q]
        else:
            print(fu[q],sei[p])
            count = fu[q] - sei[p]
        p += 1
        q += 1
    else:
        if n%2 == 0:
            if i%2 == 0:
                print(sei[p],count)
                count = sei[p]-count
                p += 1
            else:
                print(fu[q],count)
                count = fu[q]-count
                q += 1
        else:
            if i % 2 == 0:
                print(fu[q],count)
                count = fu[q]-count
                q += 1
            else:
                print(sei[p],count)
                count = sei[p]-count
                p += 1

Submission status: Wrong Answer

Solution code:
import copy
n=int(input())
L=list(map(int,input().split()))
L.sort()
ans=[]
for i in range(1,n-1):
  if L[i]>0:
    ans.append((L[0],L[i]))
    L[0]-=L[i]
for i in range(n):
  if L[i]>0:
    break
  else:
    ans.append((L[n-1],L[i]))
    L[n-1]-=L[i]
print(L[n-1])
for a in ans:
  x,y=a
  print(x,y)

Submission status: Wrong Answer

Solution code:
import copy
n=int(input())
L=list(map(int,input().split()))
L.sort()
ans=[]
for i in range(1,n-1):
  if L[i]>0:
    ans.append((L[0],L[i]))
    L[0]-=L[i]
for i in range(n):
  if L[i]>0:
    break
  else:
    ans.append((L[n-1],L[i]))
    L[n-1]-L[i]
print(L[n-1])
for a in ans:
  x,y=a
  print(x,y)

Submission status: Wrong Answer

Solution code:
import copy
n=int(input())
L=list(map(int,input().split()))
L.sort()
l=copy.copy(L)
for i in range(1,n):
  if i == n-1:
    print(l[n-1]-l[0])
  else:
    l[0] -= l[i]
for i in range(1,n):
  if i == n-1:
    print(L[n-1],L[0])
  else:
    print(L[0],L[i])
    L[0] -= L[i]

Submission status: Wrong Answer

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

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

#B
def B():
    n = II()
    xy = LIR(n)
    if n == 1:
        print(1)
        return
    ans = inf
    for i in range(n):
        for k in range(n):
            if i != k:
                b = 1
                check = []
                check.append((i,k))
                p = xy[i][0] - xy[k][0]
                q = xy[i][1] - xy[k][1] 
                for l in range(n):
                    for m in range(n):
                        if not((l,m) in check) and p == xy[l][0] - xy[m][0] and q == xy[l][1] - xy[m][1]:
                            b += 1
                            check.append((l, m))
                ans = min(ans, n-b)
    print(ans)

    return

#C
def C():
    n = II()
    a = LI()
    a.sort(key=abs)
    ans = deque()
    positive = deque()
    negative = deque()
    for i in range(n):
        if a[i] >= 0:
            positive.append(a[i])
        else:
            negative.append(a[i])
    a = deque(a)
    lenp = len(positive)
    lenn = len(negative)
    if lenp == 0:
        for i in range(n - 1):
            b, c = a.popleft(), a.popleft()
            ans.append((b, c))
            b -= c
            a.appendleft(b)
        print(b)
    elif lenn == 0:
        for i in range(n - 2):
            b, c = a.popleft(), a.popleft()
            ans.append((b, c))
            b -= c
            a.appendleft(b)
        b, c = a.popleft(), a.popleft()
        ans.append((c, b))
        c -= b
        print(c)
    else:
        print(lenn,lenp)
        p = positive.popleft()
        negative.appendleft(p)
        for i in range(lenn - 1):
            b, c = negative.popleft(), negative.popleft()
            ans.append((b, c))
            b -= c
            negative.appendleft(b)
        p, c = negative.popleft(), negative.popleft()
        positive.appendleft(c)
        for i in range(lenp - 1):
            b, c = positive.popleft(), positive.popleft()
            ans.append((b, c))
            b -= c
            positive.appendleft(b)
        b = positive.popleft()
        ans.append((p, b))
        p -= b
        print(p)
    for an in ans:
        print(" ".join(map(str, an)))
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

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

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls.sort()
if a_ls[-1] < 0:
    score = - sum(a_ls) + a_ls[-1] * 2
elif a_ls[1] < 0:
    score = sum([abs(a) for a in a_ls])
else:
    score = sum(a_ls) - a_ls[0] * 2
cur = a_ls[0]
last = a_ls[-1]
print(score)
for i in range(1,n):
    a = a_ls[i]
    if a < 0:
        print(last, a)
        last -= a
        continue
    if i < n - 1:
        print(cur, a_ls[i])
        cur -= a_ls[i]
    else:
        print(last, cur)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls.sort()
score = sum(a_ls) - min(a_ls) * 2
cur = a_ls[0]
print(score)
for i in range(1,n):
    if i < n - 1:
        print(cur, a_ls[i])
        cur -= a_ls[i]
    else:
        print(a_ls[i], cur)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = sorted(LI())
if A[0] > 0:
    print(sum(A) - 2 * A[0])
    ret = A[0]
    for i in range(1, n - 1):
        print(ret, A[i])
        ret -= A[i]
    print(A[n - 1], ret)
elif A[-1] < 0:
    print(-sum(A) + A[-1] * 2)
    ret = A[0]
    for i in range(1, n):
        print(ret, A[i])
        ret -= A[i]
else:
    print(sum([abs(a) for a in A]))
    minus = [a for a in A if a <= 0]
    zero = [a for a in A if a == 0]
    plus = [a for a in A if a > 0]
    if not minus:
        minus += zero
    else:
        plus += zero
    ret = minus[0]
    for j in range(1, len(plus)):
        print(ret, plus[j])
        ret -= plus[j]
    print(plus[0], ret)
    ret = plus[0] - ret
    for k in range(1, len(minus)):
        print(ret, minus[k])
        ret -= minus[k]

Submission status: Wrong Answer

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

ans = sum(A)-2*A[0]
print(ans)
run = 0
for i in range(1, n-1):
    print(A[0], A[i])
    A[0] = A[0]-A[i]

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = []
A.sort()
a = A.pop(0)
b = A.pop()
N = N-2
P,Q = N//2+N%2,N//2
for p in range(P):
    y = A.pop()
    ans.append([a,y])
    a-=y
for q in range(Q):
    y = A.pop()
    ans.append([b,y])
    b-=y
ans.append([b,a])
print(b-a)
for a in ans: print(*a)

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush
N = int(input())
A = list(map(int,input().split()))
heapify(A)
ans = []
for i in range(N-1,0,-1):
    n = i+1
    a,b = A.pop(0),A.pop()
    if n%2==1:
        ans.append([a,b])
        heappush(A,a-b)
    else:
        ans.append([b,a])
        heappush(A,b-a)
print(A[0])
for a in ans:
    print(*a)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = sorted(list(map(int,input().split())))
B = deque(A[:-1])
C = []
for i in range(N-2):
    a = B.popleft()
    b = B.pop()
    C.append((a,b))
    c = a-b
    B.appendleft(c)
a = A[-1]
b = B[-1]
C.append((a,b))
print(a-b)
for i in range(N-1):
    print(C[i][0],C[i][1])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
hu=0
for b in a:
    if b<0:
        hu+=1
    else:
        break
if hu==0:
    hu=1
ansl=[]
nowl=a[0]
for i in range(hu,n-1):
    ansl.append((nowl,a[i]))
    nowl-=a[i]

nowr=a[n-1]
for i in range(1,hu):
    ansl.append((nowr,a[i]))
    nowr-=a[i]

ansl.append((nowr,nowl))
print(nowr-nowl)
for i in range(n-1):
    x,y=ansl[i]
    print(x,y)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
from collections import deque
d=deque(a)
ansl=[]
if n%2:
    for i in range(n-1):
        if i%2==0:
            m=d.popleft()
            M=d.pop()
            ansl.append((m,M))
            d.appendleft(m-M)
        else:
            m=d.popleft()
            M=d.pop()
            ansl.append((M,m))
            d.append(M-m)
else:
    for i in range(n-1):
        if i%2:
            m=d.popleft()
            M=d.pop()
            ansl.append((m,M))
            d.appendleft(m-M)
        else:
            m=d.popleft()
            M=d.pop()
            ansl.append((M,m))
            d.append(M-m)

print(M-m)
for i in range(n-1):
    x,y=ansl[i]
    print(x,y)

Submission status: Wrong Answer

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

s_arr = []
for rnd in range(10**6):
    t = arr.pop(-1)
    s_arr.append(t)
    arr.reverse()
    if arr == []:
        break
s_arr.reverse()
ans = s_arr[0]
ans_arr = []
for k in range(1,n):
    i = s_arr[k]
    ans_arr.append([i,ans])
    ans = i - ans
print(ans)

Submission status: Wrong Answer

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

s_arr = []
for rnd in range(10**6):
    t = arr.pop(-1)
    s_arr.append(t)
    arr.reverse()
    if arr == []:
        break
s_arr.reverse()
ans = s_arr[0]
ans_arr = []
for k in range(1,n):
    i = s_arr[k]
    ans_arr.append([i,ans])
    ans = i - ans
print(ans)
for i in ans_arr:
    print(*i)

Submission status: Wrong Answer

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

s_arr = []
for rnd in range(10**6):
    t = arr.pop(-1)
    s_arr.append(t)
    arr.reverse()
    if arr == []:
        break
s_arr.reverse()
ans = s_arr[0]
ans_arr = []
for i in s_arr[1:]:
    ans_arr.append([i,ans])
    ans = i - ans
print(ans)
for i in ans_arr:
    print(*i)

Submission status: Wrong Answer

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

s_arr = []
while arr:
    t = arr.pop(-1)
    s_arr.append(t)
    arr.reverse()
s_arr.reverse()
ans = s_arr[0]
ans_arr = []
for i in s_arr[1:]:
    ans_arr.append([i,ans])
    ans = i - ans
print(ans)
for i in ans_arr:
    print(*i)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
sum_L = sum(L)

min_use = sum_L-2*L[0]
max_use = -sum_L + 2*L[-1]

process = []
if min_use > max_use:
    ans = min_use
    l = L[0]
    r = L[1]
    process.append(str(l)+" "+str(r))
    tmp = l-r
    for l in L[2:]:
        process.append(str(l)+" "+str(tmp))
        tmp = l - tmp
else:
    ans = max_use
    l = L[-1]
    r = L[-2]
    process.append(str(r)+" "+str(l))
    tmp = l-r
    for r in reversed(L[:-2]):
        process.append(str(tmp)+" "+str(r))
        tmp = tmp-r
print(ans)
print("\n".join(process))

Submission status: Wrong Answer

Solution code:
n=int(input())
lists=list(map(int,input().split()))
mini=min(lists)
maxi=max(lists)
if mini>=0 or maxi<=0:
    lists=sorted(lists)
    ans=0
    length=len(lists)//2
    for i in range(len(lists)):
        if i<=length-1:
            ans-=lists[i]
        else:
            ans+=lists[i]
    print(ans)
    some=0
    
    if len(lists)%2!=0:
        some=lists[length]
        print(lists[0],some)
        some=lists[0]-some
        for i in range(1,length):
            print(lists[i+length],some)
            some=lists[i+length]-some
            print(lists[i],some)
            some=lists[i]-some
        print(lists[-1],some)

        
    elif len(lists)%2==0:
        some=lists[length]
        print(lists[0],some)
        some=lists[0]-some
        for i in range(1,length-1):
            print(lists[i+length],some)
            some=lists[i+length]-some
            print(lists[i],some)
            some=lists[i]-some
        print(lists[-1],some)
        some=lists[-1]-some
        print(some,lists[length-1])
        some=some-lists[length-1]


elif mini<0 and maxi>0:
    lists=sorted(lists)
    
    positive=0
    negative=0
    zero=0
    
    for some in lists:
        if some >0:
            positive+=1  
        elif some==0:
            zero+=1 
        elif some<0:
            negative+=1
            
    ans=0
    if negative>positive:
        z=0
        for _ in  lists:
            z+=abs(_)
        print(z)
        #解答の出力
        ans=lists[-1]
        for i in range(negative-positive):
            print(ans,lists[i])
            ans=ans-lists[i]
        x=negative-positive
        for j in range(positive-1):
            print(ans,lists[x+j])
            ans=ans-lists[x+j]
            print(lists[x+j+1],ans)
            ans=lists[x+j+1]-ans

        print(lists[-negative],ans)
        for i in range(zero):
            print(z,0)

    elif negative<positive:
        z=0
        for _ in  lists:
            z+=abs(_)
        print(z)
        #解答の出力        
        ans=lists[0]
        for i in range(1,positive-negative+1):
            print(ans,lists[-i])
            ans=ans-lists[-i]
        x=positive-negative+1
        for i in range(negative-1):
            print(lists[-x-i],ans)
            ans=lists[-x-i]-ans
            print(lists[i+1],ans)  
            ans=lists[i+1]-ans
        print(lists[-positive],ans)
        for i in range(zero):
            print(z,0)
    elif negative==positive:  
        z=0
        for _ in  lists:
            z+=abs(_)
        print(z)
        #解答の出力        
        ans=lists[0]
        x=1
        for i in range(negative-1):
            print(lists[-x-i],ans)
            ans=lists[-x-i]-ans
            print(lists[i+1],ans)  
            ans=lists[i+1]-ans
        print(lists[-positive],ans)
        for i in range(zero):
            print(z,0)

Submission status: Wrong Answer

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

mini=min(lists)
maxi=max(lists)
if mini>=0 or maxi<=0:
    lists=sorted(lists)
    ans=0
    length=len(lists)//2
    for i in range(len(lists)):
        if i<=length-1:
            ans-=lists[i]
        else:
            ans+=lists[i]
    print(ans)
    some=lists[-1]
    if len(lists)%2!=0:
        some=lists[length]
        print(lists[0],some)
        some=lists[0]-some
        for i in range(1,length):
            print(lists[i+length],some)
            some=lists[i+length]-some
            print(lists[i],some)
            some=lists[i]-some
        print(lists[-1],some)
        
    elif len(lists)%2==0:
        some=lists[length]
        print(lists[0],some)
        some=lists[0]-some
        for i in range(1,length-1):
            print(lists[i+length],some)
            some=lists[i+length]-some
            print(lists[i],some)
            some=lists[i]-some
        print(lists[-1],some)
        some=lists[-1]-some
        print(some,lists[length-1])
        some=some-lists[length-1]

            
            
            


else:
    lists=sorted(lists)
    positive=0
    negative=0
    zero=0
    print(lists)
    for some in lists:
        if some >0:
            positive+=1  
        elif some==0:
            zero+=1 
        elif some<0:
            negative+=1
    ans=0
    if negative>positive:
        z=0
        for _ in  lists:
            z+=abs(_)
        print(z)
        #解答の出力
        for i in range(zero):
            print(0,0)
        ans=lists[-1]
        for i in range(negative-positive):
            print(ans,lists[i])
            ans=ans-lists[i]
        x=negative-positive
        for j in range(positive-1):
            print(ans,lists[x+j])
            ans=ans-lists[x+j]
            print(lists[x+j+1],ans)
            ans=lists[x+j+1]-ans
        print(lists[-positive],ans)

    elif negative<positive:
        z=0
        for _ in  lists:
            z+=abs(_)
        print(z)
        #解答の出力        
        for i in range(zero):
            print(0,0)
        ans=lists[0]
        for i in range(1,positive-negative+1):
            print(ans,lists[-i])
            ans=ans-lists[-i]
            
        x=positive-negative+1
        
        for i in range(negative-1):
            print(lists[-x-i],ans)
            ans=lists[-x-i]-ans
            print(lists[i+1],ans)  
            ans=lists[i+1]-ans
        print(lists[negative],ans)
    else:
        for i in range(zero):
            print(0,0)
        ans=0
        ans=lists[0]
        for j in range(1,positive):
            print(lists[-j],ans)
            ans=lists[-j]-ans
            print(lists[j],ans)
            ans=lists[j]-ans    
            print(lists[positive],ans)

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
A = list(map(int, input().strip().split()))
#print(N)
#print(A)
moves = []
while len(A) > 2:
    a = min(A)
    b = max(A)
    moves.append([a, b])
    c = a - b
    d = A.index(a)
    del A[d]
    e = A.index(b)
    del A[e]
    #print(A)
    A.append(c)
    #print(A)
print(A[0] - A[1])
moves.append([A[0], A[1]])
for i in moves:
    print(str(i[0]), str(i[1]))

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N = I()
A = III()
A.sort(reverse=True)
plus = 0
for a in A:
    if a>0:
        plus += 1
if plus==N:
    plus -= 1

ans = []
val = A[plus]
for i in range(1,plus)[::-1]:
    ans.append((val,A[i]))
    val -= A[i]
ans.append((A[0],val))
val = A[0]-val

for i in range(plus+1,N):
    ans.append((val,A[i]))
    val -= A[i]

print(val)
for a in ans:
    print(*a, sep=' ')

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N = I()
Atemp = III()
A = []

Atemp.sort(reverse=True)
for i in range((N+1)//2):
    A.append(Atemp[i])
    if i!=N-1-i:
        A.append(Atemp[N-1-i]) 

ans = []
val = A[0]
start = 1
i = 2
while i<N:
    if A[i]<=0:
        i += 1
    elif start==i:
        i += 1
    else:
        ans.append([A[i-1],A[i]])
        x = A[i-1]-A[i]
        for j in range(start,i-2):
            ans.append([val, A[j]])
            val -= A[j]
        ans.append([val,x])
        val -= x
        start = i+1
        i += 1

if start<N:
   for j in range(start,N):
        ans.append([val, A[j]])
        val -= A[j] 

print(val)
for a in ans:
    print(*a, sep=' ')

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N = I()
A = III()

ans = []
val = A[0]
start = 1
i = 2
while i<N:
    if A[i]<=0:
        i += 1
    elif start==i:
        i += 1
    else:
        ans.append([A[i-1],A[i]])
        x = A[i-1]-A[i]
        for j in range(start,i-2):
            ans.append([val, A[j]])
            val -= A[j]
        ans.append([val,x])
        val -= x
        start = i+1
        i += 1
        
if start<N:
   for j in range(start,N):
        ans.append([val, A[j]])
        val -= A[j] 

print(val)
for a in ans:
    print(*a, sep=' ')

Submission status: Wrong Answer

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

A.sort()
if N % 2 == 0:
  M = -sum(A[:N//2])+sum(A[N//2:])
else:
  M = max(-sum(A[:N//2])+sum(A[N//2:]),-sum(A[:N//2+1])+sum(A[N//2+1:]))
for j in range(2):
  ans = []
  B = A[:]
  flag = j
  BB = []
  while(len(B)>1):
    for i in range(len(B)//2):
      if flag == 0:
        ans.append([B[i],B[-1-i]])
        BB.append(B[i]-B[-1-i])
      else:
        ans.append([B[-1-i],B[i]])
        BB.append(B[-1-i]-B[i])
      flag = 1-flag
    if len(B) % 2 == 1:
      BB.append(B[len(B)//2])
    B = sorted(BB[:])
    BB = []
  if j == 0 and B[0] == M:
    break
print(M)
for a in ans:
  print(*a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left as bi
n = int(input())
a = [int(i) for i in input().split()]
a = sorted(a)
b,num = bi(a,0),a[0]
if b==n:
    p,q = 1,n-1
    print(a[-1]-sum(a[:n-1]))
elif b:
    p,q = n-b,b
    print(sum(a[b:])-sum(a[:b]))
else:
    p,q = n-1,1
    print(sum(a[1:])-a[0])
for i in range(q,n-1):
    print(num,a[i])
    num-=a[i]
print(a[-1],num)
num = a[-1]-num
for i in range(2,q):
    print(num,a[i])
    num-=a[i]

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a,x = sorted(a),[]
print(sum(a[n//2:])-sum(a[:n//2]))
if n%2:
    for i in range(n//2):
        print(a[i],a[i+n//2])
        x.append(a[i]-a[i+n//2])
    num = a[-1]
    for i in range(len(x)):
        print(num,x[i])
        num-=x[i]
else:
    for i in range(n//2-1):
        print(a[i],a[i+n//2])
        x.append(a[i]-a[i+n//2])
    print(a[-1],a[n//2-1])
    num = a[-1]-a[n//2-1]
    for i in range(len(x)):
        print(num,x[i])
        num-=x[i]

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
N = int(input())
A = [int(i) for i in input().split()]

a = []
b = []
for i in range(N):
  heappush(a,A[i])
  heappush(b,-A[i])
ans = []
if N%2==0:
  x = heappop(a)
  m = x
  for i in range(N-1):
    if i%2==0:
      x = heappop(b)
      x = -x
      ans += [(x,m)]
    else:
      x = heappop(a)
      ans += [(x,m)]
    m = x-m
else:
  x = heappop(b)
  x = -x
  m = x
  for i in range(N-1):
    if i%2==0:
      x = heappop(a)
      ans += [(x,m)]
    else:
      x = heappop(b)
      x = -x
      ans += [(x,m)]
    m = x-m
print(m)
for a in ans:
  print(*a)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
N = int(input())
A = [int(i) for i in input().split()]

a = []
b = []
for i in range(N):
  heappush(a,A[i])
  heappush(b,-A[i])
ans = []
if N%2==0:
  x = heappop(a)
  m = x
  for i in range(N-1):
    if i%2==0:
      x = heappop(b)
      x = -x
      ans += [(x,m)]
      print(m, x)
    else:
      x = heappop(a)
      ans += [(x,m)]
    m = x-m
else:
  x = heappop(b)
  x = -x
  m = x
  for i in range(N-1):
    if i%2==0:
      x = heappop(a)
      ans += [(x,m)]
    else:
      x = heappop(b)
      x = -x
      ans += [(x,m)]
    m = x-m
print(m)
for a in ans:
  print(*a)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
N = int(input())
A = [int(i) for i in input().split()]

a = []
b = []
for i in range(N):
  heappush(a,A[i])
  heappush(b,-A[i])
ans = []
if N%2==0:
  x = heappop(a)
  m = x
  for i in range(N-1):
    if i%2==0:
      x = heappop(b)
      x = -x
      ans += [(m,x)]
      print(m, x)
    else:
      x = heappop(a)
      ans += [(m,x)]
    m = x-m
else:
  x = heappop(b)
  x = -x
  m = x
  for i in range(N-1):
    if i%2==0:
      x = heappop(a)
      ans += [(m,x)]
    else:
      x = heappop(b)
      x = -x
      ans += [(m,x)]
    m = x-m
print(m)
for a in ans:
  print(*a)

Submission status: Wrong Answer

Solution code:
import bisect

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

zp = bisect.bisect_left(A, 0)
ans = []

M = A[0]
for i in range(max(zp, 1), N-1):
    ans.append((M, A[i]))
    M -= A[i]

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

for i in range(1, min(zp, N-2)):
    ans.append((M, A[i]))
    M -= A[i]


print(M)
for a, b in ans:
    print(a, b)

Submission status: Wrong Answer

Solution code:
import bisect

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

zp = bisect.bisect_left(A, 0)
ans = []

M = A[0]
for i in range(max(zp, 1), N-1):
    ans.append((M, A[i]))
    M -= A[i]

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

for i in range(1, min(zp, N-2)):
    ans.append((M, A[i]))
    M -= A[i]


print(M)
for t in ans:
    print(*t)

Submission status: Wrong Answer

Solution code:
import bisect

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

zp = bisect.bisect_left(A, 0)

M = A[0]
for i in range(zp, N-1):
    print(M, A[i])
    M -= A[i]

print(A[N-1], M)
M = A[N-1]-M

for i in range(1, zp):
    print(M, A[i])
    M -= A[i]

Submission status: Wrong Answer

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

A.sort()
c = 0
for a in A:
    if a <= 0:
        c += 1

if c == N or c == N-1:
    ans = A[-1] - sum(A[:-1])
    print(ans)
    a, b = A[-1], A[0]
    for i in range(1, N):
        print(a, b)
        a, b = a-b, A[i]
elif c == 0 or c == 1:
    ans = sum(A[1:]) - A[0]
    print(ans)
    a, b = A[0], 0
    for i in range(1, N-1):
        a, b = a-b, A[i]
        print(a, b)
    a, b = A[-1], a-b
    print(a, b)
else:
    ans = sum(A[c:]) - sum(A[:c])
    print(ans)
    a, b = A[0], A[c]
    for i in range(N-c-1):
        print(a, b)
        if i == N-c-2:
            break
        a, b = a-b, A[c+i]
    a, b = A[-1], a-b
    print(a, b)
    for i in range(c-1):
        a, b = a-b, A[i+1]
        print(a, b)

Submission status: Wrong Answer

Solution code:


def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()
    ma = max(a)
    mi = min(a)
    ans = 0
    if mi >= 0:
        t = a[0]
        for i in range(1, n-1):
            t -= a[i]
        print(ma - t)
        t = a[0]
        for i in range(1, n - 1):
            print(t, a[i], sep=" ")
            t -= a[i]
        print(ma, t, sep=" ")
    elif ma < 0:
        t = a[-1]
        for i in range(n - 2, 0, -1):
            t -= a[i]
        print(t - mi)
        t = a[-1]
        for i in range(n - 2, 0, -1):
            print(t, a[i], sep=" ")
            t -= a[i]
        print(t, mi, sep=" ")
    else:
        pass


if __name__ == '__main__':
    main()