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 = 0
P = []
m = 0
M = []
for a in A:
    if a>=0:
        p += 1
        P.append(a)
    else:
        m += 1
        M.append(a)
if m!=0 and p!=0:
    res = 0
    for a in A:
        res += abs(a)
    print(res)
    while p!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        p -= 1
        heappush(M,mm-pp)
    while m != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        m -= 1
        heappush(P,pp-mm)
elif m == 0:
    print(sum(P)-min(P)*2)
    mm = heappop(P)
    pp = heappop(P)
    print(mm,pp)
    p -= 2
    m += 1
    heappush(M,mm-pp)
    while p!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        p -= 1
        heappush(M,mm-pp)
    while m != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        m -= 1
        heappush(P,pp-mm)
else:
    res = 0
    MMM = -100000000
    for m in M:
        MMM = max(MMM,m)
        res += abs(m)
    print(res+MMM*2)
    M.sort()
    mm = M[-1]
    pp = M[-2]
    print(mm,pp)
    m -= 2
    p += 1
    M = M[:-2]
    heappush(P,mm-pp)
    while p!= 1:
        mm = heappop(M)
        pp = heappop(P)
        print(mm,pp)
        p -= 1
        heappush(M,mm-pp)
    while m != 0:
        mm = heappop(M)
        pp = heappop(P)
        print(pp,mm)
        m -= 1
        if m==0:
            break
        heappush(P,pp-mm)

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
a=sorted(A)
d=a[0]
for i in range(n-2):
  d-=a[n-1-i]
D=a[1]-d
print(D)
e=a[0]
for j in range(n-2):
  print(e a[n-1-i])
  e-=a[n-1-i]
print(a[1] e)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [0]*N
A = list(map(int, input().split()))
ans = int(0)
x = int(0)
y = int(0)

As = sorted(A)

if As[0]>0:
    for i in range(N):
        ans += As[i]

    ans -=As[0]*2
    print(ans)

    x = As[0]
    for i in range(N-2):
        y = As[i+1]
        print(x,y)
        x = x - y

    y = x
    x = As[N-1]
    print(x,y)

elif As[N-1]<0:
    for i in range(N):
        As[i] *= -1
    for i in range(N):
        ans += As[i]

    ans -=As[N-1]*2
    print(ans)

    As = sorted(As)

    x = As[0]
    for i in range(N-2):
        y = As[i+1]
        print(-x,-y)
        x = x - y

    y = x
    x = As[N-1]
    print(-y,-x)

##################################
else:
    for i in range(N):
        ans += abs(As[i])

    print(ans)

    x = As[0]
    i = int(0)
    while(i<N-2 and A[N-i-1]>0):
        y = As[N-i-1]
        print(x,y)
        x = x - y
        i +=1
#    print("ok")
    y = x
    while(i<N-2):
        x = As[i]
        print(x,y)
        y = x - y
        i +=1


    x = As[N-i]
    print(x,y)

Submission status: Runtime Error

Solution code:
N = int(input())
List = list(map(int,input().split()))
List = sorted(List)
found = False
i = N//2 
while found == False and i < N:
    if List[i]>=0:
        found = True
    else:
        i = i+1


A = List[:i]
B = List[i:]
print(A,B)
a = 0
y = 0
print(sum(B)-sum(A))
for k in range(N - i-1):
    x = B[k]
    if a != 0:
        print(x,y)
    y = x-y
    a += 1
    x = A[k]
    print(x,y)
    y = x-y
x = B[N-i-1]    
print(x,y)
x = x-y
for k in range(N-i-1,i):
    y = A[k]
    print(x,y)
    x = x - y

Submission status: Runtime Error

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

n_pos = len([a for a in A if a > 0])
if n_pos == 0:
    pos = [A[-1]]
    neg = A[:-1]
elif n_pos == N:
    pos = A[1:]
    neg = A[:1]
else:
    pos = [a for a in A if a > 0]
    neg = [a for a in A if a <= 0]
