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:
import sys, math, collections, heapq, itertools
  
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:
        for i in range(1, N-1):
            if A[i] >= 0:
                start = i-1
                break
        else: start = N - 2
        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 = [int(i) for i in input().split()]
A.sort()
a = A[0]
b = A[-1]
if N == 2:
    print(A[1]-A[0])
    print(A[1],A[0])
    sys.exit()
ans = []
for x in A[1:-1]:
    if x <= 0:
        ans.append([b, x])
        b = b - x
    else:
        ans.append([a, x])
        a = a - x
ans.append([b,a])
M = b-a
print(M)
for x in ans:
    print(x[0], x[1])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)
INF = float("inf")

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)

current = None
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:
# 負数を最後まで太らせて、最後に正の数の一番小さいやつに引く（符号がひっくりかえるので加えることになる）
N = int(input())
A = map(int, input().split())
plus, minus = [], []
for a in A:
    if a < 0:
        minus.append(a)
    else:
        plus.append(a)

plus = list(sorted(plus))
minus = list(reversed(sorted(minus)))

ans = 0
ans_list = []

if len(minus) == 0:
    # 太らせる要素がないので正の数から作る
    # 初回 最も小さい数 - 最も大きい数 して最大の負数をつくる
    # あとは残ってるやつから「引く」を加え続けて最後の一回で自らを「引く」
    a, b = plus[0], plus[-1]
    plus = plus[1:-1]
    if len(plus) > 0:
        ans_list.append([a, b])
        ans = a - b
        for p in plus[1:]:
            ans_list.append([ans, p])
            ans -= p
        ans_list.append([plus[0], ans])
        ans = plus[0] - ans
    else:
        ans_list.append([b, a])
        ans = b - a
else:
    # 残りの負数から最大の負数を作る。まず（正方向に）最大 - （正方向に）最小することで最大の正の数がデキるので、
    # あとは（正方向に）最小の一つ前まで 引くを加えていき、最後に逆に自分自身を加える
    if len(minus) == 1:
        ans = minus[0]
    elif len(minus) == 2:
        a, b = minus[0], minus[-1]
        ans_list.append([b, a])
        ans = b - a
    else:
        a, b = minus[0], minus[-1]
        ans = b
        ans_list.append([a, b])
        for m in minus[1:-2]:
            ans_list.append([ans, m])
            ans += m * (-1)
        ans_list.append([minus[-1], ans])
        ans = minus[-1] - ans

    # 正のやつを食う
    for p in plus[1:]:
        ans_list.append([ans, p])
        ans = ans - p
    # 仕上げ
    ans_list.append([plus[0], ans])
    ans = -1 * ans + plus[0]

    # もう1パターンあったわ
    ans_list2, ans2 = [], 0
    ans2 = plus[-1]
    for m in minus:
        ans_list2.append([ans2, m])
        ans2 = ans2 - m
    plus[-1] = ans2

    a, b = plus[0], plus[-1]
    tmp_ans = 0
    plus = plus[1:-1]
    if len(plus) > 0:
        ans_list2.append([a, b])
        tmp_ans = a - b
        for p in plus[1:]:
            ans_list2.append([tmp_ans, p])
            tmp_ans -= p
        ans_list2.append([plus[0], tmp_ans])
        tmp_ans = plus[0] - tmp_ans
    else:
        ans_list2.append([b, a])
        tmp_ans = b - a

    if ans < ans2:
        ans = ans2
        ans_list = ans_list2

print(ans)
for a in ans_list:
    print(a[0], a[1])

Submission status: Runtime Error

