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:
n = int(input())

nums = map(int, input().split(' '))

arr = sorted(nums)

num_zeros = 0
num_pos = 0
num_neg = 0

for num in arr:
    if num > 0:
        num_pos += 1
    elif num < 0:
        num_neg += 1
    else:
        num_zeros += 1

# all pos or zero
if num_neg == 0:
    print(sum(arr) - arr[0] * 2)
    lhs = arr[0]

    for i in range(1, n-1):
        rhs = arr[i]
        cal = lhs - rhs
        print(str(lhs) + " " + str(rhs))
        lhs = cal

    print(str(arr[n-1]) + " " + str(lhs))
    exit(0)

# all neg or zero
elif num_pos == 0:
    print(-sum(arr) + arr[n-1] * 2)
    lhs = arr[n-1]

    for i in reversed(range(0, n-1)):
        rhs = arr[i]
        cal = lhs - rhs
        print(str(lhs) + " " + str(rhs))
        lhs = cal
    exit(0)

# 0 and pos / 0 and neg / pos and neg (and 0)
else:
    plus_index = 0
    for index, num in enumerate(arr):
        if num < 0:
            continue
        else:
            plus_index = index
            break

    print(-sum(arr[:plus_index]) + sum(arr[plus_index:]))

    lhs = arr[plus_index]
    for i in reversed(range(1, plus_index)):
        rhs = arr[i]
        cal = lhs - rhs
        print(str(lhs) + " " + str(rhs))
        lhs = cal

    print(str(arr[0]) + " " + str(lhs))
    lhs = arr[0] - lhs

    for i in range(plus_index+1, n-1):
        rhs = arr[i]
        cal = lhs - rhs
        print(str(lhs) + " " + str(rhs))
        lhs = cal

    print(str(arr[n-1]) + " " + str(lhs))
    exit(0)

Submission status: Wrong Answer

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

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


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

            if a[i] >= 0:
                ans_l += [(ans, a[i])]
                ans -= a[i]

        ans_l += [(ans_e,ans)]
        ans = ans_e - ans

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

else:
    if ans_e >= 0:
        ans_l += [(ans_e,ans)]
        ans = ans_e - ans
    else:
        ans_l += [(ans,ans_e)]
        ans = ans - ans_e


print(ans)

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

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()
            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.pop(0)
            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(0)
            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 = 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)
else:
  print(score-2*abs(a[0]))
for i in range(n-2):
  print(a[0],a[-1])
  a[0] -= a[-1]
  del a[-1]
print(a[-1],a[0])

Submission status: Wrong Answer

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

op = [[0] * 2 for i in range(N - 1)]

tmp = 0
A.sort()
for i in range(N - 2):
	tmp = A[0] - A[len(A) - 2]
	op[i][0] = A[0]
	op[i][1] = A[len(A) - 2]
	A.pop(0)
	A.pop(-2)
	A.append(tmp)
	A.sort()

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

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

    s = a.pop(0)
    a.reverse()

    X, Y = [], []
    while len(a) > 1:
        ai = a.pop()
        X.append(s), Y.append(ai)
        s -= ai
    X.append(a[0]), Y.append(s)
    print(a[0] - s)
    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()))

A = sorted(A)

for i in range(N-1):
    print(A[0],A[-1])
    diff = A[0]-A[-1]
    A = A[1:len(A)-1]
    A.insert(0,diff)

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
A = list(map(int, input().split()))
steps = list()

A = sorted(A)
q = deque(A)

while len(q) > 2:
    max_tmp = q.pop()
    min_tmp = q.popleft()
    if abs(max_tmp) >= abs(min_tmp):
        steps.append((min_tmp, max_tmp))
        min_tmp -= max_tmp
        q.appendleft(min_tmp)
    else:
        steps.append((max_tmp, min_tmp))
        max_tmp -= min_tmp
        q.append(max_tmp)

max_tmp = q.pop()
min_tmp = q.popleft()
steps.append((max_tmp, min_tmp))
m = max_tmp - min_tmp
print(m)
for x, y in steps:
    print('{} {}'.format(x, y))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