if len(pos) >= 2:
    tmp = neg[0]
    for i in range(len(pos)-1):
        operations.append((tmp, pos[i]))
        tmp = tmp - pos[i]
    operations.append((pos[-1], tmp))
    tmp = pos[-1] - tmp
    for i in range(1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
else:
    tmp = pos[0]
    for i in range(1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
print(tmp)
for o in operations:
    print("{} {}".format(o[0], o[1]))

Submission status: Runtime Error

Solution code:
import numpy as np

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

A = np.array(A)

A = np.sort(A)

B = np.copy(A)

if A[0] <= 0 and A[n-1] >= 0:
    for i in range(n-1):
        A[i] = min(A[i], -A[i])

    print(np.sum(A[:-1]) * -1 + A[n-1])

    k = n - 2
    a = B[0]
    while B[k] >= 0:
        print(a, B[k])
        a = a - B[k]
        k -= 1
        if k == 0:
            break

    b = B[n-1]

    while k != 0:
        print(b, B[k])
        b = b - B[k]
        k -= 1

    print(b, a)

elif A[n-1] < 0:
    A[n-1] = A[n-1] - A[n-2]
    A[n-2] = 0
    for i in range(n-1):
        A[i] = min(A[i], -A[i])

    print(np.sum(A[:-1]) * -1 + A[n-1])

    a = B[n-1]
    for i in range(n-1):
        print(a, B[i])
        a = a - B[i]

else:
    A[0] = A[0] - A[1]
    A[1] = 0
    for i in range(n-1):
        A[i] = min(A[i], -A[i])

    print(np.sum(A[:-1]) * -1 + A[n-1])

    k = n - 2
    a = B[0]
    while B[k] >= 0:
        print(a, B[k])
        a = a - B[k]
        k -= 1
        if k == 0:
            break

    b = B[n-1]

    while k != 0:
        print(b, B[k])
        b = b - B[k]
        k -= 1

    print(b, a)

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 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-1):
    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, math, collections, heapq, itertools
from bisect import bisect_left
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = 0
    out = []
    if N == 2:
        ans = A[1] - A[0]
        out = [A[1], A[0]]
    elif A[N-1] <= 0:
        ans = A[N-1] - A[N-2]
        out.append([A[N-1], A[N-2]])
        for i in range(N-2):
            out.append([ans, A[i]])
            ans -= A[i]
    elif A[0] >= 0:
        ans = A[0] - A[1]
        out.append([A[0], A[1]])
        for i in range(2, N-1):
            out.append([ans, A[i]])
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
    else:
        start = bisect_left(A, 0)
        start -= 1
        ans = A[start] - A[start + 1]
        out.append([A[start], A[start + 1]])
        if start + 2 < N - 1:
            for i in range(start + 2, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
        out.append([A[N-1], ans])
        ans = A[N-1] - ans
        if start > 0:
            for i in range(start):
                out.append([ans, A[i]])
                ans -= A[i]

    print(ans)
    for k in out:
        print(" ".join(map(str, k)))

    return 0
  
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
ab=[abs(i) for i in a]
pos=[i for i in a if i > 0]
nega=[i for i in a if i < 0]
zero=True if 0 in a else False
if (len(pos)==0 or len(nega)==0):
  print(sum(ab)-2*min(ab))
  spe=a.pop(ab.index(min(ab)))
  if a[0]>0:
    print(str(spe)+" "+str(a[i]))
  else:
    print(str(a[i])+" "+str(spe))
  for i in range(n-1):
    print(str(spe)+" "+str(a[i]))
    spe-=a.pop(i)
else:
  print(sum(ab))
  tmp=nega.pop(0)
  lenpos=len(pos)
  for i in range(lenpos-1):
    print(str(tmp)+" "+str(pos[i]))
    tmp-=pos[i]
  lennega=len(nega)
  for i in range(lennega):
    print(str(tmp)+" "+str(nega[i]))
    tmp-=nega[i]
  for i in range(len(a)-lennega-lenpos):
    print(str(tmp)+" "+str(0))

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A = sorted(A)
    A_M = sorted(A)
    A_M[0] *= -1
    start = 0
    for i in range(1, N-1):
        if A_M[i] < 0:
            A_M[i] *= -1
            start = i
    M = sum(A_M)
    print(M)
    if start == 0:
        for i in range(1, N):
            if i < N-1:
                if i == 1:
                    x = A[0]
                    y = A[1]
                else:
                    x = tmp
                    y = A[i]
            else:
                x = A[-1]
                y = tmp
            tmp = x - y
            print('{} {}'.format(x, y))
    elif start == N - 2:
        for i in range(1, N):
            if i == 1:
                x = A[-1]
                y = A[i-1]
            else:
                x = tmp
                y = A[i-1]
            tmp = x - y
            print('{} {}'.format(x, y))
    else:
        for i in range(1, N):
            if i == 1:
                x = A[start]
                y = A[start+1]
            elif 1 < i < N - 1 - start:
                x = tmp
                y = A[start+i+1]
            elif i == N - 1 - start:
                x = A[-1]
                y = tmp
            else:
                x = tmp
                y = A[i-(N-1-start)-1]
            tmp = x - y
            print('{} {}'.format(x, y))
    return


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

a.sort()
xy = [[] for i in range(N-1)]
cnt = 0
while len(a) > 2:
    if a[len(a)-1] > 0 and a[len(a)-2] > 0:
        n = a[0] - a[len(a)-1]
        xy[cnt] = [a[0], a[len(a)-1]]
        a = [n]
        a.extend(a[1:len(a) - 1])
    else:
        n = a[len(a)-1] - a[0]
        xy[cnt] = [a[len(a)-1], a[0]]
        a = a[1:len(a) - 1]
        a.extend([n])
    cnt += 1

m = a[1] - a[0]
xy[cnt] = [a[1], a[0]]
print(m)
for _xy in xy:
    print(str(_xy[0]) + " " + str(_xy[1]))

Submission status: Runtime Error

Solution code:
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N = n()
NN = N // 2

As = sorted(nl())

import bisect
n_i = 0
p_i = max(NN, bisect.bisect_left(As, 0))

M = sum(As[p_i:]) - sum(As[:p_i])
print(M)

p_num = N - p_i
n_num = N - p_num
next_positive = p_num <= n_num
if next_positive:
    print('{} {}'.format(As[p_i], As[n_i]))
    current = As[p_i] - As[n_i]
else:
    print('{} {}'.format(As[n_i], As[p_i]))
    current = As[n_i] - As[p_i]
next_positive = not next_positive

for i in range(N-1):
    if next_positive:
        p_i += 1
        print('{} {}'.format(As[p_i], current))
        current = As[p_i] - current
    else:
        n_i += 1
        print('{} {}'.format(current, As[n_i]))
        current = current - As[n_i]
    if p_i < N-1:
        next_positive = not next_positive

Submission status: Runtime Error

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    a.sort()

    if a[-1] <= 0:
        s = max(a)
        a.remove(s)

        X, Y = [], []
        for _ in range(n - 2):
            ai = a.pop()
            X.append(s), Y.append(ai)
            s -= ai

        ai = a.pop()
        X.append(s), Y.append(ai)

        print(s - ai)
        for x, y in zip(X, Y):
            print(x, y)
    elif a[0] >= 0:
        s = min(a)
        a.remove(s)

        X, Y = [], []
        for _ in range(n - 2):
            ai = a.pop()
            X.append(s), Y.append(ai)
            s -= ai

        ai = a.pop()
        X.append(ai), Y.append(s)

        print(ai - s)
        for x, y in zip(X, Y):
            print(x, y)
    else:
        print(1/0)


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10000)
mod = 1000000007


def mul(a, b):
    return ((a % mod) * (b % mod)) % mod


def power(x, y):
    if y == 0:
        return 1
    elif y == 1:
        return x % mod
    elif y % 2 == 0:
        return power(x, y / 2) ** 2 % mod
    else:
        return power(x, y / 2) ** 2 * x % mod


def div(a, b):
    return mul(a, power(b, mod - 2))


# def gcd(a_gcd, b_gcd):
#     while b_gcd != 0:
#         a_gcd, b_gcd = b_gcd, a_gcd % b_gcd
#     return a_gcd
#
#
# def lcm(a_lcm, b_lcm):
#     return a_lcm * b // gcd(a_lcm, b_lcm)
#
#
# def max_sum(N_max_sum, a_max_sum):
#     dp = [0] * (N_max_sum + 1)
#     for i in range(N_max_sum):
#         dp[i + 1] = max(dp[i], dp[i] + a_max_sum[i])
#     return dp[N]
#
#
# def knapsack(N_knapsack, W, weight, value):
#     inf = float("inf")
#     dp = [[-inf for _ in range(W+1)] for j in range(N_knapsack + 1)]
#     for i in range(W+1):
#         dp[0][i] = 0
#
#     for i in range(N_knapsack):
#         for w in range(W+1):
#             if weight[i] <= w:
#                 dp[i+1][w] = max(dp[i][w-weight[i]]+value[i], dp[i][w])
#             else:
#                 dp[i+1][w] = dp[i][w]
#     return dp[N_knapsack][W]
#
#
# class UnionFind:
#     def __init__(self, n):
#         self.par = [i for i in range(n+1)]
#         self.rank = [0] * (n+1)
#
#     # 検索
#     def find(self, x):
#         if self.par[x] == x:
#             return x
#         else:
#             self.par[x] = self.find(self.par[x])
#             return self.par[x]
#
#     # 併合
#     def union(self, x, y):
#         x = self.find(x)
#         y = self.find(y)
#         if self.rank[x] < self.rank[y]:
#             self.par[x] = y
#         else:
#             self.par[y] = x
#             if self.rank[x] == self.rank[y]:
#                 self.rank[x] += 1
#
#     # 同じ集合に属するか判定
#     def same_check(self, x, y):
#         return self.find(x) == self.find(y)
ans = 0
N = int(input())
A = list(map(int, input().split()))
A.sort()


def insert_atsu(v):
    global A
    no = True
    for ii in range(len(A)):
        if A[ii] >= v:
            A.insert(ii, v)
            no = False
            break
    if no:
        A.insert(len(A), v)


# AA = [(0, 0) for _ in range(N)]
#
# for i in range(N):
#     AA[i] = (A[i], i)
#
# AA.sort()
# ans = 0
#
# if AA[0][0] >= 0:
#     ans =



if A[0] >= 0:
    ans = sum(A) - A[0] * 2
    print(ans)

    guuki = (N % 2)
    for i in range(N - 1):
        # print(A)
        if (i % 2) == guuki:
            va = A[1] - A[0]
            print(A[1], A[0])
            A.pop(0)
            A.pop(0)
            A.insert(0, va)
        else:
            va = A[0] - A[1]
            print(A[0], A[1])
            A.pop(0)
            A.pop(0)
            A.insert(0, va)

elif A[len(A)-1] <= 0:
    ans = - sum(A) + A[len(A)-1] * 2
    print(ans)

    guuki = (N % 2)
    for i in range(N - 1):
        # print(A)
        if (i % 2) == guuki:
            va = A[len(A) - 2] - A[len(A) - 1]
            print(A[len(A) - 2], A[len(A) - 1])
            A.pop(len(A) - 1)
            A.pop(len(A) - 1)
            A.insert(len(A), va)

        else:
            va = A[len(A) - 1] - A[len(A) - 2]
            print(A[len(A) - 1], A[len(A) - 2])
            A.pop(len(A) - 1)
            A.pop(len(A) - 1)
            A.insert(len(A), va)

else:
    ans = 0
    sei = 0
    for i in range(N):
        if A[i] < 0:
            sei = i
        ans += abs(A[i])
    sei += 1
    A1 = A[0:sei]
    A2 = A[sei:]
    A1.insert(len(A1), A[N-1])
    A1.pop(0)
    A2.pop(len(A1))
    A2.insert(0, A[0])

    guuki = (len(A1) % 2)
    for i in range(len(A1) - 1):
        # print(A)
        if (i % 2) != guuki:
            va = A1[len(A1) - 2] - A1[len(A1) - 1]
            print(A1[len(A1) - 2], A1[len(A1) - 1])
            A1.pop(len(A1) - 1)
            A1.pop(len(A1) - 1)
            A1.insert(len(A1), va)

        else:
            va = A1[len(A1) - 1] - A1[len(A1) - 2]
            print(A1[len(A1) - 1], A1[len(A1) - 2])
            A1.pop(len(A1) - 1)
            A1.pop(len(A1) - 1)
            A1.insert(len(A1), va)

    v = A1[0]
    no = True
    for ii in range(len(A2)):
        if A2[ii] >= v:
            A2.insert(ii, v)
            no = False
            break
    if no:
        A2.insert(len(A2), v)

    ans = sum(A) - A2[0] * 2
    print(ans)

    guuki = (len(A2) % 2)
    for i in range(len(A2) - 1):
        # print(A)
        if (i % 2) == guuki:
            va = A2[1] - A2[0]
            print(A2[1], A2[0])
            A2.pop(0)
            A2.pop(0)
            A2.insert(0, va)
        else:
            va = A2[0] - A2[1]
            print(A2[0], A2[1])
            A2.pop(0)
            A2.pop(0)
            A2.insert(0, va)

Submission status: Runtime Error

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
    return list(map(int,input().split()))
def m():
    return map(int,input().split())
def onem():
    return int(input())
def s(x):
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x):
    return " ".join(map(str,x))