Solution code:
import sys, math, collections, heapq, itertools
  
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:
        for i in range(1, N-1):
            if A[i] >= 0:
                start = i-1
                break
        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:
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])
    elif len(a) == 2:
        aa.append([max(a),min(a)])
        bs.insort(a,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:
a = "3"
print "1"
print "0"

Submission status: Runtime Error

Solution code:
a = "3"
print "4"

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])
        A.insert(bisect.bisect_left(A,s-t),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])
        A.insert(bisect.bisect_left(A,s-t),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])
            A.insert(bisect.bisect_left(A,s-t),s-t)
            c = 1
        else:
            s, t = A.pop(),A.popleft()
            ans.append([s,t])
            A.insert(bisect.bisect_left(A,s-t),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 sys, math, collections, heapq, itertools
  
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-2] <= 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[1] >= 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:
        for i in range(1, N-1):
            if A[i] >= 0:
                start = i-1
                break
        ans = A[start] - A[start + 1]
        out.append([A[start], A[N-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
        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_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+= a_l[:sa]
  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])
  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())
A = list(map(int,input().split()))
A.sort()
K = N-1
ans = []
while(K>1):
    if A[K-1] < 0:
        break
    l = A[0] - A[K]
    ans.append(str(A[0])+" "+str(A[K]))
    A[0] = l
    K -= 1
if K == 1:
    ans.apend(str(A[1])+" "+str(A[0]))
    print(A[1]-A[0])
else:
    b = A[K]
    for i in range(K-1,0,-1):
        c = A[i]
        ans.append(str(b)+" "+str(c))
        b = b-c
    ans.append(str(b)+" "+str(A[0]))
    print(b-A[0])
for p in ans:
    print(p)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
Aplus = [int(i) for i in A if i>=0]
Amainasu = [int(i) for i in A if i<0]
Aplus.sort(reverse=True)
Amainasu.sort()
hoge = 0
result = []
res = 0
if len(Aplus) >= 1 and len(Amainasu) >= 1:
    res = sum(Aplus) - sum(Amainasu)
    hoge = Amainasu[0]
    for i in range(len(Aplus) - 1):
        result.append((hoge,Aplus[i]))
        hoge = hoge - Aplus[i]
    for i in range(len(Amainasu)-1):
        result.append((Aplus[-1],Amainasu[i]))
    result.append((Aplus[-1],hoge))
elif len(Aplus) == 0 and len(Amainasu) >= 1:
    hoge = Amainasu[-1]
    res = sum(Amainasu) * -1 + max(Amainasu) * 2
    for i in range(len(Amainasu) - 1):
        result.append((hoge,Amainasu[i]))
        hoge -= Amainasu[i]
elif len(Aplus) >= 1 and len(Amainasu) == 0:
    if len(Aplus) >= 3:
        res = sum(Aplus) - min(Aplus) * 2
        hoge = Aplus[0]
        for i in range(len(Aplus) - 2):
            result.append((hoge,Aplus[0]))
            hoge -= Aplus[0]
            Aplus.pop(0)
            Aplus.pop(-1)
            Aplus.append(hoge)
        result.append((Aplus[0],Aplus[1]))
    else:
        result.append(Aplus[0],Aplus[1])
        res = Aplus[0] - Aplus[1]
print(res)
for i in result:
    print(str(i[0])+" "+str(i[1]))

Submission status: Runtime Error

Solution code:
from collections import deque
def resolve():
    N = int(input())
    As = list(map(int, input().split(" ")))
    pos = []
    neg = []
    for a in As:
        if a >= 0:
            pos.append(a)
        else:
            neg.append(a)
    pos = deque(sorted(pos))
    neg = deque(sorted(neg))
    x = 0
    y = 0
    ops = []
    while True:
        if len(pos) > 0 and len(neg) > 0:
            if len(pos) > len(neg):
                x = neg.popleft()
                y = pos.pop()
            else:
                x = pos.pop()
                y = neg.popleft()
        elif len(pos) > 1 and len(neg) == 0:
                x = pos.popleft()
                y = pos.pop()
        elif len(neg) > 1 and len(pos) == 0:
                x = pos.pop()
                y = pos.popleft()
        else:
            print("Error?")
            #raise Exception("Unreachable?")
        
        ops.append("{} {}".format(x, y))
        xmy = x - y

        if len(pos) == 0 and len(neg) == 0:
            print(xmy)
            break

        if xmy >= 0:
            pos.append(xmy)
        else:
            neg.appendleft(xmy)
        
    [print(i) for i in ops]

