Problem:


Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0



Sample input:
3
1 -1 2


Sample output:
4
-1 1
2 -2

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
#
# a.sort()
# ans = a[0]
# for i in range(1, n - 1):
#     ans += a[i] * (-1)
#
# print(a[-1] - ans)
# key = a[0]
# for i in range(1, n - 1):
#     print(key, a[i])
#     key = key - a[i]
# print(a[-1], key)

a.sort()
plusa = []
minusa = []
for i in a[1:-1]:
    if i < 0:
        minusa.append(i)
    else:
        plusa.append(i)
#
# print(minusa, plusa)
anslist = []
if len(plusa) == 0:
    plusa.append(minusa[-1])
    del minusa[-1]

for i in minusa:
    # print(plusa[0], i)
    anslist.append([plusa[0], i])
    plusa[0] -= i

for i in plusa:
    # print(a[0], i)
    anslist.append([a[0], i])
    a[0] -= i
# print(a[0])
print(a[-1] - a[0])
for i, j in anslist:
    print(i, j)
# print(anslist)
print(a[-1], a[0])

Submission status: Runtime Error

Solution code:
import sys
import collections

n = int(input())
a = [int(i) for i in input().split()]

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(posi) > 0:
    top = posi.pop(-1)
else:
    top = nega.pop(-1)
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:
#

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 = val1 - val2
            plus.append(new_val)
            progres.append((val1, val2))
            # 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=sorted(list(map(int,input().split())))
if a[n-1]<=0:
  print(2*a[n-1]-sum(a))
  s=a[n-1]
  for i in a[:-1]:
    print(s,i)
    s-=i
elif a[0]>=0:
  print(sum(a)-2*a[0])
  s=a[0]
  for i in a[1:]:
    print(i,s)
    s=i-s
else:
  m=0
  for i in n:
    if a[i]>=0:
      k=i
      break
    m-=a[i]
  print(m+sum(a[k:]))
  s=a[0]
  for i in a[k:-1]:
    print(s,i)
    s-=i
  print(a[-1],s)
  s=a[-1]-s
  for i in a[1:k]:
    print(s,i)
    s-=i

Submission status: Runtime Error

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


def answer(A, ans):
    ans.append([A[0], A[1]])
    A[0] = A[0]-A[1]
    del A[1]
    return A, ans

A = sorted(A)
print(A)

while True:
    A, ans = answer(A, ans)
    print(A)
    if len(A) == 2:
        M = A[1]-A[0]
        ans.append([A[1], A[0]])
        break

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

Submission status: Runtime Error

Solution code:
from collections import deque


N = int(input())
A = list(map(int, input().split()))
A.sort()
queue_plus = deque()
queue_minus = deque()
for A_i in A:
    if A_i < 0:
        queue_minus.append(A_i)
    else:
        queue_plus.append(A_i)

xy = [[] for _ in range(N-1)]
cnt = 0
if len(queue_plus) == 0:
    x = queue_minus.pop()
    y = queue_minus.pop()
    xy[cnt] = [x, y]
    cnt += 1
    queue_plus.append(x - y)
if len(queue_minus) == 0:
    x = queue_plus.popleft()
    y = queue_plus.popleft()
    xy[cnt] = [x, y]
    cnt += 1
    queue_minus.append(x - y)
while len(queue_plus) > 0 and len(queue_minus) > 1:
    x = queue_plus.pop()
    y = queue_minus.pop()
    xy[cnt] = [x, y]
    cnt += 1
    queue_plus.append(x - y)
while len(queue_plus) > 1 and len(queue_minus) > 0:
    x = queue_minus.pop()
    y = queue_plus.pop()
    xy[cnt] = [x, y]
    cnt += 1
    queue_minus.append(x - y)
