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  = sorted(A)
# print(A)
for index in range(N - 1):
    # print(A)
    if ((N - index) % 2) == 1:
        A[index + 1] = A[index] - A[index + 1]
    else:
        A[index + 1] = A[index + 1] - A[index]


print(A[N - 1])

Submission status: Wrong Answer

Solution code:
#解説参照
#構成が無理
#テストどうやってんだろ
n = int(input())
a = list(map(int, input().split( )))

a.sort()
from bisect import bisect_left

j = bisect_left(a,0)
if j==0:
    j+=1
elif j==n:
    j-=1
print(sum(a[j:])-sum(a[:j]))
x = a[0]
for i in range(j,n-1):
    print(a[0],a[i])
    a[0]-=a[i]

for i in range(j):
    print(a[-1],a[i])
    x-=a[i]

Submission status: Wrong Answer

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

ans = []
A.sort()
if A[0] >= 0:
    ans.append([A[0], A[1]])
    A[1] = A[0]-A[1]
    A.pop(0)
elif A[-1] <= 0:
    ans.append([A[-1], A[-2]])
    A[-2] = A[-1]-A[-2]
    A.pop(N-1)
for i in range(1, len(A)-1):
    if A[i] < 0:
        ans.append([A[-1], A[i]])
        A[-1] -= A[i]
    else:
        ans.append([A[0], A[i]])
        A[0] -= A[i]
ans.append([A[-1], A[0]])
print(A[-1]-A[0])
for a in ans:
    print(*a)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
li.sort(reverse=True)
plus = []
minus = []
lin = []
for i in li:
    if i < 0:
        minus.append(i)
    else:
        plus.append(i)
if not plus:
    x = minus[0]
    for i in range(1,n):
        y = minus[i]
        lin.append([x,y])
        x -= y
    print(x)
    for i in lin:
        print(*i)
elif not minus:
    x = plus[-1]
    for i in range(1,n-1):
        y = plus[i]
        lin.append([x,y])
        x -= y
    lin.append([plus[0],x])
    print(plus[0]-x)
    for i in lin:
        print(*i)
else:
    plus = plus[::-1]
    mi = minus.pop()
    pu = plus.pop()
    while minus:
        lin.append([mi,minus[-1]])
        mi = mi - minus[-1]
        minus.pop()
    while plus:
        lin.append([mi,plus[-1]])
        mi = mi - plus[-1]
        plus.pop()
    lin.append([pu,mi])
    print(pu-mi)
    for i in lin:
        print(*i)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
li.sort(reverse=True)
plus = []
minus = []
lin = []
for i in li:
    if i < 0:
        minus.append(i)
    else:
        plus.append(i)
if not plus:
    x = minus[0]
    for i in range(1,n):
        y = minus[i]
        lin.append([x,y])
        x -= y
    print(x)
    for i in lin:
        print(*i)
elif not minus:
    plus = plus[::-1]
    x = plus[0]
    for i in range(1,n-1):
        lin.append([x,plus[i]])
        x -= plus[i]
    lin.append([plus[-1],x])
    print(plus[-1]-x)
    for i in lin:
        print(*i)
else:
    plus = plus[::-1]
    mi = minus.pop()
    pu = plus.pop()
    while minus:
        lin.append([mi,minus[-1]])
        mi = mi - minus[-1]
        minus.pop()
    while plus:
        lin.append([mi,plus[-1]])
        mi = mi - plus[-1]
        plus.pop()
    lin.append([pu,mi])
    print(pu-mi)
    for i in lin:
        print(*i)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
li.sort(reverse=True)
plus = []
minus = []
lin = []
for i in li:
    if i < 0:
        minus.append(i)
    else:
        plus.append(i)
if not plus:
    x = minus[0]
    for i in range(1,n):
        y = minus[i]
        lin.append([x,y])
        x -= y
    print(x)
    for i in lin:
        print(*i)
elif not minus:
    plus = plus[::-1]
    x = plus[0]
    for i in range(1,n-1):
        lin.append([x,plus[i]])
        x -= plus[i+1]
    lin.append([plus[-1],x])
    print(plus[-1]-x)
    for i in lin:
        print(*i)
