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

# A[cut-1] < 0 and A[cut] >= 0
cut = 0
while cut < N and A[cut] < 0:
    cut += 1

if cut == 0:
    M = sum(A[1:]) - A[0]
    print(M)
    pair = [A[0], A[N-1]]
    for i in range(N-2):
        print(' '.join(map(str, pair)))
        pair = [pair[0] - pair[1], A[N-i-2]]
    print(' '.join(map(str, pair)))

elif cut == N:
    M = A[-1] - sum(A[:N-1])
    print(M)
    pair = [A[N-1], A[0]]
    for i in range(N-2):
        print(' '.join(map(str, pair)))
        pair = [pair[0] - pair[1], A[i+1]]
    print(' '.join(map(str, pair)))

else:
    M = sum(A[cut:]) - sum(A[:cut])
    print(M)

    B = deque(A[cut:]) # Aの正の部分
    C = deque(A[:cut]) # Aの負の部分

    while len(B) != 1:
        pair = [B[-1], C[-1]]
        print(' '.join(map(str, pair)))
        b = B.pop()
        C[-1] = C[-1] - b
    
    b = B.pop()

    while len(C) != 0:
        pair = [b, C[0]]
        print(' '.join(map(str, pair)))
        c = C.popleft()
        b = b - c

Submission status: Wrong Answer

Solution code:
import bisect
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
ans=[]
x=bisect.bisect_left(a, 0)
m=a[:x]
p=a[x:]
print(m,p)
if n==2 and len(p)==2:
    print(p[1]-p[0])
    print(p[1],p[0])
    exit()

if m==[]:
    m+=[p[0]-p[-1]]
    ans+=[[p[0],p[-1]]]
    del p[0]
    del p[-1]

if p==[]:
    p+=[m[-1]-m[0]]
    ans+=[[m[-1],m[0]]]
    del m[-1]
    del m[0]

while len(p)!=1:

    m+=[m[0]-p[0]]
    ans+=[[m[0],p[0]]]
    del p[0]
    del m[0]
cnt=p[0]
for i in m:
    ans+=[[cnt,i]]
    cnt-=i
print(cnt)
for i,j in ans:print(i,j)

Submission status: Wrong Answer

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

if A[0]>0:
    num=A[0]
    for i in range(1,N-1):
        ans.append([num,A[i]])
        num-=A[i]
    ans.append([A[-1],num])
    print(A[-1]-num)
    for i in range(len(ans)):
        print(ans[i])
elif A[-1]<0:
    A.sort(reverse=True)
    num=A[0]
    for i in range(1,N):
        ans.append([num,A[i]])
        num-=A[i]
    print(num)
    for i in range(len(ans)):
        print(ans[i])
else:
    M=A[-1]
    m=A[0]
    for i in range(1,N-1):
        if A[i]<0:
            ans.append([M,A[i]])
            M-=A[i]
        else:
            ans.append([m,A[i]])
            m-=A[i]
    ans.append([M,m])
    print(M-m)
    for i in range(len(ans)):
        print(ans[i])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))[::-1]
x=0
for i in a:x+=abs(i)
if a[-1]>0:x-=a[-1]*2
print(x)
for i in range(n-2):
    i=a.pop()
    j=a.pop()
    print(i,j)
    a.append(i-j)
i=a.pop()
j=a.pop()
print(j,i)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
q = deque()

x = a.pop()
while len(a) > 1:
    y = a.pop()
    if not x < 0 <= y:
        x, y = y, x
    q.append((x, y))
    s = x - y
    x = s
y = a.pop()
x, y = y, x
s = x - y
q.append((x, y))

print(s)