x = queue_plus.pop()
y = queue_minus.pop()
xy[cnt] = [x, y]
ans = x - y
print(ans)
for x_y in xy:
    print(" ".join(map(str, x_y)))

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: 
        t = a[-1]
        for i in range(d-2):
            if i == 0:
                procedure.append([a[i],a[-2]])
                t += a[0]-a[-2]
            else:
                procedure.append([t,a[i]])
                t -= a[i]
        if not(d == 1 or d == 0):
            procedure.append([a[-1],t])
            t = a[-1] - t
    else:
        if not(d == 0 or d == 1):
            t = 0
            for i in range(d-1):
                if i == 0:
                    procedure.append([a[-1],a[i]])
                    t += a[-1]-a[i]
                else:
                    procedure.append([t,a[i]])
                    t -= a[i]
        else:
            t = a[-1]
    #print(t)
    procedure.append([t,temp])
    temp = t-temp
    return temp

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 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)
    while True:
        if len(plus) == 1 and len(minus) == 1:
            p = plus.pop(0)
            m = minus.pop(0)
            print('{} {}'.format(p, m))
            break

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

        # まだ2つ以上入ってる
        if len(plus) == 0:
            val1 = minus.pop(0)
            val2 = minus.pop(1)
            new_val = val1 - val2
            index = bisect.bisect_left(plus, new_val)
            plus.append(new_val)
            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)
            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)
            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)
            print('{} {}'.format(val1, val2))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np
m = int(input())
a = np.array([int(i) for i in input().split()])
a = -np.sort(-a)
b = np.array_split(a,2)

print(np.sum(b[0]) - np.sum(b[1]))
i = range(len(b[0]))
b = np.insert(b[1], i, b[0][i])

y = b[-1]
for i in range(len(b)-2, -1, -1):
    x = b[i]
    print("{} {}".format(x, y))
    y = x - y

Submission status: Runtime Error

Solution code:
N = int(input())
list1 = [int(i) for i in input().split()]

listans = []
def func1(list_n):
    if len(list1)== 1:
        print(list1[0])
        return(list1[0])
    elif len(list1)==2:
        a = (max(list1)-min(list1))
        listmed = [max(list1), min(list1)]
        listans.append(listmed)
        list1.remove(max(list1))
        list1.remove(min(list1))
        list1.append(a)
        return func1(list1)
    else:
        a = (min(list1)-max(list1))
        listmed = [min(list1), max(list1)]
        listans.append(listmed)
        list1.remove(max(list1))
        list1.remove(min(list1))
        list1.append(a)
        return func1(list1)

func1(list1)
for i in range(len(listans)):
    print(listans[i][0], listans[i][1])

Submission status: Runtime Error

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

B = A[:]
A_MAX = max(B)
B.remove(A_MAX)

ans = []
while len(B) > 1:
    B.sort()
    x = B.pop()
    y = B.pop()
    if y >= 0:
        idx = bisect(B, x - y)
        B = B[:idx] + [x - y] + B[idx:]
        ans.append([x, y])
    else:
        idx = bisect(B, y - x)
        B = B[:idx] + [y - x] + B[idx:]
        ans.append([y, x])
    
if B[0] <= 0:
    print(A_MAX - B[0])
    ans.append([A_MAX, B[0]])
else:
    print(B[0] - A_MAX)
    ans.append([B[0], A_MAX])
for i in range(N - 1):
    print(*ans[i])

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]))

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

Submission status: Runtime Error

Solution code:
import sys

N=input()
A=map(int, sys.stdin.readline().split())
A.sort()

L=[]
for i in range(N-1):
	if i==0:
		next=A[i]-A[i+1]
		L.append( (A[i], A[i+1]) )

	else:
		if i==N-2:
			L.append( (A[i+1],next) )
			ans=A[i+1]-next
		else:
			L.append( (next, A[i+1]) )
			next=next-A[i+1]
else:
	print ans
	for x, y in L:
		print x,y

Submission status: Runtime Error

Solution code:
import heapq
n=int(input())
A=[int(i) for i in input().split()]
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)
Ans=[]
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
Ans.append([pp,-mm])
print(pp+mm)
for i in range(len(Ans)):
  print(*Ans[i])

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]))

  
while len(A)>3:  
  a=min(A)
  b=max(A)
  print("{} {}".format(b,a))
  A.remove(a)
  A.remove(b)
  
  A.append(b-a)
  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 = list(map(int, input().split()))

