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 heapq import *

n = int(input())
a = list(map(int, input().split()))
heapify(a)
r = []
while len(a)> 1:
    x = heappop(a)
    y = a.pop()
    r.append([y, x])
    heappush(a, x-y)
print(r)
print(-a[0])
for i in r[:-1]:
    print(*i)
if min(r[-1]) >= 0:
    print(r[-1][1], r[-1][0])
else:
    print(*r[-1])

Submission status: Wrong Answer

Solution code:
import sys
import copy
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()

    if A[0] >= 0:
        ans = sum(A[1:]) - A[0]
        plus = copy.deepcopy(A[1:])
        minus = [A[0]]
    elif A[-1] <= 0:
        ans = -sum(A[:-1]) + A[-1]
        plus = [A[-1]]
        minus = copy.deepcopy(A[:-1])
    else:
        plus = []
        minus = []
        ans = 0
        for a in A:
            if a >= 0:
                ans += a
                plus.append(a)
            else:
                ans -= a
                minus.append(a)

    plus = deque(plus)
    minus = deque(minus)
    print(ans)

    # minusをまず一つにする
    while len(minus) > 1:
        p = plus.pop()
        m = minus.pop()
        print(p, m)
        plus.append(p - m)

    # minusが1つになったあとの処理
    p = plus.pop()
    m = minus.pop()
    print(m, p)
    cur = m - p
    while len(plus) > 1:
        p = plus.pop()
        print(cur, p)
        cur -= p
    if len(plus) != 0:
        p = plus.pop()
        print(p, cur)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import itertools

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

A.sort()


A_sum = list(itertools.accumulate(A))