else:
    plus = plus[::-1]
    mi = minus.pop()
    pu = plus.pop()
    while minus:
        lin.append([mi,minus[-1]])
        mi = mi - minus[-1]
        minus.pop()
    while plus:
        lin.append([mi,plus[-1]])
        mi = mi - plus[-1]
        plus.pop()
    lin.append([pu,mi])
    print(pu-mi)
    for i in lin:
        print(*i)

Submission status: Wrong Answer

Solution code:
from sys import stdin,setrecursionlimit
from collections import deque
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
li.sort()
li = deque(li)
lin = []
while len(li) >= 3:
    if li[1] >= 0:
        k = li[0]-li[1]
        lin.append([li[0],li[1]])
        li.popleft();li.popleft()
        li.appendleft(k)
    else:
        k = li[1]-li[0]
        lin.append([li[1],li[0]])
        li.popleft();li.popleft()
        li.appendleft(k)
if li[0] <= li[1]:
    lin.append([li[1],li[0]])
    print(li[1]-li[0])
else:
    lin.append([li[0],li[1]])
    print(li[0]-li[1])
for i in lin:
    print(*i)

Submission status: Wrong Answer

Solution code:
from sys import stdin,setrecursionlimit
from collections import deque
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
li.sort()
li = deque(li)
lin = []
while len(li) >= 3:
    if li[1] >= 0:
        k = li[0]-li[1]
        lin.append([li[0],li[1]])
        li.popleft();li.popleft()
        li.appendleft(k)
    else:
        k = li[1]-li[0]
        lin.append([li[1],li[0]])
        li.popleft();li.popleft()
        li.appendleft(k)
lin.append([li[1],li[0]])
print(li[1]-li[0])
for i in lin:
    print(*i)

Submission status: Wrong Answer

Solution code:
from sys import stdin,setrecursionlimit
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
li.sort()
lin = []
while len(li) >= 3:
    if li[1] >= 0:
        k = li[0]-li[1]
        lin.append([li[0],li[1]])
        li.pop(0);li.pop(0)
        li = [k] + li
    else:
        k = li[1]-li[0]
        lin.append([li[1],li[0]])
        li.pop(0);li.pop(0)
        li = [k] + li
lin.append([li[1],li[0]])
print(li[1]-li[0])
for i in lin:
    print(*i)

Submission status: Wrong Answer

Solution code:
from sys import stdin,setrecursionlimit
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
li.sort()
lin = []
while len(li) >= 3:
    if li[1] >= 0:
        k = li[0]-li[1]
        lin.append([li[0],li[1]])
        li.pop(0);li.pop(0)
        li = [k] + li
    else:
        k = li[1]-li[0]
        lin.append([li[1],li[0]])
        li.pop(0);li.pop(0)
        li.append(k)
        li = [k] + li
lin.append([li[1],li[0]])
print(li[1]-li[0])
for i in lin:
    print(*i)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline

def main():
    N = int(input())
    a = list(map(int,input().split()))
    
    a.sort()
    l = len(a)
    al = []
    num = a[0]
    for i in range(l-2):
        al.append((num,a[i+1]))
        num = num-a[i+1]
        
    al.append((a[-1],num))
    print(a[-1]-num)
    for l,r in al:
        print(*(l,r))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,*A=map(int, open(0).read().split())
A.sort()
m,M=A[0],A[-1]
B=A[1:-1]
for b in B:
    if b>=0:
        print(m,b)
        m-=b
    else:
        print(M,b)
        M-=b
print(M,m)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

if A[0]<0 and A[-1]<0:
    ans = sum(map(abs, A[:-1]))+A[-1]
    A.reverse()

    r = 1
    a, b = A[0], A[1]
    t = []
    
    while 1:
        x, y = (a, b) if a>b else (b, a)
        t.append((x, y))
        r += 1
        if not r<N:
            break
        a, b = x-y, A[r]

