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()))
plus = 0
for i in range(n):
  plus += ( 2 * int( a[i] > 0 ) - 1 )
score = 0
for i in range(n):
  score += abs(a[i])
if plus != n:
  print(score)
  a.sort()
  if plus >=0:
    for k in range(plus):
      print(a[0],a[-1])
      a[0] -= a[-1]
      del a[-1]
    half = ( n - plus ) // 2
    if half != 0:
      for k in range(half-1):
        print(a[k],a[-1])
        a[k] -= a[-1]
        del a[-1]
      for k in range(half):
        print(a[-1],a[0])
        a[-1] -= a[0]
        del a[0]
  else:
    plus *= -1
    for k in range(plus):
      print(a[-1],a[0])
      a[-1] -= a[0]
      del a[0]
    half = ( n - plus ) // 2
    if half != 0:
      for k in range(half-1):
        print(a[k],a[-1])
        a[k] -= a[-1]
        del a[-1]
      for k in range(half):
        print(a[-1],a[0])
        a[-1] -= a[0]
        del a[0]
else:
  unchi

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()
            bs.insort(a,zz-z)
            aa.append([zz,z])
        else:
            z = a.pop()
            zz = a.popleft()
            bs.insort(a,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()
        bs.insort(a,z-zz)
        aa.append([z,zz])
        co += 1
    else:
        z = a.pop()
        zz = a.popleft()
        bs.insort(a,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:
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 xmy >= 0:
            pos.append(xmy)
        else:
            neg.appendleft(xmy)
        
        if len(list(pos) + list(neg)) == 1:
            print((list(pos) + list(neg))[0])
            break
    
    [print(i) for i in ops]

if '__main__' == __name__:
    resolve()

Submission status: Runtime Error

Solution code:
import bisect
N = int(input())
A = list(map(int, input().split()))
A.sort()
x = [0]*(N-1)
y = [0]*(N-1)
start_bool = 1 if N % 2 == 0 else -1
for i in range(N-1):
    print(A)
    l = A.pop(0)
    r = A.pop(-1)
    if start_bool < 0:
        if l > r:
            ret = r - l
            x[i] = r
            y[i] = l
        else:
            ret = l - r
            x[i] = l
            y[i] = r
    else:
        if l > r:
            ret = l - r
            x[i] = l
            y[i] = r
        else:
            ret = r - l
            x[i] = r
            y[i] = l
    if ret > 0:
        bisect.insort(A, ret)
    else:
        bisect.insort(A, ret)
    start_bool *= -1
print(A[0])
for i in range(N-1):
    print(x[i], y[i])

Submission status: Runtime Error

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

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

b_l = a_l[:k+1]
a_l = a_l[k+1:]
fu = len(b_l)
sei = len(a_l)
ans_l = list()
ans_ll = list()
if sei > fu:
  sa = sei-fu-1
  ans_ll+= 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])
  for i in range(sei-1):
    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] = ansll[-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())
x = list(map(int,input().split()))
#x = sorted(list(map(int,input().split())),reverse=True)
#nmax = max(x)
#ans = 2*nmax-sum(x)
lst = []

  
nmi = min(x)
nma = max(x)
del x[x.index(nmi)]
del x[x.index(nma)]

while max(x) > -1:
  mi = min(x)
  ma = max(x)
  lst.append(str(mi)+" "+str(ma))
  del x[x.index(mi)]
  del x[x.index(ma)]
  x.append(mi - ma)

for i in range(len(x)):
  lst.append(nma+" "+i)
  nma += i
print(nmi - nma)
for i in range(len(lst)):
  print(lst[i])
print(nmi,nma)

Submission status: Runtime Error

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

saidainosuuji = A[-1]
saishounosuuji = A[0]
if len(A) <= 2:
    A = []
else:
    A = A[1:-1]

fuidx = 0
while A[fuidx] < 0:
    fuidx += 1

if 0 < fuidx <= len(A):
    fu = A[:fuidx]
else:
    fu = []
if fuidx < len(A):
    sei = A[fuidx:]
else:
    sei = []

sousa = []
# 最大値をつくる
for v in fu:
    sousa.append((saidainosuuji, v))
    saidainosuuji = saidainosuuji - v

# 最小値を作る
for v in sei:
    sousa.append((saishounosuuji, v))
    saishounosuuji = saishounosuuji - v

# 最後の値
sousa.append((saidainosuuji, saishounosuuji))
ans = saidainosuuji - saishounosuuji