n = onem()

a = l()
a.sort()
a = cl.deque(a)
aa = []
co = 0
while True:
    if len(a) == 3:
        if a[1] >= 0:
            z = a.pop()
            zz = a.popleft()
            a.appendleft(zz - z)
            aa.append([zz,z])
        else:
            z = a.pop()
            zz = a.popleft()
            a.append(z - zz)
            aa.append([z,zz])
        continue
    elif len(a) == 2:
        aa.append([max(a),min(a)])
        a.append(max(a),min(a))
        break
    elif len(a) == 1:
        break
    if co % 2 == 0:
        z = a.pop()
        zz = a.popleft()
        a.append(z - zz)
        aa.append([z,zz])
        co += 1
    else:
        z = a.pop()
        zz = a.popleft()
        a.appendleft(zz - z)
        aa.append([zz,z])
        co += 1

print(a[0])
for i in range(n-1):
    print(jo(aa[i]))

Submission status: Runtime Error

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

length = len(a_l)
k = 0
for i in range(length):
  if a_l[i] < 0:
    k = i
  else:
    break

b_l = a_l[:k+1]
a_l = a_l[k+1:]
fu = len(b_l)
sei = len(a_l)
ans_l = list()
ans_ll = list()
if sei > fu:
  sa = sei-fu-1
  ans_ll.append(a_l[-1])
  for i in range(fu):
    ans_l.append([b_l[i], a_l[-i-2]])
    ans_ll.append(b_l[i]-a_l[-i-2])
  aa_l = a_l[:sa]
  aa = 0
  for i in range(len(aa_l)-1):
    ans_l.append([aa_l[i], aa_l[i+1]])
    aa = aa_l[i] - aa_l[i+1]
  ans_ll.append(aa)
  ans_ll.sort()
  lent = len(ans_ll)
  for i in range(lent-1):
    ans_l.append([ans_ll[-1], ans_ll[i]])
    ans_ll[-1] = ans_ll[-1]-ans_ll[i]
