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

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

max_list = [0] * (N - 1)
min_list = [0] * (N - 1)

for i in range(N - 1):
    max_list[i] = max(A_list)
    A_list.pop(A_list.index(max_list[i]))
    min_list[i] = min(A_list)
    A_list.pop(A_list.index(min_list[i]))
    tmp = min_list[i] - max_list[i]
    A_list.append(tmp)

print(-sum(A_list))
for i in range(N - 1):
    print(max_list[i], min_list[i])

Submission status: Wrong Answer

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

switch=0
mazu=newlist[0]
if newlist[0]>=0:
    seihu=1
else:
    seihu=-1
konzai=0

for nl in newlist:
    if mazu!=nl:
        switch=1

if switch==1:
    wa=0
    list2=[]
    for nl in range(n):
        list2.append([newlist[nl], abs(newlist[nl])])
        if seihu*newlist[nl]<0:
            konzai=1
            numb=nl
    if konzai==1:
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        print(wa)
        now=newlist[numb]
        print(now,newlist[0])
        now=abs(now)+abs(newlist[0])
        for i in range(n-1):
            if i+1!=numb:
                if newlist[i+1]<0:
                    print(now,newlist[i+1])
                    now=abs(now)+abs(newlist[i+1])
                else:
                    print(now*(-1),newlist[i+1])
                    now=abs(now)+abs(newlist[i+1])
    elif seihu==1:
        newlist=sorted(newlist)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa-=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            print(now, newlist[ll+2])
            now=-(abs(now)+abs(newlist[ll+2]))
    elif seihu==-1:
        newlist=sorted(newlist, reverse=True)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa+=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            print(now, newlist[ll+2])
            now=(abs(now)+abs(newlist[ll+2]))
    
else:
    print(mazu*(len(newlist)-2))
    for i in range(len(newlist)-1):
        print(mazu, mazu)

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    A = sorted(list(map(int, input().split())))
    cnt0 = 0
    cnt1 = 0
    cnt2 = 0
    for i in range(N):
        if A[i]<0:
            cnt1+=1
        elif A[i]>0:
            cnt2+=1
        else:
            cnt0+=1
    if 0<cnt2<N:
        M = sum(list(map(abs, A)))
        print(M)
        hu = A[0]
        sei = A[cnt1]
        tmp1 = sei
        for i in range(1, cnt1):
            print(tmp1, A[i])
            tmp1-=A[i]
        tmp2 = hu
        for i in range(cnt1+1, N):
            print(tmp2, A[i])
            tmp2-=A[i]
        print(tmp1, tmp2)
    elif cnt2==0:
        if cnt0 >0:
            M = sum(list(map(abs, A)))
            print(M)
            hu = A[0]
            sei = A[cnt1]
            tmp1 = sei
            for i in range(1, cnt1):
                print(tmp1, A[i])
                tmp1-=A[i]
            tmp2 = hu
            for i in range(cnt1+1, N):
                print(tmp2, A[i])
                tmp2-=A[i]
            print(tmp1, tmp2)
        else:
            M = -sum(A)+A[-1]*2
            print(M)
            tmp = A[-1]
            for i in range(N-1):
                print(tmp, A[i])
                tmp = tmp-A[i]
    else:
        M = sum(A)-A[0]
        print(M)
        tmp = A[0]
        for i in range(1, N-1):
            print(tmp, A[i])
            tmp = tmp-A[i]
        print(A[N-1], tmp)
solve()

Submission status: Wrong Answer

Solution code:
def main():

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

    for i in [0, n-1]:
        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)
            tmpa.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: Wrong Answer

Solution code:
import bisect
n = int(input())
a = list(map(int,input().split()))
a = sorted(a)
c = []
while(len(a)!=0):
    if(len(a)>=2):
        c.append([a[0],a[-1]])
        x = a[0]
        y = a[-1]
        del a[0]
        del a[-1]
        bisect.insort_left(a,x-y)
    else:
        break