min_ = a[0]
max_ = a[-1]
if min_ == max_:
    if min_ < 0:
        print(-min_ * (n - 2))
        print(min_, min_)
        for i in range(n - 2):
            print(i * min_, min_)
    else:
        print(min_ * (n - 2))
        print(max_, max_)
        for i in range(n - 3):
            print(i * min_, min_)
        print(max_, -(n - 3) * max_)
else:
    ans = ''
    if min_ >= 0:
        for i in range(1, n - 1):
            ans += str(min_) + ' ' + str(a[i]) + '\n'
            min_ -= a[i]
        ans += str(a[-1]) + ' ' + str(min_) + '\n'
        print(a[-1] - min_)
        print(ans, end='')
    elif max_ <= 0:
        for i in range(n - 1):
            ans += str(max_) + ' ' + str(a[i]) + '\n'
            max_ -= a[i]
        print(max_)
        print(ans, end='')
    else:
        for i in range(1, n - 1):
            if a[i] < 0:
                continue
            else:
                ans += str(min_) + ' ' + str(a[i]) + '\n'
                min_ -= a[i]
        ans += str(max_) + ' ' + str(min_) + '\n'
        max_ -= min_
        for i in range(1, n - 1):
            if a[i] >= 0:
                break
            else:
                ans += str(max_) + ' ' + str(a[i]) + '\n'
                max_ -= a[i]
        print(max_)
        print(ans, end='')

Submission status: Wrong Answer

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

A.sort()
MAX = A.pop(-1)
ans = MAX
MIN = A.pop(0)
print(N-1)
if MIN <= 0:
    if N == 2:
        ans -= MIN
        print("{0} {1}".format(MAX, MIN))
    else:
        
        for a in A:
            if a > 0:
                print("{0} {1}".format(MIN, a))
                MIN -= a
            else:
                print("{0} {1}".format(MAX, a))
                MAX -= a
        print("{0} {1}".format(MAX, MIN))
else:
    if N == 2:
        ans -= MIN
        print("{0} {1}".format(MAX, MIN))
    else:
        b = A.pop(-1)
        print("{0} {1}".format(MIN, b))
        MIN -= b
        for a in A:
            if a > 0:
                print("{0} {1}".format(MIN, a))
                MIN -= a
            else:
                print("{0} {1}".format(MAX, a))
                MAX -= a
        print("{0} {1}".format(MAX, MIN))

Submission status: Wrong Answer

Solution code:


def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g


n = int(input())
arr = list(map(int,input().split()))
arr.sort()

# print(arr)

step = n-1

out = []

if step%2 ==0:
    A = arr[-1]
    dist = 0
    # print(A)
    for i in range(step):
        if i%2==0:
            out.append([arr[0+dist],A])
            A = arr[0+dist]-A
            dist+=1

        else:
            out.append([arr[-1-dist],A])
            A = arr[-1-dist]-A

else:
    A = arr[0]
    dist = 0
    for i in range(step):
        if i%2==0:
            out.append([arr[-1-dist],A])
            A = arr[-1-dist]-A
            dist+=1

        else:
            out.append([arr[0+dist],A])
            A = arr[0+dist]-A

print(A)
for i in range(step):
    print(out[i][0]," ",out[i][1])

Submission status: Wrong Answer

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

a.sort()
xy = []
while len(a) > 2:
    n = a[0] - a[len(a)-1]
    xy.append([a[0], a[len(a)-1]])
    a.pop(0)
    a.pop(len(a)-1)
    a.insert(0, n)

m = a[1] - a[0]
xy.append([a[1], a[0]])
print(m)
for _xy in xy:
    print(str(_xy[0]) + " " + str(_xy[1]))

Submission status: Wrong Answer

Solution code:
N = int( input())
B = list( map( int, input().split()))
B.sort(reverse=True)
plus = 0
for i in range(N):
    if B[i] > 0:
        plus += 1
minus = N - plus
adding = 0
if plus <= minus:
    adding -= sum(B[plus*2:])
    A = B[:plus*2]