elif sei < fu:
  sa = fu-sei+1
  ans_ll += b_l[:sa]
  ans_ll.append(a_l[-1])
  b_l = b_l[sa:]
  for i in range(len(b_l)):
    ans_l.append([b_l[i], a_l[-i-2]])
    ans_ll.append(b_l[i]-a_l[-i-2])
  ans_ll.sort()
  lent = len(ans_ll)  
  for i in range(lent-1):
    ans_l.append([ans_ll[-1], ans_ll[i]])
    ans_ll[-1] = ans_ll[-1]-ans_ll[i]
else:
  for i in range(sei):
    if i == 0:
      ans_l.append([a_l[-i-1], b_l[i]])
      ans_ll.append(a_l[-i-1]-b_l[i])
    else:
      ans_l.append([b_l[i], a_l[-i-1]])
      ans_ll.append(b_l[i]-a_l[-i-1])
  ans_ll.sort()
  lent = len(ans_ll)  
  for i in range(lent-1):
    ans_l.append([ans_ll[-1], ans_ll[i]])
    ans_ll[-1] = ans_ll[-1]-ans_ll[i]
    
ans_length = len(ans_l)
print(ans_ll[-1])
for i in range(ans_length):
  a = ans_l[i][0]
  b = ans_l[i][1]
  print(a, b)

