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:
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 = []
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))
    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)
elif A[0]>=0:
    print(sum(A)-min(A)*2)
    mm = heappop(P)
    pp = heappop(P)
    print(mm,pp)
    heappush(M,mm-pp)
    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)
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)
    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)

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(True):
    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(neg) == 0 and len(pos) > 1:
        x = min(pos)
        y = max(pos)
        z = x - y
        print_list.append((x, y))
        neg.append(z)
        pos.remove(x)
        pos.remove(y)
    elif len(pos) == 0 and len(neg) > 1:
        x = max(neg)
        y = min(neg)
        z = x - y
        print_list.append((x, y))
        pos.append(z)
        neg.remove(x)
        neg.remove(y)
    elif len(pos) < len(neg):
        x = max(pos)
        y = min(neg)
        z = x - y
        print_list.append((x, y))
        pos.append(z)
        pos.remove(x)
        neg.remove(y)
    elif len(pos) > len(neg):
        x = min(neg)
        y = max(pos)
        z = x - y
        print_list.append((x, y))
        neg.append(z)
        neg.remove(x)
        pos.remove(y)
    elif len(pos) == len(neg):
        x = min(neg)
        y = max(pos)
        z = x - y
        print_list.append((x, y))
        neg.append(z)
        neg.remove(x)
        pos.remove(y)
    else:
        pass
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:

N = int(input())

A_lst = list(map(int, input().split()))
new_A_lst = []
logs_str = []
max_ans = 0

A_lst.sort()
if A_lst[0] >= 0:   #   全て正の整数の時
    logs_str.append(str(A_lst[0]) + " " + str(A_lst[-1]))
    new = A_lst[0] - A_lst[-1]
    A_lst.pop()
    A_lst.pop(0)
    while len(A_lst) > 2:
        logs_str.append(str(new) + " " + str(A_lst[-1]))
        new -= A_lst[-1]
        A_lst.pop()

    logs_str.append(str(A_lst[0]) + " " + str(new))
    max_ans = A_lst[0] - new

elif A_lst[-1] <= 0:    #   全て負の整数の時
    logs_str.append(str(A_lst[0]) + " " + str(A_lst[-1]))
    new = A_lst[0] - A_lst[-1]
    A_lst.pop()
    A_lst.pop(0)
    while A_lst:
        logs_str.append(str(new) + " " + str(A_lst[-1]))
        new -= A_lst[-1]
        A_lst.pop()
    max_ans = new

else:   #   正負混合の時
    center = -1
    for i in range(N):
        if A_lst[i] >= 0:
            center = i
    
    logs_str.append(str(A_lst[0]) + " " + str(A_lst[center]))
    new = A_lst[0] - A_lst[center]
    for i in range(center+1, N-1):
        logs_str.append(str(new) + " " + str(A_lst[i]))
        new -= A_lst[i]
    m = new
    new = A_lst[-1] - A_lst[1]
    for i in range(2, center):
        logs_str.append(str(new) + " " + str(A_lst[i]))
        new -= A_lst[i]

    M = new
    logs_str.append(str(M) + " " + str(m))
    max_ans = M - m

print(max_ans)
for low in logs_str:
    print(low)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
A=[a[0]]
x=0
sum=sum(a)
sumL=-9999999999
if n==2:
  print(max(a)-min(a))
  print(max(a),min(a))
else:
  for i in range(1,n):
    A.append(A[i-1]+a[i])
  for i in range(1,n):
    if sumL<-A[i-1]+sum-A[i-1]:
      sumL=-A[i-1]+sum-A[i-1]
      x=i
  print(sumL)
  L1=a[0:x]
  L2=a[x:n]

  if x!=1:
    print(L2[0],L1[0])
    z=L2[0]-L1[0]
    for i in range(1,x-1):
      print(z,L1[i])
      z=z-L1[i]
  else:
    z=L2[0]

  print(L1[x-1],L2[1])
  y=L1[x-1]-L2[1]
  for i in range(2,len(L2)):
    print(y,L2[i])
    y=y-L2[i]

  print(z,y)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
