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:

def main():
    n = input()
    aa = map(int, raw_input().split())
    if n % 2 == 1:
        aa.sort()
    else:
        aa.sort(reverse=True)
    xx = []
    i = 0
    d = n - 1
    for _ in xrange(n):
        xx.append(aa[i])
        i += d
        d = 1 - d
    order = [[xx[0], xx[1]]]
    q = xx[0] - xx[1]
    for p in xx[2:]:
        order.append([p, q])
        q = p - q
    print q
    for p, q in order:
        print p, q


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
A.sort()

B = [None] * N
B[::2] = A[::-1][: N - (N // 2)]
B[1::2] = A[:N // 2]

def calc(l):
    if len(l) > 1:
        return l[0] - calc(l[1:])
    else:
        return l[0]

print(calc(B))
tmp = B[-1]
for i in range(N - (N // 2) - 1):
    print(B[N - 2 - i], tmp)
    tmp = B[N - 2 - i] - tmp
print(A[-1], tmp)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from sys import stdin
from collections import deque


def main():
  # input
  N = int(input())
  An = list(map(int, stdin.readline().rstrip().split()))

  # sort An -> Bn
  An = sorted(An)
  Bn_p,Bn_n = [deque(), deque()]
  for a in An:
    if a >= 0:
      Bn_p.append(a)
    else:
      Bn_n.append(a)

  p,n = [len(Bn_p),len(Bn_n)]      
  xy = []
  while n+p > 2:
    if p==0:
      x = Bn_n.popleft()
      y = Bn_n.popleft()
      Bn_p.append(x-y)      
      xy.append([x,y])
    elif n==0:
      x = Bn_p.popleft()
      y = Bn_p.popleft()
      Bn_n.append(x-y)      
      xy.append([x,y])
    elif p < n:
      x = Bn_p.pop()
      y = Bn_n.pop()
      Bn_p.append(x-y)
      xy.append([x,y])
    else:
      x = Bn_p.pop()
      y = Bn_n.pop()
      Bn_n.append(y-x)
      xy.append([y,x])
    p,n = [len(Bn_p),len(Bn_n)]      

  last = list(Bn_p+Bn_n)
  xy.append([max(last),min(last)])
  ans = max(last)-min(last)


  ## output
  print(ans)
  for x,y in xy:
    print(x,y)

if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=[i for i in a if i<0]
t=sorted([i for i in a if i>0])
if len(s)==0:
    print(t[-1]-t[0]+sum(t[1:-1]))
    k=t[0]
    for i in range(1,n-1):
        print(k,t[i])
        k-=t[i]
    print(t[-1],k)
elif len(t)==0:
    d=s[0]
    for i in range(1,n):
        print(d,s[i])
        d-=s[i]
else:
    print(t[-1]-sum(s)+sum(t[:-1]))
    p=t[-1]
    for i in range(1,len(s)):
        print(p,s[i])
        p-=s[i]
    g=s[0]
    for i in range(len(t)-1):
        print(g,t[i])
        g-=t[i]
    print(p,g)

Submission status: Runtime Error

Solution code:
from collections import Counter
from collections import deque
from functools import reduce
from pprint import pprint
import bisect
import copy
import fractions
import itertools
import math
import queue
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) 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 MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers): return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers): return reduce(LCM, numbers)
def LCM(m, n): return (m * n // fractions.gcd(m, n))


n = II()
a = sorted(LI(), key=abs)
for i in range(n - 1):
    if abs(a[i]) == abs(a[i+1]) and a[i] > a[i+1]:
        hoge = a[i+1]
        a[i+1] = a[i]
        a[i] = hoge
# print(a)
newa = []
zerocnt = 0
for i in a:
    if i != 0:
        newa.append(i)
    else:
        zerocnt += 1
s = 0
ans = []
tmp = newa[0]
for i in range(n - 1 - zerocnt):
    if i == n - 2 - zerocnt:
        if tmp < newa[i+1]:
            ans.append((newa[i+1], tmp))
            tmp = newa[i+1] - tmp
            print(tmp)
        else:
            ans.append((tmp, newa[i+1]))
            tmp = tmp - newa[i+1]
            print(tmp)
    elif i == 0:
        tmp = newa[i] - newa[i+1]
        ans.append((newa[i], newa[i+1]))
    else:
        tmp = tmp - newa[i+1]
        ans.append((tmp, newa[i+1]))
for i in ans:
    print(str(i[0])+' '+str(i[1]))
for i in range(zerocnt):
    print(str(tmp)+' '+str(0))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
m = n//2

a.sort()
minus = a[0:n//2]
plus = []
i = n//2
while a[i] < 0 and i < n:
    minus.append(a[i])
    i += 1
for j in range(i, n):
    plus.append(a[j])
print(sum(plus)-sum(minus))
for i in range(n-1):
    mi = minus.pop(0)
    pl = plus.pop(0)
    if len(minus) >= len(plus):
        print(str(pl) +' '+str(mi))
        plus.append(pl-mi)
    else:
        minus.append(mi-pl)
        print(str(mi) +' '+str(pl))

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort()
d = deque(A)
if A[0] <= 0 and A[-1] > 0:
    print(sum(map(abs, A)))
    while len(d) > 2:
        while d[1] < 0 and len(d)>2:
            print(list(d))
            x = d.pop()
            y = d.popleft()
            print(x, y)
            d.append(x - y)
        x = d.popleft()
        y = d.pop()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])

elif A[0] > 0:
    print(sum(A) - 2 * A[0])
    x = d.popleft()
    y = d.pop()
    print(x, y)
    d.appendleft(x - y)
    while len(d) > 2:
        x = d.popleft()
        y = d.pop()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])
