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

ans1=[]

def gu_re(ans):
  ch=0
  l=len(ans)
  next=[]
  while ch!=l//2:
    if ch%2==0:
      x=ans[-1-ch]
      y=ans[ch]
      next.append(x-y)
      ans1.append([x,y])
      ch+=1
      
    else:
      x=ans[ch]
      y=ans[-1-ch]
      next.append(x-y)
      ans1.append([x,y])
      ch+=1
      
  next.sort()
  return next
  
  
def ki_re(ans):
  ch1=0
  l1=len(ans)
  next1=[]
  while ch1!=l1//2:
    if ch1%2==0:
      x=ans[ch1]
      y=ans[-1-ch1]
      next1.append(x-y)
      ans1.append([x,y])
      ch1+=1
    else:
      x=ans[-1-ch1]
      y=ans[ch1]
      next1.append(x-y)
      ans1.append([x,y])
      ch1+=1
      
  next1.append(ans[ch1])
  next1.sort()
  return next1

def solve(r):
  next=r
  while len(next)!=1:
    if len(next)%2==0:
      d=gu_re(next)
      
    else:
      d=ki_re(next)
      
    next=d
    
  print(next[0])
  for i in ans1:
    print(i[0],i[1])
    
solve(a)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys

# from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
printout = sys.stdout.write
sprint = sys.stdout.flush
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
#import math
# from itertools import product, accumulate, combinations, product
import bisect
# import numpy as np
# from copy import deepcopy
#from collections import deque
# from decimal import Decimal
# from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 998244353


def intread():
    return int(sysread())
def mapline(t=int):
    return map(t, sysread().split())
def mapread(t=int):
    return map(t, read().split())


def run():
    N, *A = mapread()
    A.sort()
    i = bisect.bisect_right(A, -EPS)
    cache = []
    if i <= N-1:
        if i < 1:
            i = 1
        ans = A[i-1]
        for k in range(i, N-1):
            cache.append((ans, A[k]))
            ans -= A[k]
        cache.append((A[N-1], ans))
        ans = A[N-1] - ans
        for k in range(0, i-1):
            cache.append((A[k], ans))
            ans -= A[k]
        print(ans)
        for x, y in cache:
            print(f'{x} {y}')
    elif i > N-1:
        ans = A[-1]
        for i in range(N-1):
            cache.append((ans, A[i]))
            ans -= A[i]
        print(ans)
        for x, y in cache:
            print(f'{x} {y}')


if __name__ == "__main__":
    #print(math.gcd(0, 10))
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys

# from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
printout = sys.stdout.write
sprint = sys.stdout.flush
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
#import math
# from itertools import product, accumulate, combinations, product
import bisect
# import numpy as np
# from copy import deepcopy
#from collections import deque
# from decimal import Decimal
# from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 998244353


def intread():
    return int(sysread())
def mapline(t=int):
    return map(t, sysread().split())
def mapread(t=int):
    return map(t, read().split())


def run():
    N, *A = mapread()
    A.sort()
    i = bisect.bisect_right(A, -EPS)
    cache = []
    if i <= N-1:
        if i < 1:
            i = 1
        ans = A[i-1]
        for k in range(i, N-1):
            cache.append((ans, A[k]))
            ans -= A[k]
        cache.append((ans, A[N-1]))
        ans = A[N-1] - ans
        for k in range(0, i-1):
            cache.append((A[k], ans))
            ans -= A[k]
        print(ans)
        for x, y in cache:
            print(f'{x} {y}')
    elif i > N-1:
        ans = A[-1]
        for i in range(N-1):
            cache.append((ans, A[i]))
            ans -= A[i]
        print(ans)
        for x, y in cache:
            print(f'{x} {y}')


if __name__ == "__main__":
    #print(math.gcd(0, 10))
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys

# from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
printout = sys.stdout.write
sprint = sys.stdout.flush
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
#import math
# from itertools import product, accumulate, combinations, product
import bisect
# import numpy as np
# from copy import deepcopy
#from collections import deque
# from decimal import Decimal
# from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 998244353