else:
    A = B


if plus == 0 or plus == 1:
    print(-sum(B)+B[0]*2)
    now = B[0]
    for a in B[1:]:
        print(now, a)
        now -= a
elif plus <= minus:
    N, K = plus*2, N
    print( sum(A[:N//2]) - sum(A[N//2:]) + adding)
    now = A[0]
    print(now, A[N-1])
    now -= A[N-1]
    for i in range(2,N):
        if i%2 == 0:
            print(A[N-1-i//2], now)
            now = A[N-1-i//2] - now
        else:
            print(A[i//2], now)
            now = A[i//2] - now
    for a in B[plus*2:]:
        print(now, a)
        now -= a
    
elif N%2 == 0:
    print( sum(A[:N//2]) - sum(A[N//2:]) + adding)
    now = A[0]
    print(now, A[N-1])
    now -= A[N-1]
    for i in range(2,N):
        if i%2 == 0:
            print(A[N-1-i//2], now)
            now = A[N-1-i//2] - now
        else:
            print(A[i//2], now)
            now = A[i//2] - now
    for a in A[-minus+plus:]:
        print(now, a)
        now -= a
else:
    k = N//2
    t = 1
    if A[k] < 0:
        t = 0
    print( sum(A[:k+t]) - sum(A[k+t:]))
    if t == 1:
        now = A[N-1]
        print(now, A[0])
        now = now - A[0]
        for i in range(2, N):
            if i%2 == 0:
                print(A[i//2], now)
                now = A[i//2] - now
            else:
                print(A[N-1-i//2], now)
                now = A[N-1-i//2] - now
    else:
        now = A[0]
        print(now, A[N-1])
        now -= A[N-1]
        for i in range(2, N-1):
            if i%2 == 0:
                print(A[N-1-i//2], now)
                now = A[N-1-i//2] - now
            else:
                print(A[i//2], now)
                now = A[i//2] - now
        print(now, A[k])

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int,input().split()))
ope = []
while len(a) > 1:
  x = max(a)
  y = min(a)
  ope.append([x,y])
  a.remove(x)
  a.remove(y)
  a.append(x-y)

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

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[1],A[0])
    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:
n = int(input())
a = list(map(int, input().split()))

a = sorted(a)
l = []
x = a[0]
for i in range(n-2):
    l.append([x, a[i+1]])
    x -= a[i+1]

l.append([a[n-1], x])
ans = a[n-1] - x
print(ans)
for j in range(len(l)):
    print(l[j][0], l[j][1])

Submission status: Wrong Answer

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


def answer(A, ans):
    if A[1] < A[0]:
        A = sorted(A)
    ans.append([A[0], A[-2]])
    A[0] = A[0]-A[-2]
    del A[-2]
    return A, 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 = 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: Wrong Answer

Solution code:
N = int(input())
As = list(map(int, input().split()))
As.sort(reverse=True)

i_plus = 0
# 最後のプラスになるタイミングを持っておく
plusflg = True
for i,A in enumerate(As):
    if A < 0:
        i_plus = i
        plusflg = False
        break

if plusflg:
    i_plus = N-1

ans = sum(As)
for i,A in enumerate(As[::-1]):
    if i == 0 or A < 0:
        ans -= 2*A
print(ans)

now = As[-1]
for i,A in enumerate(As[:-1]):
    if i+1 < i_plus:
        print(now,A)
        now -= A
    elif i+1 == i_plus:
        print(A, now)
        now = (A-now)
    else:
        print(max(A,now),min(A,now))        
        now = max(A,now)-min(A,now)

Submission status: Wrong Answer

Solution code:
import fractions

if __name__ == "__main__":
  N = int(input())
  A = list(map(int, input().split()))
  A.sort()
  
  print(sum(A[(len(A)) // 2:]) - sum(A[0:(len(A)) // 2]))

  for i in range(N - 1):
    a, b = A.pop(0), A.pop(0)
    A.append(a - b)
    A.sort()
    print(b, a)

Submission status: Wrong Answer

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

li.sort()
result=li[0]
re=[]
for i in range(1,len(li)):
  re.append([result,li[i]])
  result=abs(li[i]-result)

print(result)
for i in range(len(re)):
  print(re[i][0],re[i][1])

Submission status: Wrong Answer

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

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


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

            if a[i] >= 0:
                ans_l += [(ans, a[i])]
                ans -= a[i]

        ans_l += [(ans_e,ans)]
        ans = ans_e - ans

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

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

print(ans)

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
#print(a)
s=[]
ans=[]
i=1
x=a[0]
while i!=n-1:

    if x<a[i]:
        ans.append([x,a[i]])
        x=x-a[i]
    else:
        ans.append([a[i],x])
        x=a[i]-x

    i+=1
if x>a[i]:
    ans.append([x,a[i]])
    print(x-a[i])
else:
    ans.append([a[i],x])
    print(a[i]-x)



i=0
while i!=len(ans):
    print(ans[i][0],ans[i][1])
    i+=1

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
B=[]
A=sorted(A)
M=0
for i in range(N):
	if i<N//2:
		M-=A[i]
	elif N%2==1 and i==N//2 and A[i]<0:
		M-=A[i]
	else:
		M+=A[i]
print(M)

if N%2==1:
	if A[N//2]>0:
		for i in range(N//2):
			print(A[i],A[N-i-2])
			B.append(A[i]-A[N-2-i])
		B=sorted(B)
		B.append(A[-1])
	else:
		for i in range(1,N//2):
			print(A[i],A[N-1-i])
			B.append(A[i]-A[N-1-i])
		B=sorted(B)
		B.append(A[0])
		B.append(A[N//2])
		B.append(A[-1])

else:
	for i in range(1,N//2):
		print(A[i],A[N-i-1])
		B.append(A[i]-A[N-1-i])
	print(A[-1],A[0])
	B=sorted(B)
	B.append(A[-1]-A[0])

for i in range(len(B)-1):
	print(B[-1],B[i])
	B[-1]=B[-1]-B[i]

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
A_i_list = map(int, raw_input().split())

max_val = max(A_i_list)
A_i_list.sort()

if N==2:
    res = A_i_list[1] - A_i_list[0]
    print res
    print("{0} {1}".format(A_i_list[1], A_i_list[0]))
elif abs(max_val) > abs(A_i_list[0]):
    tmp_list = [0]*(N-2)
    for i in range(N-2):
        tmp_list[i] = A_i_list[i] - A_i_list[i+1]

    res = max_val - tmp_list[-1]

    print res
    print("{0} {1}".format(A_i_list[0], A_i_list[1]))
    for i in range(N-3):
        print("{0} {1}".format(tmp_list[i], A_i_list[2+i]))
    print ("{0} {1}".format(max_val, tmp_list[-1]))
else:
    A_i_list.reverse()
    tmp_list = [0] * (N - 2)
    for i in range(N - 2):
        tmp_list[i] = A_i_list[i] - A_i_list[i + 1]

    res = tmp_list[-1] - A_i_list[-1]

    print res
    print("{0} {1}".format(A_i_list[0], A_i_list[1]))
    for i in range(N - 3):
        print("{0} {1}".format(tmp_list[i], A_i_list[2 + i]))
    print ("{0} {1}".format(tmp_list[-1], A_i_list[-1]))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int, input().split()))
positive=[x for x in A if x >=0]
negative=[x for x in A if x <0]
ans=[]
if len(negative)>0:
    y=negative[0]
    for i in positive[1:]:
        ans.append([y,i])
        y-=i
else:
    y=0
if len(positive)>0:
    x=positive[0]
    for i in negative[1:]:
        ans.append([x,i])
        x-=i
else:
    x=0
M=x-y
ans.append([x,y])
print(M)
for i in ans:
    print(i[0],i[1])

Submission status: Wrong Answer

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

X = []
Y = []
A.sort()
while A[-2]>=0 and len(A)>2:
    A.sort()
    a = A[0]-A[-2]
    X.append(A[0])
    Y.append(A[-2])
    del A[0]
    del A[-2]
    A.append(a)

while len(A)>1:
    A.sort()
    b = A[-1]-A[0]
    X.append(A[-1])
    Y.append(A[0])
    del A[0]
    del A[-1]
    A.append(b)

print(A[0])
for i in range(N-1):
    print("{} {}".format(X[i],Y[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):
    if len(minus)-1==0: break
    aa=a
    a=a-minus[j+1]
    print(aa,minus[j+1])

Submission status: Wrong Answer

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


def answer(A, ans):
    if A[1] < A[0]:
        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:
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()
aa = []
al = cl.deque()
co = 0
if n % 2 == 0:
    for i in range(n//2):
        if i % 2 == 0:
            al.append([a[-(1+i)],a[i]])
            aa.append(a[-(1+i)]-a[i])
        else:
            al.append([a[i],a[-(1+i)]])
            aa.append(a[i]-a[-(1+i)])
    su = sum(map(abs,aa))
    if (n//2) % 2 == 0:
        while True:
            if len(aa) == 1:
                break
            if len(aa) == 2:
                al.append([max(aa),min(aa)])
                break
            else:
                if co % 2 == 0:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([z,zz])
                    aa.append(z-zz)
                    co += 1
                else:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([zz,z])
                    aa.append(zz-z)
    else:
        while True:
            if len(aa) == 1:
                break
            if len(aa) == 2:
                al.append([max(aa),min(aa)])
                break
            else:
                if co % 2 == 0:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([zz,z])
                    aa.append(zz-z)
                    co += 1
                else:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([z,zz])
                    aa.append(z-zz)
                    co += 1
    print(su)
    for i in range(len(al)):
        print(jo(al[i]))

else:
    if a[n//2] <= 0:
        for i in range(n//2):
            if i % 2 == 0:
                al.append([a[-(1+i)],a[i]])
                aa.append(a[-(1+i)]-a[i])
            else:
                al.append([a[i],a[-(1+i)]])
                aa.append(a[i]-a[-(1+i)])
        aa.append(a[n//2])
        su = sum(map(abs,aa))
        while True:
            if len(aa) == 1:
                break
            if len(aa) == 2:
                al.append([max(aa),min(aa)])
                break
            else:
                if co % 2 == 0:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([z,zz])
                    aa.append(z-zz)
                    co += 1
                else:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([zz,z])
                    aa.append(zz-z)
                    co += 1
    else:
        for i in range(n//2):
            if i % 2 == 0:
                al.append([a[i],a[-(1+i)]])
                aa.append(a[i]-a[-(1+i)])
            else:
                al.append([a[-(1+i)],a[i]])
                aa.append(a[-(1+i)]-a[i])
        aa.append(a[n//2])
        su = sum(map(abs,aa))
        while True:
            if len(aa) == 1:
                break
            if len(aa) == 2:
                al.append([max(aa),min(aa)])
                break
            else:
                if co % 2 == 0:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([z,zz])
                    aa.append(z-zz)
                    co += 1
                else:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([zz,z])
                    aa.append(zz-z)
                    co += 1
    print(su)
    for i in range(len(al)):
        print(jo(al[i]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input()) 
a = [int(i) for i in input().split()]
#print(n,a)

from collections import deque
#n=3
#a=[1, 1, 1]
a.sort()
a=deque(a)
ans=deque() 

while len(a)>1:
    if len(a)%2==1:
        mina=a.popleft()
        maxa=a.pop()
#        print(mina,maxa)
        ans.append((mina,maxa))
        val=mina-maxa
        a.appendleft(val)
#        print(a)
    else:
        mina=a.popleft()
        maxa=a.pop()
#        print(maxa,mina)
        ans.append((maxa,mina))
        val=maxa-mina
        a.append(val)
#        print(a)        

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

Submission status: Wrong Answer

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

Solution code:
input()
y=list(map(int,input().split()))
s=min(y)
y.remove(s)
print(sum(y)-s)
x=y.pop(0)
for i in y:
    print(str(s)+" "+str(i))
    s=s-i
print(str(x)+" "+str(s))