else:
    print(-sum(A) + 2 * A[-1])
    x = d.pop()
    y = d.popleft()
    print(x, y)
    d.append(x - y)
    while len(d) > 2:
        while d[1] < 0 and len(d) > 2:
            x = d.pop()
            y = d.popleft()
            print(x, y)
            d.append(x - y)
        x = d.pop()
        y = d.popleft()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])

Submission status: Runtime Error

Solution code:
n=int(input())
l=sorted(list(map(int,input().split())))
if l[0]>=0:
  c=l[0]
  l1=[]
  for i in range(1,n-1):
    l1.append([c,l[i]])
    c-=l[i]
  score=l[-1]-c
  print(score)
  for i in l1:
    print(i[0],i[1])
  print(l[-1],c)
else:
  l1=[]
  l2=[]
  l3=[]
  for i in l:
    if i<=0:
      l1.append(i)
    else:
      l2.append(i)
  c=l1[0]
  for i in range(len(l2)-1):
    l3.append([c,l2[i]])
    c-=l2[i]
  s1=l2[len(l2)-1]
  for i in range(1,len(l1)):
    l3.append([s1,l1[i]])
    s1-=l1[i]
  score=s1-c
  print(score)
  for i in l3:
    print(i[0],i[1])
  print(s1,c)

Submission status: Runtime Error

Solution code:
import sys
import collections

n = int(input())
a = sorted([int(i) for i in input().split()])
# print(a)
top = a.pop(-1)

nega = sorted([i for i in a if i < 0])
posi = sorted([i for i in a if i > 0])
#print(nega,posi)

if len(nega) == 0:
    nega.append(posi.pop(0))

process = []

for _ in range(len(posi)):
    x = nega.pop(0)
    y = posi.pop(0)
    nega.append(x - y)
    process.append([x, y])
x = top
y = nega.pop(0)
process.append([x, y])
x -= y
for _ in range(len(nega)):
    y = nega.pop(0)
    process.append([x, y])
    x -= y
ans = x

print(ans)
for i in range(len(process)):
    print('{} {}'.format(process[i][0], process[i][1]))

Submission status: Runtime Error

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

