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: Runtime Error

Solution code:
D = {}


def sub(A, key):
    if len(A) == 1:
        return (A[0], None), (A[0], None)
    min_sub_list = []
    max_sub_list = []
    for i in range(len(A)):
        A_i = A.copy()
        key_i = key.copy()
        a_i = A_i.pop(i)
        key_i.pop(i)
        dkey = tuple(key_i)
        if dkey not in D:
            res_min_i, res_max_i = sub(A_i, key_i)
            D[dkey] = res_min_i, res_max_i
        else:
            res_min_i, res_max_i = D[dkey]
        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)
    return res_min, res_max


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


N = int(input())
A_temp = map(int, input().split())
A = list(A_temp)
key = [i for i in range(N)]
_, res_max = sub(A, key)
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: Runtime Error

Solution code:
def sub(A):
    if len(A) == 1:
        return (A[0], None), (A[0], None)
    min_sub_list = []
    max_sub_list = []
    for i in range(len(A)):
        A_i = A.copy()
        a_i = A_i.pop(i)
        res_min_i, res_max_i = sub(A_i)
        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)
    return res_min, res_max


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


N = int(input())
A_temp = map(int, input().split())
A = list(A_temp)
_, res_max = sub(A)
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: Runtime Error

Solution code:
from bisect import bisect_left


n = int(input())
x = list(map(int, input().split()))
x.sort()
if x[-1] < 0:
# 全マイナス
    print(x[-1] + sum([abs(i) for i in x[:-1]]))
    num_one = x[-1]
    for i in x[:-1]:
        print(num_one, i)
        num_one -= i
        

elif x[0] > 0:
    print(x[0] + sum([abs(i) for i in x[1:]]))
    num_one = x[0]
    for i in x[1:-1]:
        print(num_one, i)
        num_one -= i
    print(x[-1], num_one)
    

else:
    print(sum([abs(i) for i in x]))
    y = bisect_left(x, 0)
    minus = x[:y]
    plus = x[y:]
    num_one = plus[0]
    for i in minus[1:]:
        print(num_one, i)
        num_one -= i
    num_two = minus[0]
    for j in plus[1:]:
        print(num_two, j)
        num_two -= j
    print(num_one, num_two)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left


n = int(input())
x = list(map(int, input().split()))
x.sort()
if x[-1] < 0:
# 全マイナス
    x[-1] + sum([abs(i) for i in x[:-1]])
    num_one = x[-1]
    for i in x[:-1]:
        print(num_one, i)
        num_one -= i
        

elif x[0] > 0:
    print(x[0] + sum([abs(i) for i in x[1:]]))
    num_one = x[0]
    for i in x[1:-1]:
        print(num_one, i)
        num_one -= i
    print(x[-1], num_one)
    

else:
    print(sum([abs(i) for i in x]))
    y = bisect_left(x, 0)
    minus = x[:y]
    plus = x[y:]
    num_one = plus[0]
    for i in minus[1:]:
        print(num_one, i)
        num_one -= i
    num_two = minus[0]
    for j in plus[1:]:
        print(num_two, j)
        num_two -= j
    print(num_one, num_two)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

A.sort()

for i,a in enumerate(A):
    if a>=0:break
B = A[:i]
C = A[i:]

ans = []
if not B:
    x = C[0]
    for a in C[1:-1]:
        ans.append((x,a))
        x -= a
    ans.append((C[-1],x))
    x = C[-1] - x
elif not C:
    x = B[-1]
    for a in B[:-1]:
        ans.append((x,a))
        x -= a
else:
    x = B[0]
    for a in C[1:]:
        ans.append((x,a))
        x -= a
    ans.append((C[0],x))
    x = C[0]-x
    for a in B[1:]:
        ans.append(x,a)
        x -= a

print(x)
for out in ans:
    print(*out)

Submission status: Runtime Error

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

A.sort()

for i,a in enumerate(A):
    if a>=0:break
B = A[:i]
C = A[i:]

ans = []
if not B:
    x = C[0]
    for a in C[1:-1]:
        ans.append((x,a))
        x -= a
    ans.append((C[-1],x))
    x = C[-1] - x