A=[a[0]]
x=0
sumL=-99999999
for i in range(1,n):
  A.append(a[i-1]+a[i])
for i in range(1,n):
  if sumL<-A[i-1]+sum(a)-A[i-1]:
    sumL=-A[i-1]+sum(a)-A[i-1]
    x=i
print(sumL)
L1=a[0:x]
L2=a[x:n]

if x!=1:
  print(L2[0],L1[0])
  z=L2[0]-L1[0]
  for i in range(1,x-1):
    print(z,L1[i])
    z=z-L1[i]
else:
  z=L2[0]

print(L1[x-1],L2[1])
y=L1[x-1]-L2[1]
for i in range(2,len(L2)):
  print(y,L2[i])
  y=y-L2[i]

print(z,y)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

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

positives = deque(sorted(a))
negatives = deque([positives.popleft()])
for i in range(n - 2):
    if positives[i] < 0:
        negatives.append(positives.popleft())
    else:
        break

# print M
print(sum(positives) - sum(negatives))

while len(positives) > 1:
    y = positives.popleft()
    x = negatives.pop()
    print(x, y)
    negatives.append(x - y)

x = positives.popleft()

while len(negatives) > 0:
    y = negatives.pop()
    print(x, y)
    x = x - y

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

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

positives = deque(sorted(a))
negatives = deque([positives.popleft()])
for i in range(n - 2):
    if positives[i] < 0:
        negatives.append(positives.popleft())
    else:
        break

# print M
print(sum(positives) - sum(negatives))

x = negatives.pop()
while len(positives) > 1:
    y = positives.popleft()
    print(x, y)
    x -= y
negatives.append(x)

x = positives.popleft()
while len(negatives) > 0:
    y = negatives.pop()
    print(x, y)
    x = x - y

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

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

positives = deque(sorted(a))
negatives = deque([positives.popleft()])
for i in range(n - 1):
    if a[i] < 0:
        negatives.append(positives.popleft())
    else:
        break

# print M
print(sum(positives) - sum(negatives))

while len(positives) > 1:
    y = positives.popleft()
    x = negatives.pop()
    print(x, y)
    negatives.append(x - y)

x = positives.popleft()

while len(negatives) > 0:
    y = negatives.pop()
    print(x, y)
    x = x - y

Submission status: Runtime Error

Solution code:
n=int(input())
xs=list(map(int,input().split()))
if len(xs) == 2:
    xs = sorted(xs)
    print(xs[1]-xs[0])
    print(xs[1],xs[0])
else:
    plus=sorted(x for x in xs if x >= 0)
    minus=sorted(x for x in xs if x < 0)
    proc = []
    if len(minus) > 1:
        nm = len(minus)
        minus = list(reversed(minus))
        for i in range(nm-2):
            if i == 0:
                proc.append((minus[0],minus[1]))
                b = minus[0] - minus[1]
            else:
                proc.append((b,minus[i+1]))
                b = b - minus[i+1]
        if nm == 2:
            b = minus[0]
        proc.append((minus[-1],b))
        b = minus[-1]-b
    elif minus:
        b = minus[0]
    else:
        b = None
    if b is not None:
        plus = [b] + plus
    np = len(plus)
    for i in range(np-2):
        if i == 0:
            proc.append((plus[0],plus[1]))
            a = plus[0] - plus[1]
        else:
            proc.append((a,plus[i+1]))
            a = a - plus[i+1]
    if np == 2:
        a = plus[0]
    proc.append((plus[-1],a))
    a = plus[-1]-a
    print(a)
    for x,y in proc:
        print(x,y)

Submission status: Runtime Error

Solution code:
from collections import deque
plusQ = deque([]) 
minsQ = deque([])
 
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
 
for itr, val in enumerate(A):
  if itr != len(A) - 1 and val > 0:
    plusQ.append(val)
  else:
    minsQ.append(val)
    
p1 = plusQ.popleft()
 