Submission status: Runtime Error

Solution code:
N = int(input())
List = list(map(int,input().split()))
List = sorted(List)
found = False
i = N//2 +1
while found == False and i < N:
    if List[i]>=0:
        found = True
    else:
        i += 1

A = List[:i]
B = List[i:]
print(A,B)
a = 0
y = 0
print(sum(B)-sum(A))
for k in range(N - i-1):
    x = B[k]
    if a != 0:
        print(x,y)
    y = x-y
    a += 1
    x = A[k]
    print(x,y)
    y = x-y
x = B[N-i-1]    
print(x,y)
x = x-y
for k in range(N-i-1,i):
    y = A[k]
    print(x,y)
    x = x - y

Submission status: Runtime Error

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

a.sort()
xy = [[] for i in range(N-1)]
cnt = 0
while len(a) > 2:
    if a[len(a)-1] > 0 and a[len(a)-2] > 0:
        n = a[0] - a[len(a)-1]
        xy[cnt] = [a[0], a[len(a)-1]]
        a = [n].extend(a[1:len(a) - 1])
    else:
        n = a[len(a)-1] - a[0]
        xy[cnt] = [a[len(a)-1], a[0]]
        a = a[1:len(a) - 1].extend([n])
    cnt += 1

m = a[1] - a[0]
xy[cnt] = [a[1], a[0]]
print(m)
for _xy in xy:
    print(str(_xy[0]) + " " + str(_xy[1]))

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