elif A[0]>0 and A[-1]>0:
    ans = sum(A[1:])-A[0]

    r = 1
    a, b = A[0], A[1]
    t = []
    
    while 1:
        x, y = (a, b) if a<b else (b, a)
        t.append((x, y))
        
        r += 1
        if not r<N:
            break
        a, b = x-y, A[r]
    a, b = t[-1]
    t[-1] = (b, a)

else:
    ans = sum(map(abs, A))
    p = bisect_left(A, 0)
    t = []
    s = []

    a = A[p-1]
    for i in range(p, N-1):
        b = A[i]
        t.append((a, b))
        a = a - b
        
    c, d = A[N-1], 0
    for i in ([a]+A[:p-1]):
        t.append((c, i))
        c = c-i

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

Submission status: Wrong Answer

Solution code:
N = int(input())
*A, = map(int, input().split())
A.sort()
r = 1
a, b = A[0], A[1]
t = []

while 1:
    x, y = (a, b) if a<b else (b, a)
    t.append((x, y))
    
    r += 1
    if not r<N:
        break
    a, b = x-y, A[r]

a, b = t[-1]
t[-1] = (b, a)
print(b-a)
for i in t:
    print(*i)

Submission status: Wrong Answer

Solution code:
N = int(input())
*A, = map(int, input().split())
A.sort()
r = 1
a, b = A[0], A[1]
t = []

while 1:
    x, y = (a, b) if a<b else (b, a)
    t.append((x, y))
    
    r += 1
    if not r<N:
        break
    a, b = x-y, A[r]

a, b = t[-1]
t[-1] = (b, a)
print(len(t))
for i in t:
    print(*i)

Submission status: Wrong Answer

Solution code:

N = int(input())

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

#%%

A.sort()

keyz = 0
for a in A:
    if a<0:
        keyz += 1

if keyz==0:
    keyz = 1
elif keyz == N:
    keyz = N-1

# A[0:keyz]までを負とする
    

# A[keyz:]は反転させる必要あり

if keyz == N-1:
    cur = A[-1]
    for i in range(N-1):
        print(cur,A[i])
        cur -= A[i]
    
else:            
    cur = A[0]
    for i in range(keyz,N-1):
        print(cur,A[i])
        cur = cur - A[i]
    print(A[N-1],cur)
    cur = A[N-1] - cur
    for i in range(1,keyz):
        print(cur,A[i])
        cur -= A[i]

Submission status: Wrong Answer

Solution code:
n = int(input())
L = list(map(int,input().split()))
L.sort()
T = []
plus = []
minus = []
for i in range(n):
  if L[i] >= 0:
    plus.append(L[i])
  else:
    minus.append(L[i])
if len(L) == 2:
  if len(plus) == len(minus):
    print(plus[0]-minus[0])
    print(plus[0],minus[0])
  elif len(plus) == 2:
    print(plus[1]-plus[0])
    print(plus[1],plus[0])
  else:
    print(minus[1]-minus[0])
    print(minus[1],minus[0])
else:
  if len(plus) != 0 and len(minus) != 0:
    T.append([minus[0],plus[0]])
    cur = minus[0]-plus[0]
    for i in range(1,len(plus)-1):
      T.append([cur,plus[i]])
      cur = cur-plus[i]
    T.append([plus[len(plus)-1],cur])
    cur = plus[len(plus)-1]-cur
    for i in range(1,len(minus)):
      T.append([cur,minus[i]])
      cur = cur-minus[i]
    print(cur)
    for i in range(len(T)):
      print(T[i][0],T[i][1])
  elif len(plus) == 0:
    cur = minus[len(minus)-1]
    for i in range(n-1):
      T.append([cur,minus[n-i-2]])
      cur = cur-minus[n-i-2]
    print(cur)
    for i in range(len(T)):
      print(T[i][0],T[i][1])
  else:
    cur = plus[0]
    for i in range(1,n-1):
      T.append([cur,plus[i]])
      cur = cur-plus[i]
    T.append([plus[n-1],cur])
    cur = plus[n-1]-cur
    print(cur)
    for i in range(len(T)):
      print(T[i][0],T[i][1])

Submission status: Wrong Answer

Solution code:
n = int(input())
L = list(map(int,input().split()))
L.sort()
T = []
plus = []
minus = []
for i in range(n):
  if L[i] >= 0:
    plus.append(L[i])
  else:
    minus.append(L[i])