n_pos = len([a for a in A if a > 0])
operations = []
if n_pos <= (N + 1) // 2:
    pos = [a for a in A if a > 0]
    neg = [a for a in A if a <= 0]
    tmp = pos[0]
    for i in range(n_pos-1):
        operations.append((neg[i], tmp))
        tmp = neg[i] - tmp
        operations.append((pos[i+1], tmp))
        tmp = pos[i+1] - tmp
        
    for i in range(n_pos-1, len(neg)):
        operations.append((tmp, neg[i]))
        tmp = tmp - neg[i]
else:
    neg = A[:N//2]
    pos = A[N//2:]
    tmp = pos[0]
    for i in range(N//2):
        operations.append((neg[i], tmp))
        tmp = neg[i] - tmp
        operations.append((pos[i+1], tmp))
        tmp = pos[i+1] - tmp

print(tmp)
for o in operations:
    print("{} {}".format(o[0], o[1]))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
# print(a)
a.sort()
a.reverse()
# print(a)
l1=[]
if n==2:
	print(a[0]-a[1])
	print(a[0],a[1])
else:
	for i in range(1,n):
		if a[i]<=0:
			l1.append((a[0],a[i]))
			a[0] = a[0] - a[i]
	if a[1]<=0:
		print(a[0])
		for i in range(n-1):
			print(l1[i][0],l1[i][1])
		# print(a[0],a[1])
	else:
		for i in range(n-2):
			if a[n-1-i] > 0:
				l1.append((a[n-1-i],a[n-2-i]))
				a[n-2-i] = a[n-1-i]-a[n-2-i]
		ans = a[0]-a[1]
		print(ans)
		for i in range(n-2):
			print(l1[i][0],l1[i][1])
		print(a[0],a[1])

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
if len(Aplus) >= 1 and len(Amainasu) >= 1:
    for i in range(len(Aplus) - 1):
        print(Amainasu[0],Aplus[i])
    for i in range(len(mainasu)):
        print(Aplus[-1],Amainasu[i])
elif len(Aplus) == 0 and len(Amainasu) >= 1:
    hoge = Amainasu[-1]
    for i in range(len(Amainasu) - 1):
        print(hoge,Amainasu[i])
        hoge -= Amainasu[i]
elif len(Aplus) >= 1 and len(Amainasu) == 0:
    if len(Aplus) >= 3:
        hoge = Aplus[0]
        for i in range(len(Aplus) - 2):
            print(hoge,Aplus[0])
            hoge -= Aplus[0]
            Aplus.pop(0)
            Aplus.pop(-1)
            Aplus.append(hoge)
        print(Aplus[0],Aplus[1])

Submission status: Runtime Error

Solution code:
import bisect
n = int(input())
a = list(map(int,input().split()))

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

temp = 0
c = min(plus,minus)
def plusminus(procedure,a,c,temp):
    for i in range(c):
        procedure.append([a[0],a[-1]])
        temp += a[i]-a[-1-i]
        a.pop(0)
        a.pop(-1)
    return temp

temp = plusminus(procedure,a,c,temp) #負の数
#print(temp)

d = len(a)
#print(a)
def only(procedure,a,d,temp): #最大の正の数を作る
    if a[0] >=0: 
        for i in range(d-1):
            procedure.append([temp,a[i]])
            temp = temp - a[i]
        procedure.append([a[d-1],temp])
        temp = a[d-1] - temp
    else:
        for i in range(d-1):
            procedure.append([a[d-1],a[i]])
            a[d-1] = a[d-1] - a[i]
        procedure.append([a[d-1],temp])
        temp = a[d-1] - temp

    return temp

if d != 0:
    temp = only(procedure,a,d,temp)
print(temp)
for i in range(n-1):
    print(procedure[i][0],procedure[i][1])

Submission status: Runtime Error

Solution code:
import bisect

N=int(input())
A=[int(i) for i in input().split(" ")]
A=sorted(A)
traceback=[]

if A[0]>=0:
    minus=[A[0]]
    plus=A[1::]
elif A[-1]<=0:
    minus=A[:-1:]
    plus=A[-1]
else:
    index=bisect.bisect_left(A,0)
    minus=A[:index:]
    plus=A[index::]

print(sum(plus)-sum(minus))
m=minus.pop(0)
for p in plus[:-1:]:
    print(m,p)
    m=m-p
print(plus[-1],m)
p=plus[-1]-m
for m in minus:
    print(p,m)
    p=p-m

Submission status: Runtime Error

Solution code:
import numpy as np


N = int(input())

poss, negs = [], []
npos, nneg = 0, 0
for a in input().split():
    ia = int(a)
    if ia < 0:
        negs.append(ia)
        nneg += 1
    else:
        poss.append(ia)
        npos += 1

if nneg == 0:
    print(sum(poss) - min(poss) * 2)
    argmin = np.array(poss).argmin()
    minpos = poss.pop(argmin)
    pos = poss.pop()
    print(minpos, pos)
    negs.append(minpos - pos)
    npos -= 2
    nneg += 1
elif npos == 0:
    print(-sum(negs) + max(negs) * 2)
    argmax = np.array(negs).argmax()
    maxneg = negs.pop(argmax)  # -1とか
    neg = negs.pop()  # -5とか
    print(maxneg, neg)
    poss.append(maxneg - neg)
    nneg -= 2
    npos += 1
else:
    print(sum(poss) - sum(negs))

if npos > nneg:
    for _ in range(npos - nneg):
        pos, neg = poss.pop(), negs.pop()
        print(neg, pos)
        negs.append(neg - pos)
else:
    for _ in range(nneg - npos):
        pos, neg = poss.pop(), negs.pop()
        print(pos, neg)
        poss.append(pos - neg)

for _ in range(nneg - 1):
    pos, neg = poss.pop(), negs.pop()
    print(neg, pos)
    negs.append(neg - pos)
    pos, neg = poss.pop(), negs.pop()
    print(pos, neg)
    poss.append(pos - neg)

pos, neg = poss.pop(), negs.pop()
print(pos, neg)

Submission status: Runtime Error

Solution code:
n = int(input())
x = list(map(int,input().split()))
#x = sorted(list(map(int,input().split())),reverse=True)
#nmax = max(x)
#ans = 2*nmax-sum(x)
lst = []


while len(x) > 2:
  mi = min(x)
  ma = max(x)
  if mi < 0:
    lst.append(str(ma)+" "+str(mi))
    del x[x.index(mi)]
    del x[x.index(ma)]
    x.append(ma - mi)
  lst.append(str(ma)+" "+str(mi))
  del x[x.index(mi)]
  del x[x.index(ma)]
  x.append(mi - ma)

print(max(x) - min(x))
for i in range(len(lst)):
  print(lst[i])
print(max(x),min(x))

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans_list=[]
if len(A)%2==1:
    for i in range(len(A)-1):
        m=min(A)
        M=max(A)
        A.remove(m)
        A.remove(M)
        if i%2==0:
            ans_list.append([m,M])
            A.append(m-M)
        else:
            ans_list.append([M,m])
            A.append(M-m)
else:
    for i in range(len(A)-1):
        m=min(A)
        M=max(A)
        ans_list.append([])
        A.remove(m)
        A.remove(M)
        if i%2==0:
            ans_list.append([M,m])
            A.append(M-m)
        else:
            ans_list.append([m,M])
            A.append(m-M)

print(A[0])
for i in range(N-1):
    print(ans_list[i][0],ans_list[i][1])

Submission status: Runtime Error

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

p = [i for i in a if i >= 0]
n = [i for i in a if i < 0]

process = []
if len(p) > 0 and len(n) > 0:
    p_seed = p.pop(0)
    n_seed = n.pop(0)
    while len(n) > 0:
        sub = n.pop(0)
        process.append((p_seed, sub))
        p_seed -= sub
    while len(p) > 0:
        sub = p.pop(0)
        process.append((n_seed, sub))
        n_seed -= sub
    process.append((p_seed, n_seed))
    m = p_seed - n_seed
    print(m)
    for x, y in process:
        print(f'{x} {y}')
else:
    target = p if len(p) > 0 else n
    target.sort(reverse=True)
    p_seed = target.pop(0)
    n_seed = target.pop(-1)
    while len(target) > 0:
        sub = target.pop(0)
        process.append((n_seed, sub))
        n_seed -= sub
    process.append((p_seed, n_seed))
    m = p_seed - n_seed
    print(m)
    for x, y in process:
        print(f'{x} {y}')

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(map(int,input().split()))[::-1]
B = A[:(N+1)//2]
i = (N+1)//2
if B[-1]<=0:
    for i in range(1,N+1//2):
        if B[i]<=0:
            B = B[:i]
            break
C = A[i:2*i][::-1]
D = A[2*i:]
ans = []
if (len(B)+len(C))%2:
    M = B.pop()
else:
    M = C.pop()
while B:
    if len(B)>len(C):
        p = B.pop()
    else:
        p = C.pop()
    ans.append((p,M))
    M = p - M
for d in D:
    ans.append((M,d))
    M = M - d
print(M)
for o in ans:
    print(*o)

Submission status: Runtime Error

Solution code:
import heapq
n=int(input())
Ans=[]
A=[int(i) for i in input().split()]
A.sort()
if A[0]>=0:
  Ans=[]
  heapq.heapify(A)
  for i in range(n-2):
    a1=heapq.heappop(A)
    a2=heapq.heappop(A)
    Ans.append([a1,a2])
    heapq.heappush(A,a1-a2)
  a1=heapq.heappop(A)
  a2=heapq.heappop(A)
  Ans.append([a2,a1])
  print(a2-a1)
  for i in range(n-1):
    print(*Ans[i])

elif A[-1]<=0:
  Ans=[]
  AA=[]
  for i in range(n):
    AA.append(-A[i])
  heapq.heapify(AA)
  for i in range(n-2):
    a1=heapq.heappop(AA)
    a2=heapq.heappop(AA)
    Ans.append([-a1,-a2])
    heapq.heappush(A,-(a2-a1))
  a1=heapq.heappop(A)
  a2=heapq.heappop(A)
  Ans.append([a2,-a1])
  print(a2+a1)
  for i in range(n-1):
    print(*Ans[i])
else:    
  Ap,Az,Am=[],[],[]
  for i in range(n):
    if A[i]>0:
      Ap.append(A[i])
    elif A[i]==0:
      Az.append(A[i])
    else:
      Am.append(-A[i])
  Ap.sort()
  Am.sort()
  pp,mm=Ap.pop(),Am.pop()
  heapq.heapify(Ap)
  heapq.heapify(Am)

  while len(Am)>=1:
    am=heapq.heappop(Am)
    Ans.append([pp,-am])
    pp+=am
  while len(Ap)>=1:
    ap=heapq.heappop(Ap)
    Ans.append([-mm,ap])
    mm+=ap
  for i in range(len(Az)):
    Ans.append([pp,0])
  Ans.append([pp,-mm])
  print(pp+mm)
  for i in range(len(Ans)):
    print(*Ans[i])

Submission status: Runtime Error

Solution code:
inpl = lambda: list(map(int,input().split()))

def output(plus,minus):
    print(sum(plus)-sum(minus))
    p = plus[0]
    for z in minus[1:]:
        print(p,z)
        p -= z
    q = minus[0]
    for w in plus[1:]:
        print(q,w)
        q -= w
    print(p,q)

N = int(input())
A = inpl()
plus = list(filter(lambda x: x>0, A))
minus = list(filter(lambda x: x<0, A))
Np = len(plus)
Nm = len(minus)
Nz = N-Np-Nm

if Np == N:
    plus.sort(reverse=True)
    minus = [plus.pop()]
elif Nm == N:
    minus.sort()
    plus = [minus.pop()]
else:
    if Nm == 0:
        minus = [0]*Nz
    else:
        plus.extend([0]*Nz)

output(plus,minus)

Submission status: Runtime Error

Solution code:
n = int(input())
x = list(map(int,input().split()))
#x = sorted(list(map(int,input().split())),reverse=True)
#nmax = max(x)
#ans = 2*nmax-sum(x)
lst = []


while len(x) > 2:
  mi = min(x)
  ma = max(x)
  if mi < 0:
    lst.append(str(ma)+" "+str(mi))
    del x[x.index(mi)]
    del x[x.index(ma)]
    x.append(ma - mi)
    lst.append(str(ma)+" "+str(mi))
  del x[x.index(mi)]
  del x[x.index(ma)]
  x.append(mi - ma)

print(max(x) - min(x))
for i in range(len(lst)):
  print(lst[i])
print(max(x),min(x))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
AM = A.pop(0)
xy = []
if AM > 0 and A[-1] >= 0:
    p = 1
elif AM <= 0 and A[-1] < 0:
    p = 2
else:
    p = 3
if p == 1:
    while(len(A)>1):
        x = A.pop(-1)
        y = A.pop(0)
        xy.append([x, y])
        A.append(x-y)
    xy.append([AM, A[0]])
    ans = AM - A[0]
if p == 2:
    while(len(A)>1):
        x = AM
        y = A.pop(-1)
        xy.append([x, y])
        AM -= y
    xy.append([AM, A[0]])
    ans = AM - A[0]
if p == 3:
    while(len(A)>1):
        y = A.pop(0)
        if y >= 0:
            x = A.pop(-1)
            xy.append([x, y])
            xy.append([x, y])
            A.append(x-y)
        else:
            x = AM
            xy.append([x, y])
            AM -= y
            xy.append([AM, A[0]])
            ans = AM - A[0]
print(ans)
for out in xy:
    print(*out)

Submission status: Runtime Error

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

aLength=len(listA)
bigL = listA[aLength//2:]
smallL = listA[:aLength//2]
current = 0
index = 0
smallI = 0
bigI = 0
answerL = []
if aLength%2== 0:
    while index < aLength:
        if index%2==0 and index!=0:
            if smallL[smallI]-current < current-smallL[smallI]:
                answerL.append(str(smallL[smallI])+" "+str(current))
                current=smallL[smallI]-current
            else:
                answerL.append(str(current)+" "+str(smallL[smallI]))
                current-=smallL[smallI]
            smallI+=1
        else:
            if bigL[bigI]-current > current-bigL[bigI]:
                answerL.append(str(bigL[bigI])+" "+str(current))
                current=bigL[bigI]-current
            else:
                answerL.append(str(current)+" "+str(bigL[bigI]))
                current-=bigL[bigI]
            bigI+=1
        index+=1
else:
    while index < aLength:
        if index % 2 == 1:
            if smallL[smallI]-current < current-smallL[smallI]:
                answerL.append(str(smallL[smallI])+" "+str(current))
                current=smallL[smallI]-current
            else:
                answerL.append(str(current)+" "+str(smallL[smallI]))
                current-=smallL[smallI]
            smallI+=1
        else:
            if bigL[bigI]-current > current-bigL[bigI] or index == 0:
                answerL.append(str(bigL[bigI])+" "+str(current))
                current = bigL[bigI]-current
            else:
                answerL.append(str(current)+" "+str(bigL[bigI]))
                current -= bigL[bigI]
            bigI += 1
        index += 1

print(current)
del answerL[0]
for ans in answerL:
    print(ans)

Submission status: Runtime Error

Solution code:
#

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    index = bisect.bisect_left(A, 0)
    minus = A[:index]
    plus = A[index:]
    # print(minus)
    # print(plus)
    progres = []
    ans = 0
    while True:
        if len(plus) == 1 and len(minus) == 1:
            p = plus.pop(0)
            m = minus.pop(0)
            progres.append((p, m))
            ans = p - m
            # print('{} {}'.format(p, m))
            break

        if len(plus) == 1 and len(minus) == 0:
            ans = plus[0]
            break
        if len(plus) == 0 and len(minus) == 1:
            ans = minus[0]
            break

        # まだ2つ以上入ってる
        if len(plus) == 0:
            val1 = minus.pop(0)
            val2 = minus.pop(1)
            new_val = val2 - val1
            plus.append(new_val)
            progres.append((val2, val1))
            # print('{} {}'.format(val1, val2))
            continue
        if len(minus) == 0:
            val1 = plus.pop(0)
            val2 = plus.pop(1)
            new_val = val2 - val1
            minus.append(new_val)
            progres.append((val2, val1))
            # print('{} {}'.format(val2, val1))
            continue

        if len(plus) > len(minus):
            val1 = plus.pop()
            val2 = minus.pop()
            new_val = val2 - val1
            index = bisect.bisect_left(minus, new_val)
            minus.insert(index, new_val)
            progres.append((val2, val1))
            # print('{} {}'.format(val2, val1))
        else:
            val1 = plus.pop()
            val2 = minus.pop()
            new_val = val1 - val2
            index = bisect.bisect_left(plus, new_val)
            plus.insert(index, new_val)
            progres.append((val1, val2))
            # print('{} {}'.format(val1, val2))

    print(ans)
    for p, q in progres:
        print('{} {}'.format(p, q))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()

for i in range(len(a)):
    a[i] = int(a[i])

x = 0
y = 0

operations = []

for i in range(n-2):
    a_sorted = sorted(a)
    x = a_sorted[0]
    y = a_sorted[1]

    a.remove(x)
    a.remove(y)
    a.append(x-y)
    operations.append(str(x) + " " + str(y))

a_sorted_reverse = sorted(a_sorted, reverse=True)
x = a_sorted_reverse[0]
a_sorted_reverse.remove(x)
y = sorted(a_sorted_reverse)[0]
operations.append(str(x) + " " + str(y))


a.remove(x)
if y in a:
    a.remove(y)
a.append(x-y)

print(sorted(a, reverse=True)[0])
for i in operations:
    print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
L = []
for i in range(n):
    x,y = map(int, input().split())
    L.append((x,y))

if n == 1:
    print(1)

else:

    L_sort = sorted(L)
    L_dict = {}
    for i in range(n-1):
        tmp0 = L_sort[i+1][0] - L_sort[i][0]
        tmp1 = L_sort[i+1][1] - L_sort[i][1]
        if (tmp0, tmp1) in L_dict:
            L_dict[(tmp0, tmp1)] += 1
        else:
            L_dict[(tmp0, tmp1)] = 1

    if max(L_dict.values()) == 1:
        print(n)
    else:
        ans = n - max(L_dict.values())
        print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
A.sort()
was = []
def x(A,N):
    newA = []
    sw = True
    for i in range(N//2):
        if sw:
            was.append((A[i], A[-(i+1)]))
            newA.append(A[i] - A[-(i+1)])
        else:
            was.append((A[-(i+1)], A[i]))
            newA.append(A[-(i+1)] - A[i])
        sw = not sw
    if N%2 != 0:
        newA.append(A[N//2])
    if len(newA) == 2:
        was.append((max(newA), min(newA)))
        return max(newA) - min(newA)
    else:
        newA.sort()
        return x(newA, len(newA))

print(x(A,N))
for x,y in was:
    print(x, y)

Submission status: Runtime Error

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

p = [i for i in a if i >= 0]
n = [i for i in a if i < 0]

process = []
if len(p) > 0 and len(n) > 0:
    p_seed = p.pop(0)
    n_seed = n.pop(0)
    while len(n) > 0:
        sub = n.pop(0)
        process.append((p_seed, sub))
        p_seed -= sub
    while len(p) > 0:
        sub = p.pop(0)
        process.append((n_seed, sub))
        n_seed -= sub
    process.append((p_seed, n_seed))
    print(p_seed - n_seed)
    for x, y in process:
        print(f'{x} {y}')
else:
    target = p if len(p) > 0 else n
    target.sort(reverse=True)
    p_seed = target.pop(0)
    n_seed = target.pop(-1)
    while len(target) > 0:
        sub = target.pop(0)
        process.append((n_seed, sub))
        n_seed -= sub
    process.append((p_seed, n_seed))
    print(p_seed - n_seed)
    for x, y in process:
        print(f'{x} {y}')

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()

for i in range(len(a)):
    a[i] = int(a[i])

x = 0
y = 0

operations = []

for i in range(n-2):
    a_sorted = sorted(a)
    x = a_sorted[0]
    y = a_sorted[1]

    a.remove(x)
    a.remove(y)
    a.append(x-y)
    operations.append(str(x) + " " + str(y))

a_sorted_reverse = sorted(a_sorted, reverse=True)
x = a_sorted_reverse[0]
a.remove(x)
y = sorted(a)[0]
operations.append(str(x) + " " + str(y))


a.remove(x)
if y in a:
    a.remove(y)
a.append(x-y)

print(sorted(a, reverse=True)[0])
for i in operations:
    print(i)

Submission status: Runtime Error

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

A.sort()
M = 0
for i in range(N-1):
    M += abs(A[i])
M += A[N-1]
print(M)

nminus = len([i for i in A if i < 0])
nplus = N - nminus

if nplus == 0:
    caled = A[N]
    for i in range(N-1):
        print(caled,A[i])
        caled -= A[i]
elif nminus == 0:
    caled = A[0]
    for i in range(N-2):
        print(caled, A[i+1])
        caled -= A[i+1]
    print(A[N-1],caled)
else:
    caled = A[0]
    for i in range(nplus-1):
        print(caled,A[nminus+i])
        caled -= A[nminus+i]
    caled_2 = A[N-1]
    for i in range(nminus-1):
        print(caled_2,A[i+1])
    print(caled_2,caled)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
# print(a)
a.sort()
a.reverse()
# print(a)
l1=[]
if n==2:
	print(a[0]-a[1])
	print(a[0],a[1])
else:
	for i in range(1,n):
		if a[i]<=0:
			a[0] = a[0] - a[i]
			l1.append((a[0],a[i]))
	if a[1]<=0:
		print(a[0])
		for i in range(n-2):
			print(l1[i][0],l1[i][1])
	else:
		for i in range(n-2):
			if a[n-1-i] > 0:
				l1.append((a[n-1-i],a[n-2-i]))
				a[n-2-i] = a[n-1-i]-a[n-2-i]
		ans = a[0]-a[1]
		print(ans)
		for i in range(n-2):
			print(l1[i][0],l1[i][1])
		print(a[0],a[1])

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()

for i in range(len(a)):
    a[i] = int(a[i])

x = 0
y = 0

operations = []

for i in range(n-2):
    a_sorted = sorted(a)
    x = a_sorted[0]
    y = a_sorted[1]

    a.remove(x)
    a.remove(y)
    a.append(x-y)
    operations.append(" ".join([str(x), str(y)]))

a_sorted_reverse = sorted(a_sorted, reverse=True)
x = a_sorted_reverse[0]
a_sorted_reverse.remove(x)

y = sorted(a_sorted_reverse)[0]

operations.append(" ".join([str(x), str(y)]))


a.remove(x)
if y in a:
    a.remove(y)
a.append(x-y)

print(sorted(a, reverse=True)[0])
for i in operations:
    print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
a = sorted([int(i) for i in input().split()])

if a[0] <= 0 and 0 < a[-1]:
    import bisect
    print(sum(map(lambda x: abs(x), a)))
    x0 = bisect.bisect_left(a,0)

    # 0以下から正をひいていく（一番大きいものを除く）
    print("{} {}".format(a[0], a[x0-1]))
    res = a[0] - a[x0+1]

    for x in a[x0+1:-2]:
        print("{} {}".format(res, x))
        res -= x

    # 一番大きいものから上のものと負の数を引いていく
    print("{} {}".format(a[-1], res))
    res = a[-1] - res
    for x in a[1:x0]:
        print("{} {}".format(res,x))
        res -= x

elif a[0] > 0:
    print(sum(a) - 2*a[0])

    print("{} {}".format(a[0],a[1]))
    res = a[0] - a[1]
    for x in a[2:-2]:
        print("{} {}".format(res, x))
        res -= x
    
    print("{} {}".format(a[-1], res))
else:
    print(-sum(a) + 2*a[-1])

    print("{} {}".format(a[-1],a[-2]))
    res = a[-1] - a[-2]
    for x in a[:-2]:
        print("{} {}".format(res, x))
        res -= x