if '__main__' == __name__:
    resolve()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
K = N-1
ans = []
while(K>1):
    if A[K-1] < 0:
        break
    l = A[0] - A[K]
    ans.append(str(A[0])+" "+str(A[K]))
    A[0] = l
    K -= 1
if N == 2:
    ans.apend(str(A[1])+" "+str(A[0]))
    print(A[1]-A[0])
else:
    b = A[K]
    for i in range(K-1,0,-1):
        c = A[i]
        ans.append(str(b)+" "+str(c))
        b = b-c
    ans.append(str(b)+" "+str(A[0]))
    print(b-A[0])
for p in ans:
    print(p)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

fin = sys.stdin.readline
N = int(fin())
a_list = [int(elem) for elem in fin().split()]

sort_a_list = sorted(a_list)
sort_a_deque = deque(sort_a_list)

a_list = []
max_num = sort_a_deque.pop()
while 1:
    max_a = sort_a_deque.pop()
    min_a = sort_a_deque.popleft()
    a_list.append([min_a, max_a])
    sort_a_deque.appendleft(min_a - max_a)
    if len(sort_a_deque) == 1:
        break

last_num = sort_a_deque.pop()
print(max_num - last_num)
a_list.append([max_num, last_num])
for each_elem in a_list:
    print(each_elem[0], each_elem[1])

Submission status: Runtime Error

Solution code:
import sys
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 int(input())
from collections import defaultdict
import bisect

sys.setrecursionlimit(1000000000)

n = SI()
a_list = LI()

a_list =  sorted(a_list)
minus = bisect.bisect_left(a_list, 0)
minus_list = a_list[:minus]

plus_list = a_list[minus:]

ans = 0
p_i = 0
m_i = 0
if minus == 0:
    print(sum(plus_list) - 2*plus_list[0])
    minus_list = plus_list[0:1]
    plus_list = plus_list[1:]


elif minus == n:
    print(-sum(minus_list) + 2*minus_list[-1])
    plus_list = minus_list[-1:]
    minus_list = minus_list[:-1]

else:
    print(sum(plus_list) - sum(minus_list))

ans = minus_list[m_i]
m_i += 1
minus = len(minus_list)
plus = len(plus_list)


while p_i != (plus-1):
    print(ans, plus_list[p_i])
    ans = ans - plus_list[minus]
    p_i += 1

print(plus_list[p_i], ans)
ans = plus_list[p_i] - ans

while m_i != minus:
    print(ans, minus_list[m_i])
    ans = ans - minus_list[m_i]
    m_i += 1

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int,input().split()))
ope = []
while len(a) > 1:
  a.sort()
  x = a[1]
  y = a[len(a)-2]
  
  if y - x > abs(y):
    ope.append([y,x])
    a.append(y-x)
    a.remove(x)
    a.remove(y)
  else:
    ope.append([x,y])
    a.append(x-y)
    a.remove(x)
    a.remove(y)
  
 
print(a[0])
for oo in ope:
  print(oo[0],oo[1])

Submission status: Runtime Error

Solution code:
N, *A = map(int, open(0).read().split())

A_MAX = max(abs(A[0]), abs(A[-1]))
A.remove(A_MAX)
X = sorted(A)
x = X[0]
ans = []
for v in X[1:]:
    ans.append((x, v))
    x -= v

if x <= 0:
    print(A_MAX - x)
    ans.append((A_MAX, x))
else:
    print(x - A_MAX)
    ans.append((x, A_MAX))