a.sort()
if a[0] > 0:
    m = sum(a[1:]) - a[0]
    print(m)
    tmp = a[0]
    for i in range(1, n-1):
        print(tmp, a[i])
        tmp -= a[i]
    print(a[n-1], tmp)
elif a[-1] < 0:
    m = -sum(a[:-1]) + a[-1]
    print(m)
    tmp = a[n-1]
    for i in range(0, n-1, -1):
        print(tmp, a[i])
        tmp -= a[i]
else:
    m = 0
    pos = []
    neg = []
    for ai in a:
        m += abs(ai)
        if ai >= 0:
            pos.append(ai)
        else:
            neg.append(ai)
    print(m)
    tmp = 0
    ni = 0
    pi = 0

    if len(pos) > 1:
        print(neg[ni], pos[pi])  # pos > 1
        tmp = neg[ni] - pos[pi]
        ni += 1
        pi += 1

        while pi < len(pos) - 1:
            print(tmp, pos[pi])
            tmp = tmp - pos[pi]
            pi += 1
        print(pos[pi], tmp)
        tmp = pos[pi] - tmp
        while ni < len(neg):
            print(tmp, neg[ni])
            tmp -= neg
            ni += 1
    else:
        print(pos[pi] - neg[ni])
        tmp = pos[pi] - neg[ni]
        ni += 1
        while ni < len(neg):
            print(tmp, neg[ni])
            tmp -= neg
            ni += 1

Submission status: Runtime Error

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

'''
Anega = [a for a in A if a<0]
Azero = [a for a in A if a==0]
Aposi = [a for a in A if a>0]

if len(A) == len(Anega):
  print(-sum(A[:-1]) + A[-1])
  a = A[-1]
  for i in range(2,N+1):
    print(*(a, A[-i]))
    a = a - A[-i]
    
if len(A) == len(Aposi):
  print(sum(A[1:]) - A[0])
  a = A[0]
  for i in range(1,N-1):
    print(*(a, A[i]))
    a = a-A[i]
  print(*(A[-1],a))
  
if Azero:
  print(sum(Aposi)-sum(Anega))
  for _ in range(len(Azero)-1):
    print(*(0,0))
  a = 0
  for i in range(0,len(Aposi)-1):
    print(*(a, Aposi[i]))
    a = a-Aposi[i]
  print(*(Aposi[-1],a))
  a = Aposi[-1] -a
  
  for b in Anega:
    print(*(a,b))
    a = a-b
'''
        

      
      
Anega = [a for a in A if a<=0]
Aposi = [a for a in A if a>0]

if Anega:
  print(sum(Aposi)-sum(Anega))
  temp = Anega[-1]
  for a in Aposi[:-1]:
    print(*(temp,a))
    temp = temp-a
  print(*(Aposi[-1],temp))
  temp = Aposi[-1]-temp
  for a in Anega[:-1]:
    print(*(temp,a))
    temp = temp-a
    
  
else:
  print(sum(A[1:]) - A[0])
  a = A[0]
  for i in range(1,N-1):
    print(*(a, A[i]))
    a = a-A[i]
  print(*(A[-1],a))

Submission status: Runtime Error

Solution code:
def main():

    n = int(input())
    a = list(map(int, input().split()))
    ansList = []
    ans = -10000000000

    for i in range(n):
        tmpa = []
        for j in a:
            tmpa.append(j)
        tmpaPreMax = int(tmpa.pop(i))
        tmpAnsList = []
        while len(tmpa) > 1:
            tmpa.sort()
            tmpaMax = max(tmpa)
            tmpaMin = min(tmpa)
            tmpa.remove(tmpaMax)
            tmp.remove(tmpaMin)
            tmpAnsList.append([tmpaMin, tmpaMax])
            tmpa.append(tmpaMin - tmpaMax)

        tmpAnsList.append([tmpaPreMax, tmpa[0]])
        if ans < tmpaPreMax - tmpa[0]:
            ans = tmpaPreMax - tmpa[0]
            ansList = []
            for k in tmpAnsList:
                ansList.append(k)

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = input()
An = sorted(list(map(int, input().split(" "))))