if len(plus) != 0 and len(minus) != 0:
  T.append([minus[0],plus[0]])
  cur = minus[0]-plus[0]
  for i in range(1,len(plus)-1):
    T.append([cur,plus[i]])
    cur = cur-plus[i]
  T.append([plus[len(plus)-1],cur])
  cur = plus[len(plus)-1]-cur
  for i in range(1,len(minus)):
    T.append([cur,minus[i]])
    cur = cur-minus[i]
  print(cur)
  for i in range(len(T)):
    print(T[i][0],T[i][1])
elif len(plus) == 0:
  cur = minus[len(minus)-1]
  for i in range(n-1):
    T.append([cur,minus[n-i-2]])
    cur = cur-minus[n-i-2]
  print(cur)
  for i in range(len(T)):
    print(T[i][0],T[i][1])
else:
  cur = plus[0]
  for i in range(1,n-1):
    T.append([cur,plus[i]])
    cur = cur-plus[i]
  T.append([plus[n-1],cur])
  cur = plus[n-1]-cur
  print(cur)
  for i in range(len(T)):
    print(T[i][0],T[i][1])

Submission status: Wrong Answer

Solution code:
n = int(input())
L = list(map(int,input().split()))
L.sort()
T = []
while len(L) != 2:
  cura = L.pop(len(L)-1)
  curb = L.pop(0)
  T.append([curb,cura])
  L.append(curb-cura)
  L.sort()
T.append([L[1],L[0]])
print(L[1]-L[0])
for i in range(len(T)):
  print(T[i][0],T[i][1])

Submission status: Wrong Answer

Solution code:
n = int(input())
L = list(map(int,input().split()))
L.sort()
T = []
while len(L) != 1:
  cura = L.pop(len(L)-1)
  curb = L.pop(0)
  if len(L)%2 == 0:
    T.append([cura,curb])
    L.append(cura-curb)
  else:
    T.append([curb,cura])
    L.append(curb-cura)
  L.sort()
print(L[0])
for i in range(len(T)):
  print(T[i][0],T[i][1])

Submission status: Wrong Answer

Solution code:

N = int(input())
A = list(map(int,input().split()))
#%%
A.sort()
minus = 0
for a in A:
    if a<0:
        minus += 1
        
M = 0

