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()))
pos = sorted([i for i in a if i >= 0], reverse=True)
neg = sorted([i for i in a if i < 0], reverse=True)

ans = 0
if len(neg) == 0 and len(pos) == 2:
    print(pos[0] - pos[1])
    print(pos[0], pos[1])
    exit()
elif len(pos) == 0 and len(neg) == 2:
    print(neg[0] - neg[1])
    print(neg[0], neg[1])
    exit()
elif len(neg) == 0:
    neg.append(pos.pop())

tmp = neg.pop()
ans = []
while len(pos) > 1:
    a = pos.pop()
    ans.append([tmp, a])
    tmp -= a

for i in neg:
    ans.append(tmp, i)
    tmp -= i

ans.append([pos[0], tmp])
print(pos[0] - tmp)
[print(*i) for i in ans]

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int , input().split()))
pos = sorted([i for i in a if i >= 0], reverse=True)
neg = sorted([i for i in a if i < 0])

ans = 0
if len(neg) == 0 and len(pos) == 2:
    print(pos[0], pos[1])
    exit()
elif len(neg) == 0:
    neg.append(pos.pop())

tmp = neg.pop()
ans = []
while len(pos) > 1:
    a = pos.pop()
    ans.append([tmp, a])
    tmp -= a

for i in neg:
    ans.append(tmp, i)
    tmp -= i

