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: Wrong Answer

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    a.sort()

    X, Y = [], []
    while len(a) > 2:
        ai = a.pop()
        X.append(a[0]), Y.append(ai)
        a[0] -= ai
    X.append(a[1]), Y.append(a[0])
    print(a[1] - a[0])
    for x, y in zip(X, Y):
        print(x, y)


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


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


for i in range(N-2):
    A, ans = answer(A, ans)

M = A[1] - A[0]
ans.append([A[1], A[0]])

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

Submission status: Wrong Answer

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

A_MAX = max(B)
B.remove(A_MAX)
B.sort()

ans = []
while len(B) > 1:
    x = B.pop()
    y = B.pop()
    if y >= 0:
        B.append(x - y)
        ans.append([x, y])
    else:
        B.append(y - x)
        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: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
hidari = []
migi = []
clock = 0
if len(a) % 2 == 0:
    clock = 1 #長さが偶数
else:
    clock = 0

while(len(a) != 1):
    if clock == 1:
        mx =  max(a) 
        mn =  min(a)
        a.remove(mx)
        a.remove(mn)
        a.append(mx-mn)
        hidari.append(mx)
        migi.append(mn)
        clock = 0
    else:
        mx =  max(a) 
        mn =  min(a)
        a.remove(mn)
        a.remove(mx)
        a.append(mn-mx)
        hidari.append(mn)
        migi.append(mx)
        clock = 1