max = An[-1]
del An[-1]

plus = []
minus = []
sousaList = []

for i in An:
    if i > 0:
        plus.append(i)
    else:
        minus.append(i)

while(1):
    if len(plus) == 1:
        break

    sousa = (plus[0], plus[-1])
    sousaList.append(sousa)
    plus[0] = plus[0] - plus[-1]
    del plus[-1]

if plus[0] > 0:
    sousa = (minus[0], plus[0])
    sousaList.append(sousa)
    minus[0] = minus[0] - plus[0]
else:
    minus.append(plus[0])
    

for i in minus:
    sousa = (max, i)
    sousaList.append(sousa)
    max -= i

print(max)

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

Submission status: Runtime Error

Solution code:
n = int(input())
b = list(map(int, input().split()))
b.sort()
if n == 2:
    print(b[-1]-b[0])
    print(b[-1], b[0])
    exit()
# 同符号
if b[0] > 0 and b[-1] > 0:
    ans = sum(b) - 2*b[0]
    print(ans)
    print(b[1], b[0])
    left = b[1] - b[0]
    right = b[-1]
    for i in range(2, n-1):
        x = left
        y = b[i]
        print(x, y)
        left = x - y
    print(right, left)
elif b[0] < 0 and b[-1] < 0:
    ans = -sum(b) + 2*b[-1]
    print(ans)
    b = b[::-1]
    print(b[0], b[1])
    left = 0
    right = b[0] - b[1]
    for i in range(2, n):
        x = right
        y = b[i]
        print(x, y)
        right = x - y
# 異符号
else:
    ans = abs(sum(abs(i) for i in b))
    print(ans)
    # 正,負がそれぞれ一つ以上ある
    # まず、正 - 負をして正を作って、負に引く
    now = 1
    left = b[0]
    right = b[-1]
    # 二個目以上の負を全部消化
    while b[now] <= 0:
        x = right
        y = b[now]
        print(x, y)
        right = x - y
        now += 1
    for i in range(now, n-1):
        x = left
        y = b[i]
        print(x, y)
        left = x - y
    print(right, left)

Submission status: Runtime Error

Solution code:
n = int(input())

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

# for i in range(n-1):

tmp = 0
history = []
for i in reversed(range(n - 1)):
    if i != 0:
        minNum = x.pop(x.index(min(x)))
        maxNum = x.pop(x.index(max(x)))
        tmp += minNum - maxNum
        history.append([minNum, maxNum])
    else:
        print(min(x) - tmp)
        for i in history:
            print(i[0], i[1])
        print(min(x), tmp)

Submission status: Runtime Error

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

li.sort()

if li[0] > 0:
    print(sum(li)-2*li[0])
    for i in range(n-2):
        print(li[0],li[i+1])
        li[0] -= li[i+1]
    print(li[n-1],li[0])
elif li[n-1] < 0:
    print(abs(sum(li))+2*li[n-1])
    for i in range(n-1):
        print(li[n-1],li[i])
        li[n-1] -= li[i]
else:
    for i in range(n):
        if li[i] > 0:
            a = i-1
            break
    print(abs(sum(li[:a+1])) + sum(li[a+1:]))
    for i in range(n-a-2):
        print(li[0],li[a+1+i])
        li[0] -= li[a+1+i]
    for i in range(a+1):
        print(li[n-1],li[i])
        li[n-1] -= li[i]

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
B = [abs(a) for a in A]

for i in range(N):
    if A[i] > 0:
        po = i
        break
else:
    po = -1
for i in range(N):
    if A[i] < 0:
        ne = i
        break
else:
    ne = -1
for i in range(N):
    if A[i] == 0:
        ze = i
        break
else:
    ze = -1
mi = min(B)
for i in range(N):
    if B[i] == mi:
        mii = i
        break
else:
    mii = -1