ans.append([pos[0], tmp])
print(pos[0] - tmp)
[print(*i) for i in ans]

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
A = LI()
A.sort()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a > 0:
            plus += [a]
        elif a < 0:
            minus += [a]
        else:
            if not plus:
                plus += [a]
            else:
                minus += [a]
    ret = minus.pop()
    for j in range(len(plus) - 1):
        print(ret, plus[j])
        ret -= plus[j]
    plus_now = plus.pop()
    print(plus_now, ret)
    ret = plus.pop() - ret
    for k in range(len(minus) - 1):
        print(ret, minus[k])
        ret -= minus[k]

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
A = LI()
A.sort()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(last, now)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a >= 0:
            plus += [a]
        else:
            minus += [a]
    plus_now = plus.pop()
    minus_now = minus.pop()
    while plus:
        nxt = plus.pop()
        print(minus_now, nxt)
        minus_now -= nxt
    while minus:
        nxt = minus.pop()
        plus_now -= nxt
        print(plus_now, nxt)
    print(plus_now, minus_now)

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
A = LI()
A.sort()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(now, last)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) + now * 2)
    A.remove(now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a >= 0:
            plus += [a]
        else:
            minus += [a]
    plus_now = plus.pop()
    minus_now = minus.pop()
    while plus:
        nxt = plus.pop()
        print(minus_now, nxt)
        minus_now -= nxt
    while minus:
        nxt = minus.pop()
        plus_now -= nxt
        print(plus_now, nxt)
    print(plus_now, minus_now)

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
A = LI()
A.sort()
if all([a > 0 for a in A]):
    now = min(A)
    print(sum(A) - now * 2)
    A.remove(now)
    last = A.pop()
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
    print(now, last)
elif all([a < 0 for a in A]):
    now = max(A)
    print(-sum(A) - now)
    while A:
        nxt = A.pop()
        print(now, nxt)
        now -= nxt
else:
    print(sum([abs(i) for i in A]))
    minus = []
    plus = []
    for a in A:
        if a >= 0:
            plus += [a]
        else:
            minus += [a]
    plus_now = plus.pop()
    minus_now = minus.pop()
    while plus:
        nxt = plus.pop()
        print(minus_now, nxt)
        minus_now -= nxt
    while minus:
        nxt = minus.pop()
        plus_now -= nxt
        print(plus_now, nxt)
    print(plus_now, minus_now)

Submission status: Runtime Error

Solution code:
from collections import deque
n=int(input())
A=deque(sorted(list(map(int,input().split()))))
B=[]
while len(A)!=2:
    x,y=A.popleft(),A.pop()
    A.append(x-y)
    B.append([x,y])
    A=sorted(A)

B.append([A[-1],A[0]])
print(A[-1]-A[0])
for i in B:
    print(*i)

Submission status: Runtime Error

Solution code:
n,*a = map(int,open(0).read().split())
am = []
ap = []
amap = am.append
apap = ap.append
for i in a:
  if i >= 0:
    apap(i)
  else:
    amap(i)
ans = []
ansap = ans.append
for _ in range(n-1):
  if ap == []:
    x = max(am)
    y = min(am)
    am.remove(x)
    am.remove(y)
    apap(x-y)
  elif am == []:
    x = min(ap)
    y = max(ap)
    ap.remove(x)
    ap.remove(y)
    amap(x-y)
  elif len(ap) > len(am):
    x = am.pop()
    y = ap.pop()
    amap(x-y)
  else:
    x = ap.pop()
    y = am.pop()
    apap(x-y)
  ansap(" ".join((str(x),str(y))))

print(ap[0])
for i in ans:
  print(i)

Submission status: Runtime Error

Solution code:
n,*a = map(int,open(0).read().split())
am = []
ap = []
amap = am.append
apap = ap.append
for i in a:
  if i >= 0:
    apap(i)
  else:
    amap(i)
ans = []
ansap = ans.append
while len(am + ap) >1:
  if ap == []:
    x = max(am)
    y = min(am)
    am.remove(x)
    am.remove(y)
    apap(x-y)
  elif am == []:
    x = min(ap)
    y = max(ap)
    ap.remove(x)
    ap.remove(y)
    amap(x-y)
  elif len(ap) > len(am):
    x = am.pop()
    y = ap.pop()
    amap(x-y)
  else:
    x = ap.pop()
    y = am.pop()
    apap(x-y)
  ansap(" ".join((str(x),str(y))))

print(ap[0])
for i in ans:
  print(i)

Submission status: Runtime Error

Solution code:
m,*a = map(int,open(0).read().split())
am = []
ap = []
amap = am.append
apap = ap.append
for i in a:
  if i >= 0:
    apap(i)
  else:
    amap(i)
ans = []
ansap = ans.append
for _ in range(m-1):
  if ap == []:
    x = max(am)
    y = min(am)
    am.remove(x)
    am.remove(y)
    apap(x-y)
  elif am == []:
    x = min(ap)
    y = max(ap)
    ap.remove(x)
    ap.remove(y)
    amap(x-y)
  elif len(ap) > len(am):
    x = am.pop()
    y = ap.pop()
    amap(x-y)
  else:
    x = ap.pop()
    y = am.pop()
    apap(x-y)
  ansap(" ".join((str(x),str(y))))

print(ap[0])
print("\n".join(ans))

Submission status: Runtime Error

Solution code:
m,*a = map(int,open(0).read().split())
am = []
ap = []
amap = am.append
apap = ap.append
for i in a:
  if i >= 0:
    apap(i)
  else:
    amap(i)
ans = []
ansap = ans.append
for _ in range(m-1):
  if len(ap) == 0:
    x = max(am)
    y = min(am)
    am.remove(x)
    am.remove(y)
    apap(x-y)
  elif len(am) == 0:
    x = min(ap)
    y = max(ap)
    ap.remove(x)
    ap.remove(y)
    amap(x-y)
  elif len(ap) > len(am):
    x = am.pop()
    y = ap.pop()
    amap(x-y)
  else:
    x = ap.pop()
    y = am.pop()
    apap(x-y)
  ansap(" ".join((str(x),str(y))))

print(ap[0])
print("\n".join(ans))

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
A.sort()
ans = []

# 全て正
if min(A)>0:
    print(sum(A)-2*A[0])
    for i in range(n):
        if i==0:
            x = A[i]
        elif i==n-1:
            print(A[i], x)
        else:
            print(x, A[i])
            x -= A[i]

# 全て負
elif max(A)<=0:
    A = A[::-1]
    print(sum(A)*(-1) + 2*A[0])
    for i in range(n):
        if i==0:
            x = A[i]
        elif i==n-1:
            print(A[i], x)
        else:
            print(x, A[i])
            x -= A[i]

# 正負
else:
    m,p = [], []
    for a in A:
        if a<0:
            m.append(a)
        else:
            p.append(a)
    print(sum(p)-sum(m))
    m.sort()
    p.sort()
    x = m[0]
    for i in range(len(p)-1):
        print(x, p[i])
        x -= p[i]
    else:
        print(p[-1], x)
        x = p[-1] - x
        
    for j in range(1, len(m)):
        print(x, m[j])
        x -= m[j]

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
A.sort()
ans = []

# 全て正
if min(A)>0:
    print(sum(A)-2*A[0])
    for i in range(n):
        if i==0:
            x = A[i]
        elif i==n-1:
            print(A[i], x)
        else:
            print(x, A[i])
            x -= A[i]

# 全て負
elif max(A)<0:
    A = A[::-1]
    print(sum(A)*(-1) + 2*A[0])
    for i in range(n):
        if i==0:
            x = A[i]
        elif i==n-1:
            print(A[i], x)
        else:
            print(x, A[i])
            x -= A[i]

# 正負
else:
    m,p = [], []
    for a in A:
        if a<0:
            m.append(a)
        else:
            p.append(a)
    print(sum(p)-sum(m))
    m.sort()
    p.sort()
    x = m[0]
    for i in range(len(p)-1):
        print(x, p[i])
        x -= p[i]
    else:
        print(p[-1], x)
        x = p[-1] - x
        
    for j in range(1, len(m)):
        print(x, m[j])
        x -= m[j]

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
A.sort()
ans = []

# 全て正
if min(A)>0:
    print(sum(A)-2*A[0])
    for i in range(n):
        if i==0:
            x = A[i]
        elif i==n-1:
            print(A[i], x)
        else:
            print(x, A[i])
            x -= A[i]

# 全て負
elif max(A)<0:
    A = A[::-1]
    print(sum(A)*(-1) + 2*A[0])
    for i in range(n):
        if i==0:
            x = A[i]
        elif i==n-1:
            print(A[i], x)
        else:
            print(x, A[i])
            x -= A[i]

# 正負
else:
    m,p = [], []
    for a in A:
        if a<0:
            m.append(a)
        else:
            p.append(a)
    print(sum(p)-sum(m))
    m.sort()
    p.sort()
    x = m[0]
    for i in range(len(p)-1):
        print(x, p[i])
        x -= p[i]
    else:
        print(p[-1], x)
        x = p[-1] - x
        
    for j in range(1, len(m)):
        print(x, m[j])

Submission status: Runtime Error

Solution code:
import sys
INF = 10 ** 11
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
from heapq import heapify,heappush,heappop   

def main():
    n = int(input())
    a = list(map(int,input().split()))
    m = []
    p = []
    z = 0
    for num in a:
        if num < 0:
            m.append(num)
        elif num > 0:
            p.append(num)
        else:
            z += 1
    
    ans = []
    if len(p) * len(m) > 0:
        num1 = p[-1]
        p.pop()
        num2 = m[-1]
        m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
        for i in range(len(p)):
            ans.append((num2,p[i]))
            num2 -= p[i]
        ans.append((num1,num2))
        num1 -= num2
    
    elif len(p) == 0:
        m.sort()
        num1 = m[-1]
        m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
    
    elif len(m) == 0:
        p.sort()
        num1 = p[0]
        for i in range(1,len(p) - 1):
            ans.append((num1,p[i]))
            num1 -= p[i]
        ans.append((p[-1],num1))
        num1 = p[-1] - num1
    
    for i in range(z):
        ans.append((num1,0))
    print(num1)
    for i in range(n - 1):
        print(*ans[i])

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
n=int(input())
a=list(map(int,input().split()))
a.sort()
if(a[0]>=0):
    #aが全部正
    a=deque(a)
    max_a=a.pop()
    min_a=a.popleft()
    ans=[]
    while(len(a)>0):
        ai=a.popleft()
        ans.append([min_a,ai])
        min_a-=ai
    ans.append([max_a,min_a])
    print(max_a-min_a)
    for i in range(len(ans)):
        print("{0} {1}".format(ans[i][0],ans[i][1]))
elif(a[-1]<0):
    #aが全部負
    a=deque(a)
    max_a=a.pop()
    min_a=a.popleft()
    ans=[]
    while(len(a)>0):
        ai=a.pop()
        ans.append([max_a,ai])
        max_a-=ai
    ans.append([max_a,min_a])
    print(max_a-min_a)
    for i in range(len(ans)):
        print("{0} {1}".format(ans[i][0],ans[i][1]))
else:
    print(a[1000000000])

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) {if(a<b) a=b;}
#define chmin(a, b) {if(a>b) a=b;}
#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF=1e18;
static const ll MOD=1e9+7;
static const ll MAX=101010;