else:
    x = B[0]
    for a in C[1:]:
        ans.append((x,a))
        x -= a
    ans.append((C[0],x))
    x = C[0]-x
    for a in B[1:]:
        ans.append(x,a)
        x -= a

print(x)
for out in ans:
    print(*out)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
A=list(map(int,input().split()))
A.sort()
process=[]
count=0

if N==2:
    process.append((A[1],A[0]))
    ans=A[1]-A[0]
else:
    for k in (0,1,-1,-2):
        if A[k]!=0:
            count=count+1
    if count<=1:
        ans=abs(sum(A))
        process.append((A[-1],A[0]))
        for _ in range(len(A)-2):
            process.append((ans,0))
    elif A[0]>=0:
        process.append((A[0],A[-1]))
        A=[A[0]-A[-1]]+A[1:len(A)-2]
        N=len(A)
    elif A[-1]<=0:
        process.append((A[-1],A[0]))
        A=A[1:len(A)-2]+[A[-1]-A[0]]
        N=len(A)

if A[0]<0 and A[N-1]>0:
    i=1
    ans1=A[N-1]
    while A[i]<0:
        ans1=ans1-A[i]#正の値
        process((A[N-1],A[i]))
        i=i+1
    j=N-2
    ans2=A[0]
    while A[j]>0:
        ans2=ans2-A[j]#負の値
        process.append((A[0],A[j]))
        j=j-1
    if i-1!=j:
        n=j-(i-1)
        for _ in range(n):
            process.append((ans1,0))
    ans=ans1-ans2
    process.append((ans1,ans2))
print(ans)
for x,y in process:
    print(x,y)

Submission status: Runtime Error

Solution code:
from bisect import insort_left

N = int(input())
*A, = map(int, input().split())
operations = []
pos = []; zero = []; neg = [];
for a in A:
    if a > 0: insort_left(pos, a)
    elif a == 0: zero.append(a)
    else: insort_left(neg, a)
if len(neg) == 0:
    pos = zero + pos
    operations.append((pos[0], pos[-1]))
    neg = [pos[0] - pos[-1]]
    pos = pos[1:-1]
elif len(pos) == 0:
    neg = neg + zero
    operations.append((neg[-1], neg[0]))
    pos = [neg[-1] - neg[0]]
    neg = neg[1:-1]
else:
    neg  = neg + zero
neg = neg[::-1]
while len(pos) > 1 and len(neg) > 1:
    n, p = neg.pop(), pos.pop()
    operations.append((p, n))
    pos.append(p-n)
if len(neg) == 1:
    while len(pos) > 1:
        n, p = neg.pop(), pos.pop()
        operations.append((n, p))
        neg.append(n-p)
else:
    while len(neg) > 1:
        n, p = neg.pop(), pos.pop()
        operations.append((p, n))
        neg.append(p-n)
operations.append((pos[0], neg[0]))
ans = pos[0] - neg[0]
print(ans)
for x, y in operations:
    print(x, y)

Submission status: Runtime Error

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
br = []
ar.sort()
while True:
    br.append([ar[0],ar[-2]])
    ar.append(ar[0]-ar[-2])
    del ar[-3]
    del ar[0]
    ar.sort()
    if len(ar) == 2:
        br.append([ar[1],ar[0]])
        print(ar[1] - ar[0])
        break
for r in br:
    print(r[0],r[1])

Submission status: Runtime Error

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

if a[0]>=0:
    g=sum[1:]-a[0]
    print(g)
    for i in range(n-1):
        if len(a)>2:
            print(str(a[0])+" "+str(a[-1]))
            a[0]-=a[-1]
            a.remove(a[-1])
        else:
            print(str(a[1]+" "+str(a[0])))
    
elif a[-1]<=0:
    g=a[-1]-sum(a[:-1])
    print(g)
    for i in range(n-1):
        print(str(a[-1])+" "+str(a[0]))
        a[-1]-=a[0]
        a.remove(a[0])