if minus<=(N//2):
    m = N//2
elif minus==N:
    m = N-1
else:
    m = minus
# m: マイナスをつける数

for i,a in enumerate(A):
    if i<m:
        M -= a
    else:
        M += a

print(M)

mins = A[:m]
pls = A[m:]

inv = N-1-m
invs = []

idx = 0
mi = 0
nxt_ = pls[-1]
for i in range(N-1):
    if i<inv:
        nxt = mins[i]-pls[i]
        invs.append(nxt)
        print(mins[i],pls[i])
    else:
        L = len(invs)
        if idx<L:
            print(nxt_, invs[idx])
            nxt_ = nxt_ - invs[idx]
            idx += 1
        else:
            print(nxt_, mins[inv+mi])
            nxt_ = nxt_ - mins[inv+mi]
            mi += 1

Submission status: Wrong Answer

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=10**9+7

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

lst=[]
tmp=a[0]
flag=0
for i in range(n-1):
    if a[i+1]>0:
        flag=i+1
        break
if flag==0:
    flag=n-1

for i in range(n):
    if i<flag:
        lst.append([tmp, a[-1-i]])
        tmp-=a[-1-i]
    if i==flag:
        lst.append([a[-i-1],tmp])
        tmp=a[-i-1]-tmp
    if i>flag:
        lst.append([tmp,a[-1-i]])
        if i==n-1:
            continue
        tmp-=a[-1-i]

print(tmp)
for i in range(n-1):
    print(*lst[i])
    
# print(lst)

Submission status: Wrong Answer

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=10**9+7

n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=0
minus=1
lst=[]

ans=-a[0]
tmp=a[0]
flag=0
for i in range(n-1):
    if a[i+1]>0:
        flag=i+1
        break
# print(flag)
for i in range(n-1):
    # print(a[-i-1])
    if i<flag:
        lst.append([tmp, a[-1-i]])
        tmp-=a[-1-i]
    if i==flag:
        lst.append([a[-i-1],tmp])
        tmp=a[-i-1]-tmp
    if i>flag:
        lst.append([tmp,a[-1-i]])
        tmp-=a[-1-i]

print(tmp)
for i in range(n-1):
    print(*lst[i])
    
# print(lst)

Submission status: Wrong Answer

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=10**9+7

n=int(input())
a=list(map(int,input().split()))
a.sort()
ans1=0
for i in range(n):
    if i<n//2:
        ans1-=a[i]
    else:
        ans1+=a[i]
# print(ans1)

a.reverse()
ans2=0
for i in range(n):
    if i<n//2:
        ans2-=a[i]
    else:
        ans2+=a[i]
# print(ans2)
lst=[]
if ans1>=ans2:
    print(ans1)
    a.reverse()
else:
    print(ans2)
d=deque(a)
for i in range(n):
    if i%2==0:
        lst.append(d.pop())
    else:
        lst.append(d.popleft())
# print(lst)
anslst=[]
d=deque(lst)
tmp=d.pop()
while d:
    tmp2=d.pop()
    anslst.append([tmp2,tmp])
    tmp=tmp2-tmp
# print(anslst)
for i in range(n-1):
    print(*anslst[i])

Submission status: Wrong Answer

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=10**9+7

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

for i in range(n):
    if i<n//2:
        ans-=a[i]
    else:
        ans+=a[i]
print(ans)
lst=[]
d=deque(a)
for i in range(n):
    if i%2==0:
        lst.append(d.pop())
    else:
        lst.append(d.popleft())
# print(lst)
anslst=[]
d=deque(lst)
tmp=d.pop()
while d:
    tmp2=d.pop()
    anslst.append([tmp2,tmp])
    tmp=tmp2-tmp
# print(anslst)
for i in range(n-1):
    print(*anslst[i])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
xy=[]
for i in range(n-2,0,-1):
  y=a[i]
  if y>0:
    x=a[0]
    a[0]-=y
  else:
    x=a[n-1]
    a[n-1]-=y
    xy.append((x,y))
print(a[n-1]-a[0])
xy.append((a[n-1],a[0]))
print('\n'.join([' '.join(map(str,t)) for t in xy]))

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(str(a[1])+' '+str(a[0]))
  exit()
ans=0
for i in range(n):
  ans+=abs(a[i])
if a[0]>0:
  print(sum(a)-2*a[0])
elif a[-1]<0:
  print(2*a[-1]-sum(a))
else:
  print(ans)
plus=list()
minus=list()
for i in range(n):
  if a[i]>=0:
    plus.append(a[i])
  else:
    minus.append(a[i])
if len(minus)>0:
  while len(plus)>=2:
    p=plus.pop(0)
    m=minus.pop(0)
    minus.append(m-p)
    print(str(m)+' '+str(p))
if len(plus)>=2 and len(minus)==0:
  plus.sort()
  p1=plus.pop(0)
  p2=plus.pop(-1)
  minus.append(p1-p2)
  print(str(p1)+' '+str(p2))
if len(plus)==0:
  minus.sort()
  m1=minus.pop(-1)
  m2=minus.pop(-2)
  plus.append(m1-m2)
  print(str(m1)+' '+str(m2))
while len(minus)>0:
  p=plus.pop(0)
  m=minus.pop(0)
  plus.append(p-m)
  print(str(p)+' '+str(m))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=0
for i in range(n):
  ans+=abs(a[i])
if a[0]>0:
  print(sum(a)-2*a[0])
else:
  print(ans)
plus=list()
minus=list()
for i in range(n):
  if a[i]>=0:
    plus.append(a[i])
  else:
    minus.append(a[i])
if len(minus)>0:
  while len(plus)>=2:
    p=plus.pop(0)
    m=minus.pop(0)
    minus.append(m-p)
    print(str(m)+' '+str(p))
if len(plus)>=2 and len(minus)==0:
  while len(plus)>=3:
    plus.sort()
    p1=plus.pop(0)
    p2=plus.pop(-1)
    plus.insert(0,p1-p2)
    print(str(p2)+' '+str(p1))
  print(str(plus[1])+' '+str(plus[0]))
if len(plus)==0:
  minus.sort()
  m1=minus.pop(-1)
  m2=minus.pop(-2)
  plus.append(m1-m2)
  print(str(m1)+' '+str(m2))
while len(minus)>0:
  p=plus.pop(0)
  m=minus.pop(0)
  plus.append(p-m)
  print(str(p)+' '+str(m))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
l=[a[0]]
r=[a[-1]]
for i in range(1,n-1):
    if a[i]<=0:
        r.append(a[i])
    else:
        l.append(a[i])
r.sort()
l.sort()
if len(r)==1:
    X=r[0]
    p=[]
else:
    p=[[r[-1],r[-2]]]
    for i in range(len(r)-3,-1,-1):
        p.append([p[-1][0]-p[-1][1],r[i]])
    X=p[-1][0]-p[-1][1]
if len(l)==1:
    Y=l[0]
    q=[]
else:
    q=[[l[0],l[1]]]
    for i in range(2,len(l)):
        q.append([q[-1][0]-q[-1][1],l[i]])
    Y=q[-1][0]-q[-1][1]
    #print(q)
print(X-Y)
if p and q:
    for i in range(len(p)):
        print(*p[i])
    for i in range(len(q)):
        print(*q[i])
elif p:
    for i in range(len(p)):
        print(*p[i])
    print(X,Y)
elif q:
    for i in range(len(q)):
        print(*q[i])
    print(X,Y)
else:
    print(X,Y)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
l=[a[0]]
r=[a[-1]]
for i in range(1,n-1):
    if a[i]<=0:
        r.append(a[i])
    else:
        l.append(a[i])
if len(r)==1:
    X=r[0]
    p=[]
else:
    p=[[r[-1],r[-2]]]
    for i in range(len(r)-2,-1,-1):
        p.append([p[-1][0]-p[-1][1],r[i]])
    X=p[-1][0]-p[-1][1]
if len(l)==1:
    Y=l[0]
    q=[]
else:
    q=[[l[0],l[1]]]
    for i in range(2,len(l)):
        q.append([q[-1][0]-q[-1][1],l[i]])
    Y=q[-1][0]-q[-1][1]
    #print(q)
print(X-Y)
if p and q:
    for i in range(len(p)):
        print(*p[i])
    for i in range(len(q)):
        print(*q[i])
elif p:
    for i in range(len(p)):
        print(*p[i])
    print(X,Y)
elif q:
    for i in range(len(q)):
        print(*q[i])
    print(X,Y)
else:
    print(X,Y)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
A.sort()
# -の合計数は最大N-1、最小N//2
minus = bisect_left(A, 0)
# minusは負の数の個数
m = min(minus, N)
if m == 0: m += 1
answer = -sum(A[:m]) + sum(A[m:])
print(answer)
cur = A[0]
for i in range(m, N-1):
    print(cur, A[i])
    cur -= A[i]

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

# 53

Submission status: Wrong Answer

Solution code:
def main(lis):
    if len(lis) == 2:
        print(max(lis) - min(lis))
        print(max(lis), min(lis))
        return
    min_val = min(lis)
    min_idx = lis.index(min_val)
    others = [x for i, x in enumerate(lis) if i != min_idx]
    sub = min_val
    print(sum(others) - min_val)
    for o in others[:-1]:
        print(sub, o)
        sub = sub - o
    print(others[-1], sub)

input()
lis = list(map(int, input().split()))
main(lis)

Submission status: Wrong Answer

Solution code:
def main(lis):
    if len(lis) == 2:
        print(max(lis) - min(lis))
        print(max(lis), min(lis))
        return
    min_val = min(lis)
    min_idx = lis.index(min_val)
    others = [x for i, x in enumerate(lis) if i != min_idx]
    sub = min_val
    print(sum(others) - min_val)
    for o in others[:-1]:
        print(min_val, o)
        sub = sub - o
    print(others[-1], sub)

input()
lis = list(map(int, input().split()))
main(lis)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
M = A[0]
X = []
pos = N
for i in range(N):
  if A[i] >= 0:
    pos = i
    break
if pos == 0:
  for i in range(1, N - 1):
    M -= A[i]
    X.append(M)
  S = M
  M = A[N-1] - M
  print(M)
  for i in range(N - 2):
    print(X[i], A[i+1])
  print(A[N - 1], S)
elif 1 <= pos <= N - 1:
  M = sum(abs(_) for _ in A)
  print(M)
  L = A[0]
  for j in range(pos, N - 1):
    print(L, A[j])
    L -= A[j]
  R = A[N - 1]
  print(R, L)
  R -= L
  for j in range(1, pos):
    print(R, A[j])
    R -= A[j]
elif pos == N:
  print(A[N-1] - sum(A[i] for i in range(N-1)))
  R = A[N - 1]
  for i in range(N-1):
    print(R, A[i])
    R -= A[i]

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = list(map(int,input().split()))
minus,plus = [],[]
for num in lis:
    if num >= 0:
        plus.append(num)
    elif num < 0:
        minus.append(num)
plus.sort()
minus.sort()
ans = []
if len(plus) == 0:
    now = minus.pop()
    for num in minus:
        ans.append([now,num])
        now -= num
    print(now)
elif len(minus) == 0:
    now = plus.pop()
    last_num = plus.pop()
    for num in plus:
        ans.append([now,num])
        now -= num
    ans.append([last_num,now])
    last_num -= now
    print(last_num)
else:
    now = minus.pop()
    last_num = plus.pop()
    for num in plus:
        ans.append([now,num])
        now -= num
    for num in minus:
        ans.append([last_num,num])
        last_num -= num
    ans.append([last_num,now])
    last_num -= now
    print(last_num)
for i in range(n-1):
    print(*ans[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = list(map(int,input().split()))
minus,plus = [],[]
for num in lis:
    if num >= 0:
        plus.append(num)
    elif num < 0:
        minus.append(num)
plus.sort()
minus.sort()
ans = []
if len(plus) == 0:
    now = minus.pop()
    for num in minus:
        ans.append([now,num])
        now -= num
    print(now)
elif len(minus) == 0:
    now = plus.pop()
    last_num = plus.pop()
    for num in plus:
        ans.append([now,num])
        now -= num
    ans.append([last_num,now])
    last_num -= now
    print(last_num)
elif len(plus) == 1:
    now = plus.pop()
    for num in minus:
        ans.append([now,num])
        now -= num
    print(now)
else:
    now = minus.pop()
    last_num = plus.pop()
    for num in plus:
        ans.append([now,num])
        now -= num
    for num in minus:
        ans.append([last_num,num])
        last_num -= num
    ans.append([last_num,now])
    last_num -= now
    print(last_num)
for i in range(n-1):
    print(*ans[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = list(map(int,input().split()))
minus,plus = [],[]
for num in lis:
    if num >= 0:
        plus.append(num)
    elif num < 0:
        minus.append(num)
plus.sort()
minus.sort()
ans = []
if len(plus) == 0:
    now = minus.pop()
    for num in minus:
        ans.append([now,num])
        now -= num
    print(now)
elif len(minus) == 0:
    now = plus.pop()
    last_num = plus.pop()
    for num in plus:
        ans.append([now,num])
        now -= num
    ans.append([last_num,now])
    print(now)
elif len(plus) == 1:
    now = plus.pop()
    for num in minus:
        ans.append([now,num])
        now -= num
    print(now)
else:
    now = minus.pop()
    last_num = plus.pop()
    for num in plus:
        ans.append([now,num])
        now -= num
    for num in minus:
        ans.append([last_num,num])
        last_num -= num
    ans.append([last_num,now])
    last_num -= now
    print(last_num)
for i in range(n-1):
    print(*ans[i])

Submission status: Wrong Answer

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

A.sort()
xy=[]
l=0
r=N-1
for i in range(N-1):
    if (N-1-i)&1:
        #残り奇数回は小さい方を引く
        x=A[l+1]
        y=A[l]
        A[l+1]-=y
        l+=1
    else:
        #残り偶数回は大きい方を引く
        x=A[l]
        y=A[r]
        A[l]-=y
        r-=1
    xy.append((x,y))

print(A[r])
print('\n'.join([' '.join(map(str,t)) for t in xy]))

Submission status: Wrong Answer

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

A.sort()
xy=[]
l=0
r=N-1
for i in range(N-1):
    if (N-1-i)&1:
        #残り奇数回は小さい方を引く
        x=A[r]
        y=A[l]
        A[l+1]-=y
        l+=1
    else:
        #残り偶数回は大きい方を引く
        x=A[l]
        y=A[r]
        A[l]-=y
        r-=1
    xy.append((x,y))

print(A[r])
print('\n'.join([' '.join(map(str,t)) for t in xy]))

Submission status: Wrong Answer

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

A.sort()
xy=[]
l=0
r=N-1
for i in range(N-1):
    if (N-1-i)&1:
        #残り奇数回は小さい方を引く
        x=A[r]
        y=A[l]
        A[r]-=y
        l+=1
    else:
        #残り偶数回は大きい方を引く
        x=A[l]
        y=A[r]
        A[l]-=y
        r-=1
    xy.append((x,y))

print(A[r])
print('\n'.join([' '.join(map(str,t)) for t in xy]))

Submission status: Wrong Answer

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

M=max(lists)
m=min(lists)
if M<=0:
    sums=0
    for i in range(n):
        if i!=n-1:
            sums-=lists[i]
        else:
            sums+=lists[i]
    print(sums)
    
    c=lists[n-1]
    for i in range(1,n):
            
            print(c,lists[n-1-i])
            c-=lists[i]

elif m>=0:
    print(sum(lists)-2*lists[0])
    c=lists[0]
    for i in range(1,n):
        if i!=n-1:
            print(c,lists[i])
            c-=lists[i]
        else:
            print(lists[i],c)
        
else:
    index=0
    sums=0
    flag=True
    for i in range(n):
        sums+=abs(lists[i])
        if lists[i]<0 and lists[i+1]>=0 and flag:
            index=i
            flag=False
    print(sums)
    c=lists[index]
    for i in range(index+1,n-1):
        print(c,lists[i])
        c-=lists[i]
    print(lists[n-1],c)
    c=lists[n-1]-c
    for i in range(index):
        print(c,lists[i])
        c-=lists[i]

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
A=sorted(A,reverse=True)
lst=[]

while len(A)>2:
    a,b=A.pop(),A.pop()
    lst.append([a,b])
    A.append(a-b)
a,b=A[0],A[1]
lst.append([a,b])
print(a-b)
for a,b in lst:
    print(a,b)

Submission status: Wrong Answer

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

M=max(lists)
m=min(lists)
if M<=0:
    sums=0
    for i in range(n):
        if i!=n-1:
            sums-=lists[i]
        else:
            sums+=lists[i]
    print(sums)
    
    c=lists[n-1]
    for i in range(1,n):
            
            print(c,lists[n-1-i])
            c-=lists[i]

elif m>=0:
    print(sum(lists)-2*lists[0])
    c=lists[0]
    for i in range(1,n):
        if i!=n-1:
            print(c,lists[i])
            c-=lists[i]
        else:
            print(lists[i],c)
        
else:
    index=0
    sums=0
    flag=True
    for i in range(n):
        sums+=abs(lists[i])
        if lists[i]<0 and lists[i+1]>0 and flag:
            index=i
            flag=False
    print(sums)
    c=lists[index]
    for i in range(index+1,n-1):
        print(c,lists[i])
        c-=lists[i]
    print(lists[n-1],c)
    c=lists[n-1]-c
    for i in range(index):
        print(c,lists[i])
        c-=lists[i]

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
A=sorted(A,reverse=True)
lst=[]

while len(A)>2:
    a,b=A.pop(),A.pop()
    lst.append([a,b])
    A.append(a-b)
a,b=A[0],A[1]
lst.append([a,b])
A.append(a-b)

print(A[0])
for a,b in lst:
    print(a,b)