s=[]
ans=[]

i=1
x=a[0]
if abs(min(a))>abs(max(a)):
    a.sort(reverse=True)
    x = a[0]
    #print(a)
    while i != n - 2:

        if  abs(x - a[i])>abs(a[i] - x):
            ans.append([x, a[i]])
            x = x - a[i]
        else:
            ans.append([a[i], x])
            x = a[i] - x
        i += 1
    if x > a[i]:

        ans.append([x, a[i]])
        x = x - a[i]
    else:
        ans.append([a[i], x])
        x = a[i] - x
    i += 1
    ans.append([x, a[i]])
    print(x - a[i])
else:
    a.sort()
    x = a[0]
    while i != n - 2:
        if abs(x - a[i])>abs(a[i] - x):
            ans.append([x, a[i]])
            x = x - a[i]
        else:
            ans.append([a[i], x])
            x = a[i] - x
        i += 1
    if x<a[i]:

        ans.append([x,a[i]])
        x=x-a[i]
    else:
        ans.append([a[i],x])
        x =a[i]-x
    i+=1

Submission status: Runtime Error

Solution code:
from collections import deque
import bisect

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

A = deque(A)
ans = []
c = 0
if min(A) > 0:
    while len(A) > 2:
        t, s = A.pop(),A.popleft()
        ans.append([s,t])
        bisect.insort_left(A,s-t)
    print(A[1]-A[0])
    for e in ans:
        print(*e)
    print(A[1],A[0])
elif max(A) < 0:
    while len(A) > 2:
        s, t = A.pop(),A.popleft()
        ans.append([s,t])
        bisect.insort_left(A,s-t)
    print(A[1]-A[0])
    for e in ans:
        print(*e)
    print(A[1],A[0])