print(a[0])
for i in range(len(hidari)):
    print(hidari[i], migi[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
ai = [int(x) for x in input().split()]

print(-4)
print(-1, 1)
print(-2, 2)

Submission status: Wrong Answer

Solution code:
N = int(input())
tmp = input().split(" ")

A = []
DP = []

for n in range(N):
    A.append(int(tmp[n]))
    DP.append([0,0])

A.sort()

DP[0][0] = A[0]
DP[0][1] = A[0]
for n in range(1,N):
    p0 = DP[n-1][0] - A[n]
    p1 = A[n] - DP[n-1][0]
    p2 = DP[n-1][1] - A[n]
    p3 = A[n] - DP[n-1][1]

    DP[n][0] = max(p0,p1,p2,p3)
    DP[n][1] = min(p0,p1,p2,p3)

print(DP[N-1][0])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
ap = [int(i) for i in a if int(i) >= 0]
an = [int(i) for i in a if int(i) < 0 ]
tmp = 0
if len(ap) > 0 and len(an) > 0:
    print(sum(ap) - sum(an))
    a.sort(reverse = True)
    m = an.pop(0)
    for i in range(len(ap)):
        if i != len(ap)-1:
            print(m,ap[i])
        else:
            print(ap[i],m)
        m = m - ap[i]
    if len(an) > 0:
        for i in range(len(an)-1):
            if i != len(an)-1:
                print(m,an[i])
            else:
                print(an[i],m)
            m = m - an[i]
elif len(ap) > 0:
    print(sum(ap)- 2* min(ap))
    ap.sort()
    mx = ap.pop(len(ap)-1)
    mn = ap.pop(0)
    if len(ap) > 0:
        for i in range(n-2):
            print(mn,ap[i])
            mn = mn - ap[i]
    print(mx,mn)
else:
    #len(ap) == 0
    an.sort(reverse = True)
    m = an.pop(0)
    print(m - sum(an))
    for i in range(n-1):
        if i == 0:
            print(m,an[i])
            tmp = m - an[i]
        else:
            print(tmp,an[i])
            tmp = tmp - an[i]

Submission status: Wrong Answer

Solution code:
N=int(input())
li=list(map(int,input().split()))
plus=[] 
minus=[]
for i in range(0,N):
  if li[i]>0:
    plus.append(li[i])
  else:
    minus.append(li[i])
    
plus.sort()
minus.sort()
#print(plus)
if len(minus)==0:
  M=sum(plus)-2*plus[0]
  print(M)
  a=plus[1]-plus[0]
  aa=0
  print(plus[1],plus[0])
  for i in range(0,N-3):
    aa=a
    a=plus[i+2]-a
    print(aa,plus[i+2])
  print(plus[N-1],aa)
    
    
    
  
elif len(plus)==0:
  M=-sum(minus)+2*minus[len(minus)-1]
  print(M)
  aa=0
  a=minus[N-1]-minus[N-2]
  print(minus[N-1],minus[N-2])
  for i in range(0,N-2):
    aa=a
    a=-minus[N-i-3]+a
    print(minus[N-i-3],aa)
    
  
  
elif len(plus) ==0:
  M=sum(plus)-sum(minus)
  print(M)
  for i in range(0,len(minus)):
    aa=a
    a=aa-minus[i]
    print(aa,minus[i])
  
  
  
else:
  M=sum(plus)-sum(minus)
  print(M)
  aa=0
  a=minus[0]-plus[0]
  print(minus[0],plus[0])
  for i in range(0,len(plus)-2):
    if len(plus)-2==0: break
    aa=a
    a=a-plus[i+1]
    print(aa,plus[i+1])
  aa=a
  a=-a+plus[len(plus)-1]
  print(plus[len(plus)-1],aa)
    
  for j in range(0,len(minus)-1):
    aa=a
    a=a-minus[j+1]
    print(aa,minus[j+1])

Submission status: Wrong Answer

Solution code:
n = int(input())
li = list(map(int,input().split()))
lipos = [i for i in li if i>0]
lineg = [i for i in li if i<0]
li0 = [i for i in li if i==0]
np = len(lipos)
ng = len(lineg)
n0 = len(li0)

if ng == 0:
  lipos = lipos + li0
  np = len(lipos)
  print(sum(lipos)-2*min(lipos))
  lipos.sort()
  tmp = lipos[0]
  for i in range(1,np-1):
    print("{} {}".format(tmp,lipos[i]))
    tmp -= lipos[i]
  print("{} {}".format(lipos[np-1],tmp))
elif np == 0:
  lineg = lineg + li0
  ng = len(lineg)
  print(-sum(lineg)+2*max(lineg))
  lineg.reverse()
  tmp = lineg[0]
  for i in range(1,ng):
    print("{} {}".format(tmp,lineg[i]))
    tmp -= lineg[i]
else:
  lipos = lipos + li0
  np = len(lipos)
  print(sum(list(map(abs,lineg))) + sum(lipos))
  lipos.sort()
  lineg.reverse()
  tmp = lineg[0]
  for i in range(1,np):
    print("{} {}".format(tmp,lipos[i]))
    tmp -= lipos[i]
  print("{} {}".format(lipos[0],tmp))
  tmp = lipos[0] - tmp
  for i in range(1,ng):
    print("{} {}".format(tmp,lineg[i]))
    tmp -= lineg[i]

Submission status: Wrong Answer

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

A_MAX = max(B)
B.remove(A_MAX)

ans = []
while len(B) > 1:
    x = B.pop()
    y = B.pop()
    if y >= 0:
        B.append(x - y)
        ans.append([x, y])
    else:
        B.append(y - x)
        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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder A
"""

import sys, math, random
import numpy as np

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column
# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']
# S.replace('ABC','X') # "testABCABC" -> "testXX"
 
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
 
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    N=I()
    A=LI()
    A.sort()
    print(int(sum(A)-2*A[0]))
    x=A.pop(0)
    for n in range(N-1):
        if n==N-2:
            print("%d %d"%(A.pop(0),x))
        else:
            y=A.pop(-2)
            print("%d %d"%(x,y))
            x=x-y
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
print(sum(A[N//2:])-sum(A[:N//2]))
B = [0]*N
l,r = 0,N-1
for i in range(N):
    if i%2 == 0:
        B[i] = A[r]
        r -= 1
    else:
        B[i] = A[l]
        l += 1
hoge = B[-1]
for i in range(N-1):
    print(B[N-i-2],hoge)
    hoge = B[N-i-2]-hoge

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
a = sorted(a)
print(a)

if a[-1] < 0:
    ans = a[-1]
    for i in range(0,n-1):
        ans = ans - a[i]
    print(ans)

    ko = a[-1]
    for i in range(0,n-1):
        print(ko,a[i])
        ko = ko-a[i]

elif a[0] < 0:
    ans = 0
    negn = 0
    for i in range(n):
        ans += abs(a[i])
        if a[i]<0:
            negn += 1
    print(ans)
    s = a[0]
    for i in range(negn , n-1):
        print(s,a[i])
        s = s-a[i]
    k = a[-1]
    for i in range(1,negn):
        print(k,a[i])
        k = k-a[i]
    print(k,s)

else:
    ans = 0
    for i in range(n):
        ans += abs(a[i])
    print(ans-2*a[0])
    m = a[0]
    for i in range(1,n-1):
        print(m,a[i])
        m = m-a[i]
    print(a[-1],m)

Submission status: Wrong Answer

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()
            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.pop()
            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.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: Wrong Answer

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

A_lis.sort()

N_neg = 0
for j in A_lis:
  if j < 0:
    N_neg += 1
  else:
    break

N_pos = len(A_lis) - N_neg

seq = []

neg = A_lis[0]

for j in range(N_pos-1):
  ind = N_neg + j
  seq.append((neg, A_lis[ind]))
  neg = neg - A_lis[ind]

pos = A_lis[-1]
  
for j in range(N_neg-1):
  ind = j + 1
  seq.append((pos, A_lis[ind]))
  pos = pos - A_lis[ind]
  
seq.append((pos, neg))
print(pos-neg)

for j in seq:
  print("%d %d" % j)

Submission status: Wrong Answer

Solution code:
n = int(input())
ai = [int(x) for x in input().split()]

print(0)
print(1, -1)
print(2, 2)

Submission status: Wrong Answer

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

high_ls=ls[int(n/2):]
low_ls=ls[:int(n/2)]

print(sum(high_ls)-sum(low_ls))

if n%2==0:
    tmp=low_ls.pop(0)
    left_ls=high_ls
else:
    tmp=high_ls.pop(0)
    left_ls=low_ls
for i in range(n-1):
    lefter=left_ls.pop(0)
    print(lefter,tmp)
    tmp=lefter-tmp
    if left_ls==high_ls:
        left_ls=low_ls
    else:
        left_ls=high_ls

Submission status: Wrong Answer

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
        x.append(tmp)
        history.append([minNum, maxNum])
    else:
        print(max(x) - min(x))
        for i in history:
            print(i[0], i[1])
        print(max(x), min(x))

Submission status: Wrong Answer

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

a.sort()

if a[-1] <= 0:
    print(2*a[-1]-sum(a))
    p = a[-1]
    for i in range(1,n):
        print(p,a[n-1-i])
        p -= a[n-1-i]
    exit()

#k回マイナスを使う
k = bisect.bisect_right(a,0)
if k == 0:
    print(sum(a)-2*a[0])
    p = a[0]
    for i in range(n-2):
        print(p,a[i])
        p -= a[i]
    print(a[-1],p)
    exit()

res = 0
for i in range(k):
    res -= a[i]
for i in range(k,n):
    res += a[i]
print(res)
p = a[0]
for i in range(k,n-1):
    print(p,a[i])
    p -= a[i]
print(a[-1],p)
p = a[-1]-p

for i in range(1,k):
    print(p,a[i])
    p -= a[i]

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

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

A.sort()
if N == 2:
    print(A[1]-A[0])
    print(A[0],A[1])
    exit()

A.reverse()
max_num = A[0]
min_num = A[-1]-A[1]
exchanges = [() for i in range(N-1)]
exchanges[0] = (A[-1],A[1])
for i in range(2,N-1):
    exchanges[i-1] = (min_num,A[i])
    min_num = min_num - A[i]

exchanges[-1] = (max_num,min_num)
ans = max_num-min_num
print(ans)
for i in range(N-1):
    a,b = exchanges[i]
    print(a,b)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split()))

A.sort()
A = np.array(A)

if A[0] < 0 and A[N - 1] > 0:
	print(sum(np.abs(A)))
	total = A[0]
	i = N - 1
	first_ = 0
	second_ = 0
	while i > 1:
		if A[i-1] >= 0:
			first_ = total
			second_ = A[i]
		else:
			first_ = A[i]
			second_ = total
		print(first_,second_)
		total = first_ - second_
		i -= 1
	if A[1] >= 0:
		print(A[1],-1*abs(total))
	else:
		print((abs(total)),A[1])
		
elif A[0] >= 0:
	sum_A = sum(A)
	print(sum_A - A[0] * 2)
	total = A[0]
	for i in range(N-2):
		print(total,A[i+1])
		total = total - A[i+1]
	print(A[N-1],total)
	
else:
	sum_A = -1 * (sum(A))
	print(sum_A + A[N-1] * 2)
	total = A[N-1]
	for i in range(N-2):
		print(total,A[N-2-i])
		total = total - A[N-2-i]
	print(total,A[0])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
X = 0
Y = 0
Seisu = 0
M = 0
ls = []
K = 0
for i in range(N-1):
    K += 1
    if K == N-1:
        X = max(A)
        ls.append(X)
        A.remove(X)
        Y = max(A)
        ls.append(Y)
        A.remove(Y)
    else:
        X = min(A)
        ls.append(X)
        A.remove(X)
        Y = min(A)
        ls.append(Y)
        A.remove(Y)
    Seisu = X-Y
    A.append(Seisu)
print(A[0])
for j in range(N-1):
    print(ls[2*j],ls[2*j+1])

Submission status: Wrong Answer

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(a) == 2:
    print(max(a) - min(a))
    print(max(a), min(a))
if min(a) >= 0:
    for i in range(1, n - 1):
        anslist.append([a[0], a[i]])
        a[0] -= a[i]
    anslist.append([a[-1], a[0]])
    print(a[-1] - a[0])
    for i, j in anslist:
        print(i, j)
else:
    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: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
lx =[]
ly = []
for i in range(N-2):
        x = min(A)
        y = max(A)
        lx.append(x)
        ly.append(y)
        A.remove(min(A))
        A.remove(max(A))
        num = x - y
        A.append(num)
x = max(A)
y = min(A)
lx.append(x)
ly.append(y)
num = x - y
print(num)
for i in range(N-1):
        print(str(lx[i]) + ' ' + str(ly[i]))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
if a[0] >= 0:
    print(sum(a) - 2 * min(a))
    if n == 2:
        print(a[1], a[0])
    else:
        print(a[0], a[1])
        now = a[0] - a[1]
        for i in range(2, n-1):
            print(now, a[i])
            now = now - a[i]
        print(a[-1], now)
elif a[-1] <= 0:
    print(sum(map(abs, a)) + 2 * max(a))
    b = a[::-1]
    print(a[0], a[1])
    now = a[0] - a[1]
    for i in range(1, n):
        print(now, a[i])
        now -= a[i]
else:
    print(sum(map(abs, a)))
    if n == 2:
        print(a[1], a[0])
        exit()
    hu = list(filter(lambda x: x<0, a))
    se = list(filter(lambda x: x>=0, a))
    if len(se) == 1:
        print(a[-1], a[0])
        now = a[-1] - a[0]
        for i in range(1, n-1):
            print(now, a[i])
            now = now - a[i]
    else:
        print(hu[0], se[0])
        now = hu[0] - se[0]
        for i in range(1, len(se)-1):
            print(now, se[i])
            now -= se[i]
        print(se[-1], now)
        now = se[-1] - now
        for i in range(1, len(hu)):
            print(now, hu[i])
            now -= hu[i]

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder A
"""

import sys, math, random
import numpy as np

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column
# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']
# S.replace('ABC','X') # "testABCABC" -> "testXX"
 
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
 
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    N=I()
    A=LI()
    A.sort()
    print(sum(A)-2*A[0])
    x=A.pop(0)
    for n in range(N-1):
        if n==N-2:
            print("%d %d"%(A.pop(0),x))
        else:
            y=A.pop(-2)
            print("%d %d"%(x,y))
            x=x-y
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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:
            if smallL[smallI]-current < current-smallL[smallI] or index == 0:
                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: Wrong Answer

Solution code:
import bisect 

n=int(input())
l=list(map(int,input().split()))
l.sort()
if l[0]>=0:
    #print('hello')
    a=l[0]
    ans=0
    for i in range(n):
        ans+=l[i]
    print(ans-2*l[0])
    for i in range(1,n-1):
        print(a,l[i])
        a-=l[i]
    print(l[-1],a)
elif l[-1]<=0:
    #print('hello')
    a=l[-1]
    ans=l[-1]
    for i in range(n-1):
        ans-=l[i]
    print(ans)
    for i in range(n-1):
        print(a,l[i])
        a-=l[i]
else:
    #print('hello')
    idx=bisect.bisect(l,0)
    ans=0
    for i in range(n):
        if l[i]>=0:
            ans+=l[i]
        else:
            ans-=l[i]
    print(ans)
    a=l[0]
    for i in range(idx,n-1):
        print(a,l[i])
        a-=l[i]
    for i in range(1,idx):
        print(l[-1],l[i])
    print(l[-1],a)

Submission status: Wrong Answer

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

ans = None
ans_l = []
ans_e = a[-1]

if ans_e >= 0:
    ans_frg = 0
    ans_l = []
    ans_e = a[-1]
    for i in range(0,n-1):
        if a[i] < 0:
            ans_l += [(ans_e, a[i])]
            ans_e -= a[i]

        if a[i] >= 0 and ans_frg == 0:
            if i<n-2:
                ans = a[i]
                ans_frg = 1
            else:
                ans_l += [(ans_e, a[i])]
                ans_e -= a[i]
        if a[i] >= 0 and ans_frg == 1:
            ans_l += [(ans, a[i])]
            ans -= a[i]

    if ans is None:
        ans = ans_e
    else:
        ans_l += [(ans_e,ans)]
        ans = ans_e - ans

else:
    ans = a[0]
    ans_l = []
    for i in range(1,n):
        ans_l += [(ans, a[i])]
        ans -= a[i]

print(ans)

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

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
ans = []
while len(arr) != 1:
  x = arr.pop()
  y = arr.pop()
  ans.append([x, y])
  arr.append((x-y))
  arr.sort()
print(arr[0])
[print(x, y) for x, y in ans]

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
print(a[-1]-a[0]+sum(a[1:n-1]))
for i in range(n-1):
    if i==n-2:
        print(a[-1],a[n-2])
    else:
        print(a[i],a[i+1])
        a[i+1]=a[i]-a[i+1]

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
A = list(map(int,input().split()))
A.sort()
B = [a for a in A]
print(B)
while(len(B)>2):
    L = len(B)
    l = B[0] - B[L-1]
    print(B[0],B[L-1])
    C = [b for b in B[1:L-1]]
    insert_index = bisect.bisect_left(C,l)
    C.insert(insert_index,l)
    B = [c for c in C]
print(B[1],B[0])
print(B[1]-B[0])

Submission status: Wrong Answer

Solution code:
n = int(input())
ai = [int(x) for x in input().split()]

print(4)
print(-1, 1)
print(2, -2)