/*
 for(i=0; i<N; i++)
   cin >> a[i];
*/


int main(void) {
  ll i, j, k, l;
  
  ll N;
  cin >> N;
  ll s=0;
  ll a[MAX];
  for(i=0; i<N; i++) {
    cin >> a[i];
    s+=abs(a[i]);
  }
  sort(a, a+N);
  
  if((a[0]<=0 && a[N-1]>=0)) {
    pt(s);
  }else {
    pt(max(s-a[0]*2, s-abs(a[N-1])*2));
  }
  
  
}

Submission status: Runtime Error

Solution code:
import heapq
from collections import defaultdict
N = int(input())
nums  = list(map(int, input().split(" ")))
# 正の数、負の数の個数で操作をコントロール？
p_count = 0
m_count = 0
pos = []
minus = []
for num in nums:
    if num < 0:
        m_count += 1
        minus.append(num)
    else:
        p_count += 1
        pos.append(num)
pos.sort()
minus.sort() #小さい順番
途中式 = []
if N == 2:
    print(max(nums) - min(nums))
else:
    #正の数を一つにして、そこから引きまくる
    if m_count == 0: #マイナスが無かったら作る
        x = min(pos)
        y = max(pos)
        途中式.append((x, y))
        minus.append(x - y)
        pos.remove(x)
        pos.remove(y)
        p_count -=2
        m_count += 1

    if p_count > 1: # 正が複数あったら、マイナスから引くことで減らしていく
        for i in range(p_count - 1):
            #マイナスから正が1個になるまで引いていく
            x = minus.pop()
            y = pos.pop()
            途中式.append((x,y))
            minus.append(x- y)
    elif p_count == 0 : #正が一つも無かったら、作る
        x = minus.pop()
        y = minus.pop()
        途中式.append((x, y))
        pos.append(x - y)

    x = pos.pop()
    for i in minus:
        y = i
        途中式.append((x, y))
        x -=y