ans = deque([])
while len(minsQ) > 0:
  if len(plusQ) > 0:
    p, m = plusQ.popleft(), minsQ.popleft()
    ans.append([m, p])
    minsQ.append(m - p)    
  else:
    m = minsQ.popleft()
    ans.append([p1, m])
    p1 = p1 - m

print(p1)
while len(ans) > 0:
  hoge = ans.popleft()
  print(hoge[0],hoge[1])

Submission status: Runtime Error

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    print(A[-1], tmp)

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)

elif count_p == 0:
    print(A[2*N])
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

else:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Runtime Error

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    print(A[-1], tmp)

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)

elif count_z > 20 and count_p > 20 and count_p > 20:
    print(A[2*N])
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

else:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Runtime Error

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    print(A[-1], tmp)

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)

elif count_z > 20:
    print(A[2*N])
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

else:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Runtime Error

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    print(A[-1], tmp)

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)

elif count_z > 10:
    print(A[2*N])
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

else:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Runtime Error

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    print(A[-1], tmp)

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)

elif count_z > 0:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

elif count_z == 0:
    print(A[2*N])
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Runtime Error

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)

else:
    print(A[2*N])
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Runtime Error

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)
    print(A[2*N])

else:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Runtime Error

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]
    print(A[2*N])

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)

else:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Runtime Error

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    print(A[-1], tmp)
    print(A[2*N])

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)

else:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Runtime Error

Solution code:
import sys

def bigsmall(x,y):
    return (x,y) if x>y else (y,x)

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

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[-1])
    if a[1] <= 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    a.remove(len(a))
    a.append(-1) # to set answer > 0

    for i in range(1, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] <= 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    minus = []
    plus = []
    M = I()
    A = LI()
    zero = []
    ans = []
    for a in A:
        if a < 0:
            minus.append(a)
        elif a > 0:
            plus.append(a)
        else:
            zero.append(0)
    minus.sort()
    plus.sort(reverse = True)
    if not minus:
        #print(minus,plus,zero)
        if len(plus) >= 2:
            a = plus.pop()
            b = plus.pop()
            #a < b
        else:
            a = zero.pop()
            b = plus.pop()
        minus.append(a-b)
        ans.append("{} {}".format(a, b))
    
    if not plus:
        if len(minus):
            a = minus.pop()
            b = minus.pop()
            #a < b
        else:
            a = zero.pop()
            b = minus.pop()
        plus.append(a-b)
        ans.append("{} {}".format(a, b))
    while minus and plus:
        #print(minus,plus,zero)
        if len(minus) == len(plus) == 1:
            a = plus.pop()
            b = minus.pop()
            ans.append("{} {}".format(a,b))
            plus.append(a - b)
            break
        a = minus.pop()
        b = plus.pop()
        if len(minus) > len(plus):
            ans.append("{} {}".format(b,a))
            plus.append(b-a)
        else:
            ans.append("{} {}".format(a,b))
            minus.append(a - b)

    while zero:
        if minus:
            a = zero.pop()
            b = minus.pop()
            #print(a, b)
        elif plus:
            a = plus.pop()
            b = zero.pop()
            #print(a, b)
        elif len(zero) >= 2:
            a = zero.pop()
            b = zero.pop()
            #print(a,b)
        plus.append(a - b)
        ans.append("{} {}".format(a,b))

    if zero:
        print(*zero)
    elif minus:
        print(*minus)
    else:
        print(*plus)
    print(*ans,sep="\n")

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main():
    N = II()
    A = LI()

    A.sort()
    from bisect import bisect
    slice_ = bisect(A, 0)
    from collections import deque
    minus = deque(A[:slice_])
    plus = deque(A[slice_:])
    # print(minus, plus)

    if minus and plus:
        first_plus = plus.popleft()
        first_minus = minus.popleft()
    elif minus and not plus:
        first_plus = minus.popright()  # must be the max of minuses
        first_minus = minus.popleft()
    elif plus and not minus:
        first_minus = plus.popleft()  # must be the min of pluses
        first_plus = plus.popleft()

    # first_plus - (first_minus - plus - plus - ...) - minus - minus - ...
    tmp = first_minus
    ans_li = []
    while len(plus) > 0:
        p = plus.popleft()
        ans_li.append((tmp, p))
        tmp = tmp - p
    ans_li.append((first_plus, tmp))
    tmp = first_plus - tmp
    while len(minus) > 0:
        m = minus.popleft()
        ans_li.append((tmp, m))
        tmp = tmp - m
    print(tmp)
    for i in ans_li: print('{} {}'.format(*i))