else:
    g=0 
    for i in range(n):
        g+=abs(a[i])
    print(g)
    for _ in range(n-1):
        if a[-2]>=0:
            print(str(a[0])+" "+str(a[-1]))
            a[0]-=a[-1]
            a.remove(a[-1])
        else:
            print(str(a[-1])+" "+str(a[0]))
            a[-1]-=a[0]
            a.remove(a[0])

Submission status: Runtime Error

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:
        if len(a)>1:
            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: Runtime Error

Solution code:
(lambda __g, __y, __operator, __print: [[(a.sort(), [(lambda __items, __after, __sentinel: __y(lambda __this: lambda: (lambda __i: [(lambda __after: [__after() for __g['mx'] in [(__operator.isub(__g['mx'], i))]][0] if (i < 0) else [__after() for __g['mn'] in [(__operator.isub(__g['mn'], i))]][0])(lambda: __this()) for __g['i'] in [(__i)]][0] if __i is not __sentinel else __after())(next(__items, __sentinel)))())(iter(a[1:-1]), lambda: (__print((mx - mn)), [(lambda __items, __after, __sentinel: __y(lambda __this: lambda: (lambda __i: [(lambda __after: (__print(((str(mx) + ' ') + str(i))), [__after() for __g['mx'] in [(__operator.isub(__g['mx'], i))]][0])[1] if (i < 0) else (__print(((str(mn) + ' ') + str(i))), [__after() for __g['mn'] in [(__operator.isub(__g['mn'], i))]][0])[1])(lambda: __this()) for __g['i'] in [(__i)]][0] if __i is not __sentinel else __after())(next(__items, __sentinel)))())(iter(a[1:-1]), lambda: (__print(((str(mx) + ' ') + str(mn))), None)[1], []) for (__g['mn'], __g['mx']) in [((a[0], a[-1]))]][0])[1], []) for (__g['mn'], __g['mx']) in [((a[0], a[-1]))]][0])[1] for __g['a'] in [(list(map(int, input().split())))]][0] for __g['n'] in [(int(input()))]][0])(globals(), (lambda f: (lambda x: x(x))(lambda y: f(lambda: y(y)()))), __import__('operator', level=0), __import__('builtins', level=0).__dict__['print'])

Submission status: Runtime Error

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 len(a_plus) == 1:
            x = a_plus.pop(0)
            y = a_minus.pop(0)
            print(str(x) + " " + str(y))
        elif (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: Runtime Error

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:
    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))
else:
    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))

Submission status: Runtime Error

Solution code:
from heapq import heappop,heappush
n = int(input())
A = sorted(list(map(int,input().split())))
P = []
M = []
if n == 2:
    print(A[1]-A[0])
    print(A[1],A[0])
    exit()
def move():
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
for a in A:
    if a>=0:
        P.append(a)
    else:
        M.append(a)

if A[-1]>=0 and A[0]<0:
    print(sum(abs(a) for a in A))
elif A[0]>=0:
    print(sum(A)-min(A)*2)
    mm = heappop(P)
    pp = heappop(P)
    print(mm,pp)
    heappush(M,mm-pp)
else:
    print(sum(abs(a) for a in A)+max(A)*2)
    mm = M[-1]
    pp = M[-2]
    print(mm,pp)
    M = M[:-2]
move()

Submission status: Runtime Error

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

ai.sort()

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

o1 = -ai[0] + sum(temp) + ai[-1]

print(o1)

temp1 = ai.pop(-1)
num_p = sum(x >= 0 for x in ai)
if num_p is len(ai):
  num_p = len(ai) - 1
for i in range(num_p):
  temp2 = ai.pop(-1)
  print(ai[0], temp2)
  ai[0] -= temp2

for i in range(len(ai)):
  temp2 = ai.pop(0)
  print(temp1, temp2)
  temp1 -= temp2

Submission status: Runtime Error

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

ai.sort()

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

o1 = -ai[0] + sum(temp) + ai[-1]

print(o1)

temp1 = ai.pop(-1)
num_p = sum(x >= 0 for x in ai)
if num_p is len(ai):
  num_p = len(ai) - 1
for i in range(num_p):
  temp2 = ai.pop(-1)
  temp3 = ai[0]
  print(temp3, temp2)
  ai[0] = temp3 -temp2