print(x)
for temp in 途中式:
    x,y = temp
    print(x,y)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=0
for i in range(n):
  ans+=abs(a[i])
if a[0]>0:
  print(sum(a)-2*a[0])
else:
  print(ans)
plus=list()
minus=list()
for i in range(n):
  if a[i]>=0:
    plus.append(a[i])
  else:
    minus.append(a[i])
if len(minus)>0:
  while len(plus)>=2:
    p=plus.pop(0)
    m=minus.pop(0)
    minus.append(m-p)
    print(str(m)+' '+str(p))
if len(plus)>=2 and len(minus)==0:
  plus.sort()
  p1=plus.pop(0)
  p2=plus.pop(-1)
  minus.append(p1-p2)
  print(str(p1)+' '+str(p2))
if len(plus)==0:
  minus.sort()
  m1=minus.pop(-1)
  m2=minus.pop(-2)
  plus.append(m1-m2)
  print(str(m1)+' '+str(m2))
while len(minus)>0:
  p=plus.pop(0)
  m=minus.pop(0)
  plus.append(p-m)
  print(str(p)+' '+str(m))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=0
for i in range(n):
  ans+=abs(a[i])
print(ans)
plus=list()
minus=list()
for i in range(n):
  if a[i]>=0:
    plus.append(a[i])
  else:
    minus.append(a[i])
while len(plus)>=2 :
  p=plus.pop(0)
  m=minus.pop(0)
  minus.append(m-p)
  print(str(m)+' '+str(p))