main()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
plus=[a[i] for i in range(n) if a[i]>=0]
l_plus=len(plus)
minus=[a[i] for i in range(n) if a[i]<0]
l_minus=len(minus)
x_cal=[]
y_cal=[]
if n==2:
  a.sort()
  print(a[1]-a[0])
  print(a[0],a[1])
else:
  if plus!=[] and minus!=[]:
    x_cal.append(minus[0])
    for j in range(1,l_plus-1):
      x_cal.append(x_cal[-1]-plus[j])
    for k in range(1,l_plus):
      y_cal.append(plus[k])
    y_last=x_cal[-1]-y_cal[-1]
    x_cal.append(plus[0])
    for j in range(1,l_minus):
      x_cal.append(x_cal[-1]-minus[j])
    for k in range(1,l_minus):
      y_cal.append(minus[k])
    y_cal.append(y_last)
    print(x_cal[-1]-y_cal[-1])
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])
  elif minus==[]:
    a.sort()
    print(sum(a)-2*a[0])
    x_cal.append(a[0])
    for ij in range(1,n-2):
      x_cal.append(x_cal[-1]-a[ij])
    for jj in range(0,n-2):
      y_cal.append(a[jj+1])
    y_cal.append(x_cal[-1]-y_cal[-1])
    x_cal.append(a[-1])
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])
  else:
    a.sort()
    print(a[-1]*2-sum(a))
    x_cal.append(a[-1])
    for ij in range(1,n-2):
      x_cal.append(x_cal[-1]-a[ij])
    for jj in range(0,n-2):
      y_cal.append(a[jj+1])
    x_cal.append(x_cal[-1]-y_cal[-1])
    y_cal.append(a[0])
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])

Submission status: Runtime Error

Solution code:
import sys

def bigsmall(x,y):
    return x,y if x>y else y,x

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

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    if a[0] < 0 and a[-1] > 0:
        first = a.pop(-1)
        a.insert(0,first)

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[1])
    if a[2] <= 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    for i in range(2, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] <= 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    if a[-1] <= 0:
        ops.append((curr, a[-1]))
        curr = curr - a[-1]
    else:
        ops.append((a[-1], curr))
        curr = a[-1] - curr

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
N=int(input())
listA=list(map(int,input().split()))
listB=[abs(i) for i in listA]
MAX=max(listA)
MIN=min(listA)
listD=heapq.nsmallest(2,listA)
ans=sum(listB)
if MAX<=0:
  print(ans+MAX)
elif MIN<=0:
  print(ans)   
else:
  print(ans-MIN*2)
listC=sorted(listA)  
for i in range(len(listA)-2):
  if listC[i+1]>=0:
    print(MIN,listC[i+1])
    MIN=MIN-listC[i+1]
  else:
    print(MAX,listC[i+1])
    MAX=MAX-listC[i+1]
print(MAX,MIN)

Submission status: Runtime Error

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    for i in range(N-2):
        a = A.pop(0)
        b = A.pop(0)
        print(a, b)
        A.insert(0, a-b)
    a = A.pop(0)
    b = A.pop(0)
    print(b, a)

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    for i in range(N-1):
        a = A.pop(-1)
        b = A.pop(0)
        print(a, b)
        A.append(a-b)

else:
    print(sum_abs)
    cmz = count_m + count_z
    for i in range(cmz - 1):
        a = A.pop(0)
        b = A.pop(-1)
        print(b, a)
        A.append(b-a)
    for i in range(count_p - 1):
        a = A.pop(0)
        b = A.pop(0)
        print(a, b)
        A.insert(0, a-b)
    a = A.pop(0)
    b = A.pop(0)
    print(b, a)