for i in range(len(ai)):
  temp2 = ai.pop(0)
  print(temp1, temp2)
  temp1 = temp1 - temp2

Submission status: Runtime Error

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

ai.sort()

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

o1 = -ai[0] + sum(temp) + ai[-1]

print(o1)

temp1 = ai.pop(-1)
num_p = sum(x >= 0 for x in ai)
for i in range(num_p):
  temp2 = ai.pop(-1)
  temp3 = ai[0]
  print(temp3, temp2)
  ai[0] = temp3 -temp2

for i in range(len(ai)):
  temp2 = ai.pop(0)
  print(temp1, temp2)
  temp1 = temp1 - temp2

Submission status: Runtime Error

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-1]
a_2 = a[k-1:]
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: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split(' ')))
a.sort(reverse=True)

process = []
p_seed = a.pop(0)
n_seed = a.pop(-1)
for e in a:
  	if e >= 0:
    	process.append((n_seed, e))
    	n_seed -= e
  	else:
    	process.append((p_seed, e))
    	p_seed -= e
process.append((p_seed, n_seed))
m = p_seed - n_seed
print(m)
for x, y in process:
    print(str(x) + ' ' + str(y))

Submission status: Runtime Error

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 = 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: Runtime Error

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

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


while len(p)!=1:

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

Submission status: Runtime Error

Solution code:
import itertools
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
for i in range(n-2):
    x,y=a[i],a[i+1]
    del a[i:i+2]
    a+=[x-y]
    a.sort()
    print(x,y)
print(a[1],a[0])

Submission status: Runtime Error

Solution code:
from collections import deque
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
ap = deque([n for n in a if n >= 0])
an = deque([n for n in a if n < 0])
q = deque()

while len(ap) > 1 and len(an) > 0:
    y, x = ap.popleft(), an.popleft()
    q.append((x, y))
    an.appendleft(x - y)

while len(ap) + len(an) > 2:
    y, x = ap.popleft(), ap.popleft()
    q.append((x, y))
    ap.appendleft(x - y)

if len(an):
    x = ap.popleft()
    y = an.popleft()
else:
    y = ap.popleft()
    x = ap.popleft()

q.append((x, y))

print(x - y)

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

Submission status: Runtime Error

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

mxs = sorted(x for x in A if x < 0)
pxs = sorted(x for x in A if x >= 0)

if len(mxs) == 0:
    mxs = [pxs[0]]
    pxs = pxs[1:]

#print(mxs, pxs)

result = sum(pxs) - sum(mxs)
print(result)

if len(pxs) == 1:
    for y in mxs:
        print(pxs[0], y)
        pxs[0] -= y
else:
    for y in pxs[:-1]:
        print(mxs[0], y)
        mxs[0] -= y
    print(pxs[-1], mxs[0])
    pxs[-1] -= mxs[0]
    for y in mxs[1:]:
        print(pxs[-1], y)
        pxs[-1] -= y

#for i in range

Submission status: Runtime Error

Solution code:
def successive_subtraction(N, A, i=1, l=[]):
    mx = max(A)
    mn = min(A)
    if i == N - 1:
        print(mx - mn)
        return l
    l.append((mn, mx))
    A.remove(mn)
    A.remove(mx)
    A.append(mn - mx)
    return successive_subtraction(N, A, i + 1, l)

N = int(input())
A = list(map(int, input().split()))
l = successive_subtraction(N, A)
for v in l:
    print(v[0], v[1])

Submission status: Runtime Error

Solution code:
import sys

from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7

# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]


N = int(input())
A = [int(x) for x in input().split()]

Apos = deque(sorted(filter(lambda x: x > 0, A)))
Azero = deque(sorted(filter(lambda x: x == 0, A)))
Aneg = deque(sorted(filter(lambda x: x < 0, A)))

L = []
T = []