while len(q):
    x, y = q.popleft()
    print(x, y)

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.append([mi,ma])
    else:
        li.append([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:
N = int(input())
A = [int(_) for _ in input().split()]

xs = sorted(A)

pxs = xs[N//2:]
mxs = xs[:N//2]

ys = [0] * N
ys[0::2] = sorted(pxs, reverse=True)
ys[1::2] = sorted(mxs, reverse=True)

result = sum(pxs) - sum(mxs)

print(result)

for i in range(N - 1):
    print(ys[N - 2 - i], ys[N - 1 - i])
    ys[N - 2 - i] -= ys[N - 1 - i]

Submission status: Wrong Answer

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

xs = sorted(A)

pxs = xs[N//2:]
mxs = xs[:N//2]

ys = [0] * N
ys[0::2] = pxs
ys[1::2] = mxs

result = sum(pxs) - sum(mxs)

print(result)

for i in range(N - 1):
    print(ys[i + 1], ys[i])
    ys[i + 1] -= ys[i]

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
anslist = []
if a[0] >= 0:
  pass
  
elif a[-1] < 0:
  pass
  
else:
  for i in range(1,n-1):
    if a[i] < 0:
      anslist.append([a[-1], a[i]])
      a[-1] -= a[i]
    else:
      anslist.append([a[0], a[i]])
      a[0] -= a[i]
  anslist.append([a[-1], a[0]])
  ans = a[-1]-a[0]
  print(ans)
  for i in range(len(anslist)):
    print(anslist[i][0], anslist[i][1])

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
A = sorted([int(_) for _ in input().split()])
i = bisect.bisect_left(A, 0)
if i:
    i -= 1
now = A[i]
ans = []
for a in A[i + 1:N - 1]:
    ans += [[now, a]]
    now -= a
ans += [[A[N - 1], now]]
now = A[N - 1] - now
for a in A[:i]:
    ans += [[now, a]]
    now -= a
print(now)
[print(*xy) for xy in ans]

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
A.sort()
if A[-1]<0:
    A.sort(reverse=True)
    ANS=[]
    num=A[0]
    for i in range(1,N):
        ANS.append([num,A[i]])
        num-=A[i]
    print(num)
    for i in ANS:
        print(i[0],i[1])
else:
    num=A[0]
    ANS=[]
    for i in range(1,N-1):
        ANS.append([num,A[i]])
        num-=A[i]
    ANS.append([A[-1],num])
    num=A[-1]-num
    print(num)
    for i in ANS:
        print(i[0],i[1])

Submission status: Wrong Answer

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

ANS=[]
num=A[0]
for i in range(1,N-1):
    ANS.append([num,A[i]])
    num-=A[i]
ANS.append([A[-1],num])
print(A[-1]-num)
for i in ANS:
    print(i[0],i[1])

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
inpt = input()
A = [int(a) for a in inpt.split()]
A = deque(sorted(A))
outputs = []
negative = 0
positive = 0
for a in A:
    if a > 0:
        positive += 1
    elif a <= 0:
        negative += 1
for i in range(N-1):
    maxA = A[-1]
    minA = A[0]
    A.pop()
    A.popleft()
    if positive > negative:
        A.appendleft(minA-maxA)
        outputs.append(str(minA)+" "+str(maxA))
        negative += 1
    else:
        A.append(maxA-minA)
        outputs.append(str(maxA)+" "+str(minA))
        positive += 1

print(A[0])
for output in outputs:
    print(output)

Submission status: Wrong Answer

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
#N = 3
#A = [1,1,1]
A.sort(reverse = True)
idx = N
for i in range(N):
    if A[i] < 0:
        idx = i
        break
if idx == N:
    print(sum(A)-2*A[-1])
    tmp_last = A[-1]
    for i in range(N-2):
        print(tmp_last,A[-2-i])
        tmp_last = tmp_last - A[-2-i]
    print(A[0],tmp_last)
elif idx == 0:
    if N == 2:
        print(A[0] - A[1])
    else:
        print(abs(sum(A))+2*(A[0]+A[1]))
    first_tmp = A[0]
    for i in range(2,N-1):
        print(first_tmp,A[i])
        first_tmp = first_tmp - A[i]
    tmp_last = A[1]
    if N >= 3:
        print(A[-1], A[1])
        tmp_last = A[-1] - A[1]
    print(first_tmp, tmp_last)
else:
    if N == 2:
        print(A[0] - A[1])
        print(A[0],A[1])
    else:
        ans = 0
        for  i in range(N):
            if i < idx:
                ans += A[i]
            else:
                ans += -A[i]
        print(ans)
        tmp_first = A[0]
        for i in range(idx,N-1):
            print(tmp_first, A[i])
            tmp_first = tmp_first - A[i]
        tmp_last = A[-1]
        for i in range(1,idx):
            print(tmp_last,A[i])
            tmp_last = tmp_last - A[i]
        print(tmp_first, tmp_last)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import collections
sys.setrecursionlimit(10**7)
def lmi(): return list(map(int, input().split()))

n = int(input())
a = lmi()
a.sort()
b = [-1 for i in range(n)]
for i in range(1, n):
    if a[i] > 0:
        b[i] = 1

print(sum([a[i]*b[i] for i in range(n)]))

x = a[0]
for y in [a[i] for i in range(n-1) if b[i] == 1]:
    print(x, y)
    x -= y

y = x
x = a[-1]
print(x, y)
x -= y
for y in [a[i] for i in range(1,n-1) if b[i] == -1]:
    print(x, y)
    x -= y

Submission status: Wrong Answer

Solution code:
import bisect
import os
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import re
import string
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

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

plus = []
minus = []

for a in A:
    if a >= 0:
        plus.append(a)
    else:
        minus.append(a)
if not plus:
    plus.append(minus.pop())
if not minus:
    minus.append(plus.pop())

hist = []
x = minus.pop()
while len(plus) > 1:
    y = plus.pop()
    hist.append((x, y))
    x = x - y
minus.append(x)

x = plus.pop()
while len(minus) > 1:
    y = minus.pop()
    hist.append((x, y))
    x = x - y
y = minus.pop()
hist.append((x, y))

print(x - y)
for h in hist:
    print(*h)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
m = a[-1]
mn = a[0]
t = mn
for i in range(1,n-1):
    if a[i]<0:
        m-=a[i]
    else:
        mn-=a[i]
if a[-2]>0:
    print(m-mn)
else:
    print(m)
m = a[-1]
t = a[0]
mn = a[0]
for i in range(1,n-1):
    if a[i]<0:
        print(m,a[i])
        m-=a[i]
    else:
        print(t,a[i])
        t-=a[i]
print(m,t)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(sum(A[N//2:N])-sum(A[0:N//2]))
#TLEテスト
'''
if N%2==0:
  for i in range(N-1):
    if i%2==0:
      print(str(A[N-i-1])+' '+ str(A[0]))
      A[N-i-1] = A[N-i-1]-A[0]
      A.pop(0)
    else:
      print(str(A[0])+' '+ str(A[N-i-1]))
      A[0] = A[0] - A[N-i-1]
      A.pop(N-i-1)
else:
  for i in range(N-1):
    if i%2==0:
      print(str(A[0])+' '+ str(A[N-i-1]))
      A[0] = A[0] - A[N-i-1]
      A.pop(N-i-1)
    else:
      print(str(A[N-i-1])+' '+ str(A[0]))
      A[N-i-1] = A[N-i-1]-A[0]
      A.pop(0)
'''

Submission status: Wrong Answer

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

#TLEテスト

'''
print(sum(A[N//2:N])-sum(A[0:N//2]))

if N%2==0:
  for i in range(N-1):
    if i%2==0:
      print(str(A[N-i-1])+' '+ str(A[0]))
      A[N-i-1] = A[N-i-1]-A[0]
      A.pop(0)
    else:
      print(str(A[0])+' '+ str(A[N-i-1]))
      A[0] = A[0] - A[N-i-1]
      A.pop(N-i-1)
else:
  for i in range(N-1):
    if i%2==0:
      print(str(A[0])+' '+ str(A[N-i-1]))
      A[0] = A[0] - A[N-i-1]
      A.pop(N-i-1)
    else:
      print(str(A[N-i-1])+' '+ str(A[0]))
      A[N-i-1] = A[N-i-1]-A[0]
      A.pop(0)
'''

Submission status: Wrong Answer

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

print(sum(A[N//2:N])-sum(A[0:N//2]))

if N%2==0:
  for i in range(N-1):
    if i%2==0:
      print(str(A[N-i-1])+' '+ str(A[0]))
      A[N-i-1] = A[N-i-1]-A[0]
      A.pop(0)
    else:
      print(str(A[0])+' '+ str(A[N-i-1]))
      A[0] = A[0] - A[N-i-1]
      A.pop(N-i-1)
else:
  for i in range(N-1):
    if i%2==0:
      print(str(A[0])+' '+ str(A[N-i-1]))
      A[0] = A[0] - A[N-i-1]
      A.pop(N-i-1)
    else:
      print(str(A[N-i-1])+' '+ str(A[0]))
      A[N-i-1] = A[N-i-1]-A[0]
      A.pop(0)

Submission status: Wrong Answer

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

from collections import deque

deq = deque(a_list)
res = []
while len(deq) > 2:
    min_num = deq.popleft()
    max_num = deq.pop()
    if min_num < 0 and max_num > 0:
        ne = deq.pop()
        if ne >= 0:
            res.append((min_num, ne))
            min_num -= ne
            deq.appendleft(min_num)
            deq.append(max_num)
        else:
            res.append((max_num, ne))
            max_num -= ne
            deq.append(max_num)
            deq.appendleft(min_num)
    else:
        if max_num < 0:
            res.append((max_num, min_num))
            max_num = max_num - min_num
            deq.append(max_num)
        else:
            res.append((min_num, max_num))
            min_num = min_num - max_num
            deq.appendleft(min_num)

tmp1 = deq[0]
tmp2 = deq[1]
res.append((max(tmp1, tmp2), min(tmp2, tmp1)))
ans = max(tmp1, tmp2) - min(tmp2, tmp1)
print(ans)
for x,y in res:
    print(x, y)

Submission status: Wrong Answer

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

a.sort()
ope = []
max_num = a[-1]
min_num = a[0]
p = bisect.bisect_left(a, 0)
for i in range(1, max(p, 1)):
    ope.append([max_num, a[i]])
    max_num -= a[i]
for i in range(max(p, 1), n - 1):
    ope.append([min_num, a[i]])
    min_num -= a[i]
ope.append([max_num, min_num])
print(max_num - min_num)
for i in range(n - 1):
    print(" ".join(map(str, ope[i])))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = sorted(list(map(int,input().split())))
M = A[-1]
m = A[0]
if M>=0 and m<=0:
    print(sum(abs(a) for a in A))
    for i in range(1,n-1):
        print(m,A[i])
        m -= A[i]
    print(M,m)
elif m>=0:
    print(sum(A)-min(A)*2)
    for i in range(1,n-1):
        print(m,A[i])
        m -= A[i]
    print(M,m)
else:
    print(sum(abs(a) for a in A)+max(A)*2)
    A.sort(reverse = 1)
    for i in range(1,n-1):
        print(M,A[i])
        M -= A[i]
    print(m,M)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
if len(A)%2==0:
    A.sort()
    ans=0
    for n in range(N):
        if n<N/2:
            ans=ans-A[n]
        else:
            ans=ans+A[n]
    print(ans)
else:
    A.sort()
    ans=0
    for n in range(N):
        if n<(N-1)/2:
            ans=ans-A[n]
        else:
            ans=ans+A[n]
    print(ans)
if len(A)%2==0:
    count=0
    for n in range(N):
        if n==0:
            syou=min(A)
            A.remove(syou)
            count=syou
        elif n%2==1:
            dai=max(A)
            A.remove(dai)
            print(dai,count)
            count=dai-count
        else:
            syou=min(A)
            A.remove(syou)
            print(syou,count)
            count=syou-count
else:
    count=0
    for n in range(N):
        if n==0:
            dai=max(A)
            A.remove(dai)
            count=dai
        elif n%2==1:
            syou=min(A)
            A.remove(syou)
            print(syou,count)
            count=syou-count
        else:
            syou=max(A)
            A.remove(syou)
            print(syou,count)
            count=syou-count

Submission status: Wrong Answer

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())
###template###

N = int(input())
As = list(mi())

TL = [[], [], []]
#[0] : zeroList
#[1] : minusList
#[2] : plusList

for a in As:
  if a == 0: TL[0].append(a)
  elif a < 0: TL[1].append(a)
  else: TL[2].append(a)

#print(TL)

ans = 0
printlist = []
#全てxxの場合（3パターン）
if len(TL[0])==N: #全てゼロの場合
  print(0)
  for i in range(N-1):
    print(0, 0)
  exit()
elif len(TL[1])==N: #全てminusの場合
  TL[1] = sorted(TL[1], reverse=True) #降順に並べる（-値の低いものから）
  printlist.append((TL[1][0],TL[1][1])) #痛みを最小に
  hand = TL[1][0]-TL[1][1] #これでプラスになった
  idx = 2
  while idx < N:
    printlist.append((hand, TL[1][idx]))
    hand -= TL[1][idx]
    idx += 1
  ans = hand
  #答えを出力する
  print(ans)
  for x, y in printlist:
    print(x, y)
  exit()
elif len(TL[2])==N: #全てplusの場合
  TL[2] = sorted(TL[2]) #昇順に並べる
  printlist.append((TL[2][0],TL[2][1])) #痛みを最小に
  hand = TL[2][0]-TL[2][1] #これで-になった
  idx = 2
  while idx < N-1:
    printlist.append((hand, TL[2][idx]))
    hand -= TL[2][idx]
    idx += 1
  #idx==N-1
  printlist.append((TL[2][idx], hand))
  hand = TL[2][idx]-hand
  ans = hand
  #答えを出力する
  print(ans)
  for x, y in printlist:
    print(x, y)
  exit()

# ここから先は、0,-,+のうちどれか2つはあるので、全て集合させられる
# ただし、0と-が1つずつ、0と+が1つずつというパターンは別途処理する必要がある
# 以下、そのパターンを処理
if len(TL[0])==1 and len(TL[1])==1:
  #0と-が1つずつ。0から-を引いて終わり
  left, right = TL[0].pop(), TL[1].pop()
  print(left-right)
  print(left, right)
  exit()
elif len(TL[0])==1 and len(TL[2])==1:
  #0と+が1つずつ。+から0を引いて終わり
  left, right = TL[2].pop(), TL[0].pop()
  print(left-right)
  print(left, right)
  exit()

# ここから先は、0,-,+のうちどれか2つはあり、前述のパターンも無いので、全て集合させられるし、最後に-と+が1つずつという最終処理パターンに持っていける
# よって一律のループ処理でOK
# 0が0個、+が1つ、-が1つになったら処理を抜け、最終手順に向かう
# 最初からそうなら、while文は実行されないのでＯＫ
# また、全てを集合させられる＝取り出す順番はどうでもいいので、高速化のため全てpop()で取り出していく
while not (len(TL[0])==0 and len(TL[1])==1 and len(TL[2])==1):
  left = 0
  right = 0
  if len(TL[0])>=1:
    #0があれば、まず0を消化する
    if len(TL[1])==0: #-がなければ、0を使って-を作る
      left, right = TL[0].pop(), TL[2].pop()
      printlist.append((left, right))
      TL[1].append(left-right)
    elif len(TL[2])==0: #+がなければ、0を使って+を作る
      left, right = TL[0].pop(), TL[1].pop()
      printlist.append((left, right))
      TL[2].append(left-right)
    else: #-も+もあるなら消化するだけなので、どっちでもいいが、-で消化する
      left, right = TL[1].pop(), TL[0].pop()
      printlist.append((left, right))
      TL[1].append(left-right)
  #ここからは、0がなくなったパターン
  #0がなくなったということは、+と-は必ず両方あることになる
  #またwhile文の条件から、「どちらも1つしかない」ということはありえない
  if len(TL[1])>=len(TL[2]): #-のが多い、もしくは同じ（もちろん2:2以上）
    left, right = TL[2].pop(), TL[1].pop()
    printlist.append((left, right))
    TL[2].append(left-right)
  else: #+のが多いパターン
    print(printlist)
    left, right = TL[1].pop(), TL[2].pop()
    printlist.append((left, right))
    TL[1].append(left-right)

# 最後に、プラス-マイナスでプラス数字を作って終わり
left, right = TL[2].pop(), TL[1].pop()
printlist.append((left, right))
ans = left-right


#答えを出力する
print(ans)
for x, y in printlist:
  print(x, y)

Submission status: Wrong Answer

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

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

positives = deque(sorted(a))
negatives = deque([positives.popleft()])
for i in range(n - 2):
    if a[i] < 0:
        negatives.append(positives.popleft())
    else:
        break

# print M
print(sum(positives) - sum(negatives))

while len(positives) > 1:
    y = positives.popleft()
    x = negatives.pop()
    print(x, y)
    negatives.append(x - y)

x = positives.popleft()

while len(negatives) > 0:
    y = negatives.pop()
    print(x, y)
    x = x - y

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(s) for s in input().split(' ')]
A.sort()
print(sum([abs(a) for a in A[1:]]) - A[0])
answer = ''
for i in range(1, len(A) - 1):
    if(A[i] > 0):
        answer += "{} {}\n".format(A[0], A[i])
        A[0] -= A[i]
    else:
        answer += "{} {}\n".format(A[N - 1], A[i])
        A[N - 1] -= A[i]
answer += "{} {}\n".format(A[N - 1], A[0])
print(answer)

Submission status: Wrong Answer

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

## 2個しかない場合
if N == 2:
  A.sort()
  print(A[-1] - A[0])
  print(A[-1], A[0])
  exit()

## 全部非負整数の場合
if sum(a <= 0 for a in A) == 0:
  A.sort()
  solver = []
  var = A[0]
  for i in range(1,N-1):
    solver.append((var, A[i]))
    var = var - A[i]
  else:
    solver.append((A[-1], var))
    var = A[-1] - var

  print(var)
  for i in range(len(solver)):
    print(solver[i][0], solver[i][1])
  exit()
  
## 全部非正整数の場合
if sum(a >= 0 for a in A) == 0:
  A.sort(reverse=True)
  solver = []
  var = A[0]
  for i in range(1,N):
    solver.append((var, A[i]))
    var = var - A[i]

  print(var)
  for i in range(len(solver)):
    print(solver[i][0], solver[i][1])
  exit()
  
## 正整数も負整数もある場合
A.sort()
n_nega = sum(a < 0 for a in A)
n_posi = sum(a > 0 for a in A)
solver = []
var = A[n_nega-1]
if n_posi > 1:
  for i in range(n_nega,N-1):
    solver.append((var, A[i]))
    var = var - A[i]
solver.append((A[-1], var))
var = A[-1] - var
if n_nega > 1:
  for i in range(n_nega-1):
    solver.append((var, A[i]))
    var = var - A[i]

print(var)
for i in range(len(solver)):
  print(solver[i][0], solver[i][1])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
print(sum(a)-2*a[0])
for i in range(1,n-2):
  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 = [int(s) for s in input().split(' ')]
A.sort()
print(sum([abs(a) for a in A[1:]]) - A[0])
answer = ''
for i in range(1, len(A) - 1):
    answer += "{} {}\n".format(A[0], A[i])
    A[0] -= A[i]
answer += "{} {}\n".format(A[N - 1], A[0])
print(answer)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
print(sum(a)-2*a[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:
# -*- coding: utf-8 -*-
# input
s = input()
t = input()
ls = len(s)
lt = len(t)
ans = 0
# calc
for i in range(ls, lt-1, -1):
    temp = s[i-lt:i]
    # print(temp)
    counter = 0
    for j in range(lt-1, -1, -1):
        if t[j] == temp[j] or temp[j] == '?':
            counter += 1
            # print(j)
            # print(temp[j])
    if counter == lt:
        ans = i
        break
# output
if ans == 0:
    print('UNRESTORABLE')
else:
    print((s[:ans-lt]+t+s[ans:]).replace('?', 'a'))

Submission status: Wrong Answer

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

A.sort()

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

solver = []
var = A[0]
for i in range(1,N-1):
  solver.append((var, A[i]))
  var = var - A[i]
else:
  solver.append((A[-1], var))
  var = A[-1] - var

print(var)
for i in range(len(solver)):
  print(solver[i][0], solver[i][1])

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
print(sum(A[:-(-N//2)]) - sum(A[-(-N//2):]))
A = deque(A)
if (N-1)%2 == 0:
    p = A.popleft()
else:
    p = A.pop()
for i in range(1, N):
    if i % 2 == (N-1) % 2:
        k = A.popleft()
        print(k, p)
        p = k - p
    else:
        k = A.pop()
        print(k, p)
        p = k - p

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    A = i2nn()
    A.sort()
    a0 = A[0]
    a3 = A[N-1]
    iz = bisect_left(A, 0)
    A1 = A[1:iz]
    A2 = A[iz:N-1]

    logs = []
    def logging(x, y):
        logs.append('%d %d' % (x, y))
        return x - y
    x = a0
    for y in A2:
        x = logging(x, y)
    y = x
    x = a3
    x = logging(x, y)
    for y in A1:
        x = logging(x, y)
    print(x)
    for log in logs:
        print(log)

main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(list(map(int,input().split())))
if a[n-1] < 0:
    ans = 2*a[n-1]-sum(a)
    print(ans)
    p,q =a[n-1],a[0]
    for i in range(n-1):
        print(p,q)
        p = p-q
        q = a[i+1]
elif a[0] >= 0:
    ans = sum(a)-2*a[n-1]
    print(ans)
    p,q = a[0],a[1]
    for i in range(n-2):
        print(p,q)
        p = p-q
        q = a[i+1]
    print(q,p)

else:
    num = 0
    ans = 0
    for i in range(n):
        if a[i] < 0:
            ans -=a[i]
        else:
            if a[i-1] < 0 and a[i] >= 0:
                num = i
            ans += a[i]
    print(ans)
    p = a[0]
    for i in range(num,n-1):
        print(p,a[i])
        p -= a[i]
    q = a[n-1]
    print(q,p)
    q -= p
    for i in range(1,num):
        print(q,a[i])
        q -= a[i]

Submission status: Wrong Answer

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

# A[cut-1] < 0 and A[cut] >= 0
cut = 0
while cut < N and A[cut] < 0:
    cut += 1

if cut == 0:
    M = sum(A[1:]) - A[0]
    print(M)
    pair = [A[0], A[1]]
    for i in range(N-2):
        print(' '.join(map(str, pair)))
        pair = [pair[0] - pair[1], A[i+2]]
    print(' '.join(map(str, pair)))

elif cut == N:
    M = A[-1] - sum(A[:N-1])
    print(M)
    pair = [A[N-1], A[0]]
    for i in range(N-2):
        print(' '.join(map(str, pair)))
        pair = [pair[0] - pair[1], A[i+1]]
    print(' '.join(map(str, pair)))

else:
    M = sum(A[cut:]) - sum(A[:cut])
    print(M)
    pair = [A[cut-1], A[N-1]]
    for i in range(N - cut - 2):
        print(' '.join(map(str, pair)))
        pair = [pair[0] - pair[1], A[N-i-2]]
    print(' '.join(map(str, pair)))
    
    rem = pair[0] - pair[1]
    if cut == 1 and N != 2:
        pair = [A[cut], rem]
        print(' '.join(map(str, pair)))
    elif N != 2:
        pair = [A[cut], A[0]]
        for i in range(cut - 2):
            print(' '.join(map(str, pair)))
            pair = [pair[0] - pair[1], A[i+1]]
        print(' '.join(map(str, pair)))
        pair = [pair[0]- pair[1], rem]
        print(' '.join(map(str, pair)))

Submission status: Wrong Answer

Solution code:
from bisect import bisect

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

ans = []

if len(A) == 2 :
    ans.append(A[::-1])
    calc = A[1] - A[0]

else :
    ind = bisect(A,0)

    X = A[:ind]
    Y = A[ind:]
    
    if len(X) == 0 :
        ans.append([Y[0],Y[1]])
        X.append(Y[0]-Y[1])
        Y.pop(0)
        Y.pop(0)
    elif len(Y) == 0 :
        ans.append([X[1],X[0]])
        Y.append(X[1]-X[0])
        X.pop(-1)
        X.pop(-1)
    
    while True :
        if len(X) >= 2 :
            ans.append([Y[0],X[-1]])
            Y[0] -= X.pop(-1)
        elif len(Y) >= 2 :
            ans.append([X[-1],Y[0]])
            X[-1] -= Y.pop(0)
        else :
            ans.append([Y[0],X[0]])
            calc = Y.pop(0) - X.pop(0)
            break
    
print(calc)
for i in range(len(ans)) :
    print(*ans[i])

Submission status: Wrong Answer

Solution code:
from bisect import bisect

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

ans = []

if len(A) == 2 :
    ans.append(A)
    calc = A[1] - A[0]

else :
    ind = bisect(A,0)

    X = A[:ind]
    Y = A[ind:]
    
    if len(X) == 0 :
        ans.append([Y[0],Y[1]])
        X.append(Y[0]-Y[1])
        Y.pop(0)
        Y.pop(0)
    elif len(Y) == 0 :
        ans.append([X[1],X[0]])
        Y.append(X[1]-X[0])
        X.pop(-1)
        X.pop(-1)
    
    while True :
        if len(X) >= 2 :
            ans.append([Y[0],X[-1]])
            Y[0] -= X.pop(-1)
        elif len(Y) >= 2 :
            ans.append([X[-1],Y[0]])
            X[-1] -= Y.pop(0)
        else :
            ans.append([Y[0],X[0]])
            calc = Y.pop(0) - X.pop(0)
            break
    
print(calc)
for i in range(len(ans)) :
    print(*ans[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

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


max_a = a.pop()
min_a = a.popleft()

ans_s = ""
while True:
    if len(a) <= 0:
        ans = max_a - min_a
        ans_s += "{} {}\n".format(max_a, min_a)
        break

    if max_a <= 0:
        max_a -= min_a
        ans_s += "{} {}\n".format(max_a, min_a)

        min_a = a.popleft()
    elif min_a >= 0:
        min_a -= max_a
        ans_s += "{} {}\n".format(min_a, max_a)

        max_a = a.pop()
    else:
        w = a.pop()
        if w < 0:
            max_a -= w
            ans_s += "{} {}\n".format(max_a, w)
        else:
            min_a -= w
            ans_s += "{} {}\n".format(min_a, w)

print(ans)
print(ans_s, end="")

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
plus = [x for x in A if x > 0]
minus = [x for x in A if x <= 0]
plus.sort()
minus.sort()
minA = minus.pop() if minus else plus.pop(0)
M = sum(plus) - sum(minus) - minA
print(M)
xy = minA
for i in range(N - 1):
  if len(plus) > 1:
    y = plus.pop()
    print('{} {}'.format(xy, y))
    xy -= y
  elif len(plus) == 1:
    x = plus.pop()
    print('{} {}'.format(x, xy))
    xy = x - xy
  else:
    y = minus.pop()
    print('{} {}'.format(xy, y))
    xy -= y

Submission status: Wrong Answer

Solution code:
import bisect
import heapq
N = int(input())
A = sorted([int(_) for _ in input().split()])
i = min(N - 1, bisect.bisect_left(A, 0))
j = max(1, bisect.bisect_right(A, 0))
now = A[0]
ans = []
for a in A[j:N - 1]:
    ans += [[now, a]]
    now -= a
ans += [[A[-1], now]]
now = A[-1] - now
for a in A[1:i]:
    ans += [[now, a]]
    now -= a
print(now)
for xy in ans:
    print(*xy)
for _ in range(N - 1 - len(xy)):
    print(now, 0)

Submission status: Wrong Answer

Solution code:
import bisect
import heapq
N = int(input())
A = sorted([int(_) for _ in input().split()])
i = min(N - 1, bisect.bisect_left(A, 0))
j = max(1, bisect.bisect_right(A, 0))
now = A[0]
ans = []
for a in A[j:N - 1]:
    ans += [[now, a]]
    now -= a
ans += [[A[-1], now]]
now = A[-1] - now
for a in A[1:i]:
    ans += [[now, a]]
    now -= a
print(now)
for xy in ans:
    print(*xy)

Submission status: Wrong Answer

Solution code:
import bisect
import heapq
N = int(input())
A = sorted([int(_) for _ in input().split()])
i = bisect.bisect_left(A, 0)
j = bisect.bisect_right(A, 0)
now = A[0]
ans = []
for a in A[j:N - 1]:
    ans += [[now, a]]
    now -= a
ans += [[A[-1], now]]
now = A[-1] - now
for a in A[1:i]:
    ans += [[now, a]]
    now -= a
print(now)
for xy in ans:
    print(*xy)