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:
N = int(input())
A = list(map(int, input().split()))
A.sort()
A_plus = [a for a in A if a >= 0]
A_minus = [a for a in A if a < 0]

if len(A_minus) == 0:
    M = A[-1] - (A_plus[0] - sum(A_plus[1:-1]))
elif len(A_plus) == 0:
    M = A[-1] - sum(A_plus[:-1])
else:
    M = (A_plus[-1] - sum(A_minus[1:])) - (A_minus[0] - sum(A_plus[:-1]))
print(M)

opes = []
n_max = A[-1]
n_min = A[0]
for a in A:
    if a >= 0:
        print(n_min, a)
        n_min -= a
    else:
        print(n_max, a)
        m_max -= a

print(n_max, n_min)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
r=[]
for i in input().split():
  a.append(int(i))
a.sort()
t=a.pop(-1)
b=a(0)
for i in a:
  if i>=0:
    r.append([b,i])
    b=b-i
  else:
    r.append([t,i])
    t=t-i
print(t-b)
r.append([t,b])
for i in r:
  print(r[0],r[1])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
A_plus = [a for a in A if a >= 0]
A_minus = [a for a in A if a < 0]

if len(A_minus) == 0:
    M = A[-1] - (A_plus[0] - sum(A_plus[1:-1]))
elif len(A_plus) == 0:
    M = A[-1] - sum(A_plus[:-1])
else:
    M = (A_plus[-1] - sum(A_minus[1:])) - (A_minus[0] - sum(A_plus[:-1]))
print(M)

opes = []
n_max = A[-1]
n_min = A[0]
for a in A[1:-1]:
    if a >= 0:
        print(n_min, a)
        n_min -= a
    else:
        print(n_max, a)
        m_max -= a

print(n_max, n_min)

Submission status: Runtime Error

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

# A[cut-1] < 0 and A[cut] >= 0
cut = 0
while A[cut] < 0:
    cut += 1

if cut == 0:
    M = sum(A[1:]) - A[0]
    print(M)
    pair = [A[0], A[1]]
    for i in range(N-2):
        print(' '.join(map(str, pair)))
        pair = [pair[0] - pair[1], A[i+2]]
    print(' '.join(map(str, pair)))

elif cut == N-1:
    M = A[-1] - sum(A[:N-1])
    print(M)
    pair = [A[N-1], A[0]]
    for i in range(N-2):
        print(' '.join(map(str, pair)))
        pair = [pair[0] - pair[1], A[i+1]]
    print(' '.join(map(str, pair)))

else:
    M = sum(A[cut:]) - sum(A[:cut])
    print(M)
    pair = [A[cut-1], A[N-1]]
    for i in range(N - cut - 2):
        print(' '.join(map(str, pair)))
        pair = [pair[0] - pair[1], A[N-i-2]]
    print(' '.join(map(str, pair)))
    
    rem = pair[0] - pair[1]
    if cut == 1:
        pair = [A[cut], rem]
        print(' '.join(map(str, pair)))
    else:
        pair = [A[cut], A[0]]
        for i in range(cut - 2):
            print(' '.join(map(str, pair)))
            pair = [pair[0] - pair[1], A[i+1]]
        print(' '.join(map(str, pair)))
        pair = [pair[0]- pair[1], rem]
        print(' '.join(map(str, pair)))

Submission status: Runtime Error

Solution code:
N = int(input())

A_list = list(map(int, input().split()))

A_sorted = sorted(A_list)

pos_count = len([a for a in A_sorted if a >= 0])
neg_count = len([a for a in A_sorted if a < 0])

output_list = [0] * (N - 1)
count = 0
for q in range(neg_count, N - 1):
    output_list[count] = [A_sorted[0], A_sorted[q]]
    count += 1
    A_sorted[0] -= A_sorted[q]


for i in range(neg_count):
    output_list[count] = [A_sorted[N - 1], A_sorted[i]]
    count += 1
    A_sorted[N - 1] -= A_sorted[i]