print(ans)
for s in sousa:
    print(s[0], s[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[-2:-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 = [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:
    for i in range(len(Aplus) - 1):
        res = sum(Aplus) - sum(Amainasu)
        result.append((Amainasu[0],Aplus[i]))
        Amainasu[0] -= Aplus[i]
    for i in range(len(Amainasu)):
        result.append((Aplus[-1],Amainasu[i]))
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:
M = int(input())
A = list(map(int, input().split()))
A.sort()

saidainosuuji = A[-1]
saishounosuuji = A[0]
A = A[1:-1]

fuidx = 0
while A[fuidx] < 0:
    fuidx += 1

if 0 < fuidx <= len(A):
    fu = A[:fuidx]
else:
    fu = []
if fuidx < len(A):
    sei = A[fuidx:]
else:
    sei = []

sousa = []
# 最大値をつくる
for v in fu:
    sousa.append((saidainosuuji, v))
    saidainosuuji = saidainosuuji - v

# 最小値を作る
for v in sei:
    sousa.append((saishounosuuji, v))
    saishounosuuji = saishounosuuji - v

# 最後の値
sousa.append((saidainosuuji, saishounosuuji))
ans = saidainosuuji - saishounosuuji

print(ans)
for s in sousa:
    print(s[0], s[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:
N = int(input())
A = list(map(int,input().split()))
a = 0
if N%2 == 1:
  a = max(A)
  A.remove(a)
b = max(A)
A.remove(b)
c = 0
ans = [[A[0],b]]
com = A[0]-b
c += com
for i in range(1,(N//2)*2-2):
  ans.append(c,[A[i]])
  com = c-A[i] 
  c += com
print(a-c)
print(ans[0][0],ans[0][1])
for j in range((N//2)*2-2):
  print(ans[j][0],ans[j][1])
if N%2 == 1:
  print(a,c)

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 xmy >= 0:
            pos.append(xmy)
        else:
            neg.appendleft(xmy)
        
        if len(pos + neg) == 1:
            print((pos + neg)[0])
            break
    
    [print(i) for i in ops]

if '__main__' == __name__:
    resolve()

Submission status: Runtime Error

Solution code:
n = int(input())
lis = list(map(int,input().split()))
lis.sort(reverse = True)
for i in range(n):
  if lis[i] < 0:
    lia = lis[:i]
    lib = lis[i:]
    break
  if i == n-1:
    lia = lis
    lib = []
num = 0
m = len(lia)
if m > 1:
  if m % 2 == 0:
    print(abs(sum(lia[:m//2]))-abs(sum(lia[m//2:])))-sum(lib)
    print(lia[0],lia[-1])
    num = lia[0]-lia[-1]
    for i in range(1,m//2):
      print(lia[-(i+1)],num)
      num = lia[-(i+1)]-num
      print(lia[i],num)
      num = lia[i]-num

  else:
    print(sum(lia[:m//2])-sum(lia[m//2+1:])+abs(lia[m//2]))-sum(lib)
    print(lia[-1],lia[0])
    num = lia[-1]-lia[0]
    for i in range(1,m//2):
      print(lia[i],num)
      num = lia[i]-num
      print(lia[-(i+1)],num)
      num = lia[-(i+1)]-num     
    print(lia[m//2],num)
elif m == 1:
  print(lia[0] -sum(lib))
  print(lia[0],lib[0])
  num = lia[0]-lib[0]
else:
  print(-sum(lib)+lib[0] * 2)
  num = lib[0]
for i in range(len(lib)-1):
  print(num,lib[i+1])
  num -= lib[i+1]

Submission status: Runtime Error

Solution code:
#

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

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

        if len(plus) == 1 and len(minus) == 0:
            ans = plus.pop()
            break
        if len(plus) == 0 and len(minus) == 1:
            ans = minus.pop()
            break

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

        if len(minus) == 0:
            val1 = plus.popleft()
            val2 = plus.pop()
            new_val = val1 - val2
            minus.append(new_val)
            progres.append((val1, val2))
            # print('{} {}'.format(val2, val1))
            continue

        if len(plus) > len(minus):
            val1 = plus.pop()
            val2 = minus.popleft()
            new_val = val2 - val1
            progres.append((val2, val1))

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

            index = bisect.bisect_left(plus, new_val)
            plus.insert(index, new_val)
            # 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:
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
    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])

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
ans = []
a.append(0)
a.sort()

if a.index(0) == 0:
    x = a.pop(1)
    y = a.pop()
    tmp = x-y
    ans.append([x,y])
    a.append(tmp)
    a.sort()
elif a.index(0) == n-1:
    x = a.pop(-2)
    y = a.pop(0)
    tmp = x-y
    ans.append([x,y])
    a.append(tmp)
    a.sort()

a.remove(0)
if len(a) == 1:
    print(a[0])
    print(ans[0][0], ans[0][1])
else:
    maximum = a.pop()
    minimum = a.pop(0)
    while len(a) > 0:
        x = a.pop()
        if x > 0:
            ans.append([minimum, x])
            minimum -= x
        else:
            ans.append([maximum, x])
            maximum -= x

    m = maximum-minimum
    print(m)
    ans.append([maximum, minimum])
    for i in ans:
        print(i[0], i[1])

Submission status: Runtime Error

Solution code:
from collections import deque
plusQ = deque([]) 
minsQ = deque([])

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

for itr, val in enumerate(A):
  if itr + 1 <= (len(A) + 1) // 2 and val > 0:
    plusQ.append(val)
  else:
    minsQ.append(val)

# print(plusQ, minsQ)
p1 = plusQ.popleft()

ans = deque([])
while len(minsQ) > 0:
  if len(plusQ) > 0:
    p, m = plusQ.popleft(), minsQ.popleft()
    ans.append([m, p])
    minsQ.append(m - p)    
  else:
    m = minsQ.popleft()
    ans.append([p1, m])
    p1 = p1 - m

print(p1)
while len(ans) > 0:
  hoge = ans.popleft()
  print(hoge[0],hoge[1])

Submission status: Runtime Error

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

BB = A[:]


def dfs(BB, ope):
    if len(BB) == 1:
        return BB[0], ope
    
    ret_list = []
    for i, x in enumerate(BB):
        for j, y in enumerate(BB[i+1:]):
            ret_list.append(dfs(BB[:i]+BB[i+1:i+1+j]+BB[i+1+j+1:]+[x-y], ope[:] + [(x, y)]))
            ret_list.append(dfs(BB[:i]+BB[i+1:i+1+j]+BB[i+1+j+1:]+[y-x], ope[:] + [(y, x)]))
    return max(ret_list)

ans = dfs(BB, [])

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

Submission status: Runtime Error

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

info.sort()
a=n//2
for i in range(a):
    info[i]=0-info[i]
    
print(sum(info))

k=info[0]

for i in range(n-1):
    if i%2==0:
        print("{} {}".format(info[i+a],k))
        k=k-info[i+a]
        
    else:
        print("{} {}".format(info[i],k))
        k=k+info[i]

Submission status: Runtime Error

Solution code:
n=int(input())
lst=[int(i) for i in input().split()]
lst.sort()
cnt=0
for i in range(n):
    if lst[i]>0:
        break
    if lst[i]<0:
        cnt+=1
ans=0
for i in lst:
    if i<0:
        ans-=i
    else:
        ans+=i
if cnt!=n and cnt!=0:
    print(ans)
    if n>2:
        for i in range(cnt+1,n):
            if i==cnt+1:
                print(lst[0],lst[i])
                res=lst[0]-lst[i]
            else:
                print(res,lst[i])
                res-=lst[i]
        if cnt>1:
            for i in range(1,cnt):
                print(res,lst[i])
                res-=lst[i]
        print(lst[cnt],res)
    else:
        print(lst[1],lst[0])
elif cnt==n:
    ans=ans+2*lst[-1]
    print(ans)
    for i in range(n-1):
        if i==0:
            print(lst[-1],lst[i])
            res=lst[-1]-lst[i]
        else:
            print(res,lst[i])
            res-=lst[i]
else:
    ans=ans-2*lst[0]
    print(ans)
    if n>2:
        for i in range(1,n-1):
            if i==1:
                print(lst[0],lst[i])
                res=lst[0]-lst[i]
            else:
                print(res,lst[i])
                res-=lst[i]
        print(lst[-1],res)
    else:
        print(lst[1],lst[0])

Submission status: Runtime Error

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

saidainosuuji = A[-1]
saishounosuuji = A[0]
A = A[1:-1]

fuidx = 0
while A[fuidx] < 0:
    fuidx += 1

fu = A[:fuidx]
if fuidx < len(A):
    sei = A[fuidx:]
else:
    sei = []

sousa = []
# 最大値をつくる
for v in fu:
    sousa.append((saidainosuuji, v))
    saidainosuuji = saidainosuuji - v

# 最小値を作る
for v in sei:
    sousa.append((saishounosuuji, v))
    saishounosuuji = saishounosuuji - v

# 最後の値
sousa.append((saidainosuuji, saishounosuuji))
ans = saidainosuuji - saishounosuuji

print(ans)
for s in sousa:
    print(s[0], s[1])

Submission status: Runtime Error

Solution code:
l = sorted(list(map(int,input().split())))
ans = l[0]
ans_list = []
for i in range(1,n):
    if i == n-1:
        ans_list.append(str(l[i])+' '+str(ans))
        ans = l[n-1] - ans
        break
    ans_list.append(str(ans)+' '+str(l[i]))
    ans -= l[i]

print(ans)
for item in ans_list:
    print(item)

Submission status: Runtime Error

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

saidainosuuji = A[-1]
saishounosuuji = A[0]
A = A[1:-1]

fuidx = 0
while A[fuidx] < 0:
    fuidx += 1

fu = A[:fuidx]
sei = A[fuidx:]

sousa = []
# 最大値をつくる
for v in fu:
    sousa.append((saidainosuuji, v))
    saidainosuuji = saidainosuuji - v

# 最小値を作る
for v in sei:
    sousa.append((saishounosuuji, v))
    saishounosuuji = saishounosuuji - v

# 最後の値
sousa.append((saidainosuuji, saishounosuuji))
ans = saidainosuuji - saishounosuuji

print(ans)
for s in sousa:
    print(s[0], s[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:]
    if N % 2 == 1:
        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
    else:
        operations.append((pos[0], neg[0]))
        tmp = pos[0] - neg[0]
        for i in range(1, N//2):
            operations.append((neg[i], tmp))
            tmp = neg[i] - tmp
            operations.append((pos[i], tmp))
            tmp = pos[i] - tmp

print(tmp)
for o in operations:
    print("{} {}".format(o[0], o[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
result = []
res = 0
if len(Aplus) >= 1 and len(Amainasu) >= 1:
    for i in range(len(Aplus) - 1):
        res = sum(Aplus) - sum(Amainasu)
        result.append((Amainasu[0],Aplus[i]))
        Amainasu[0] -= Aplus[i]
    for i in range(len(Amainasu)):
        result.append((Aplus[-1],Amainasu[i]))
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)
        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:
n=int(input())
info=list(map(int,input().split()))

info.sort()
a=n//2
for i in range(a):
    info[i]=0-info[i]
    
print(sum(info))
k=info[i]
for i in range(n-1):
    if i%2==0:
        print("{} {}".format(info[i+a],k))
        k=k-info[i+a]
        
    else:
        print("{} {}".format(info[i],k))
        k=k-info[i]

Submission status: Runtime Error

Solution code:
from sys import stdin

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

    A.sort()
    xy = []
    for i in range(N - 2):
        xy.append((A[i], A[i+1]))
        n = A[i] - A[i+1]
    xy.append((A[-1], n))

    print(xy[-1][0] - xy[-1][1])
    for x, y in xy:
        print(x, y)

input = lambda: stdin.readline()
main()

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
ans = []
a.append(0)
a.sort()

if a.index(0) == 0:
    x = a.pop(1)
    y = a.pop()
    tmp = x-y
    ans.append([x,y])
    a.append(tmp)
    a.sort()
elif a.index(0) == n-1:
    x = a.pop(-2)
    y = a.pop(0)
    tmp = x-y
    ans.append([x,y])
    a.append(tmp)
    a.sort()

a.remove(0)

maximum = a.pop()
minimum = a.pop(0)
while len(a) > 0:
    x = a.pop()
    if x > 0:
        ans.append([minimum, x])
        minimum -= x
    else:
        ans.append([maximum, x])
        maximum -= x

m = maximum-minimum
print(m)
ans.append([maximum, minimum])
for i in ans:
    print(i[0], i[1])

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
p = []
m = []
for a in A:
    if a < 0:
        m.append(a)
    else:
        p.append(a)
was = []
if N == 2:
    print(max(A)-min(A))
    print(max(A), min(A))
    quit()
p.sort()
if len(m) == 0:
    m.append(p[-1] - p[0])
    was.append((p[-1], p[0]))
    p = p[1:-1]

mm = m[0]
m = m[1:]
pp = p[0]
p = p[1:]
for x in p:
    was.append((mm,x))
    mm -= x
for x in m:
    was.append((pp,x))
    pp -= x
was.append((pp, mm))
print(pp-mm)
for p,m in was:
    print(p,m)

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]
    if n!=2:
        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]
    print(s[0]-sum(s[1:]))
    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:
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:
    for i in range(len(Aplus) - 1):
        result.append((Amainasu[0],Aplus[i]))
        Amainasu[0] -= Aplus[i]
    for i in range(len(Amainasu)):
        result.append((Aplus[-1],Amainasu[i]))
    res = sum(Aplus) - sum(Amainasu)
elif len(Aplus) == 0 and len(Amainasu) >= 1:
    hoge = Amainasu[-1]
    for i in range(len(Amainasu) - 1):
        result.append((hoge,Amainasu[i]))
        hoge -= Amainasu[i]
    res = sum(Amainasu) * -1 + max(Amainasu) * 2
elif len(Aplus) >= 1 and len(Amainasu) == 0:
    if len(Aplus) >= 3:
        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]))
        res = sum(Aplus) - min(Aplus)
    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]))