def aaa(i, j):
    t1, t2 = A[i], A[j]
    if t1 > t2:
        t1, t2 = t2, t1
    del A[t2]
    del A[t1]
    A = [t1, t2] + A
    
if po >= 0 and ne >= 0:
    aaa(po, ne)
else:
    print(sum(B) - 2 * mi)
    del A[mii]
    A = [mi] + A
# print(A)
prev = A[0]
for i in range(N-1):
    if i == N-2 or A[i+2] < 0:
        print(max(prev, A[i+1]), min(prev, A[i+1]))
        prev = max(prev, A[i+1]) - min(prev, A[i+1])
    else:
        print(min(prev, A[i+1]), max(prev, A[i+1]))
        prev = min(prev, A[i+1]) - max(prev, A[i+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]))

  
while len(A)!=3:  
  a=A[0]
  b=A[len(A)-1]
  print("{} {}".format(b,a))
  del A[0]
  del A[len(A)-1]
  A.append(b-a)
print("{} {}".format(A[0],A[1]))
print("{} {}".format(A[2],A[0]-A[1]))

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())
A = [int(i) for i in input().split()]
p = deque()
m = deque()

A.sort(reverse=True)
for i in A:
    if i > 0:
        p.append(i)
    else:
        m.append(i)

ans = []
for _ in range(N-2):
    if p and m:
        if len(p) < len(m):
            tt = p.popleft()
            zz = m.pop()
            p.appendleft(tt-zz)
            ans.append([tt,zz])
        else:
            tt = m.pop()
            zz = p.popleft()
            m.append(tt-zz)
            ans.append([tt,zz])
    elif p:
        tt = p.pop()
        zz = p.popleft()
        m.append(tt-zz)
        ans.append([tt,zz])

    else:
        tt = m.popleft()
        zz = m.pop()
        p.append(tt-zz)
        ans.append([tt,zz])

if p and m:
    tt = p.pop()
    zz = m.pop()
    ans.append([tt,zz])
    print(tt-zz)
else:
    tt = p.popleft()
    zz = p.pop()
    ans.append([tt,zz])
    print(tt-zz)

for i in range(N-1):
    tt,zz = ans[i]
    print(tt,zz)

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,k = LI()
    print(n%k)
    return

#B
def B():

    n = I()
    po = LIR(n)
    return

#C
def C():
    n = I()
    a = LI()
    a.sort()
    if a[0] >= 0:
        print(sum(a)-2*a[0])
        for i in range(1,n-1):
            print(a[0],a[i])
            a[0] -= a[i]
        print(a[n-1],a[0])
    else:
        if a[-1] < 0:
            print(2*a[-1]-sum(a))
            for i in range(n-1):
                print(a[-1],a[i])
                a[-1] -= a[i]
        else:
            m = bisect.bisect_left(a,0)
            p = n-m
            ans = -sum(a[:m])+sum(a[m:])
            print(ans)
            if p > m:
                k = a[m-1]-a[m]
                print(a[m-1],a[m])
                for i in range(m-1):
                    print(a[m+i],k)
                    k = a[m+i]-k
                    print(a[m-i-1],k)
                    k = a[m-i-1]-k
                for i in range(p-m-1):
                    print(k,a[p-m+i])
                    k -= a[p-m+i]
                print(a[-1],k)
            else:
                k = a[m]-a[m-1]
                print(a[m],a[m-1])
                for i in range(p-1):
                    print(a[m-i-1],k)
                    k = a[m-i-1]-k
                    print(a[m+i],k)
                    k = a[m+i]-k
                for i in range(m-p):
                    print(k,a[i])
                    k -= a[i]
    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#G
def G():
    n = I()

    return

#H
def H():
    n = I()

    return

#I
def I_():
    n = I()

    return

#J
def J():
    n = I()

    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Runtime Error

Solution code:
from copy import deepcopy
N=int(input())
A=list(map(int,input().split()))
K=deepcopy(A)
A.sort()
ans=10**(-20)
S=sum(A)
for i in range(N-1):
    S=S-A[i]+(-A[i])
    if ans<S:
        ans=S
        j=i