M = A_sum[-1] - A_sum[(N//2-1)] * 2
print(M)

smaller = A[:(N//2)]
larger = A[(N//2):]

if N % 2 == 0:
    x = larger.pop()
    y = smaller.pop()
    for i in range(N-1):
        if i == 0:
            print(x, y)
            new = x - y
        elif i % 2 == 1:
            x = smaller.pop()
            print(x, new)
            new = x - new
        else:
            x = larger.pop()
            print(x, new)
            new = x - new
else:
    x = smaller.pop()
    y = larger.pop()
    for i in range(N-1):
        if i == 0:
            print(x, y)
            new = x - y
        elif i % 2 == 1:
            x = larger.pop()
            print(x, new)
            new = x - new
        else:
            x = smaller.pop()
            print(x, new)
            new = x - new

Submission status: Wrong Answer

Solution code:
#diverta 2019 Programming Contest 2 -C Successive Subtraction
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
n = int(readline())
lst1 = list(map(int,readline().split()))

lst1.sort()
lst2 = []
l,r = lst1[0],lst1[-1]
for i in range(1,n-1):
    mid = lst1[i]
    if mid < 0:
        lst2.append([r,mid])
        r -= mid
    else:
        lst2.append([l,mid])
        l -= mid
lst2.append([l,r])
print(r-l)
for i in lst2:
    print(*i)

Submission status: Wrong Answer

Solution code:
#diverta 2019 Programming Contest 2 -C Successive Subtraction
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
n = int(readline())
lst1 = list(map(int,readline().split()))

lst1.sort()
lst2 = []
l,r = lst1[0],lst1[-1]
for i in range(1,n-1):
    mid = lst1[i]
    if mid < 0:
        lst2.append([r,mid])
        r -= mid
    else:
        lst2.append([l,mid])
        l -= mid
lst2.append([l,r])
print(abs(r-l))
for i in lst2:
    print(*i)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
tot=sum(a) * (-1)
ans = -10000000000
for b in a:
  tmp = tot + 2 * b
  ans = max(ans, tmp)
  
print(ans)

Submission status: Wrong Answer

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

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

a.sort()

from bisect import bisect_left
from bisect import bisect_right

pos = n - bisect_left(a, 1)
neg = bisect_right(a, -1)


ans_exec = []

if pos>=neg:
    aa = a[max(0,neg-1):n-neg+1]
    for i in range(neg-1):
        ans_exec.append((a[-1-i], a[i]))
        aa.append(a[-1-i] - a[i])
    aa.sort()
    tmp = aa[0]
    for i in range(1,len(aa)-1):
        ans_exec.append((tmp, aa[i]))
        tmp = tmp-aa[i]
    ans_exec.append((aa[-1], tmp))
    ans = aa[-1] - tmp

elif pos<=neg:
    aa = a[max(0,pos-1):n-pos+1]
    for i in range(pos-1):
        ans_exec.append((a[i], a[-1-i]))
        aa.append(a[i] - a[-1-i])
    aa.sort()
    tmp = aa[-1]
    ans_exec.append((aa[-1], aa[0]))
    tmp = aa[-1]-aa[0]
    for i in range(1,len(aa)-1):
        ans_exec.append((tmp, aa[i]))
        tmp = tmp-aa[i]
    ans = tmp-aa[-1]


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

Submission status: Wrong Answer

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

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

a.sort()

from bisect import bisect_left
from bisect import bisect_right

pos = n - bisect_left(a, 1)
neg = bisect_right(a, -1)

if pos>=neg:
    aa = a[neg-1:n-neg+1]
    for i in range(neg-1):
        print(a[-1-i], a[i])
        aa.append(a[-1-i] - a[i])
    aa.sort()
    tmp = aa[0]
    for i in range(1,len(aa)-1):
        print(tmp, a[i])
        tmp = tmp-a[i]
    print(a[-1], tmp)
    exit()

if pos<=neg:
    aa = a[pos-1:n-pos+1]
    for i in range(pos-1):
        print(a[i], a[-1-i])
        aa.append(a[i] - a[-1-i])
    aa.sort()
    tmp = aa[-1]
    print(aa[-1], aa[0])
    tmp = aa[-1]-aa[0]
    for i in range(1,len(aa)-1):
        print(tmp, a[i])
        tmp = tmp-a[i]
    exit()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
plus,minus=[],[]
for i in range(n):
    if a[i]>=0:
        plus.append(a[i])
    else:
        minus.append(a[i])
order=[]
if plus and minus:
    if len(plus)>=2:
        for num in plus[:-1]:
            order.append((minus[0],num))
            minus[0]-=num
    for num in minus:
        order.append((plus[-1],num))
        plus[-1]-=num
    print(plus[-1])
else:
    if minus:
        a=a[::-1]
    for i in range(1,n-1):
        order.append((a[0],a[i]))
        a[0]-=a[i]
    order.append((a[-1],a[0]))
    a[-1]-=a[0]
    print(a[-1])
for x,y in order:
    print(x,y)

Submission status: Wrong Answer

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

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

print(A[N-1]-res)
for i in l:
  print(*i)

Submission status: Wrong Answer

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

print(sum(A[1:])-A[0])
res = A[0]
for i in range(N-2):
  print(res,A[i+1])
  res -= A[i+1]
print(A[N-1],res)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from collections import deque

n = ni()
a = na()
a.sort()

cur = a[0]
ans = []
for i in range(n - 1, 1, -1):
    ans.append((cur, a[i]))
    cur -= a[i]

ans.append((a[1], cur))
print(a[1] - cur)
for l, r in ans:
    print(l, r)

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

A.sort()


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

if A[0] >= 0:
    ans = sum(A) - A[0] * 2
    print(ans)
    for i in range(0, N - 1):
        if i == 0:
            print(A[0], A[1])
            s = A[0] - A[1]
        elif i == N - 2:
            print(A[N - 1], s)
        else:
            print(s, A[i + 1])
            s -= A[i + 1]

elif A[len(A) - 1] < 0:
    ans = A[len(A) - 1] * 2 - sum(A)
    A.reverse()
    print(ans)
    for i in range(N - 1):
        if i == 0:
            print(A[0], A[1])
            s = A[0] - A[1]
        else:
            print(s,  A[i + 1])
else:
    ans = sum(map(lambda x: -x if x < 0 else x, A))
    neg = list(filter(lambda x: x < 0, A))
    pos = list(filter(lambda x: x >= 0, A))
    print(ans)
    for i in range(len(pos) - 1):
        if i == 0:
            print(neg[0], pos[1])
            s = neg[0] - pos[1]
        else:
            print(s, pos[i + 1])
            s -= pos[i + 1]
    print(pos[0], s)
    s = pos[0] - s
    for i in range(1, len(neg)):
        print(s, neg[i])

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

A.sort()


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

if A[0] >= 0:
    ans = sum(A) - A[0] * 2
    print(ans)
    for i in range(0, N - 1):
        if i == 0:
            print(A[0], A[1])
            s = A[0] - A[1]
        elif i == N - 2:
            print(A[N - 1], s)
        else:
            print(s, A[i + 1])
            s -= A[i + 1]

elif A[len(A) - 1] < 0:
    ans = A[len(A) - 1] * 2 - sum(A)
    A.reverse()
    print(ans)
    for i in range(N - 1):
        if i == 0:
            print(A[0], A[1])
            s = A[0] - A[1]
        else:
            print(s,  A[i + 1])
else:
    ans = sum(map(lambda x: -x if x < 0 else x, A))
    print(ans)
    isFirst = True
    for i in range(N - 1):
        if i == 0:
            print(A[0], A[1])
            s = A[0] - A[1]
        elif A[i + 1] < 0:
            print(s, A[i + 1])
            s -= A[i + 1]
        else:
            if isFirst:
                print(A[i + 1], s)
            else:
                print(s, A[i + 1])

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

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

A.sort()


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

if A[0] >= 0:
    ans = sum(A) - A[0] * 2
    print(ans)
    for i in range(1, N):
        if i == 1:
            print(A[1], A[2])
            s = A[1] - A[2]
        elif i == N - 1:
            print(A[0], s)
        else:
            print(s, A[i + 1])
            s -= A[i + 1]

elif A[len(A) - 1] < 0:
    ans = A[len(A) - 1] * 2 - sum(A)
    print(ans)
    for i in range(N - 2):
        if i == 0:
            print(A[0], A[1])
            s = A[0] - A[1]
        else:
            print(s,  A[i + 1])
    print(A[N - 1], s)
else:
    ans = sum(map(lambda x: -x if x < 0 else x, A))
    print(ans)
    isFirst = True
    for i in range(N - 1):
        if i == 0:
            print(A[0], A[1])
            s = A[0] - A[1]
        elif A[i + 1] < 0:
            print(s, A[i + 1])
            s -= A[i + 1]
        else:
            if isFirst:
                print(A[i + 1], s)
            else:
                print(s, A[i + 1])

Submission status: Wrong Answer

Solution code:
# 符号が異なる数同士の引き算をすれば絶対値が大きくなる
# 基本は負から正を引いて大きな負の数を作る
# 最後だけ正から負を引いて大きな正の数を作る
# 正と正しか存在しない場合は最も小さい数からその他の数を引くことで負の数を作れる
# 負と負しか存在しない場合は最も大きい数からその他の数を引くことで正の数を作れる
# 正のリストと負のリスト両方を管理する
# 0は何も影響を与えないのでとりあえず正のリストに入れておく

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

if len(A) == 2:
  print(abs(A[0] - A[1]))
  exit(0)

plusA = []
minusA = []
for a in A:
  if a >= 0:
    plusA += [a]
  else:
    minusA += [a]
    
method = [] # 計算過程をメモ
if len(plusA) == 0:
  minusA = sorted(minusA) # 最も小さな数を取り出すため
#  print("minusA",minusA)
  x = minusA.pop()
  y = minusA.pop()
#  print("x",x,"y",y)
  method.append([x,y])
  plusA.append(x-y)
#  print("plusA",plusA,"minusA",minusA,"method",method)
elif len(minusA) == 0:
  plusA = sorted(plusA)[::-1] # 最も小さな数を取り出すため
  x = plusA.pop()
  y = plusA.pop()
  method.append([x,y])
  minusA.append(x-y)
  
while True:
  if len(minusA) + len(plusA) == 1:
    print(x-y)
    for m in method:
      print(*m)
    break
  if len(plusA) == 1:
    # 正の数が一つのときは、この数から残りの負の数を引いていく
    x = plusA.pop()
    y = minusA.pop()
    method.append([x,y])
    plusA.append(x-y)
  else:
    # 正の数が複数あるときは、負の数を作り続ける
    x = minusA.pop()
    y = plusA.pop()
    method.append([x,y])
    minusA.append(x-y)

Submission status: Wrong Answer

Solution code:
# 符号が異なる数同士の引き算をすれば絶対値が大きくなる
# 基本は負から正を引いて大きな負の数を作る
# 最後だけ正から負を引いて大きな正の数を作る
# 正と正しか存在しない場合は最も小さい数からその他の数を引くことで負の数を作れる
# 負と負しか存在しない場合は最も大きい数からその他の数を引くことで正の数を作れる
# 正のリストと負のリスト両方を管理する
# 0は何も影響を与えないのでとりあえず正のリストに入れておく

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

if len(A) == 2:
  print(abs(A[0] - A[1]))
  exit(0)

plusA = []
minusA = []
for a in A:
  if a >= 0:
    plusA += [a]
  else:
    minusA += [a]
    
method = [] # 計算過程をメモ
if len(plusA) == 0:
  minusA = sorted(minusA) # 最も小さな数を取り出すため
#  print("minusA",minusA)
  x = minusA.pop()
  y = minusA.pop()
#  print("x",x,"y",y)
  method.append([x,y])
  plusA.append(x-y)
#  print("plusA",plusA,"minusA",minusA,"method",method)
elif len(minusA) == 0:
  plusA = sorted(plusA)[::-1] # 最も小さな数を取り出すため
  x = plusA.pop()
  y = plusA.pop()
  method.append([x,y])
  minusA.append(x-y)
  
while True:
  if len(minusA) + len(plusA) == 2:
    break
  if len(plusA) == 1:
    # 正の数が一つのときは、この数から残りの負の数を引いていく
    x = plusA.pop()
    y = minusA.pop()
    method.append([x,y])
    plusA.append(x-y)
  else:
    # 正の数が複数あるときは、負の数を作り続ける
    x = minusA.pop()
    y = plusA.pop()
    method.append([x,y])
    minusA.append(x-y)
  
# この時点で正と負が一つずつ
x = plusA.pop()
y = minusA.pop()
method.append([x,y])
print(x-y)
for m in method:
  print(*m)

Submission status: Wrong Answer

Solution code:
# 符号が異なる数同士の引き算をすれば絶対値が大きくなる
# 基本は負から正を引いて大きな負の数を作る
# 最後だけ正から負を引いて大きな正の数を作る
# 正と正しか存在しない場合は最も小さい数からその他の数を引くことで負の数を作れる
# 負と負しか存在しない場合は最も大きい数からその他の数を引くことで正の数を作れる
# 正のリストと負のリスト両方を管理する
# 0は何も影響を与えないのでとりあえず正のリストに入れておく

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

if len(A) == 2:
  print(abs(A[0] - A[1]))
  exit(0)

plusA = []
minusA = []
for a in A:
  if a >= 0:
    plusA += [a]
  else:
    minusA += [a]
    
method = [] # 計算過程をメモ
if len(plusA) == 0:
  minusA = sorted(minusA) # 最も小さな数を取り出すため
  print("minusA",minusA)
  x = minusA.pop()
  y = minusA.pop()
  print("x",x,"y",y)
  method.append([x,y])
  plusA.append(x-y)
  print("plusA",plusA,"minusA",minusA,"method",method)
elif len(minusA) == 0:
  plusA = sorted(plusA)[::-1] # 最も小さな数を取り出すため
  x = plusA.pop()
  y = plusA.pop()
  method.append([x,y])
  minusA.append(x-y)
  
while True:
  if len(minusA) + len(plusA) == 2:
    break
  if len(plusA) == 1:
    # 正の数が一つのときは、この数から残りの負の数を引いていく
    x = plusA.pop()
    y = minusA.pop()
    method.append([x,y])
    plusA.append(x-y)
  else:
    # 正の数が複数あるときは、負の数を作り続ける
    x = minusA.pop()
    y = plusA.pop()
    method.append([x,y])
    minusA.append(x-y)
  
# この時点で正と負が一つずつ
x = plusA.pop()
y = minusA.pop()
method.append([x,y])
print(x-y)
for m in method:
  print(*m)

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = list(map(int,input().split()))
a.sort()
ans = []
p,m,z = deque([]),deque([]),deque([])
for i in range(n):
  if a[i] < 0: m.append(a[i])
  elif a[i] == 0: z.append(a[i])
  else: p.append(a[i])

if len(p) == 0 and len(m) == 0:
  for i in range(len(z)-1):
    ans.append([0,0])
  final = 0

elif len(p) > 0 and len(m) > 0:
  while len(m) > 1:
    x,y = p.popleft(),m.popleft()
    ans.append([x,y])
    p.append(x-y)
  while len(p) > 1:
    x = m[0]
    y = p.popleft()
    ans.append([x,y])
    x = x-y
  y = x
  x = p.popleft()
  ans.append([x,y])
  x = x-y
  for i in range(len(z)):
    ans.append([x,0])
  final = x

elif len(p) > 0:
  l = sorted(list(p))
  zero = len(z)
  if zero > 0:
    x = 0
    for i in range(len(l)-1):
      y = l[i]
      ans.append([x,y])
      x = x-y
    y = x
    x = l[-1]
    ans.append([x,y])
    final = x-y
  else:
    x = l[0]
    for i in range(2,len(l)):
      y = l[i]
      ans.append([x,y])
      x = x-y
    y = x
    x = l[1]
    ans.append([x,y])
    final = x-y
  if zero > 1:
    for i in range(zero-1):
      ans.append([final,0])
else:
  l = sorted(list(m),reverse = True)
  zero = len(z)
  if zero > 0:
    x = 0
    for i in range(len(l)):
      y = l[i]
      ans.append([x,y])
      x = x-y
  else:
    x = l[0]
    for i in range(1,len(l)):
      y = l[i]
      ans.append([x,y])
      x = x-y
  final = x
  if zero > 1:
    for i in range(zero-1):
      ans.append([final,0])
    
print(final)
for l in ans:
  print(*l)

Submission status: Wrong Answer

Solution code:
# 最も絶対値の大きい数を最後に残す
# 最後に残すのが正の数の場合 -> できるだけ小さい数を作るのが良い # 小さいほうから大きいほう引く
# 最後に残すのが負の数の場合 -> できるだけ大きい数を作るのが良い # 大きいほうから小さいほう引く

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

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

maxAbsA = 0
maxAbsInd = -1
allA = True
for i in range(len(A)):
  if A[i] != 0:
    allA = False
  if abs(A[i]) > abs(maxAbsA):
    maxAbsA = A[i]
    maxAbsInd = i

if allA:
  print(0)
  exit(0)

maxData = A.pop(maxAbsInd)
#print(maxData)
#print(A)
if maxAbsA > 0:
  # 小さいほうから大きいほうを引く
  ans = []
  cur = A[0]
  for i in range(1,len(A)):
    x,y = min(cur,A[i]),max(cur,A[i])
    ans += [[x,y]]
    cur = x - y
  
  x,y = maxData,cur
  ans += [[x,y]]
  print(x - y)
  for a in ans:
    print(*a)
else:
  # 大きいほうから小さいほうを引く
  ans = []
  cur = A[0]
  for i in range(1,len(A)):
    x,y = max(cur,A[i]),min(cur,A[i])
#    print("cur",cur,"i",i,"A[i]",A[i],x,y)
    ans += [[x,y]]
    cur = x - y
  x,y = cur,maxData
  ans += [[x,y]]
  print(x - y)
  for a in ans:
    print(*a)

Submission status: Wrong Answer

Solution code:
# できるだけ小さい数を作り、最後にそれを引く
# 両方正の数の場合、小さいほうから大きい方を引く
# 10 20 -> 10 - 20 = -10
# 両方負の数の場合も、小さい方から大きい方を引く
# -20 -10 -> -20 - - 10 = -10
# 正の数と負の数の場合、
# -10 10 -> -10 - 10 = -20


# 最後に残った数からできるだけ小さい数を引くことで答えが求められる

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

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

from collections import deque
q = deque(A)

seq = []
x = q.popleft()
last = q.pop()
y = q.pop()
seq += [[x,y]]
x = x-y
while len(q):
  y = q.pop()
  seq += [[x,y]]
  x = x-y

seq += [[last,x]]
print(last - x)
for s in seq:
  print(*s)

Submission status: Wrong Answer

Solution code:
# 最も小さい数から最も大きい数を引く、を繰り返すことで、できるだけ小さい数が作れる
# 最後に残った数からできるだけ小さい数を引くことで答えが求められる

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

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

from collections import deque
q = deque(A)

seq = []
x = q.popleft()
y = q.pop()
seq += [[x,y]]
x = x-y
while len(q) > 1:
  y = q.pop()
  seq += [[x,y]]
  x = x-y

y = q.pop()
seq += [[y,x]]
print(y - x)
for s in seq:
  print(*s)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int , input().split()))
pos = sorted([i for i in a if i >= 0], reverse=True)
neg = sorted([i for i in a if i < 0], reverse=True)

ans = []
if len(neg) == 0 and len(pos) == 2:
    print(pos[0] - pos[1])
    print(pos[0], pos[1])
    exit()
elif len(pos) == 0:
    tmp = neg[0]
    for i in range(1,len(neg)):
        ans.append([tmp, neg[i]])
        tmp -= neg[i]
    print(tmp)
    [print(*i) for i in ans]
    exit()
elif len(pos) == 1:
    tmp = pos[0]
    for i in range(len(neg)):
        ans.append([tmp, neg[i]])
        tmp -= neg[i]
    print(tmp)
    [print(*i) for i in ans]
    exit()
elif len(neg) == 0:
    neg.append(pos.pop())

tmp = neg.pop()
while len(pos) > 1:
    a = pos.pop()
    ans.append([tmp, a])
    tmp -= a

for i in neg:
    ans.append([tmp, i])
    tmp -= i

ans.append([pos[0], tmp])
print(pos[0] - tmp)
[print(*i) for i in ans]

Submission status: Wrong Answer

Solution code:

import heapq



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

    max_heap = []
    min_heap = []

    for e in a:
        heapq.heappush(max_heap, -e)
        heapq.heappush(min_heap, e)

    hist = []
    while n > 2:
        curr_min = heapq.heappop(min_heap)
        curr_max = -heapq.heappop(max_heap)
        if n % 2:
            # 奇数が残る時にはmin - maxを行う
            s = curr_min - curr_max
            heapq.heappush(max_heap, -s)
            heapq.heappush(min_heap, s)
            hist.append((curr_min, curr_max))
        else:
            # 偶数が残る時にはmax - minを行う
            s = curr_max - curr_min
            heapq.heappush(max_heap, -s)
            heapq.heappush(min_heap, s)
            hist.append((curr_max, curr_min))
        n -= 1

    curr_min = heapq.heappop(min_heap)
    curr_max = -heapq.heappop(max_heap)
    hist.append((curr_max, curr_min))
    
    print(curr_max - curr_min)
    for h in hist:
        print(*h)


submit()

Submission status: Wrong Answer

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

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


n = I()
A = LI()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    minus_now = minus.pop()
    plus_now = plus.pop()
    while plus:
        nxt = plus.pop()
        print(minus_now, nxt)
        minus_now -= nxt
    plus_now -= minus_now
    while minus:
        nxt = minus.pop()
        print(plus_now, nxt)
        plus_now -= nxt
    print(plus_now, minus_now)

Submission status: Wrong Answer

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

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


n = I()
A = LI()
A.sort()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    ret = minus.pop()
    for j in range(len(plus) - 1):
        print(ret, plus[j])
        ret -= plus[j]
    print(plus[-1], ret)
    ret = plus[-1] - ret
    for k in range(len(minus) - 1):
        print(ret, minus[k])
        ret -= minus[k]

Submission status: Wrong Answer

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

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


n = I()
A = LI()
A.sort()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    ret = minus.pop()
    for j in range(len(plus) - 1):
        print(ret, plus[j])
        ret -= plus[j]
    plus_now = plus.pop()
    print(plus_now, ret)
    ret = plus_now - ret
    for k in range(len(minus) - 1):
        print(ret, minus[k])
        ret -= minus[k]

Submission status: Wrong Answer

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

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


n = I()
A = LI()
A.sort()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    ret = minus.pop()
    for j in range(len(plus) - 1):
        print(ret, plus[j])
        ret -= plus[j]
    plus_now = plus.pop()
    print(plus_now, ret)
    ret = plus_now - ret
    for k in range(len(minus) - 1):
        print(ret, minus[k])
        ret -= minus[k]