print(abs(a[0]))
for i in range(len(c)-1):
    print(*c[i])
print(c[-1][1],c[-1][0])

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    nums = list(map(int, input().split()))
    nums.sort()
    res = []
    while len(nums) != 2:
        x = nums.pop(0)
        y = nums.pop(0)
        nums.append(x-y)
        res.append((x,y))
    print(nums[0]-nums[1])
    for r in res:
        print(r[0], r[1])
    print(nums[0], nums[1])




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans_list = list()

v_min = min(As)
ans = max(As)
for i in range(n - 2):
    ans_list.append((v_min, As[i]))
    v_min = v_min - As[i]
ans_list.append((ans, v_min))
ans = ans - v_min

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

Submission status: Wrong Answer

Solution code:
N = int(input())

numbers = list(map(int, input().split()))
xs = []
ys = []

numbers.sort(reverse=True)
max_num = numbers.pop(0)

for _ in range(N-2):
    _min = numbers.pop()
    _max = numbers.pop(0)
    new_number = _min - _max
    numbers.append(new_number)
    xs.append(_min)
    ys.append(_max)

min_num = numbers[0]
result = max_num - min_num
xs.append(max_num)
ys.append(min_num)

print(result)

[print(xs[i], ys[i]) for i in range(N-1)]

Submission status: Wrong Answer

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(As[p_i], As[n_i]))
    current = As[p_i] - As[n_i]
    next_positive = False