print(A_sorted[N - 1])
for item in output_list:
    print(item[0], item[1])

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]
r=[]
for i in input().split():
  a.append(int(i))
a.sort()
t=a[-1]
b=a[0]
for i in a[1:-1]:
  if i>=0:
    r.append([b,i])
    b=b-i
  else:
    r.append([t,i])
    t=t-i
print(t-b)
for i in r:
  print(r[0],r[1])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
A_plus = [a for a in A if a >= 0]
A_minus = [a for a in A if a < 0]

opes = []
n_max = A[-1]
n_min = A[0]
for a in A[1:-1]:
    if a >= 0:
        opes.append((n_min, a))
        n_min -= a
    elif a < 0:
        opes.append((n_max, a))
        m_max -= a

opes.append((n_max, n_min))
m = n_max - n_min
print(m)
for ope in opes:
    print(*ope)

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-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]]
    else:
        plus = bisect_left(A, 0)
        if plus == 0:
            ans = A[0]
            for i in range(1, N-1):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[N-1], ans])
            ans = A[N-1] - ans
        elif plus < N:
            ans = A[0]
            for i in range(plus, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[N-1], ans])
            ans = A[N-1] - ans
            for i in range(1, plus):
                out.append([ans, A[i]])
                ans -= A[i]
        else:
            ans = A[N-1]
            for i in range(N-1):
                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:
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]]
    else:
        plus = bisect_left(A, 0)
        if plus == 0: plus = 1
        if plus < N:
            ans = A[0]
            for i in range(plus, N - 1):
                out.append([ans, A[i]])
                ans -= A[i]
            out.append([A[N-1], ans])
            ans = A[N-1] - ans
            for i in range(1, plus):
                out.append([ans, A[i]])
                ans -= A[i]
        else:
            ans = A[N-1]
            for i in range(N-1):
                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()))

BB = A[:]

BB_P = []
BB_M = []


for b in BB:
    if b < 0:
        BB_M.append(b)
    else:
        BB_P.append(b)

        
ope = []
if not BB_M:
    BB_P = sorted(BB_P)
    p = BB_P[-1]
    m = BB_P[0]
    for b in BB_P[1:-1]:
        ope.append((m,b))
        m = m-b
    ope.append((p,m))
    ans = p - m
elif not BB_P:
    BB_M = sorted(BB_M)
    p = BB_M[-1]
    for b in BB_M[:-1]:
        ope.append((p, b))
        p = p-b
    ans = p
else:
    p = BB_P.pop()
    m = BB_M.pop()
    for b in BB_P:
        ope.append((m ,b))
        m = m-b
    for b in BB_M:
        ope.append((p ,b))
        p = p-b
    ope((p,m))
    ans = p-m
    
print(ans)
for x,y in ope:
    print(x, y)

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
A.sort()
deq = collections.deque(A)
PRINT = []
if A[N-1] < 0:
    x = deq.pop()
    y = deq.pop()
    deq.append(x-y)
    PRINT.append([x,y])

elif A[0] > 0:
    x = deq.popleft()
    y = deq.popleft()
    PRINT.append([x,y])
    deq.appendleft(x-y)

count = 0
while(True):
    if deq[count] <= 0:
        count += 1
    else:
        break
length = len(deq)
for i in range(count-1):
    r = deq.pop()
    l = deq.popleft()
    PRINT.append([r,l])
    deq.append(r-l)
for i in range(length-count-1):
    r = deq.pop()
    l = deq.popleft()
    PRINT.append([l,r])
    deq.appendleft(l-r)
r = deq.pop()
l = deq.popleft()
PRINT.append([l,r])
print(r-l)