def f(p, z, n):
    if len(p) + len(z) + len(n) == 1:
        return (p + z + n)[0]

    if len(z) == 0:
        if len(p) >= 1 and len(n) >= 1:
            if len(p) > 1:
                T.append((n[0], p[0]))
                n[0] -= p.popleft()
                return f(p, z, n)
            elif len(n) > 1:
                T.append((p[0], n[0]))
                p[0] -= n.popleft()
                return f(p, z, n)
            else:
                T.append((p[0], n[0]))
                p[0] -= n.popleft()
                return f(p, z, n)
        elif len(p) == 0:
            n1 = n.popleft()
            n2 = n.popleft()
            T.append((n1, n2))
            p.append(n1 - n2)
            return f(p, z, n)
        else:
            p1 = p.popleft()
            p2 = p.popleft()
            if len(p) == 0:
                T.append((p2, p1))
                p.append(p2 - p1)
                return f(p, z, n)
            else:
                T.append((p1, p2))
                n.append(p1 - p2)
                return f(p, z, n)


    elif len(z) >= 2:
        T.append((0, 0))
        z.popleft()
        return f(p, z, n)
    else:
        if len(p) >= 1 and len(n) >= 1:
            T.append((p[0], 0))
            n.popleft()
            return f(p, z, n)
        elif len(p) == 0:
            T.append((0, n[0]))
            p.append(-n.popleft())
            return f(p, z, n)
        elif len(n) == 0:
            T.append((0, p[0]))
            n.append((-p.popleft()))
            return f(p, z, n)


print(f(Apos, Azero, Aneg))
for x, y in T:
    print("{} {}".format(x, y))

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
ans=0
a.sort()
a.reverse()
if n%2==0:
    p=n//2
else:
    p=n//2+1
c=1
lp=[]
lm=[]
for i in a:
    if i>0 and c<=p:
        ans+=i
        lp.append(i)
    else:
        ans-=i
        lm.append(i)
    c+=1
print(ans)
llp=len(lp)
llm=len(lm)
q=lm[0]
i=0
j=0
k=0
o=0

for i in range(llp-1):
    print(q,lp[i])
    q=q-lp[i]
print(lp[-1],q)
q=lp[-1]-q
for i in range(1,llm):
    print(q,lm[i])
    q=q-lm[i]

Submission status: Runtime Error

Solution code:
# coding: utf-8
n = int(input())
a = list(map(int, input().split()))
#print(n, a)

print_list = []
    
pos = [x for x in a if x>=0]
neg = [x for x in a if x<0]
#print(pos, neg)
while(len(pos) != len(neg)):
    if len(pos) == 0:
        x = max(neg)
        y = min(neg)
        z = x - y
        neg.remove(x)
        neg.remove(y)
        pos.append(z)
        print_list.append((x, y))
    elif len(neg) == 0:
        x = min(pos)
        y = max(pos)
        z = x - y
        pos.remove(x)
        pos.remove(y)
        neg.append(z)
        print_list.append((x, y))
    elif len(pos) < len(neg):
        x = max(pos)
        y = min(neg)
        z = x - y
        pos.remove(x)
        neg.remove(y)
        pos.append(z)
    elif len(pos) > len(neg):
        x = min(neg)
        y = max(pos)
        z = x - y 
        neg.remove(x)
        pos.remove(y)
        neg.append(z)
        print_list.append((x, y))
    else:
        pass
# len(pos) == len(neg)
while(True):
    tmp_pos, tmp_neg = [], []
    if len(pos) == 1 and len(neg) == 1:
        x = max(pos)
        y = min(neg)
        z = x - y
        print_list.append((x, y))
        break
    elif len(pos)%2 == 0:
        for i in range(len(pos)):
            if i % 2 == 0:
                x, y = pos[i], neg[i]
                z = x - y
                tmp_pos.append(z)
                print_list.append((x, y))
            else:
                x, y = neg[i], pos[i]
                z = x - y
                print_list.appen((x, y))
                tmp_neg.append(z)
        pos = tmp_pos
        neg = tmp_neg
    else:
        for i in range(len(pos)-1):
            if i % 2 == 0:
                x, y = pos[i], neg[i]
                z = x - y
                print_list.append((x, y))
                tmp_pos.append(z)
            else:
                x, y = neg[i], pos[i]
                z = x - y
                print_list.append((x, y))
                tmp_neg.append(z)
        tmp_pos.append(pos[i+1])
        tmp_neg.append(neg[i+1])
        pos = tmp_pos
        neg = tmp_neg
            
            