for i in range(N - 1):
    print(*ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import defaultdict
from functools import reduce

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

# np.random.seed(1234)
# N = 5
# A = [np.random.randint(-10, 1) for _ in range(N)]

A.sort(reverse=True)


n2 = N//2
k = max(n2, len(list(filter(lambda x: x < 0, A)))) # マイナスをつける個数
t = N-k
_M = sum(A[:t]) - sum(A[t:])
s = set(range(N))
res = []

if N%2 == 0:
    M = A[0]-A[N-1]
    res.append((A[0], A[N-1]))
    t -= 1
    s.remove(0)
    s.remove(N-1)
else:
    raise
    M = A[0]
    t -= 1
    s.remove(0)

B = []

for i in range(t):
    if N%2 == 0:
        j = N-1-i-1
    else:
        j = N-1-i
    B.append(A[j]-A[i+1])
    res.append((A[j], A[i+1]))
    s.remove(j)
    s.remove(i+1)

for b in B:
    res.append((M,b))
    M = M - b

for i in s:
    res.append((M,A[i]))
    M = M - A[i]

from collections import Counter

c = Counter()
for a in A:
    c[a] += 1

print(M)
for r in res:
    c[r[0]] -= 1
    c[r[1]] -= 1
    if c[r[0]] < 0 or c[r[1]] < 0:
        raise
    c[(r[0]-r[1])] += 1
    print(r[0], r[1])
# print(c)
# print(M, _M)
# print(M == res[-1][0] - res[-1][1])

Submission status: Runtime Error

Solution code:
from collections import deque
def resolve():
    N = int(input())
    As = list(map(int, input().split(" ")))
    pos = []
    neg = []
    for a in As:
        if a >= 0:
            pos.append(a)
        else:
            neg.append(a)
    pos = deque(sorted(pos))
    neg = deque(sorted(neg))
    x = 0
    y = 0
    ops = []
    while True:
        if len(pos) > 0 and len(neg) > 0:
            if len(pos) > len(neg):
                x = neg.popleft()
                y = pos.pop()
            else:
                x = pos.pop()
                y = neg.popleft()
        elif len(pos) > 1 and len(neg) == 0:
                x = pos.popleft()
                y = pos.pop()
        elif len(neg) > 1 and len(pos) == 0:
                x = pos.pop()
                y = pos.popleft()
        else:
            raise Exception("Unreachable?")
        
        ops.append("{} {}".format(x, y))
        xmy = x - y

        if len(pos) == 0 and len(neg) == 0:
            print(xmy)
            break

        if xmy >= 0:
            pos.append(xmy)
        else:
            neg.appendleft(xmy)
        
    [print(i) for i in ops]

if '__main__' == __name__:
    resolve()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
K = N-1
ans = []
while(K>1):
    if A[K-1] < 0:
        break
    l = A[0] - A[K]
    ans.append(str(A[0])+" "+str(A[K]))
    A[0] = l
    K -= 1
if N == 2:
    ans.apend(str(A[1])+" "+str(A[0]))
    print(A[1]-A[0])
else:
    b = A[K]
    for i in range(K-1,0,-1):
        c = A[i]
        print(b,c)
        b = b-c
    ans.append(str(b)+" "+str(A[0]))
    print(b-A[0])
for p in ans:
    print(p)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
Aplus = [int(i) for i in A if i>=0]
Amainasu = [int(i) for i in A if i<0]
Aplus.sort(reverse=True)
Amainasu.sort()
hoge = 0
result = []
res = 0
if len(Aplus) >= 1 and len(Amainasu) >= 1:
    res = sum(Aplus) - sum(Amainasu)
    hoge = Amainasu[0]
    for i in range(len(Aplus) - 1):
        result.append((hoge,Aplus[i]))
        hoge = hoge - Aplus[i]
    for i in range(len(Amainasu)-1):
        result.append((Aplus[-1],Amainasu[i]))
    result.append(Aplus[-1],hoge)
elif len(Aplus) == 0 and len(Amainasu) >= 1:
    hoge = Amainasu[-1]
    res = sum(Amainasu) * -1 + max(Amainasu) * 2
    for i in range(len(Amainasu) - 1):
        result.append((hoge,Amainasu[i]))
        hoge -= Amainasu[i]
elif len(Aplus) >= 1 and len(Amainasu) == 0:
    if len(Aplus) >= 3:
        res = sum(Aplus) - min(Aplus) * 2
        hoge = Aplus[0]
        for i in range(len(Aplus) - 2):
            result.append((hoge,Aplus[0]))
            hoge -= Aplus[0]
            Aplus.pop(0)
            Aplus.pop(-1)
            Aplus.append(hoge)
        result.append((Aplus[0],Aplus[1]))
    else:
        result.append(Aplus[0],Aplus[1])
        res = Aplus[0] - Aplus[1]
print(res)
for i in result:
    print(str(i[0])+" "+str(i[1]))

Submission status: Runtime Error

Solution code:
from collections import deque
def resolve():
    N = int(input())
    As = list(map(int, input().split(" ")))
    pos = []
    neg = []
    for a in As:
        if a >= 0:
            pos.append(a)
        else:
            neg.append(a)
    pos = deque(sorted(pos))
    neg = deque(sorted(neg))
    x = 0
    y = 0
    ops = []
    while True:
        if len(pos) > 0 and len(neg) > 0:
            if len(pos) > len(neg):
                x = neg.popleft()
                y = pos.pop()
            else:
                x = pos.pop()
                y = neg.popleft()
        elif len(pos) > 1 and len(neg) == 0:
                x = pos.popleft()
                y = pos.pop()
        elif len(neg) > 1 and len(pos) == 0:
                x = pos.pop()
                y = pos.popleft()
        else:
            raise Exception("Unreachable?")
        
        ops.append("{} {}".format(x, y))
        xmy = x - y

        if len(pos) == 0 and len(neg == 0):
            print(xmy)
            break

        if xmy >= 0:
            pos.append(xmy)
        else:
            neg.appendleft(xmy)
        
    [print(i) for i in ops]

if '__main__' == __name__:
    resolve()

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int,input().split()))
ope = []
while len(a) > 1:
  a.sort()
  x = a[1]
  y = a[-2]
  a.remove(x)
  a.remove(y)
  
  if y - x > abs(y):
    ope.append([y,x])
    a.append(y-x)
    a.remove(x)
    a.remove(y)
  else:
    ope.append([x,y])
    a.append(x-y)
    a.remove(x)
    a.remove(y)
  