for i in PRINT:
    print(i[0],i[1])

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 collections import deque

    if N%2 != 0:
        if -sum(A[:N//2])+sum(A[N//2:]) > -sum(A[:N//2+1]) + sum(A[N//2+1:]):
            minus = deque(A[:N//2])
            plus = deque(A[N//2:])
            ans_li = []
            p = plus.popleft()
            m = minus.popleft()
            tmp = m - p
            ans_li.append('{} {}'.format(m, p))
            while True:
                try:
                    p = plus.popleft()
                    ans_li.append('{} {}'.format(p, tmp))
                    tmp = p - tmp
                    m = minus.popleft()
                    ans_li.append('{} {}'.format(m, tmp))
                    tmp = m - tmp
                except:
                    print(tmp)
                    for i in ans_li: print(i)
                    return

        else:
            minus = deque(A[:N//2+1])
            plus = deque(A[N//2+1:])
            m = minus.popleft()
            p = plus.popleft()
            tmp = p - m
            ans_li = ['{} {}'.format(p, m)]
            while True:
                try:
                    m = minus.popleft()
                    if len(minus) == 0:
                        ans_li.append('{} {}'.format(tmp, m))
                        tmp = tmp - m
                    else:
                        ans_li.append('{} {}'.format(m, tmp))
                        tmp = m - tmp
                    p = plus.popleft()
                    ans_li.append('{} {}'.format(p, tmp))
                    tmp = p - tmp
                except:
                    print(tmp)
                    for i in ans_li: print(i)
                    return

    else:
        minus = deque(A[:N//2])
        plus = deque(A[N//2:])
        m = plus.popleft()
        p = minus.popleft()
        tmp = m - p
        ans_li.append('{} {}'.format(m, p))
        while True:
            try:
                m = minus.popleft()
                ans_li.append('{} {}'.format(m, tmp))
                tmp = m - tmp
                p = plus.popleft()
                ans_li.append('{} {}'.format(p, tmp))
                tmp = p - tmp
            except:
                print(tmp)
                for i in ans_li: print(i)
                return


main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
p = N//2
ans =sum(A[p:])-sum(A[:p])
hoge = ans
for i in range(p,N-1):
    hoge -= A[i]*2
    if hoge > ans:
        p = i
        ans = hoge
print(ans)
B=[0]*N
l =0
hoge = A[p+1]
for i in range(p+2,N):
    print(A[l],hoge)
    hoge = A[l]-hoge
    l += 1
    print(A[i],hoge)
    hoge = A[i]-hoge
for i in range(l,p+1):
    print(hoge,A[i])
    hoge -= A[i]

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 collections import deque

    if N%2 != 0:
        if -sum(A[:N//2])+sum(A[N//2:]) > -sum(A[:N//2+1]) + sum(A[N//2+1:]):
            minus = deque(A[:N//2])
            plus = deque(A[N//2:])
            ans_li = []
            p = plus.popleft()
            m = minus.popleft()
            tmp = m - p
            ans_li.append('{} {}'.format(m, p))
            while True:
                try:
                    p = plus.popleft()
                    ans_li.append('{} {}'.format(p, tmp))
                    tmp = p - tmp
                    m = minus.popleft()
                    ans_li.append('{} {}'.format(m, tmp))
                    tmp = m - tmp
                except:
                    print(tmp)
                    for i in ans_li: print(i)
                    return

        else:
            minus = deque(A[:N//2+1])
            plus = deque(A[N//2+1:])
            m = minus.popleft()
            p = plus.popleft()
            tmp = p - m
            ans_li = ['{} {}'.format(p, m)]
            while True:
                try:
                    m = minus.popleft()
                    if len(minus) == 0:
                        ans_li.append('{} {}'.format(tmp, m))
                        tmp = tmp - m
                    else:
                        ans_li.append('{} {}'.format(m, tmp))
                        tmp = m - tmp
                    p = plus.popleft()
                    ans_li.append('{} {}'.format(p, tmp))
                    tmp = p - tmp
                except:
                    print(tmp)
                    for i in ans_li: print(i)
                    return

    else:
        m = plus.popleft()
        p = minus.popleft()
        tmp = m - p
        ans_li.append('{} {}'.format(m, p))
        while True:
            try:
                m = minus.popleft()
                ans_li.append('{} {}'.format(m, tmp))
                tmp = m - tmp
                p = plus.popleft()
                ans_li.append('{} {}'.format(p, tmp))
                tmp = p - tmp
            except:
                print(tmp)
                for i in ans_li: print(i)
                return


main()

Submission status: Runtime Error

Solution code:
import sys

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

    diff_idx = -1
    min_idx = -1
    _min = 10*6
    for i in range(len(a)-1):
        if a[i] * a[i+1] <= 0 and (a[i] != 0 or a[i+1] != 0):
            diff_idx = i
            break
        tmp_min = abs(a[i]) + abs(a[i+1])
        if tmp_min < _min:
            _min = tmp_min
            min_idx = i

    start_idx = diff_idx if diff_idx != -1 else tmp_min
    ops = []

    a2 = a[start_idx:]
    a2.extend(a[:start_idx])

    b,s = (a[0],a[1]) if a[0] > a[1] else (a[1],a[0])

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

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

        if curr < a[i]:
            b,s = s,b

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

    if a2[-1] < 0:
        ops.append((curr, a2[-1]))
        curr = curr - a2[-1]
    else:
        ops.append((a2[-1], curr))
        curr = a2[-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())
A = list(map(int,input().split()))
A.sort()
p = N//2
ans =sum(A[p:])-sum(A[:p])
hoge = ans
for i in range(p,N-1):
    hoge -= A[i]*2
    if hoge > ans:
        p = i
        ans = hoge
print(hoge)
B=[0]*N
l =0
hoge = A[p+1]
for i in range(p+2,N):
    print(A[l],hoge)
    hoge = A[l]-hoge
    l += 1
    print(A[i],hoge)
    hoge = A[i]-hoge
for i in range(l,p+1):
    print(hoge,A[i])
    hoge -= A[i]

Submission status: Runtime Error

Solution code:
def simulateBoard(posNums, negNums):
  P, N = len(posNums), len(negNums)
  out_str = ""
  
  if P == 0:
    negNums.sort(reverse=True)
    
    num1 = negNums[0]
    num2 = negNums.pop()
    
    last_pos = num1 - num2
    negNums = negNums[1:]
    print(abs(sum(negNums)) + last_pos)
    print(num1, ' ', num2) 
    last_neg = negNums.pop() 
    
    while len(negNums) != 0:
      cur_neg = negNums.pop()
      cur_diff = last_pos - cur_neg
      print(last_pos, ' ', cur_neg, ' ')
      last_pos = cur_diff
   	
    cur_diff = last_pos - last_neg
    print(last_pos, ' ', last_neg, ' ')
    
  
  elif N == 0:
    posNums.sort()
    
    num1 = posNums[0]
    num2 = posNums.pop()
    
    last_neg = num1 - num2
    posNums = posNums[1:]
    print(abs(last_neg) + sum(posNums))
    print(num1, ' ', num2)
    last_pos = posNums.pop()

    while len(posNums) != 0:
      cur_pos = posNums.pop()
      cur_diff = last_neg - cur_pos
      print(last_neg, ' ', cur_pos, ' ')
      last_neg = cur_diff
   	
    cur_diff = last_pos - last_neg
    print(last_pos, ' ', last_neg, ' ')
    
    
    
  
  #if N == 0:
    #last_neg = negNums.pop()
    
    #while len(negNums) != 0:
      #cur_neg = negNums.pop()
      #cur_diff = last_pos - cur_neg
      #print(last_pos, ' ', cur_neg, ' ')
      #last_pos = cur_diff
    
  
  elif P>=1 and N>=1:
    print(abs(sum(negNums)) + sum(posNums)) 
    
    last_pos = posNums.pop()
    last_neg = negNums.pop()
    
    while len(negNums) != 0:
      cur_neg = negNums.pop()
      cur_diff = last_pos - cur_neg
      print(last_pos, ' ', cur_neg, ' ')
      last_pos = cur_diff
    
    while len(posNums) != 0:
      cur_pos = posNums.pop()
      cur_diff = last_neg - cur_pos
      print(last_neg, ' ', cur_pos, ' ')
      last_neg = cur_diff
   	
    cur_diff = last_pos - last_neg
    print(last_pos, ' ', last_neg, ' ')

N = int(input())
nums = input().split()
nums = [int(i) for i in nums]
pos_nums = []
neg_nums = []

for num in nums:
  if num >= 0: pos_nums.append(num)
  else: neg_nums.append(num)
    
simulateBoard(pos_nums, neg_nums)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a_2 = [abs(a[i]) for i in range(n)]
a.sort()

if a[0] >= 0:
    print([sum(a_2) -2, sum(a_2)][a[0] == 0])
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(aa, a[i + 1])
            aa = aa - a[i + 1]
        elif i == n - 2:
            print(a[i + 1], aa)
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

elif a[-1] <= 0:
    print(aaaaaa)

    print([sum(a_2) -2, sum(a_2)][a[-1] == 0])
    if a[-1] == 0:
        a = [a[0]] + [a[-1]] + a[1:]
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(a[i+1], aa)
            aa = a[i + 1] - aa
        elif i == n - 2:
            print(aa, a[i + 1])
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

else:
    print(sum(a_2))
    minus = []
    plus = []
    for i in range(n):
        if a[i] < 0:
            minus.append(a[i])
        else:
            plus.append(a[i])
    minus2 = minus.pop()
    plus2 = plus.pop()
    for i in range(len(minus)):
        print(plus2 ,minus[i])
        plus2 -= minus[i]
    for i in range(len(plus)):
        print(minus2, plus[i])
        minus2 -= plus[i]
    print(plus2, minus2)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a_2 = [abs(a[i]) for i in range(n)]
a.sort()

if a[0] >= 0:
    print([sum(a_2) -2, sum(a_2)][a[0] == 0])
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(aa, a[i + 1])
            aa = aa - a[i + 1]
        elif i == n - 2:
            print(a[i + 1], aa)
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

elif a[-1] <= 0:
    if:
        
    print([sum(a_2) -2, sum(a_2)][a[-1] == 0])
    if a[-1] == 0:
        a = [a[0]] + [a[-1]] + a[1:]
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(a[i+1], aa)
            aa = a[i + 1] - aa
        elif i == n - 2:
            print(aa, a[i + 1])
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

else:
    print(sum(a_2))
    minus = []
    plus = []
    for i in range(n):
        if a[i] < 0:
            minus.append(a[i])
        else:
            plus.append(a[i])
    minus2 = minus.pop()
    plus2 = plus.pop()
    for i in range(len(minus)):
        print(plus2 ,minus[i])
        plus2 -= minus[i]
    for i in range(len(plus)):
        print(minus2, plus[i])
        minus2 -= plus[i]
    print(plus2, minus2)

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])
  if len(b_l) != 0:
    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
  if sei != 0:
    ans_ll.append(a_l[-1])
  b_l = b_l[:sa]
  if sei != 0:
    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])
  b_l.reverse()
  aa = 0
  for i in range(len(b_l)-1):
    ans_l.append([b_l[i], b_l[i+1]])
    aa = b_l[i] - b_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]
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())
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(minus)==0:
    print(a[-1]-a[0]+sum(a[1:n-1]))
    for i in range(n):
        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(plus)==0:
    print(a[-1]-a[0]-sum(a[1:n-1]))
    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:-1]))
    for i in range(len(plus)-1):
        print(a[0],plus[i])
        a[0]-=plus[i]
    for i in range(0,len(minus)):
        print(a[-1],minus[i])
        a[-1]-=minus[i]

Submission status: Runtime Error

Solution code:
import sys

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

    diff_idx = -1
    min_idx = -1
    _min = 10*6
    for i in range(len(a)-1):
        if a[i] * a[i+1] <= 0 and (a[i] != 0 or a[i+1] != 0):
            diff_idx = i
            break
        tmp_min = abs(a[i]) + abs(a[i+1])
        if tmp_min < _min:
            _min = tmp_min
            min_idx = i

    start_idx = diff_idx if diff_idx != -1 else tmp_min
    ops = []

    a2 = a[start_idx:]
    a2.extend(a[:start_idx])

    b,s = (a[0],a[1]) if a[0] > a[1] else (a[1],a[0])

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

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

        if curr < a[i]:
            b,s = s,b

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

    if a2[-1] < 0:
        curr = curr - a2[-1]
        ops.append((curr, a2[-1]))
    else:
        curr = a2[-1] - curr
        ops.append((a2[-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())
a=list(map(int,input().split()))
a.sort()
ans=[]
R=a.pop()
L=a.pop(0)
s=L
if not a:
    while a[-1]>0:
        r=a.pop()
        ans.append([s,r])
        s=s-r
        if not a:break
ans.append([R,s])
s=R-s
while a:
    L=a.pop()
    ans.append([s,L])
    s=s-L
print(s)
for x in ans:
    print(*x)

Submission status: Runtime Error

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

m,p = [],[]
m_total = 0
p_total = 0
p_max,p_min = 0,10**10
c,p_min_idx,p_max_idx = 0,0,0
for a in A:
    if a < 0:
        m_total += a
        m.append(a)
    else:
        if p_min > a:
            p_min = a
            p_min_idx = c
        if p_max < a:
            p_max = a
            p_max_idx = c
        p_total += a
        p.append(a)
        c += 1

if len(m) > 0:
    print(p_total-m_total)
    for i,v in enumerate(p):
        if i != p_max_idx:
            print(m[0], v)
            m[0] -= v
    for i in range(len(m)):
        print(p[p_max_idx],m[i])
        p[p_max_idx] -= m[i]
else:
    print(p_total-2*p_min)
    for i in range(len(p)-1):
        if i != p_min_idx:
            print(p[p_min_idx],p[i])
            p[p_min_idx] -= p[i]
    print(p[-1],p[p_min_idx])

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 ji in range(1,l_minus-1):
    x_cal.append(x_cal[-1]-minus[ji])
  for ki in range(1,l_minus):
    y_cal.append(minus[ki])
  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 jk in range(0,n-2):
    y_cal.append(a[jk+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:
#!/usr/bin/env python3
import bisect
n = int(input())
a = sorted(map(int, input().split()))
if n == 2:
    print(a[1] - a[0])
    print(a[1], a[0])
    exit()

p = bisect.bisect_left(a, 0)
if p < 2 and n == 3:
    print(sum(a) - 2 * a[0])
    print(a[0], a[1])
    b = a[0] - a[1]
    print(a[2], b)
    exit()

p = max(2, p)
print(sum(a[p:]) - sum(a[:p]))
b = a[0]
for i in range(p + 1, n):
    print(b, a[i])
    b -= a[i]
c = a[p]
for i in range(1, p):
    print(c, a[i])
    c -= a[i]
print(c, b)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=[]
R=a.pop()
L=a.pop(0)
s=L
while a[-1]>0:
    r=a.pop()
    ans.append([s,r])
    s=s-r
    if not a:break
ans.append([R,s])
s=R-s
while a:
    L=a.pop()
    ans.append([s,L])
    s=s-L
print(s)
for x in ans:
    print(*x)

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 ff == 0:
      if f == 0:
        if A[i] >= 0:
          f = 1
        print(tmp,A[i])
        tmp -= A[i]
      else:
        print(A[i],tmp)
        ff = 1
        tmp = A[i] - tmp
    else:
      print(tmp,A[i])
      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]