print(z)
for i in range(len(print_list)):
    x = print_list[i][0]
    y = print_list[i][1]
    print(x, y)

Submission status: Runtime Error

Solution code:
from heapq import heappop,heappush
n = int(input())
A = sorted(list(map(int,input().split())))
P = []
M = []
if n == 2:
    print(A[1]-A[0])
    print(A[1],A[0])
    exit()
if all(a == 0 for a in A):
    print(0)
    for i in range(n-1):
        print(0,0)
    exit()
def move():
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
for a in A:
    if a>0:
        P.append(a)
    else:
        M.append(a)

if A[-1]>=0 and A[0]<=0:
    print(sum(abs(a) for a in A))
elif A[0]>=0:
    print(sum(A)-min(A)*2)
    mm = heappop(P)
    pp = heappop(P)
    print(mm,pp)
    heappush(M,mm-pp)
else:
    print(sum(abs(a) for a in A)+max(A)*2)
    mm = M[-1]
    pp = M[-2]
    print(mm,pp)
    M = M[:-2]
    heappush(P,mm-pp)
move()

Submission status: Runtime Error

Solution code:
from heapq import heappop,heappush
n = int(input())
A = sorted(list(map(int,input().split())))
P = []
M = []
if n == 2:
    print(A[1]-A[0])
    print(A[1],A[0])
    exit()
def move():
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
for a in A:
    if a>0:
        P.append(a)
    else:
        M.append(a)
if all(a == 0 for a in A):
    print(0)
    for i in range(n-1):
        print(0,0)
    exit()
if A[-1]>=0 and A[0]<=0:
    print(sum(abs(a) for a in A))
elif A[0]>=0:
    print(sum(A)-min(A)*2)
    mm = heappop(P)
    pp = heappop(P)
    print(mm,pp)
    heappush(M,mm-pp)
else:
    print(sum(abs(a) for a in A)+max(A)*2)
    mm = M[-1]
    pp = M[-2]
    print(mm,pp)
    M = M[:-2]
    heappush(P,mm-pp)
move()

Submission status: Runtime Error

Solution code:
from heapq import heappop,heappush
n = int(input())
A = sorted(list(map(int,input().split())))
P = []
M = []
if n == 2:
    print(A[1]-A[0])
    print(A[1],A[0])
    exit()
def move():
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
for a in A:
    if a>0:
        P.append(a)
    else:
        M.append(a)

if A[-1]>=0 and A[0]<=0:
    print(sum(abs(a) for a in A))
elif A[0]>=0:
    print(sum(A)-min(A)*2)
    mm = heappop(P)
    pp = heappop(P)
    print(mm,pp)
    heappush(M,mm-pp)
else:
    print(sum(abs(a) for a in A)+max(A)*2)
    mm = M[-1]
    pp = M[-2]
    print(mm,pp)
    M = M[:-2]
    heappush(P,mm-pp)
move()

Submission status: Runtime Error

Solution code:
from heapq import heappop,heappush
n = int(input())
A = sorted(list(map(int,input().split())))
P = []
M = []
if n == 2:
    print(A[1]-A[0])
    print(A[1],A[0])
    exit()
if all(a == 0 for a in A):
    print(0)
    for i in range(n-1):
        print(0,0)
    exit()
def move():
    while len(P)!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        heappush(M,mm-pp)
    while len(M) != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        heappush(P,pp-mm)
for a in A:
    if a>=0:
        P.append(a)
    else:
        M.append(a)

if A[-1]>=0 and A[0]<=0:
    print(sum(abs(a) for a in A))
elif A[0]>=0:
    print(sum(A)-min(A)*2)
    mm = heappop(P)
    pp = heappop(P)
    print(mm,pp)
    heappush(M,mm-pp)
else:
    print(sum(abs(a) for a in A)+max(A)*2)
    mm = M[-1]
    pp = M[-2]
    print(mm,pp)
    M = M[:-2]
    heappush(P,mm-pp)
move()