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:


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

        for i in range(n - 1, -1, -1):
            if a[i] < 0 and flg:
                flg = False
                t = a[i]
            elif not flg:
                ansl.append([t, a[i]])
                t -= a[i]
        print(t - pp)
        for a, b in ansl:
            print(a, b, sep=" ")
        print(t, pp, sep=" ")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
N= int(input())
A = np.array(sorted([int(x) for x in input().split()]))
B = np.copy(A)
for i in range(N-2):
        A[i+1] = A[i]-A[i+1]
print(A[N-1] - A[N-2])
for i in range(N-2):
        print(B[i],B[i+1])
        B[i+1] = B[i]-B[i+1]
print(B[N-1],B[N-2])

Submission status: Wrong Answer

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

A.sort()

ans = []
if N % 2 == 1:
    ans.append((A[0], A[-1]))
    A[0] -= A[-1]
    A.pop()

B = []
for n, p in zip(A[:N // 2], A[N // 2:]):
    B.append(n)
    B.append(p)

now = B[0]
for b in B[1:]:
    ans.append((b, now))
    now = b - now

print(now)
for a in ans:
    print(*a)

Submission status: Wrong Answer

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

A.sort()
if N % 2 == 1:
    last = A.pop()

B = []
for n, p in zip(A[:len(A) // 2], A[len(A) // 2:]):
    B.append(n)
    B.append(p)

now = B[0]
ans = []
for b in B[1:]:
    ans.append((b, now))
    now = b - now

if N % 2 == 1:
    ans.append((last, now))
    now = last - now

print(now)
for a in ans:
    print(*a)

Submission status: Wrong Answer

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

A.sort()
if N % 2 == 1:
    last = A.pop()

B = []
for n, p in zip(A[:len(A) // 2], A[len(A) // 2:]):
    B.append(n)
    B.append(p)

now = B[0]
ans = []
for b in B[1:]:
    ans.append((now, b))
    now = now - b

if N % 2 == 1:
    ans.append((last, now))
    now = last - now

print(now)
for a in ans:
    print(*a)

Submission status: Wrong Answer

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

A.sort()
ans = sum(A[N//2:]) - sum(A[:N//2])
print(ans)

A = deque(A)
ans = 0
for i in range(N - 1):
    x, y = A.popleft(), A.pop()
    print(y, x)
    if x - y > 0:
        A.append(x - y)
    else:
        A.appendleft(x - y)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from collections import deque

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

sorted_A = deque(sorted(A))
ans_list = []

if N % 2 != 0:
    toggle = 1
    ans = sorted_A.pop()
else:
    toggle = 0
    ans = sorted_A.popleft()

while sorted_A:
    if toggle:
        item = sorted_A.popleft()
    else:
        item = sorted_A.pop()
    ans_list.append([item, ans])
    ans = item - ans
    toggle ^= 0

print(ans)
for x, y in ans_list:
    print(x, y)

Submission status: Wrong Answer

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

sorted_A = sorted(A)
ans = sorted_A[N-1]
ans_list = []
for i in range(N-1):
    if i % 2 == 0:
        ans_list.append([sorted_A[i], ans])
    else:
        ans_list.append([sorted_A[i], -ans])

    if i < N // 2:
        ans -= sorted_A[i]
    else:
        ans += sorted_A[i]
print(ans)
for x, y in ans_list:
    print(x, y)

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]

n = i()
a = wi()

minus = n // 2
plus = (n + 2 - 1) // 2

a.sort()
pa = 0; ma = 0; za = 0
for i in range(n):
    if a[i] > 0:
        pa += 1
    elif a[i] < 0:
        ma += 1
    else:
        za += 1
mli = []
pli = []
for i in range(1, n + 1):
    if i <= minus:
        ans += - a[i - 1]
        mli.append(a[i - 1])
    else:
        ans += a[i - 1]
        pli.append(a[i - 1])

kisucnt = 1
gusucnt = 1
print(ans)
if minus == plus:
    for i in range(n - 1):
        if i == 0:
            print(pli[0], mli[0])
            tmp = pli[0] - mli[0]
        elif i % 2 == 1:
            print(mli[kisucnt], tmp)
            tmp = mli[kisucnt] - tmp
            kisucnt += 1
        else:
            print(pli[gusucnt], tmp)
            tmp = pli[gusucnt] - tmp
            gusucnt += 1
else:
    for i in range(n - 1):
        if i == 0:
            print(mli[0], pli[0])
            tmp = mli[0] - pli[0]
        elif i % 2 == 1:
            print(pli[kisucnt], tmp)
            tmp = pli[kisucnt] - tmp
            kisucnt += 1
        else:
            print(mli[gusucnt], tmp)
            tmp = mli[gusucnt] - tmp
            gusucnt += 1

Submission status: Wrong Answer

Solution code:
N=int(input())
A=sorted(map(int,input().split()))
m,M=A[0],A[-1]
L=[]

for i in range(1,N-1):
    v=A[i]
    if v>=0:
        L.append((m,v))
        m-=v
    else:
        L.append((M,v))
        M+=v
L.append((M,m))
ans=M-m

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

Submission status: Wrong Answer

Solution code:
def sub(A, D):
    A.sort()
    for i in range(len(A)):
        D[tuple([i])] = (A[i], None), (A[i], None)
    stuff = [[i for i in range(j)] for j in range(2, len(A) + 1)]
    for key in stuff:
        min_sub_list = []
        max_sub_list = []
        key_i = list(key)
        index = key_i.pop()
        ikey = tuple(key_i)
        res_min_i, res_max_i = D[ikey]
        a_i = A[index]
        b_i_min, pair_i_min = res_min_i
        b_i_max, pair_i_max = res_max_i
        min_sub_list.append((-abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
        min_sub_list.append((-abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
        max_sub_list.append((abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
        max_sub_list.append((abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
        res_min = min(min_sub_list)
        res_max = max(max_sub_list)
        D[tuple(key)] = res_min, res_max


def pair_extract(p, pair_list):
    while p is not None:
        a = p[0]
        b = p[1]
        p_next = p[2]
        pair_list.append((a, b))
        p = p_next


N = int(input())
A_temp = map(int, input().split())
A = list(A_temp)
D = {}
sub(A, D)
res_max = D[tuple([i for i in range(len(A))])][1]
max_val = res_max[0]
pair_list = []
pair_extract(res_max[1], pair_list)
pair_list.reverse()
print(max_val)
for p in pair_list:
    print(p[0], p[1])

Submission status: Wrong Answer

Solution code:
N = int(input())
inputs = sorted([int(i) for i in input().split()], reverse=True)

ans = inputs[-1]
for i in range(N-1, 1, -1):
    ans -= inputs[i-1]
print(inputs[0] - ans)

tmp = inputs[-1]
for i in range(N-1, 1, -1):
    print(tmp, inputs[i-1])
    tmp -= inputs[i-1]
print(inputs[0], tmp)

Submission status: Wrong Answer

Solution code:
N = int(input())
inputs = sorted([int(i) for i in input().split()], reverse=True)

ans = inputs[-1]
for i in range(N-1, 1, -1):
    ans -= inputs[i-1]
print(inputs[0] - ans)

tmp = inputs[-1]
for i in range(N-1, 0, -1):
    print(tmp, inputs[i-1])
    tmp -= inputs[i-1]

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
ret = [int(_) for _ in raw_input().split(" ")]
ans = ()

v = 0

for i in ret:
  if i < 0:
    ans += [v, i, True],
    v = v - i
  else:
    ans += [i, -v, False],
    v = (i + v)

flg = True
for i in ans[::-1]:
  if flg:
    flg = i[2]
  else:
    t = i[0]
    i[0] = i[1]
    i[1] = t
    i[2]  = not i[2]
    flg = not i[2]

print v
for i in ans[1:]:
  print i[0], i[1]

Submission status: Wrong Answer

Solution code:
N = int(input())
As = [int(w) for w in input().split()]
As.sort()
#print(As)
r = sum(As)-2*As[0]
print(r)
c = As[0]
for i in range(N-1):
  if i != N-2:
    print(c, As[i+1])
    c = c-As[i+1]
  else:
    print(As[i+1], c)
    c = As[i+1]-c

Submission status: Wrong Answer

Solution code:
n = int(input())
data = list(map(int, input().split()))
sum = 0
first = max(data)
ans = []
del data[data.index(max(data))]
while len(data) != 1:
    ans.append([max(data) ,min(data)])
    add = min(data) - max(data)
    del data[data.index(min(data))]
    del data[data.index(max(data))]
    data.append(add)
print(first - data[0])
for i in ans:
    print("{} {}".format(i[0], i[1]))
print("{} {}".format(first, data[0]))

Submission status: Wrong Answer

Solution code:
n = int(input())
data = list(map(int, input().split()))
sum = 0
maximam = max(data)
ans = []
del data[data.index(max(data))]
for i in range(n - 2):
    ans.append([max(data) ,min(data)])
    data.append(min(data) - max(data))
    del data[data.index(max(data))]
    del data[data.index(min(data))]
print(maximam - data[0])
for i in ans:
    print("{} {}".format(i[0], i[1]))
print("{} {}".format(maximam, data[0]))

Submission status: Wrong Answer

Solution code:
N = [int(i) for i in input().split()][0]
D = [int(i) for i in input().split()]

D.sort()
num = int(N//2)
count = D[num]-D[num-1]
print(count)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
#n,m=map(int,input().split())
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
if a[-1]<=0:
    x=a.pop()
    print(x-sum(a))
    for y in a:
        print(x,y)
        x=x-y
elif a[0]>=0:
    a=a[::-1]
    x=a.pop()
    print(-x+sum(a))
    while a:
        y=a.pop()
        if len(a)>0:
            print(x,y)
            x=x-y
        else:
            print(y,x)
            
else:
    for i in range(n):
        if a[i]>0:
            ap=a[i:]
            an=a[:i]
            break
    print(sum(ap)-sum(an))
    z=ap.pop()
    x=an.pop()
    for y in ap:
        print(x,y)
        x=x-y
    for y in an:
        print(x,y)
        x=x-y
    print(z,x)

Submission status: Wrong Answer

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

AAA.sort()  # 昇順ソート

m = AAA[0]
M = AAA[N-1]
ANS = []

for i in range(1, N-1):
    if AAA[i] > 0:
        ANS.append((m, AAA[i]))
        m -= AAA[i]
        continue
    ANS.append((M, AAA[i]))
    M -= AAA[i]
ANS.append((M, m))

"""
わざと誤答する
"""
ans = 0
for a in AAA:
    ans += abs(a)

print(ans)
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()
q = 0
for ai in a:
    if ai < 0:
        q += 1
    else:
        break
if q == 0:
    q = 1
elif q == n:
    q = n - 1
ans = []
x = a[0]
for ai in a[q:-1]:
    ans.append([x, ai])
    x -= ai

ans.append([a[-1], x])
y = a[-1] - x

for ai in a[2:q]:
    ans.append([y, ai])
    y -= ai

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

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(raw_input())
    A = [int(_) for _ in raw_input().split()]
    A.sort()
    ret = abs(A[N-1] - A[0])
    rever_cnt = 0
    for i in range(1, N-1):
        if A[i] >= 0:
            ret = ret + A[i]
            rever_cnt += 1
        else:
            ret = ret - A[i]
    print ret
    def ManPrint(a, b):
        if rever_cnt % 2 == 0:
            print a, b
        else:
            print b, a
    def PrintAndCalcSave(a, b):
        print a, b
        return a - b

    if A[N-1] > A[0]:
        if rever_cnt % 2 == 1:
            save = PrintAndCalcSave(A[N-1], A[0])
        else:
            save = PrintAndCalcSave(A[0], A[N-1])
    else:
        if rever_cnt % 2 == 1:
            save = PrintAndCalcSave(A[0], A[N-1])
        else:
            save = PrintAndCalcSave(A[N-1], A[0])
    rever_cnt -= 1
    for i in range(1, N - 1):
        if rever_cnt % 2 == 1:
            save = PrintAndCalcSave(save, A[i])
        else:
            save = PrintAndCalcSave(A[i], save)
        rever_cnt -= 1

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
A.sort()
A2 = collections.deque(A)
#print(N, A)
x = [0] * (N-1)
y = [0] * (N-1)
for i in range(N -1):
    amin = A2.popleft()
    amax = A2.pop()
    if i != N - 2:
        amin2 = abs(A2[0])
        amax2 = abs(A2[len(A2)-1])
        if i == N - 3:
            if A2[0] > 0:
                A2.appendleft(amin - amax)
                x[i] = amin
                y[i] = amax
            else:
                A2.append(amax - amin)
                x[i] = amax
                y[i] = amin
        else:
            if amax2 >= amin2:
                A2.appendleft(amin - amax)
                x[i] = amin
                y[i] = amax
            else:
                A2.append(amax - amin)
                x[i] = amax
                y[i] = amin

    else:
        A2.append(amax - amin)
        x[i] = amax
        y[i] = amin

    #print("A", A2)

print(A2[0])
#print(x, y)
for i in range(N - 1):
    print(x[i], y[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

A = sorted(A)
sum_A = sum(A)


cand1 = sum_A-A[0]*2
cand2 = A[-1]*2-sum_A

if cand1 >= cand2:
    tmp = A[0]
    print(cand1)
    f = True
    for a in A[1:]:
        if f:
            print(tmp, a)
            tmp = tmp-a
        else:
            print(a, tmp)
            tmp = a-tmp
        f = not f
else:
    print(cand2)
    tmp = A[-1]
    for a in reversed(A[:-1]):
        print(tmp, a)
        tmp -= a

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

A = sorted(A)
sum_A = sum(A)


cand1 = sum_A-A[0]*2
cand2 = A[-1]*2-sum_A

print(cand1, cand2)
if cand1 >= cand2:
    tmp = A[0]
    print(cand1)
    f = True
    for a in A[1:]:
        if f:
            print(tmp, a)
            tmp = tmp-a
        else:
            print(a, tmp)
            tmp = a-tmp
        f = not f
else:
    print(cand2)
    tmp = A[-1]
    for a in reversed(A[:-1]):
        print(tmp, a)
        tmp -= a

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
def c(n,a):
    a.sort()
    max = a.pop()
    min = a.pop(0)
    opr = []
    for i in a:
        if i >= 0:
            opr.append('{} {}'.format(min,i))
            min-=i
        else:
            opr.append('{} {}'.format(max,i))
            max-=i
        opr.append('{} {}'.format(max,min))
        return str(max - min) + '\n' + '\n'.join(opr)
print(c(n,a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
def c(n,a):
    a.sort()
    max = a.pop()
    min = a.pop(0)
    opr = []
    for i in a:
        if i > 0:
            opr.append('{} {}'.format(min,i))
            min-=i
        else:
            opr.append('{} {}'.format(max,i))
            max-=i
        opr.append('{} {}'.format(max,min))
        return str(max - min) + '\n' + '\n'.join(opr)
print(c(n,a))

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
point = bisect.bisect_right(a, 0)
a_minus = a[:point]
a_plus = a[point:]
len_plus = len(a_plus)
if (len(a_minus) > 0) & (len(a_plus) > 0):
    ans = sum(a_plus) - sum(a_minus)
    print(ans)
    for i in range(len(a_plus)):
        if i == 0:
            x = a_minus.pop(0)
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
        elif i == len_plus - 1:
            y = x - y
            x = a_plus.pop(0)
            print(str(x) + " " + str(y))
        else:
            x = x - y
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
    for j in range(len(a_minus)):
        x = x - y
        y = a_minus.pop(0)
        print(str(x) + ' ' + str(y))
elif len(a_plus) > 0:
    ans = sum(a_plus) - ((a[0]) * 2)
    print(ans)
    for i in range(len(a_plus) - 1):
        if i == 0:
            x = a_plus.pop(0)
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
        elif i == len_plus - 2:
            y = x - y
            x = a_plus.pop(0)
            print(str(x) + " " + str(y))
        else:
            x = x - y
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
elif len(a_minus) > 0:
    ans = (a_minus[-1] * 2) - sum(a_minus)
    print(ans)
    for i in range(len(a_minus) - 1):
        if i == 0:
            x = a_minus[-1]
            y = a_minus.pop(0)
            print(str(x) + " " + str(y))
        elif i == len_plus - 2:
            x = x - y
            y = a_minus.pop(0)
            print(str(x) + " " + str(y))
        else:
            x = x - y
            y = a_minus.pop(0)
            print(str(x) + " " + str(y))

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
point = bisect.bisect_right(a, 0)
a_minus = a[:point]
a_plus = a[point:]
len_plus = len(a_plus)
if (len(a_minus) > 0) & (len(a_plus) > 0):
    ans = sum(a_plus) - sum(a_minus)
    print(ans)
    for i in range(len(a_plus)):
        if i == 0:
            x = a_minus.pop(0)
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
        elif i == len_plus - 1:
            y = x - y
            x = a_plus.pop(0)
            print(str(x) + " " + str(y))
        else:
            x = x - y
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
    for j in range(len(a_minus)):
        x = x - y
        y = a_minus.pop(0)
        print(str(x) + ' ' + str(y))
elif len(a_plus) > 0:
    ans = sum(a_plus) - ((a[0]) * 2)
    print(ans)
    for i in range(len(a_plus) - 1):
        if i == 0:
            x = a_plus.pop(0)
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
        elif i == len_plus - 2:
            y = x - y
            x = a_plus.pop(0)
            print(str(x) + " " + str(y))
        else:
            x = x - y
            y = a_plus.pop(0)
            print(str(x) + " " + str(y))
elif len(a_minus) > 0:
    ans = (a_minus[-1] * 2) - sum(a_minus)
    print(ans)
    for i in range(len(a_minus) - 1):
        if i == 0:
            x = a_minus.pop(0)
            y = a_minus.pop(0)
            print(str(x) + " " + str(y))
        elif i == len_plus - 2:
            y = x - y
            x = a_minus.pop(0)
            print(str(x) + " " + str(y))
        else:
            x = x - y
            y = a_minus.pop(0)
            print(str(x) + " " + str(y))

Submission status: Wrong Answer

Solution code:
m = int(input())
a = [int(i) for i in input().split()]
maxi = max(a)
mini = min(a)
flag = maxi * mini > 0
if flag == 1: #プラスかマイナスどっちかしかない場合
	res = sum([abs(i) for i in a]) - abs(mini * 2)
	print(res)
	a.remove(maxi)
	a.remove(mini)
	if mini > 0:
		cur = mini - maxi
		print(mini, maxi)
		for i in range(len(a)-1):
			print(cur, a[i])
			cur -= a[i]
		print(max(cur, a[-1]), min(cur, a[-1]))
	else:
		cur = maxi - mini
		print(maxi, mini)
		for i in range(len(a)-1):
			print(cur, a[i])
			cur -= a[i]
		print(max(cur, a[-1]), min(cur, a[-1]))
else:
	res = sum([abs(i) for i in a])
	print(res)
	a.remove(maxi)
	a.remove(mini)
	for i in range(len(a)):
		if i >= 0:
			print(mini, a[i])
			mini -= a[i]
		else:
			print(maxi, a[i])
			maxi -= a[i]
	print(max(maxi, mini), min(maxi, mini))

Submission status: Wrong Answer

Solution code:
import sys
import collections as col
import numpy as np


def main():
    n = int(input())
    aarray = [int(e) for e in input().split()]
    aqueue = col.deque(sorted(aarray))
    record = []
    amin = aqueue.popleft()
    while len(aqueue) > 1:
        amax = aqueue.pop()
        record.append((amin, amax))
        amin -= amax
    amax = aqueue.pop()
    record.append((amax, amin))
    print(amax - amin)
    for r in record:
        print(r[0], r[1])


main()

Submission status: Wrong Answer

Solution code:
n = int(input())
ai = [int(x) for x in input().split()]

ai.sort()

ai[1:-1] = [abs(x) for x in ai[1:-1]]

out = -ai[0] + sum(ai[1:len(ai)-1]) + ai[-1]

print(out)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
if n % 2 == 0:
    k = n // 2
else:
    k = (n // 2) + 1
a_1 = a[0:k]
a_2 = a[k:]
ans = sum(a_2) - sum(a_1)
print(ans)
if n % 2 == 1:
    for i in range(n - 1):
        if i == 0:
            x = a_2.pop(0)
            y = a_1.pop(0)
            print(str(x) + " " + str(y))
        else:
            if i == n - 2:
                x = x - y
                y = a_1.pop(0)
                print(str(x) + " " + str(y))
            elif i % 2 == 0:
                y = x - y
                x = a_2.pop(0)
                print(str(x) + " " + str(y))
            elif i % 2 == 1:
                y = x - y
                x = a_1.pop(0)
                print(str(x) + " " + str(y))
                
elif n % 2 == 0:
    for i in range(n - 1):
        if i == 0:
            x = a_2.pop(0)
            y = a_1.pop(0)
            print(str(x) + " " + str(y))
        else:
            if i % 2 == 0:
                y = x - y
                x = a_2.pop(0)
                print(str(x) + " " + str(y))
            elif i % 2 == 1:
                y = x - y
                x = a_1.pop(0)
                print(str(x) + " " + str(y))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
if n % 2 == 0:
    k = n // 2
else:
    k = (n // 2)
a_1 = a[0:k]
a_2 = a[k:]
ans = - sum(a_1) + sum(a_2)
print(ans)
if n % 2 == 1:
    for i in range(n - 1):
        if i == 0:
            x = a_1.pop(0)            
            y = a_2.pop(0)
            print(str(x) + " " + str(y))
        else:
            if i % 2 == 0:
                y = x - y
                x = a_1.pop(0)
                print(str(x) + " " + str(y))
            elif i % 2 == 1:
                y = x - y
                x = a_2.pop(0)
                print(str(x) + " " + str(y))
elif n % 2 == 0:
    for i in range(n - 1):
        if i == 0:
            x = a_2.pop(0)
            y = a_1.pop(0)
            print(str(x) + " " + str(y))
        else:
            if i % 2 == 0:
                y = x - y
                x = a_2.pop(0)
                print(str(x) + " " + str(y))
            elif i % 2 == 1:
                y = x - y
                x = a_1.pop(0)
                print(str(x) + " " + str(y))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
if n % 2 == 0:
    k = n // 2
else:
    k = (n // 2) + 1
a_1 = a[0:k]
a_2 = a[k:]
a_1.sort()
a_2.sort()
ans = sum(a_1) - sum(a_2)
print(ans)
if n % 2 == 1:
    for i in range(n - 1):
        if i == 0:
            x = a_2.pop(0)            
            y = a_1.pop(0)
            print(str(x) + " " + str(y))
        else:
            if i % 2 == 0:
                y = x - y
                x = a_2.pop(0)
                print(str(x) + " " + str(y))
            elif i % 2 == 1:
                y = x - y
                x = a_1.pop(0)
                print(str(x) + " " + str(y))
elif n % 2 == 0:
    for i in range(n - 1):
        if i == 0:
            x = a_2.pop(0)
            y = a_1.pop(0)
            print(str(x) + " " + str(y))
        else:
            if i % 2 == 0:
                y = x - y
                x = a_2.pop(0)
                print(str(x) + " " + str(y))
            elif i % 2 == 1:
                y = x - y
                x = a_1.pop(0)
                print(str(x) + " " + str(y))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
if n % 2 == 0:
    k = n // 2
else:
    k = (n // 2) + 1
a_1 = a[0:k]
a_2 = a[k:]
a_1.sort()
a_2.sort()
ans = sum(a_1) - sum(a_2)
print(ans)
if n % 2 == 1:
    for i in range(n - 1):
        if i == 0:
            x = a_2.pop(0)            
            y = a_1.pop(0)
            print(str(x) + " " + str(y))
        else:
            if i % 2 == 0:
                y = x - y
                x = a_2.pop(0)
                print(str(x) + " " + str(y))
            elif i % 2 == 1:
                y = x - y
                x = a_1.pop(0)
                print(str(x) + " " + str(y))
elif n % 2 == 0:
    for i in range(n - 1):
        if i == 0:
            x = a_1.pop(0)
            y = a_2.pop(0)
            print(str(x) + " " + str(y))
        else:
            if i % 2 == 0:
                y = x - y
                x = a_1.pop(0)
                print(str(x) + " " + str(y))
            elif i % 2 == 1:
                y = x - y
                x = a_2.pop(0)
                print(str(x) + " " + str(y))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = input().split()
for i in range(len(a)):
    a[i]=int(a[i])
# print(a)
a.sort()
# print(a)
# #両端をとっておく
hashi1 = a[0]
hashi2 = a[-1]
a.remove(a[0])
a.remove(a[-1])
# print(a)
minus_list = []
plus_list = []
for i in range(len(a)):
    if a[i]>0:
        plus_list.append(a[i])
    else:
        minus_list.append(a[i])
# print(minus_list)
# print(plus_list)
for j in range(len(minus_list)):
    hashi2 = hashi2-minus_list[j]
for l in range(len(plus_list)):
    hashi1 = hashi1 - plus_list[l]

print(hashi2-hashi1)

Submission status: Wrong Answer

Solution code:
import sys
import collections as col
import numpy as np


def main():
    n = int(input())
    aarray = [int(e) for e in input().split()]
    aqueue = col.deque(sorted(aarray))
    record = []
    while len(aqueue) > 2:
        amin = aqueue.popleft()
        amax = aqueue.pop()
        record.append((amin, amax))
        anext = aqueue.pop()
        record.append((anext, (amin - amax)))
        aqueue.append(anext - (amin - amax))
        aqueue = col.deque(sorted(aqueue))
    if len(aqueue) == 2:
        amin = aqueue.popleft()
        amax = aqueue.pop()
        print(amax - amin)
        record.append((amax, amin))
    else:
        print(aqueue.pop())
    for r in record:
        print(r[0], r[1])


main()

Submission status: Wrong Answer

Solution code:
import sys
import collections as col
import numpy as np


def main():
    n = int(input())
    aarray = [int(e) for e in input().split()]
    aqueue = col.deque(sorted(aarray))
    record = []
    while len(aqueue) > 2:
        amin = aqueue.popleft()
        amax = aqueue.pop()
        record.append((amin, amax))
        anext = aqueue.pop()
        record.append((anext, (amin - amax)))
        aqueue.append(anext - (amin - amax))
    if len(aqueue) == 2:
        amin = aqueue.popleft()
        amax = aqueue.pop()
        print(amax - amin)
        record.append((amax, amin))
    else:
        print(aqueue.pop())
    for r in record:
        print(r[0], r[1])


main()