else:
    while len(A) > 2:
        if c == 0:
            t, s = A.pop(),A.popleft()
            ans.append([s,t])
            bisect.insort_left(A,s-t)
            c = 1
        else:
            s, t = A.pop(),A.popleft()
            ans.append([s,t])
            bisect.insort_left(A,s-t)
            c = 0
    print(A[1]-A[0])
    for e in ans:
        print(*e)
    print(A[1],A[0])

Submission status: Runtime Error

Solution code:
import bisect
N=int(input())
AL=[int(x) for x in input().split()]
AL.sort()
OL=[]

for i in range(int(N/2)):
    if AL[i]<0 and AL[-i-1]<0:
        OL.append((tmp,AL[i]))
        tmp=tmp-AL[i]
        OL.append((tmp,AL[-i-1]))
        tmp=tmp-AL[-i-1]
    else:
        try:
            OL.append((AL[i],tmp))
            tmp=AL[i]-tmp
            OL.append((AL[-i-1],tmp))
            tmp=AL[-i-1]-tmp
        except:
            OL.append((AL[-i-1],AL[i]))
            tmp=AL[-i-1]-AL[i]
if tmp<0:
    OL=((-x[0],-x[1]) for x in OL)
    
if N%2==1:
    OL.append((AL[int(N/2)],tmp))
    tmp+=AL[int(N/2)]
    

print(tmp)
for o in OL:
    print(o[0],o[1])

Submission status: Runtime Error

Solution code:
n=int(input())
A=sorted(list(map(int,input().split())))
print(sum(A[n//2:])-sum(A[:n//2]))

i=0
while len(A)>3:  
  a=min(A)
  b=max(A)
  if i%2==0:
    print("{} {}".format(b,a))
    A.append(b-a)
  else:
    print("{} {}".format(a,b))
    A.append(a-b)
  i+=1
  A.remove(a)
  A.remove(b)
  
  
A.sort()
print("{} {}".format(A[0],A[1]))
print("{} {}".format(A[2],A[0]-A[1]))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
minus=[i for i in a if i<0]
plus=[i for i in a if i>=0]
if len(plus)==0:
    print(a[-1]-a[0]+sum(a[1:n-1]))
    for i in range(n-1):
        if i==n-2:
            print(a[-1],a[n-2])
        else:
            print(a[i],a[i+1])
            a[i+1]=a[i]-a[i+1]
elif len(minus)==0:
    print(a[-1]-a[0]-sum(a[1:-2]))
    print(a[-1],a[0])
    a[-1]=a[-1]-a[0]
    for i in range(1,n-1):
        print(a[-1],a[i])
        a[-1]-=a[i]
else:
    print(a[-1]-a[0]-sum(minus[1:-1])+sum(plus[0:-2]))
    for i in range(len(plus)-1):
        print(a[0],plus[i])
        a[0]-=plus[i]
    for j in range(1,len(minus)):
        print(a[-1],minus[i])
        a[-1]-=minus[i]

Submission status: Runtime Error

Solution code:
import bisect
from operator import itemgetter
import math
import collections
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
A = IL()
A.sort()

l,h = 0,0
if A[0] < 0:
  l = 1
if A[-1] >= 0:
  h = 1

f = 0
ff = 0
if l == 1 and h == 1:
  ans = 0
  for i in range(N):
    if A[i] < 0:
      ans -= A[i]
    else:
      ans += A[i]
  print(ans)

  tmp = A[0]
  for i in range(1,N-1):
    if f == 0:
      if A[i] >= 0:
        f = 1
      print(tmp,A[i])
      tmp -= A[i]
    else:
      print(tmp,A[i])
  else:
    print(A[-1],tmp)
elif l == 0 and h == 1:
  print(sum(A[2:])-(A[0]-A[1]))
  tmp = A[0]
  for i in range(1,N-1):
    print(tmp,A[i])
    tmp -= A[i]
  else:
    print(A[i],tmp)
elif l == 1 and h == 0:
  print(abs(sum(A[2:]))+(A[0]-A[1]))
  tmp = A[0]
  for i in range(1,N):
    print(tmp,A[i])
    tmp -= A[i]