print(a[0])
for oo in ope:
  print(oo[0],oo[1])

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
input = sys.stdin.readline


def ansans(ans):
    for a in ans:
        a, b = a
        print(a, b)
    exit()


ans = []
N = int(input())
A = list(map(int, input().split()))
cnt = 0
z = 0
big = -float('inf')
small = float('inf')
PL = deque()
ML = deque()
for a in A:
    if a > big:
        big = a
    if a < small:
        small = a
    if a > 0:
        PL.append(a)
    elif a < 0:
        ML.append(a)
    else:
        z += 1

if len(PL) == len(ML) == 0:
    for i in range(z):
        ans.append((0, 0))
    print(0)
    ansans(ans)
    exit()

if big == small:
    if big > 0:
        z += 1
        PL.popleft()
        PL.popleft()
        ans.append((big, big))
        small = 0
    else:
        ML.popleft()
        ML.popleft()
        z += 1
        ans.append((small, small))
        big = 0

if z == 0 and len(PL) == 0:
    ans.append(big, small)
    cnt += 1
    PL.append(big - small)
elif z == 0 and len(ML) == 0:
    ans.append(small, big)
    cnt += 1
    ML.append(small - big)
elif z != 0:
    if len(PL) == 0 and len(ML) > 1:
        ans.append((0, ML[0]))
        PL.append(-ML[0])
        cnt += 1
        ML.popleft()
        z -= 1
    elif len(ML) == 0 and len(PL) > 1:
        ans.append((0, PL[0]))
        ML.append(-PL[0])
        cnt += 1
        PL.popleft()
        z -= 1

if len(PL) == 0 and len(ML) == 1:
    num = -ML[0]
    ans.append((0, ML[0]))
    for i in range(z):
        ans.append((num, 0))
    print(num)
    ansans(ans)
    exit()

if len(ML) == 0 and len(PL) == 1:
    num = PL[0]
    for i in range(z):
        ans.append((num, 0))
    print(num)
    ansans(ans)
    exit()