while len(minus)>0:
  p=plus.pop(0)
  m=minus.pop(0)
  plus.append(p-m)
  print(str(p)+' '+str(m))

Submission status: Runtime Error

Solution code:
import bisect
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=0
b1=bisect.bisect_left(a,0)
b2=bisect.bisect_right(a,0)
l=[]
#print(a,b1,b2)
if a[0]>0 or b1==1 or (b1==0 and b2>=1):
    l.append([a[0],a[1]])
    ans=l[0][0]-l[0][1]
    for i in range(1,n-1):
        if i!=n-2:
            l.append([l[-1][0]-l[-1][1],a[i]])
        else:
            l.append([a[-1],l[-1][0]-l[-1][1]])
        ans=l[i][0]-l[i][1]
    print(ans)
elif a[-1]<0 or n-b2==1 or (b2==n and b1<=n-1):
    l.append([a[-1],a[-2]])
    ans=l[0][0]-l[0][1]
    for i in range(n-2,0,-1):
        if i!=1:
            l.append([a[i],l[-1][0]-l[-1][1]])
        else:
            l.append([l[-1][0]-l[-1][1],a[0]])
        ans=l[i][0]-l[i][1]
    print(ans)
else:
    l.append([a[-1],a[1]])
    ans_1=l[0][0]-l[0][1]
    for i in range(b1,2,-1):
        l.append([a[i],l[-1][0]-l[-1][1]])
        ans_1=l[i][0]-l[i][1]
    l.append([a[0],a[-2]])
    ans_2=l[-1][0]-l[-1][1]
    for i in range(b1+1,n-1):
        l.append([l[-1][0]-l[-1][1],a[i]])
        ans_2=l[i][0]-l[i][1]
    l.append([ans_1,ans_2])
    ans=l[i][0]-l[i][1]
    print(ans)
for i in range(n-1):
    print(*l[i])

Submission status: Runtime Error

Solution code:
def main(lis):
    if len(lis) == 2:
        print(max(lis) - min(lis))
        print(max(lis), min(lis))
        return
    min_val = min(lis)
    others = list(set(lis) - {min_val})
    sub = min_val
    print(sum(others) - min_val)
    for o in others[:-1]:
        print(min_val, o)
        sub = sub - o
    print(others[-1], sub)

input()
lis = list(map(int, input().split()))
main(lis)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;


void solve(long long N, std::vector<long long> A){
    std::sort(A.begin(),A.end());
    int positive_start_idx = 0;
    for (int i=0;i<N;i++){
        if(A[i]>0) break;
        positive_start_idx++;
    }
    ll n_pos = N-positive_start_idx;
    ll n_neg = N -n_pos;
    if (n_pos==0){
        n_pos==1;
        n_neg==N-1;
    }
    if (n_neg==0){
        n_neg==1;
        n_pos==N-1;
    }
    vector<pair<ll,ll>> ret_print;
    ll x = A[0];
    for (int i = n_neg;i<N-1;i++){
        ret_print.push_back(make_pair(x,A[i]));
        x = x- A[i];
    }
    ret_print.push_back(make_pair(A[N-1],x));
    x = A[N-1] -x ;
    for (int i=1;i<n_neg;i++){
        ret_print.push_back(make_pair(x,A[i]));
        x = x - A[i];
    }
    printf("%lld\n",x);
    for (const auto &pair: ret_print ){
        printf("%lld %lld\n",pair.first,pair.second);
    }

}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, std::move(A));
    return 0;
}

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:
n = int(input())
lis = list(map(int,input().split()))
minus,plus = [],[]
for num in lis:
    if num >= 0:
        plus.append(num)
    elif num < 0:
        minus.append(num)
plus.sort()
minus.sort()
ans = []
if len(plus) == 0:
    now = minus.pop()
    for num in minus:
        ans.append([now,num])
        now -= num
    print(now)
elif len(minus) == 0:
    now = plus.pop()
    last_num = plus.pop()
    for num in minus:
        ans.append([now,num])
        now -= num
    ans.append([last_num,now])
    print(now)