else:
    print('{} {}'.format(As[n_i], 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(As[p_i], current))
        current = As[p_i] - current
    else:
        n_i += 1
        print('{} {}'.format(As[n_i], current))
        current = As[n_i] - current
    next_positive = not next_positive

Submission status: Wrong Answer

Solution code:
import sys
N=int(input())


a=list(map(int,input().split()))
if N==2:
    print(abs(a[0]-a[1]))
    print(max(a),min(a))
    sys.exit()

a.sort()
print(sum(a)-2*a[0])
z=a[0]
for i in range(1,N-1):
    print(z,a[i])
    z-=a[i]

print(a[N-1],z)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    nums = list(map(int, input().split()))

    max_num = max(nums)
    nums.remove(max_num)
    operations = []

    nums.sort()
    nums_len = len(nums)
    nums_tmp = []
    for i in range(int(nums_len/2)):
        operations.append((nums[i], nums[nums_len-i-1]))
        nums_tmp.append(nums[i] - nums[nums_len-i-1])
    
    num_center = 0
    if nums_len % 2 == 1:
        num_center = nums[int(nums_len/2)]
    
    nums = nums_tmp
    for n in nums:
        if n <= 0:
            operations.append((max_num, n))
            max_num -= n
    if nums_len % 2 == 1:
        operations.append((max_num, num_center))
        max_num -= num_center

    print(max_num)
    for num in operations:
        print("%d %d" % num)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

arr.sort()

minus = arr[:(n // 2)]
plus = arr[(n // 2):]

print(sum(plus) - sum(minus))

res_arr = []
for i in range(n):
    if i % 2 == 0:
        res_arr.append(plus.pop())
    else:
        res_arr.append(minus.pop())

for i in range(n - 1):
    y = res_arr.pop()
    x = res_arr.pop()
    print(x, y)
    res_arr.append(x - y)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n = ri()
    la = rli()
    la.sort()
    if la[-1] < 0:
        la.sort(reverse=True)
    lo = []
    # 符号の判定
    if la[0] * la[-1] < 0:
        print(sum(abs(a) for a in la))
        l = la[0]
        r = la[-1]
        la = la[1:-1]
    else:
        print(sum(abs(a) for a in la[2:]) + abs(la[0]-la[1]))
        if la[0] < 0:
            l = la[1]
            r = la[0]
        else:
            l = la[0]
            r = la[1]
        la = la[2:]
    while len(la) > 0:
        c = la.pop()
        if c >= 0:
            lo.append((l, r))
            l, r = min(l-r, c), max(l-r, c)
        else:
            lo.append((r, l))
            l, r = min(r-l, c), max(r-l, c)
    if r-l > l-r:
        lo.append((r, l))
    else:
        lo.append((l, r))
    for o in lo:
        print(o[0], o[1])



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
m = int(input())
a = list(map(int, input().split()))
s = sorted([c for c in a if c <= 0])
p = sorted([c for c in a if c > 0])
c = p.pop(0) if len(s) == 0 else s.pop(0)
ans = []
if len(p) > 0:
    for v in p[1:]:
        ans.append((c, v))
        c = c - v
    ans.append((p[0], c))
    c = p[0] - c
for v in s:
    ans.append((c, v))
    c = c - v
print(c)
for a, b in ans:
    print(a, b)

Submission status: Wrong Answer

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

a.sort()

if a[0]<0:
    m = 0
    for i in range(n):
        m+=abs(a[i])
else:
    m = sum(a)-a[0]*2

print(m)

b = [a[0]] # -
c = [] # +

for i in range(1,n-1):
    if a[i]<0:
        b.append(a[i])
    else:
        c.append(a[i])

for x in c:
    print(b[-1],end=' ')
    print(x)
    b[-1]-=x
for y in b:
    print(a[-1],end=' ')
    print(y)
    a[-1]-=y

Submission status: Wrong Answer

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

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip

import random
import collections
import math
import itertools

def real_main():
    N = read_int()
    nums = read_int_array()
    x, actions = solve(N, nums)
    print(x)
    for a, b in actions:
        print(a, b)


def solve(n, nums):
    nums.sort()
    next_small = n // 2 - 1
    next_big = next_small + 1
    next_op = n % 2

    actions = []
    if next_op:
        last = nums[next_small] - nums[next_big]
        actions.append((nums[next_small], nums[next_big]))
    else:
        last = nums[next_big] - nums[next_small]
        actions.append((nums[next_big], nums[next_small]))

    next_big += 1
    next_small -= 1

    next_op = 1 - next_op
    while next_small >= 0 or next_big < n:
        if next_op:
            actions.append((nums[next_small], last))
            last = nums[next_small] - last
            next_small -= 1
        else:
            actions.append((nums[next_big], last))
            last = nums[next_big] - last
            next_big += 1
        next_op = 1 - next_op

    return last, actions


def main():
    if False and 'PYCHARM_HOSTED' in os.environ:
        main_pycharm()
    else:
        real_main()


from copy import deepcopy
def main_pycharm():
    solution = solve

    test_inputs = None
    test_outputs = None
    judge = None
    slow_solution = None
    if solution is not None:
        if test_outputs is not None:
            test_with_answers(solution, test_inputs, test_outputs)
        if judge is not None:
            test_with_judge(solution, test_inputs, judge)
        if slow_solution is not None:
            test_with_slow_solution(solution, test_inputs, slow_solution)


def test_with_answers(solution, inputs_answers):
    total, wrong = 0, 0
    for args, test_ans in inputs_answers:
        ans = solution(*args.copy())
        if ans != test_ans:
            print('WRONG! ans=%s, test_ans=%s, args=%s' % (ans, test_ans, args))
            wrong += 1
        else:
            print('GOOD')
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_judge(solution, inputs_gen, judge):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        if not judge(deepcopy(ans), *deepcopy(args)):
            print('WRONG! ans=%s, args=%s' % (ans, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_slow_solution(solution, inputs_gen, solution_slow):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        slow = solution_slow(*deepcopy(args))
        if ans != slow:
            print('WRONG! ans=%s, slow=%s, args=%s' % (ans, slow, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    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(b"\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()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    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()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

read = input
read_int = lambda: int(input())
read_array = lambda sep=None, maxsplit=-1: input().split(sep, maxsplit)
read_int_array = lambda: [int(a) for a in read_array()]

# endregion

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def inverse(a):
    return pow(a, mod - 2, mod)

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()
    if a[0] > 0:
        print(sum(a) - 2 * a[0])
        tmp = a[0]
        for i in range(n-2):
            print(tmp, a[i])
            tmp -= a[i]
        print(a[-1], tmp)
        return
    if a[-1] < 0:
        print(-sum(a) + 2 * a[-1])
        tmp = a[-1]
        for i in range(n-1):
            print(tmp, a[i])
            tmp -= a[i]
        return
    mid = lsearch(0, a) + 1
    print(sum(abs(i) for i in a))
    for i in range(mid+1, n):
        print(a[0], a[i])
        a[0] -= a[i]

    for i in range(mid):
        print(a[mid], a[i])
        a[mid] -= a[i]


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def lsearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] < x:
            lft = mid
        else:
            rgt = mid
    return lft


main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


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

  # sort An -> Bn
  An = deque(sorted(An))
  Bn = [None for _ in range(N)]
  if N%2 == 0: ## Even
    for i in range(N):
      if i%2==0:
        Bn[i] = An.popleft()
      else:
        Bn[i] = An.pop()        
  else: ## Odd
    for i in range(N):
      if i%2==0:
        Bn[i] = An.pop()
      else:
        Bn[i] = An.popleft()

  ## solve
  xy = [[None, None] for _ in range(N-1)]
  ans = Bn[0]
  for i in range(1,N):
    xy[i-1] = [Bn[i], ans]
    ans = Bn[i] - ans

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



if __name__ == "__main__":
  main()

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

else:
    if a[n//2] <= 0:
        for i in range(n//2):
            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:
                z = aa.pop()
                zz = aa.pop()
                al.append([z,zz])
                aa.append(z-zz)
    else:
        for i in range(n//2):
            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:
                z = aa.pop()
                zz = aa.pop()
                al.append([z,zz])
                aa.append(z-zz)
    print(su)
    for i in range(len(al)):
        print(jo(al[i]))

Submission status: Wrong Answer

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 A[0] >= 0 or A[N-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]])
            ans -= A[i]
        out.append([A[N-1], ans])
        ans = A[N - 1] - ans
    else:
        for i in range(1, N):
            if A[i] >= 0: 
                start = i - 1
                break
        ans = A[start] - A[start + 1]
        out.append([A[start], A[start + 1]])
        if start < N - 2:
            if start == 0:
                for i in range(start + 2, N-1):
                    out.append([ans, A[i]])
                out.append([A[N-1], ans])
                ans = A[N-1] - ans
            else:
                for i in range(start + 2, N):
                    out.append([ans, A[i]])
                    ans -= A[i]
        if start > 0:
            out.append([A[0], ans])
            ans = A[0] - ans
            for i in range(1, start):
                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: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = list(map(int, input().split()))
a.sort()
a = deque(a)
ans = [[]] * (n - 1)
for i in range(1, n):
    mi = a.popleft()
    ma = a.pop()
    if (n - i) % 2 == 0:
        ans[i - 1] = [mi, ma]
        a.appendleft(mi - ma)
    else:
        ans[i - 1] = [ma, mi]
        a.append(ma - mi)
print(a.pop())
for x, y in ans:
    print(x, y)

Submission status: Wrong Answer

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(As[p_i], As[n_i]))
    current = As[p_i] - As[n_i]
    next_positive = False
else:
    print('{} {}'.format(As[n_i], 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(As[p_i], current))
        current = As[p_i] - current
    else:
        n_i += 1
        print(n_i)
        print('{} {}'.format(As[n_i], current))
        current = As[n_i] - current
    next_positive = not next_positive

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)
  lst.append(str(mi)+" "+str(ma))
  del x[x.index(min(x))]
  del x[x.index(max(x))]
  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:
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]))
else:
    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]))

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]
	else:
		M+=A[i]
print(M)

if N%2==1:
	for i in range(N//2):
		print(A[i],A[N-i-1])
		B.append(A[i]-A[N-1-i])
	B.append(A[N//2])
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.append(A[-1]-A[0])
B=sorted(B)
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, *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:
        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(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
mi = min(B)
for i in range(N):
    if B[i] == mi:
        mii = i
        break
else:
    mii = -1

if po >= 0 and ne >= 0:
    print(sum(B))
    t1, t2 = A[po], A[ne]
    if po > ne:
        po, ne = ne, po
    # print("pone =", po, ne)
    if po == ne:
        del A[ne]
        A = [0] + A
    else:
        del A[ne]
        del A[po]
        A = [t1, t2] + A
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: Wrong Answer

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

a.sort()

if a[0]<0:
    m = 0
    for i in range(n):
        m+=abs(a[i])
else:
    m = sum(a)-a[0]*2

print(m)

b = [a[0]] # -
c = [] # +

for i in range(1,n-1):
    if a[i]<0:
        b.append(a[i])
    else:
        c.append(a[i])

for x in c:
    print(b[-1],end=' ')
    print(x)
    b[-1]-=x
for y in b:
    print(a[-1],end=' ')
    print(y)

Submission status: Wrong Answer

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

switch=0
mazu=newlist[0]
if newlist[0]>=0:
    seihu=1
else:
    seihu=-1
konzai=0

for nl in newlist:
    if mazu!=nl:
        switch=1

if switch==1:
    wa=0
    list2=[]
    for nl in range(n):
        list2.append([newlist[nl], abs(newlist[nl])])
        if seihu*newlist[nl]<0:
            konzai=1
            numb=nl
    if konzai==1:
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        print(wa)
        now=newlist[numb]
        print(now,newlist[0])
        now=abs(now)+abs(newlist[0])
        for i in range(n-1):
            if i+1!=numb:
                if newlist[i+1]<0:
                    print(now,newlist[i+1])
                    now=abs(now)+abs(newlist[i+1])
                else:
                    print(now*(-1),newlist[i+1])
                    now=abs(now)+abs(newlist[i+1])
    else:
        newlist=sorted(newlist)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa-=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            print(now, newlist[ll+2])
            now=-(abs(now)+abs(newlist[ll+2]))
    
else:
    print(mazu*(len(newlist)-2))
    for i in range(len(newlist)-1):
        print(mazu, mazu)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
mi,ma=a[0],a[-1]
for i in a[1:-1]:
  if i<0:print(ma,i);ma-=i
  else:print(mi,i);mi-=i
print(ma,mi)

Submission status: Wrong Answer

Solution code:
def inverse(a):
    return pow(a, mod - 2, mod)

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()
    if a[0] >= 0:
        print(sum(a) - 2 * a[0])
        tmp = a[0]
        for i in range(n-2):
            print(tmp, a[i])
            tmp -= a[i]
        print(a[-1], tmp)
        return
    if a[-1] <= 0:
        print(-sum(a) + 2 * a[-1])
        tmp = a[-1]
        for i in range(n-1):
            print(tmp, a[i])
            tmp -= a[i]
        return
    mid = usearch(0, a) + 1
    print(sum(abs(i) for i in a))
    for i in range(mid+1, n):
        print(a[0], a[i])
        a[0] -= a[i]

    for i in range(mid):
        print(a[mid], a[i])
        a[mid] -= a[i]


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def lsearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] < x:
            lft = mid
        else:
            rgt = mid
    return lft


main()

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]
	else:
		M+=A[i]
print(M)

if N%2==1:
	for i in range(N//2):
		print(A[i],A[N-i-1])
		B.append(A[i]-A[N-1-i])
	B.append(A[N//2])
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.append(A[-1]-A[0])
B=sorted(B)
for i in range(len(B)-1):
	print(B[-1],B[i])
	B[-1]=B[-1]+B[i]