while len(PL) != 1:
    num = ML[0]
    ML.popleft()
    ans.append((num, PL[0]))
    num = num - PL[0]
    PL.popleft()
    cnt += 1
ML.append(num)

while len(ML) != 0:
    num = PL[0]
    PL.popleft()
    ans.append((num, ML[0]))
    num = num - ML[0]
    ML.popleft()
    cnt += 1

for i in range(z):
    ans.append((num, z))

print(num)
print(ansans(ans))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
Aplus = [int(i) for i in A if i>=0]
Amainasu = [int(i) for i in A if i<0]
Aplus.sort(reverse=True)
Amainasu.sort()
hoge = 0
result = []
res = 0
if len(Aplus) >= 1 and len(Amainasu) >= 1:
    res = sum(Aplus) - sum(Amainasu)
    hoge = Amainasu[0]
    for i in range(len(Aplus) - 1):
        result.append(hoge,Aplus[i]))
        hoge -= Aplus[i]
    for i in range(len(Amainasu)-1):
        result.append((Aplus[-1],Amainasu[i]))
    result.append(Aplus[-1],hoge)
elif len(Aplus) == 0 and len(Amainasu) >= 1:
    hoge = Amainasu[-1]
    res = sum(Amainasu) * -1 + max(Amainasu) * 2
    for i in range(len(Amainasu) - 1):
        result.append((hoge,Amainasu[i]))
        hoge -= Amainasu[i]
elif len(Aplus) >= 1 and len(Amainasu) == 0:
    if len(Aplus) >= 3:
        res = sum(Aplus) - min(Aplus) * 2
        hoge = Aplus[0]
        for i in range(len(Aplus) - 2):
            result.append((hoge,Aplus[0]))
            hoge -= Aplus[0]
            Aplus.pop(0)
            Aplus.pop(-1)
            Aplus.append(hoge)
        result.append((Aplus[0],Aplus[1]))
    else:
        result.append(Aplus[0],Aplus[1])
        res = Aplus[0] - Aplus[1]
print(res)
for i in result:
    print(str(i[0])+" "+str(i[1]))

Submission status: Runtime Error

Solution code:
import sys, math, collections, heapq, itertools
  
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-2] <= 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[1] >= 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:
        for i in range(1, N-1):
            if A[i] >= 0:
                start = i-1
                break
        ans = A[0] - A[N-1]
        out.append([A[0], A[N-1]])
        if start + 2 < N - 1:
            for i in range(start + 2 , N -1):
                out.append([ans, A[i]])
                ans -= A[i]
        if abs(A[start]) <= A[start + 1]:
            out.append([ans, A[start + 1]])
            ans -= A[start + 1]
            out.append([A[start], ans])
            ans = A[start] - ans
            for i in range(1, start):
                out.append([ans, A[i]])
                ans -= A[i]
        else:
            out.append([A[start + 1], ans])
            ans = A[start + 1] - ans
            for i in range(1, start + 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 bisect
n = int(input())
a = list(map(int,input().split()))

minus = 0
plus = 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    else:
        plus += 1

a.sort()
procedure = []

if minus != 0:
    if plus != 0:
        for i in range(plus): #全部マイナスにする
            procedure.append([a[0],a[-1]])
            a[0] = a[0] - a[-1]
            a.pop(-1)

    d = len(a)
    for i in range(d-2):
        procedure.append([a[-1],a[i]])
        a[-1] = a[-1] - a[i]
        a.pop(0)
    if d != 1:
        procedure.append([a[-1],a[0]])
        a[-1] = a[-1] - a[0]
    else:
        temp = procedure[-1][0]
        procedure[-1][0] = procedure[-1][1]
        procedure[-1][1] = temp
        a[-1] = -a[-1]
    print(a[-1])

else:
    d = len(a)
    for i in range(d-2):
            procedure.append([a[0],a[1]])
            a[0] = a[0] - a[1]
            a.pop(1)
    procedure.append([a[1],a[0]])
    a[0] = a[1] - a[0]
    print(a[0])

for i in range(n-1):
    print(procedure[i][0],procedure[i][1])