def intread():
    return int(sysread())
def mapline(t=int):
    return map(t, sysread().split())
def mapread(t=int):
    return map(t, read().split())


def run():
    N, *A = mapread()
    A.sort()
    i = bisect.bisect_right(A, -EPS)
    cache = []
    if i <= N-1:
        if i < 1:
            i = 1
        ans = A[i-1]
        for k in range(i, N-1):
            cache.append((ans, A[k]))
            ans -= A[k]
        cache.append((ans, A[N-1]))
        ans = A[N-1] - ans
        for k in range(0, i-1):
            cache.append((ans, A[k]))
            ans -= A[k]
        print(ans)
        for x, y in cache:
            print(f'{x} {y}')
    elif i > N-1:
        ans = A[-1]
        for i in range(N-1):
            cache.append((ans, A[i]))
            ans -= A[i]
        print(ans)
        for x, y in cache:
            print(f'{x} {y}')


if __name__ == "__main__":
    #print(math.gcd(0, 10))
    run()

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
A=sorted(A)
B=A[:N//2]
A=A[N//2:]
print(sum(A)-sum(B))
if N%2==0:
  a=B[0]
  for i in range(N-1):
    if i%2==0:
      print(A[i//2],a)
      a=A[i//2]-a
    else:
      print(B[(i//2)+1],a)
      a=B[(i//2)+1]-a
else:
  a=A[0]
  for i in range(N-1):
    if i%2==0:
      print(B[i//2],a)
      a=B[i//2]-a
    else:
      print(A[(i//2)+1],a)
      a=A[(i//2)+1]-a

Submission status: Wrong Answer

Solution code:
from collections import *
n=int(input())
a=list(map(int,input().split()))
a.sort()
a=deque(a)
ans=[]
aa=a.pop()
tmp=a.popleft()
while a:
    t=a.popleft()
    ans.append((tmp,t))
    tmp-=t
ans.append((aa,tmp))
print(aa-tmp)
for i in ans:
    print(i[0],i[1])

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = sorted(list(map(int,input().split())))
    m,p = [],[]
    ans = []
    for a_ in a:
        if a_<0:
            m.append(a_)
        else:
            p.append(a_)
    if len(m)==0:
        s = p[0]
        for i in range(1,n-1):
            ans.append([s,a[i]])
            s -= a[i]
        ans.append([a[-1],s])
        s = a[-1]-s
    elif len(p)==0:
        s = m[0]
        for i in range(1,n-1):
            ans.append([s,a[i]])
            s -= a[i]
        ans.append([a[-1],s])
        s = a[-1]-s
    else:
        for i in range(len(p)-1):
            ans.append([m[0],p[i]])
            m[0] -= p[i]
        s = p[-1]
        for m_ in m:
            ans.append([s,m_])
            s -= m_
    print(s)
    for an in ans:
        print(an[0],an[1])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = sorted(list(map(int,input().split())))
    s = a[0]
    ans = []
    for i in range(1,n-1):
        ans.append([s,a[i]])
        s -= a[i]
    ans.append([a[-1],s])
    print(a[-1]-s)
    for an in ans:
        print(an[0],an[1])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

A = sorted(A)

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

print(res)
for i in range(N-1):
  print(*ans[i])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
import sys
#input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))
N = int(input())
A = sorted(inpl())

print(sum(map(abs, A[1:])) - A[0])
for i in range(N-2, 0, -1):
  a = A[i]
  if a > 0:
    print(A[0], a)
    A[0] -= a
  else:
    print(A[-1], a)
    A[-1] -= a
print(A[-1], A[0])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict, deque
import sys
#input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))
N = int(input())
A = sorted(inpl())
if A[-1] < 0:
    print(A[-1] - sum(A[:-1]))
    for i in range(N-2, -1, -1):
        print(A[-1], A[i])
        A[-1] -= A[i]
else:
    print(sum(map(abs, A)))
    for i in range(N-2, 0, -1):
        a = A[i]
        if a > 0:
            print(A[0], a)
            A[0] -= a
        else:
            print(A[-1], a)
            A[-1] -= a
    print(A[-1], A[0])

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = sorted(list(map(int,input().split())))
    ans = []
    while len(a)>2:
        p = a[0:2]
        a = a[2:]
        a.insert(0,p[0]-p[1])
        ans.append(p)
    print(a[1]-a[0])
    for i in range(len(ans)):
        print(ans[i][0],ans[i][1])
    print(a[1],a[0])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

N = int(input())
A = list(map(int, input().split()))
A.sort()
ans = sum(abs(a) for a in A)
if A[0] > 0:
    ans -= 2 * A[0]
elif A[-1] < 0:
    ans += 2 * A[-1]
print(ans)
nonneg_idx = bisect_left(A, 0)
if nonneg_idx == 0:
    nonneg_idx = 1
elif nonneg_idx == N:
    nonneg_idx = N - 1
acc = A[nonneg_idx - 1]
for a in A[nonneg_idx + 1 :]:
    print(acc, a)
    acc -= a
print(A[nonneg_idx], acc)
acc = A[nonneg_idx] - acc
for a in A[: nonneg_idx - 1]:
    print(acc, a)

Submission status: Wrong Answer

Solution code:
N, *A = map(int, open(0).read().split())
A.sort()
ans = []
cur = A[0]
for y in A[1:-1]:
    ans.append((cur, y))
    cur -= y
ans.append((A[-1], cur))
print(ans[-1][0] - ans[-1][1])
for x, y in ans:
    print(x, y)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
if a[0] >= 0:
    print(sum(a) - 2 * a[0])
    x, y = a[0], 0
    for i in range(n - 2):
        x -= y
        y = a[i + 1]
        print(x, y)
    x -= y
    print(a[-1], x)
elif a[-1] <= 0:
    print(-sum(a) + 2 * a[0])
    x, y = a[-1], 0
    for i in range(n - 1):
        x -= y
        y = a[-(i + 2)]
        print(x, y)
else:
    m = 0
    c = 0
    for i in range(n):
        m += abs(a[i])
        if a[i] <= 0:
            c = i
    print(m)
    x, y = a[0], 0
    for i in range(n - 1, c + 1, -1):
        x -= y
        y = a[i]
        print(x, y)
    x -= y
    print(a[c + 1], x)
    x = a[c + 1] - x
    for i in range(1, c + 1):
        y = a[i]
        print(x, y)
        x -= y

Submission status: Wrong Answer

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

A.sort()
Pos = A[N//2:]
Neg = A[:N//2]
#print(Pos)
#print(Neg)
NP,NN = len(Pos), len(Neg)

ANS = []
if N % 2 == 1:
  ANS.append((Neg[0], Pos[0]))
  wk = Neg[0] - Pos[0]
  for i in range(1,NN):
    ANS.append((Neg[i], wk))
    wk = Neg[i] - wk
    ANS.append((wk, Pos[i]))
    wk = wk - Pos[i]
  ANS.append((Pos[-1], wk))
  wk = Pos[-1] - wk
else:
  ANS.append((Pos[0], Neg[0]))
  wk = Pos[0] - Neg[0]
  for i in range(1,NN):
    ANS.append((wk, Pos[i]))
    wk = wk - Pos[i]
    ANS.append((Neg[i], wk))
    wk = Neg[i] - wk
  
print(wk)
for a,b in ANS:
  print(a,b)

Submission status: Wrong Answer

Solution code:
from heapq import*
n=int(input())
a=list(map(int,input().split()))
qmax,qmin=[],[]
for i in a:
  heappush(qmax,-i)
  heappush(qmin,i)
ans=[]
for i in range(n-1):
  x,y=heappop(qmin),heappop(qmax)
  ans+=[(x,-y)]
  heappush(qmin,x+y)
  heappush(qmax,-x-y)
print(-heappop(qmin))
for i in ans[:-1]:
  print(*i)
print(*ans[-1][::-1])

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush
n = int(input())
A = list(map(int, input().split()))
heapify(A)
out = []
while len(A) > 2:
    x = heappop(A)
    y = heappop(A)
    out.append((x, y))
    heappush(A, x-y)
y = heappop(A)
x = heappop(A)
out.append((x, y))
ans = x - y
print(ans)
for x, y in out:
    print('{} {}'.format(x, y))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
A.sort()
x = A[0]
out = []
for i in range(1, n-1):
    out.append((x, A[i]))
    x -= A[i]
out.append((A[-1], x))
ans = A[-1] - x
print(ans)
for p in out:
    print(*p, sep=' ')

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

    A_p = [i for i in A if i >= 0]
    A_p.sort()
    A_n = [i for i in A if i < 0]
    A_n.sort()

    if A_p and A_n:
        # 正の数を1個以外負の数から引く
        # 残した正の数から負の数を全部引く
        print(sum(A_p) - sum(A_n))

        tmp = A_n[0]
        for i in range(1, len(A_p)):
            print(tmp, A_p[i])
            tmp -= A_p[i]
        print(A_p[0], tmp)
        tmp = A_p[0] - tmp
        for i in range(1, len(A_n)):
            print(tmp, A_n[i])
            tmp -= A_n[i]

    elif A_p and not A_n:
        # 正の数しかない場合、最小値から他の数字を1個残して引いたものを、残った数字から引く
        # print(sum(A_p[1:]) - A_p[0])

        # tmp = A_p[0]
        # for i in range(1, len(A_p) - 1):
        #     print(tmp, A_p[i])
        #     tmp -= A_p[i]
        # print(A[-1], tmp)
        print('')

    elif not A_p and A_n:
        # 負の数しかない場合、最大値から他の数字を全部引く
        print(A_n[-1] - sum(A_n[:-1]))

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

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

    A_p = [i for i in A if i > 0]
    A_p.sort()
    A_n = [i for i in A if i <= 0]
    A_n.sort()

    if A_p and A_n:
        # 正の数を1個以外負の数から引く
        # 残した正の数から負の数を全部引く
        print(sum(A_p) - sum(A_n))

        tmp = A_n[0]
        for i in range(1, len(A_p)):
            print(tmp, A_p[i])
            tmp -= A_p[i]
        print(A_p[0], tmp)
        tmp = A_p[0] - tmp
        for i in range(1, len(A_n)):
            print(tmp, A_n[i])
            tmp -= A_n[i]

    elif A_p and not A_n:
        # 正の数しかない場合、最小値から他の数字を1個残して引いたものを、残った数字から引く
        print(sum(A_p[1:]) - A_p[0])

        tmp = A_p[0]
        for i in range(1, len(A_p) - 1):
            print(tmp, A_p[i])
            tmp -= A_p[i]
        print(A[-1], tmp)

    elif not A_p and A_n:
        # 負の数しかない場合、最大値から他の数字を全部引く
        print(A_n[-1] - sum(A_n[:-1]))

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

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

    A_p = [i for i in A if i >= 0]
    A_p.sort()
    A_n = [i for i in A if i < 0]
    A_n.sort()

    if A_p and A_n:
        # 正の数を1個以外負の数から引く
        # 残した正の数から負の数を全部引く
        print(sum(A_p) - sum(A_n))

        tmp = A_n[0]
        for i in range(1, len(A_p)):
            print(tmp, A_p[i])
            tmp -= A_p[i]
        print(A_p[0], tmp)
        tmp = A_p[0] - tmp
        for i in range(1, len(A_n)):
            print(tmp, A_n[i])
            tmp -= A_n[i]

    elif A_p and not A_n:
        # 正の数しかない場合、最小値から他の数字を1個残して引いたものを、残った数字から引く
        print(sum(A_p[1:]) - A_p[0])

        tmp = A_p[0]
        for i in range(1, len(A_p) - 1):
            print(tmp, A_p[i])
            tmp -= A_p[i]
        print(A[-1], tmp)

    elif not A_p and A_n:
        # 負の数しかない場合、最大値から他の数字を全部引く
        print(A_n[-1] - sum(A_n[:-1]))

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


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=LI()
lis2=sorted(lis)
pm=[-1 for i in range(n)]
for i in range(n):
    if lis2[i]>=0:
        pm[i]=1
if pm[0]==1:
    pm[0]=-1
elif pm[-1]==-1:
    pm[-1]=1
ans=0
for i in range(n):
    ans=ans+pm[i]*lis2[i]
print(ans)
x=lis2[0]
for i in range(1,n-1):
    if pm[i]==1:
        print(x,lis2[i])
    x=x-lis2[i]
for i in range(1,n-1):
    if pm[i]==-1:
        print(lis2[-1],lis2[i])
        lis2[-1]=lis2[-1]-lis2[i]
print(lis2[-1],x)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=LI()
lis2=sorted(lis)
pm=[-1 for i in range(n)]
for i in range(n):
    if lis2[i]>=0:
        pm[i]=1
if pm[0]==1:
    pm[0]=-1
elif pm[-1]==0:
    pm[-1]=1
ans=0
for i in range(n):
    ans=ans+pm[i]*lis2[i]
print(ans)
x=lis2[0]
for i in range(1,n-1):
    if pm[i]==1:
        print(x,lis2[i])
    x=x-lis2[i]
for i in range(1,n-1):
    if pm[i]==-1:
        print(lis2[-1],lis2[i])
        lis2[-1]=lis2[-1]-lis2[i]
print(lis2[-1],x)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=LI()
lis2=sorted(lis)
pm=[-1 for i in range(n)]
for i in range(n):
    if lis2[i]>=0:
        pm[i]=1
if pm[0]==1:
    pm[0]=-1
elif pm[-1]==0:
    pm[-1]=1
ans=0
for i in range(n):
    ans=ans+pm[i]*lis2[i]
print(ans)
x=lis2[0]
for i in range(1,n-1):
    print(x,lis2[i])
    x=x-lis2[i]
print(lis2[-1],x)

Submission status: Wrong Answer

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

A.sort()
A = deque(A)
B = []
for i in range(N):
    if i%2==0:
        a = A.pop()
        B.append(a)
    else:
        a = A.popleft()
        B.append(a)

b0 = B[0]
data = []
for b in B[1:-1]:
    data.append((b, b0))
    b0 = b - b0
if b0 - B[-1] < B[-1] - b0:
    data.append((B[-1],b0))
    print(B[-1] - b0)
else:
    data.append((b0, B[-1]))
    print(b0 - B[-1])

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

"""
3
-1 -1 -1
"""

Submission status: Wrong Answer

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

A.sort()
A = deque(A)
B = []
for i in range(N):
    if i%2==0:
        a = A.pop()
        B.append(a)
    else:
        a = A.popleft()
        B.append(a)

b0 = B[0]
data = []
for b in B[1:]:
    data.append((b, b0))
    b0 = b - b0
if b0 < 0:
    b0 = B[-1]
    data = []
    for b in reversed(B[:-1]):
        data.append((b, b0))
        b0 = b - b0
print(b0)
for x, y in data:
    print(x,y)

Submission status: Wrong Answer

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

A.sort()
A = deque(A)
B = []
for i in range(N):
    if i%2==0:
        a = A.pop()
        B.append(a)
    else:
        a = A.popleft()
        B.append(a)

b0 = B[0]
data = []
for b in B[1:]:
    data.append((b, b0))
    b0 = b - b0
print(b0)
for x, y in data:
    print(x,y)

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()
    A.sort()

    # 最小値にマイナスをかけて全部足したものが最大
    print(sum(A[1:]) - A[0])
    # 最小値から他の値を引いていき、最後の一つからそれらを引けばよい
    tmp = A[0]
    for i in range(1, N - 1):
        print(tmp, A[i])
        tmp -= A[i]

    print(A[-1], tmp)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

A_plus = []
A_minus = []
for i in range(N):
    if A[i] >= 0:
        A_plus.append(A[i])
    else:
        A_minus.append(A[i])

if len(A_plus) == 0:
    A_plus.append(A_minus.pop(A_minus.index(min(A_minus))))
elif len(A_minus) == 0:
    A_minus.append(A_plus.pop(A_plus.index(min(A_plus))))
        
ans_list = []
while len(A_plus) + len(A_minus) != 1:
    if len(A_plus) <= len(A_minus):
        x = A_plus.pop(0)
        y = A_minus.pop(0)
        ans_list.append([x, y])
        A_plus.append(x-y)
    else:
        x = A_minus.pop(0)
        y = A_plus.pop(0)
        ans_list.append([x, y])
        A_minus.append(x-y)

M = A_plus[0]
print(M)
for i in range(len(ans_list)):
    print(ans_list[i][0], ans_list[i][1])

Submission status: Wrong Answer

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

A_plus = []
A_minus = []
for i in range(N):
    if A[i] >= 0:
        A_plus.append(A[i])
    else:
        A_minus.append(A[i])
        
ans_list = []
while len(A_plus) + len(A_minus) != 1:
    if len(A_plus) == 0:
        x = A_minus.pop(0)
        y = A_minus.pop(0)
        ans_list.append([x, y])
        A_plus.append(x-y)
        continue
    elif len(A_minus) == 0:
        x = A_plus.pop(0)
        y = A_plus.pop(0)
        ans_list.append([x, y])
        A_plus.append(x-y)
        continue
        
    if len(A_plus) <= len(A_minus):
        x = A_plus.pop(0)
        y = A_minus.pop(0)
        ans_list.append([x, y])
        A_plus.append(x-y)
    else:
        x = A_minus.pop(0)
        y = A_plus.pop(0)
        ans_list.append([x, y])
        A_minus.append(x-y)

M = A_plus[0]
print(M)
for i in range(len(ans_list)):
    print(ans_list[i][0], ans_list[i][1])

Submission status: Wrong Answer

Solution code:
import sys,math,collections,itertools,bisect
input = sys.stdin.readline

N=int(input())
A=list(map(int,input().split()))
process = []
#--最大最小を見つける--#
maxA = A.pop(A.index(max(A)))
minA = A.pop(A.index(min(A)))
A.append(minA-maxA)
process.append([minA,maxA])

for i in range(len(A)-2):
    maxA = A.pop(A.index(max(A)))
    process.append([maxA,A[-1]])
    A[-1]-=maxA
process.append([A[-1],A[0]])   
print(A[0]-A[-1])
for pro in process:
    print(*pro)

Submission status: Wrong Answer

Solution code:
import sys,math,collections,itertools,bisect
input = sys.stdin.readline

N=int(input())
A=sorted(list(map(int,input().split())))
q=collections.deque(A)
process = []
for i in range(N-2):
    maxA = q.pop()
    minA = q.popleft()
    tmp = minA-maxA
    q.appendleft(tmp)
    process.append(list(q))
maxA = q.pop()
minA = q.popleft()
print(maxA-minA)
for pro in process:
    print(*pro)

Submission status: Wrong Answer

Solution code:
import sys,math,collections,itertools,bisect
input = sys.stdin.readline

N=int(input())
A=sorted(list(map(int,input().split())))
q=collections.deque(A)
process = []
for i in range(N-2):
    maxA = q.pop()
    minA = q.popleft()
    tmp = minA-maxA
    q.insert(bisect.bisect_left(A,tmp),tmp)
    process.append(list(q))
maxA = q.pop()
minA = q.popleft()
print(maxA-minA)
for pro in process:
    print(*pro)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())

plus=[]
minus=[]
for aa in a:
    if aa>=0:
        plus.append(aa)
    else:
        minus.append(aa)

if len(plus)==n:
    plus.sort(reverse=True)
    minus.append(plus.pop())
elif len(minus)==n:
    minus.sort()
    plus.append(minus.pop())

print(sum(plus)-sum(minus))

xy=minus[0]
for i in range(len(plus)-1):
    print('{} {}'.format(xy,plus[i]))
    xy-=plus[i]

print('{} {}'.format(plus[-1],xy))
xy=plus[-1]-xy

for i in range(1,len(minus)):
    print('{} {}'.format(xy,minus[i]))

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())

a=sorted(a,reverse=True)

lenmax=(n+1)//2
print(sum(a[:lenmax])-sum(a[lenmax:]))

b=[]
for i in range(n):
    if i%2==0:
        b.append(a[i//2])
    else:
        b.append(a[lenmax+(i-1)//2])
xy=b[-1]
for i in range(n-2,-1,-1):
    print('{} {}'.format(b[i],xy))
    xy=b[i]-xy

Submission status: Wrong Answer

Solution code:
n=int(input())

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

if n==2:
    print(max(a)-min(a))
    exit()

#+ -が最低一つ必要
#あとはどちらの符号にもできる？

a_p=[]
#a_zero=[]
a_m=[]

for aa in a:
    if aa>=0:
        a_p.append(aa)
    else:
        a_m.append(aa)

#どっちもある

ans=[]

if len(a_m)>0 and len(a_p)>0:
    if len(a_p)==1:
        tmp=a_p[0]
        for v in a_m:
            ans.append([tmp,v])
            tmp=tmp-v
    else:
        tmp=a_m[0]
        for v in a_p[1:]:
            ans.append([tmp,v])
            tmp=tmp-v
        ans.append([a_p[0],tmp])
        tmp=a_p[0]-tmp
        for v in a_m[1:]:
            ans.append([tmp,v])
            tmp=tmp-v

elif len(a_p)>0:
    a_p.sort()

    tmp=a_p[0]
    for v in a_p[2:]:
        ans.append([tmp,v])
        tmp=tmp-v
    ans.append([a_p[1],tmp])

else:
    a_m.sort(reverse=True)

    tmp=a_m[0]
    for v in a_m[1:]:
        ans.append([tmp,v])
        tmp=tmp-v


print(ans[-1][0]-ans[-1][1])
for item in ans:
    print(item[0],item[1])

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    A = sorted([int(x) for x in input().strip().split()])
    maxi = max(A)
    mini = min(A)
    plus = [a for a in A[1:-1] if a > 0]
    minus = [a for a in A[1:-1] if a < 0]
    print(maxi + sum(plus) - sum(minus) - mini)
    for p in plus:
        print(mini, p)
        mini -= p
    print(maxi, mini)
    maxi -= mini
    for m in minus:
        print(maxi, m)
        maxi -= m

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

pos_cnt = neg_cnt = 0

for c in a:
    if c >= 0:
        pos_cnt += 1
    else:
        neg_cnt += 1

if pos_cnt > 0 and neg_cnt == 0:
    a.sort()
    a[0] = a[1] - a[0]
    a[1] = 0
    print(sum(a))
elif pos_cnt == 0 and neg_cnt > 0:
    a.sort()
    a[-2] = a[-2] - a[-1]
    a[-1] = 0
    print(sum(a) * -1)
else:
    a = list(map(abs, a))
    print(sum(a))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
ind = -1
for i in range(N):
    if A[i]>=0:
        ind = i
        break
if ind>0:
    M = A[0]
    for i in range(1,N-1):
        M = M-A[i]
    M = A[-1]-M
    print(M)
    M = A[0]
    for i in range(1,N-1):
        print(M,A[i])
        M = M-A[i]
    print(A[-1],M)
else:
    A = A[::-1]
    M = A[0]
    for i in range(1,N):
        M = M-A[i]
    print(M)
    M = A[0]
    for i in range(1,M):
        print(M,A[i])
        M = M-A[i]