elif len(plus) == 1:
    now = plus.pop()
    for num in minus:
        ans.append([now,num])
        now -= num
    print(now)
else:
    now = minus.pop()
    last_num = plus.pop()
    for num in plus:
        ans.append([now,num])
        now -= num
    for num in minus:
        ans.append([last_num,num])
        last_num -= num
    ans.append([last_num,now])
    last_num -= now
    print(last_num)
for i in range(n-1):
    print(*ans[i])

Submission status: Runtime Error

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

A.sort(reverse=True)
P=[]
M=[]

for i in A:
    if i<0:
        M.append(i)
    else:
        P.append(i)
ans=[]
if len(M)==0:
    x=P.pop()
    y=P.pop()
    ans.append([x,y])
    M.append(x-y)
if len(P)==0:
    x=M.pop()
    y=M.pop()
    ans.append([x,y])
    P.append(x-y)


for i in range(len(P)-1):
    y=P.pop()
    x=M.pop()
    ans.append([x,y])
    M.append(x-y)

for i in range(len(M)):
    y=M.pop()
    x=P.pop()
    ans.append([x,y])
    P.append(x-y)

print(P[0])

for x,y in ans:
    print(x,y)

Submission status: Runtime Error

Solution code:
N = int(input())
AA = list(map(int,input().split()))#0を含む可能性あり

A = []
count = 0
for i in range(N):
    if AA[i] != 0:
        A.append(AA[i])
    else:
        count += 1

B = [abs(A[i]) for i in range(N)]

det = 1
s = A[0]//B[0]#最初のやつの符号
for i in range(1,N):
    if s * A[i] < 0:
        det = -1
        break

if det == 1:#全て同符号
    m = min(B)

    M = sum(B) - 2 * m
    print(M)

    for i in range(N):
        if m == B[i]:
            p = i
            break

    C = [A[i]//B[i] for i in range(N)]

    C[p] *= -1

else:#異符号が含まれる
    M = sum(B)
    print(M)
    C = [A[i]//B[i] for i in range(N)]

fp = -1 #firstplus
fm = -1 #firstminus
for i in range(N):
    if fp == -1 and C[i] == 1:
        fp = i
    elif fm == -1 and C[i] == -1:
        fm = i
    elif fp != -1 and fm != -1:
        break

X = []
Y = []

for i in range(N):
    if i == fp or i == fm:
        continue
    if C[i] == -1:
        X.append(i)
    else:
        Y.append(i)

x = A[fp]
for i in X:
    print(x,A[i])
    x -= A[i]

y = A[fm]
for i in Y:
    print(y,A[i])
    y -= A[i]

print(x,y)

for i in range(count):
    print(x-y,0)

Submission status: Runtime Error

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

zerocount = 0
while True:
    if 0 in A:
        A.remove(0)
        zerocount += 1
    else:
        break

B = [abs(A[i]) for i in range(N)]

det = 1
s = A[0]//B[0]#最初のやつの符号
for i in range(1,N):
    if s * A[i] < 0:
        det = -1
        break

if det == 1:#全て同符号
    m = min(B)

    M = sum(B) - 2 * m
    print(M)

    for i in range(N):
        if m == B[i]:
            p = i
            break

    C = [A[i]//B[i] for i in range(N)]

    C[p] *= -1

else:#異符号が含まれる
    M = sum(B)
    print(M)
    C = [A[i]//B[i] for i in range(N)]

fp = -1 #firstplus
fm = -1 #firstminus
for i in range(N):
    if fp == -1 and C[i] == 1:
        fp = i
    elif fm == -1 and C[i] == -1:
        fm = i
    elif fp != -1 and fm != -1:
        break

X = []
Y = []

for i in range(N):
    if i == fp or i == fm:
        continue
    if C[i] == -1:
        X.append(i)
    else:
        Y.append(i)

x = A[fp]
for i in X:
    print(x,A[i])
    x -= A[i]

y = A[fm]
for i in Y:
    print(y,A[i])
    y -= A[i]

print(x,y)

for i in range(zerocount):
    print(x-y,0)