print(ans)
for i in range(j+1,N-1):
    print(A[0],A[i])
    A[0]-=A[i]
print(A[N-1],A[0])

Submission status: Runtime Error

Solution code:
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N = n()
PN = N // 2
if N % 2 == 1:
    PN += 1

As = sorted(nl(), reverse=True)
M = sum(As[:PN]) - sum(As[PN:])
print(M)

p_i = 0
n_i = PN
if N % 2 == 0:
    print('{} {}'.format(str(As[p_i]), str(As[n_i])))
    current = As[p_i] - As[n_i]
    next_positive = False
else:
    print('{} {}'.format(str(As[n_i]), str(As[p_i])))
    current = As[n_i] - As[p_i]
    next_positive = True

for i in range(N-2):
    if next_positive:
        p_i += 1
        print('{} {}'.format(str(As[p_i]), current))
        current = As[p_i] - current
    else:
        n_i += 1
        print('{} {}'.format(str(As[n_i]), current))
        current = As[n_i] - current
    next_positive != next_positive

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(f"{x} {y}")
a_sorted = sorted(a)
a_sorted_reverse = sorted(a_sorted, reverse=True)
x = a_sorted_reverse[0]
a_sorted_reverse.remove(x)
a_sorted = sorted(a_sorted_reverse)
y = a_sorted[0]
operations.append(f"{x} {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()))

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

ans = []
if len(n) == 0:
    p.sort()
    x = p.pop(0)
    y = p.pop()
    ans.append('{} {}'.format(x,y))
    n.append(x-y)
if len(p) == 0:
    n.sort()
    x = n.pop()
    y = n.pop(0)
    ans.append('{} {}'.format(x,y))
    p.append(x-y)

x = n.pop()
while len(p)>1:
    y = p.pop()
    ans.append('{} {}'.format(x,y))
    x -= y
y = x
x = p[0]
ans.append('{} {}'.format(x,y))
x -= y
while n:
    y = n.pop()
    ans.append('{} {}'.format(x,y))
    x -= y

print(x)
for s in ans:
    print(s)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    xy = ii2nnn(N)
    dicx = defaultdict(lambda: 0)
    xy.sort(key=lambda v: v[1])
    xy.sort(key=lambda v: v[0])
    for i in range(1, len(xy)):
        dx = xy[i][0] - xy[i-1][0]
        dy = xy[i][1] - xy[i-1][1]
        if dx == 0 and dy == 0:
            continue
        key = '%d %d' % (dx, dy)
        dicx[key] += 1
    dicy = defaultdict(lambda: 0)
    xy.sort(key=lambda v: v[1])
    for i in range(1, len(xy)):
        dx = xy[i][0] - xy[i-1][0]
        dy = xy[i][1] - xy[i-1][1]
        if dx == 0 and dy == 0:
            continue
        key = '%d %d' % (dx, dy)
        dicy[key] += 1
    nm = 0
    for v in dicx.values():
        if nm < v:
            nm = v
    for v in dicy.values():
        if nm < v:
            nm = v
    print(N - nm)

main()

Submission status: Runtime Error

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

min_idx = 0
max_idx = N-1

ans = []

if N % 2 == 0:
    last = A[max_idx] -A[min_idx]
    ans.append([A[max_idx], A[min_idx]])
    max_idx -= 1
    min_idx += 1
    N -= 1
else:
    last = A[max_idx]
    max_idx -= 1

for i in range(0, N-1, 2):
    ans.append([A[min_idx], last])
    last = A[min_idx] - last
    min_idx += 1
    ans.append([A[max_idx], last])
    last = A[max_idx] - last
    max_idx += 1

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

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, Counter
import bisect

sys.setrecursionlimit(10000000)

n = SI()
xy = [(0,0) for _ in range(n)]
div = []
for i in range(n):
    x, y = LI()
    xy[i] = (x, y)
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        else:
            div.append("{}_{}".format(xy[i][0] - xy[j][0], xy[i][1]- xy[j][1]))
c = Counter(div)
max_ = c.most_common()[0][1]
print(n - max_)