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:
from collections import Counter

def solve(N, A):
    s = []
    if all(a < 0 for a in A):
        A = sorted(A)
        x = A[-1]
        for i in range(0, N-1):
            s.append((x, A[i]))
            x -= A[i]
        print(x)
        for x in s:
            print(*x)
        return
    if all(a > 0 for a in A):
        A = sorted(A)
        x = A[0]
        for i in range(2, N):
            s.append((x, A[i]))
            x -= A[i]
        s.append((A[1], x))
        print(A[1] -x)
        for x in s:
            print(*x)
        return 

    A = sorted(A)
    pos = 0
    for i in range(N):
        if A[i] >= 0:
            pos =i
            break

    x = A[pos] # x > 0
    for i in range(pos-1):
        s.append((x, A[i]))
        x -= A[i]
    
    y = A[pos-1] # y < 0
    for i in range(pos+1, N):
        s.append((y, A[i]))
        y -= A[i] # y < 0
    s.append((x, y))
    print(x-y)
    for x in s:
        print(*x)

def main():
    N = int(input())
    A = [int(x) for x in input().split()]

    solve(N, A)

main()

Submission status: Wrong Answer

Solution code:
n = int(input())
alist = list(map(int,input().split()))
ans = []

if len(alist) == 2:
    alist.sort(reverse = True)
    print(alist[0]-alist[1])
    print(' '.join(map(str,alist)))
else:
    plus =[w for w in alist if w>=0]
    minus =[x for x in alist if x<0]
    if minus == []:
        plus.sort()
        for y in range(len(plus)-2):
            ans.append(' '.join(map(str,plus[y:y+2])))
            plus[y+1] = plus[y]-plus[y+1]
        pplus = plus[::-1]
        ans.append(' '.join(map(str,pplus[0:2])))
        print(pplus[0]-pplus[1])
        for y in ans:
            print(y)
    elif plus == []:
        minus.sort(reverse = True)
        for y in range(len(minus)-1):
            ans.append(' '.join(map(str,minus[y:y+2])))
            minus[y+1] = minus[y]-minus[y+1]
        print(minus[-1])
        for y in ans:
            print(y)
    else:
        for z in range(len(plus)-1):
            ans.append(' '.join(map(str,[minus[0],plus[z]])))
            minus[0] = minus[0]-plus[z]
        for q in minus:
            ans.append(''.join(map(str,[plus[-1],q])))
            plus[-1] = plus[-1]-q
        print(plus[-1])
        for t in ans:
            print(t)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
pos, neg = [], []
for num in a:
    if num >= 0:
        pos.append(num)
    else:
        neg.append(num)

def maximizeNeg():
    cur = neg[0]
    for i in range(1, len(neg)):
        ret.append((cur, a[i]))
        cur -= a[i]
    return cur

def maximizePos():
    cur = pos[0]
    for i in range(1, len(pos) - 1):
        ret.append((cur, pos[i]))
        cur -= pos[i]
    ret.append((pos[-1], cur))
    cur = pos[-1] - cur
    return cur

pos.sort()
neg.sort(reverse=True)
ret = []
if not neg:
    cur = maximizePos()
elif not pos:
    cur = maximizeNeg()
else:
    curNeg = neg[-1]
    for i in range(len(pos) - 1):
        ret.append((curNeg, pos[i]))
        curNeg -= pos[i]
    curPos = pos[-1]
    for i in range(len(neg) - 1):
        ret.append((curPos, neg[i]))
        curPos -= neg[i]
    ret.append((curPos, curNeg))
    cur = curPos - curNeg

print(cur)
print('\n'.join('%d %d' % (xy[0], xy[1]) for xy in ret))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
minus=[i for i in a if i<0]
plus=[i for i in a if i>=0]
if len(minus)==0:
    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]
elif len(plus)==0:
    print(a[-1]-a[0]-sum(a[1:n-1]))
    print(a[-1],a[0])
    a[-1]=a[-1]-a[0]
    for i in range(1,n-1):
        print(a[-1],a[i])
        a[-1]-=a[i]
else:
    print(a[-1]-a[0]-sum(minus[1:-1])+sum(plus[0:-1]))
    for i in range(len(plus)-1):
        print(minus[0],plus[i])
        minus[0]-=plus[i]
    for i in range(len(minus)):
        print(a[-1],minus[i])
        a[-1]-=minus[i]

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
H = list(map(int, input().split()))
H.sort()
negative_count = bisect.bisect_left(H, 0)
positive_count = N - negative_count

def f():
    # answer
    ans = ""

    if positive_count <= 1:
        ans += "{0} {1}\n".format(H[-1], H[0])
        sum_ans = H[-1] - H[0]
    else:
        ans += "{1} {0}\n".format(H[-1], H[0])
        sum_ans = - H[-1] + H[0]

        count = positive_count - 2
        for i in range(count):
            v = H[negative_count + i]
            ans += "{0} {1}\n".format(sum_ans, v)
            sum_ans = sum_ans - v

        if count == 0:
            return ans, sum_ans
        v = H[-2]
        ans += "{1} {0}\n".format(sum_ans, v)
        sum_ans = v - sum_ans

    count = negative_count if positive_count > 0 else negative_count - 1
    for i in range(1, count):
        v = H[i]
        ans += "{0} {1}\n".format(sum_ans, v)
        sum_ans = sum_ans - v

    return ans, sum_ans


ans, sum_ans = f()
ans = "{}\n".format(sum_ans) + ans
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


def plus(pl):
    if len(pl) == 1:
        return pl[0]
    pl.sort()
    tmp = pl.pop(0)
    while len(pl) > 1:
        tmp2 = pl.pop(0)
        print(tmp, tmp2)
        tmp -= tmp2
    print(pl[0], tmp)
    return pl[0]-tmp


def minas(mi):
    if len(mi) == 1:
        return mi[0]
    mi.sort()
    tmp = mi.pop()
    while len(mi) > 1:
        tmp2 = mi.pop()
        print(tmp, tmp2)
        tmp -= tmp2
    print(tmp, mi[0])
    return tmp-mi[0]


n = int(input())

mi = []
pl = []

for i in [int(j) for j in input().split()]:
    if i < 0:
        mi.append(i)
    else:
        pl.append(i)

if len(pl) == 0:
    print(abs(sum(mi)-max(mi)))
    minas(mi)
elif len(mi) == 0:
    print(sum(pl)-min(pl))
    plus(pl)
else:
    print(abs(sum(mi))+sum(pl))
    mi.sort()
    pl.sort(reverse=True)
    while len(pl) > 1:
        tmp = pl.pop()
        print(mi[0], tmp)
        mi[0] -= tmp

    while len(mi) > 1:
        tmp = mi.pop()
        print(mi[0], tmp)
        mi[0] -= tmp

    print(pl[0], mi[0])

Submission status: Wrong Answer

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

plus=[]
minus=[]

if min(A)>=0:
    tmp=min(A)
    print(sum(A)-tmp*2)
    if len(A)==2:
        print(max(A),min(A))
        sys.exit()
    tmpind=A.index(tmp)
    for i in range(len(A)-1):
        if i==tmpind:continue
        print(tmp,A[i])
        tmp-=A[i]
    print(A[-1],tmp)
elif max(A)<=0:
    tmp=max(A)
    print(-(sum(A)-tmp*2))
    if len(A)==2:
        print(max(A),min(A))
        sys.exit()
    tmpind=A.index(tmp)
    for i in range(len(A)-1):
        if i==tmpind:continue
        print(tmp,A[i])
        tmp-=A[i]
    print(tmp,A[-1])
else:
    print(sum(map(abs,A)))
    for i in range(len(A)):
        if A[i]>0:
            plus.append(A[i])
        else:
            minus.append(A[i])
    if len(plus)==1 and len(minus)==1:
        print(plus[0],minus[0])
    elif len(minus)==1:
        tmp=minus[0]
        for i in range(len(plus)-1):
            print(tmp,plus[i])
            tmp-=A[i]
        print(plus[-1],tmp)
    else:
        for i in range(1,len(minus)):
            print(plus[-1],minus[i])
            plus[-1]-=minus[i]
        tmp=minus[0]
        for i in range(len(plus)-1):
            print(tmp,plus[i])
            tmp-=plus[i]
        print(plus[-1],tmp)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    A.sort()
    M = A.pop()
    heapq.heapify(A)

    ans = []
    while A:
        a = heapq.heappop(A)
        if len(A) != 0:
            b = heapq.heappop(A)
            if b < 0:
                ans.append((M, a))
                ans.append((M, b))
                M -= a
                M -= b
            else:
                ans.append((a, b))
                heapq.heappush(A, a-b)
        else:
            ans.append((M, a))
            M -= a

    print(M)
    for x, y in ans:
        print(x, y)


def main():
    N = read_int()
    A = read_int_n()
    (slv(N, A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

def main():
    n = int(input())
    a = sorted(list(map(int, input().split())))
    a_m = [aa for aa in a if aa < 0]
    a_p = [aa for aa in a if aa >= 0]
    sousa = []
    flag = 1
    if len(a_m) == 0:
        while len(a_m) == 0:
            if len(a_p) == 2:
                sousa += [[a_p[1], a_p[0]]]
                a_m += [a_p.pop(-1) - a_p.pop(-1)]
                flag = 0
            else:
                tmp = 1
                for i in range(len(a_p) - 1):
                    if a_p[0] - a_p[i + 1] < 0:
                        sousa += [[a_p[i], a_p[i + 1]]]
                        a_m += [a_p.pop(0) - a_p.pop(i)]
                        tmp = 0
                        break
                if tmp:
                    sousa += [[a_p[i], a_p[i + 1]]]
                    a_p = [a_p.pop(0) - a_p.pop(0)] + a_p
    elif len(a_p) == 0:
        while len(a_p) == 0:
            if len(a_m) == 2:
                sousa += [[a_m[1], a_p[0]]]
                a_p += [a_m.pop(-1) - a_m.pop(-1)]
                flag = 0
            else:
                tmp = 1
                for i in range(len(a_m) - 1):
                    if a_m[-1] - a_m[i] > 0:
                        sousa += [[a_m[i + 1], a_m[i]]]
                        a_p += [a_m.pop(-1) - a_m.pop(i)]
                        tmp = 0
                        break
                if tmp:
                    sousa += [[a_m[i + 1], a_m[i]]]
                    a_p += [a_m.pop(-1) - a_m.pop(-1)]
    if flag:
        accum_m = a_m[0]
        for i in range(len(a_p) - 1):
            sousa += [[accum_m, a_p[i]]]
            accum_m -= a_p[i]
        accum_p = a_p[-1]
        for i in range(1, len(a_m)):
            sousa += [[accum_p, a_m[i]]]
            accum_p -= a_m[i]
        sousa += [[accum_p, accum_m]]
        print(accum_p - accum_m)
    else:
        if len(a_p) > len(a_m):
            print(a_p[0])
        else:
            print(a_m[0])
    for i in sousa:
        print(*i)

if __name__ == '__main__':
    main()

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)):
    # print(x)
    if i != 0:
        minNum = x.pop(x.index(min(x)))
        maxNum = x.pop(x.index(max(x)))
        if maxNum > 0 and minNum < 0:
            tmp = minNum - maxNum
            x.append(tmp)
            history.append([minNum, maxNum])
        elif maxNum < 0 and minNum < 0:
            tmp = maxNum - minNum
            x.append(tmp)
            history.append([maxNum, minNum])
        elif maxNum == 0:
            tmp = maxNum - minNum
            x.append(tmp)
            history.append([maxNum, minNum])
    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 numpy as np


N = int(input())

poss, negs = [], []
npos, nneg = 0, 0
for a in input().split():
    ia = int(a)
    if ia < 0:
        negs.append(ia)
        nneg += 1
    else:
        poss.append(ia)
        npos += 1

# import random
# N = 100000
# npos, nneg = 40000, 60000
# poss = [random.randint(1, 1000) for _ in range(npos)]
# negs = [random.randint(-1000, -1) for _ in range(nneg)]

poss = []
negs = [-1, -1, -1]
npos, nneg = len(poss), len(negs)
N = npos + nneg

if N == 2:
    a1, a2 = (poss + negs)
    if a1 < a2:
        print(a2 - a1)
        print(a2, a1)
    else:
        print(a1 - a2)
        print(a1, a2)
    exit(0)

if nneg == 0:
    print(sum(poss) - min(poss) * 2)
    argmin = np.array(poss).argmin()
    minpos = poss.pop(argmin)
    pos = poss.pop()
    print(minpos, pos)
    negs.append(minpos - pos)
    npos -= 2
    nneg += 1
elif npos == 0:
    print(-sum(negs) + max(negs) * 2)
    argmax = np.array(negs).argmax()
    maxneg = negs.pop(argmax)  # -1とか
    neg = negs.pop()  # -5とか
    print(maxneg, neg)
    poss.append(maxneg - neg)
    nneg -= 2
    npos += 1
else:
    print(sum(poss) - sum(negs))

if npos > nneg:
    for _ in range(npos - nneg):
        pos, neg = poss.pop(), negs.pop()
        print(neg, pos)
        negs.append(neg - pos)
elif npos < nneg:
    for _ in range(nneg - npos):
        pos, neg = poss.pop(), negs.pop()
        print(pos, neg)
        poss.append(pos - neg)

for i in range(min(nneg, npos) - 1):
    pos, neg = poss.pop(), negs.pop()
    print(neg, pos)
    negs.append(neg - pos)
    pos, neg = poss.pop(), negs.pop()
    print(pos, neg)
    poss.append(pos - neg)

pos, neg = poss.pop(), negs.pop()
print(pos, neg)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import bisect
n = int(input())
a = sorted(map(int, input().split()))
if n == 2:
    print(a[1] - a[0])
    print(a[1], a[0])
    exit()

p = bisect.bisect_left(a, 0)
if (p == n):
    p = 0
if p < 2 and n == 3:
    print(sum(a) - 2 * a[0])
    print(a[0], a[1])
    b = a[0] - a[1]
    print(a[2], b)
    exit()

p = min(max(2, p), n - 2)
print(sum(a[p:]) - sum(a[:p]))
b = a[0]
for i in range(p + 1, n):
    print(b, a[i])
    b -= a[i]
c = a[p]
for i in range(1, p):
    print(c, a[i])
    c -= a[i]
print(c, b)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import bisect
n = int(input())
a = sorted(map(int, input().split()))
if n == 2:
    print(a[1] - a[0])
    print(a[1], a[0])
    exit()

p = bisect.bisect_left(a, 0)
if (p == n):
    p = 0
if p < 2 and n == 3:
    print(sum(a) - 2 * a[0])
    print(a[0], a[1])
    b = a[0] - a[1]
    print(a[2], b)
    exit()

p = min(max(2, p), n - 1)
print(sum(a[p:]) - sum(a[:p]))
b = a[0]
for i in range(p + 1, n):
    print(b, a[i])
    b -= a[i]
c = a[p]
for i in range(1, p):
    print(c, a[i])
    c -= a[i]
print(c, b)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import bisect
n = int(input())
a = sorted(map(int, input().split()))
if n == 2:
    print(a[1] - a[0])
    print(a[1], a[0])
    exit()

p = bisect.bisect_left(a, 0)
if (p == n):
    p = 0
if p < 2 and n == 3:
    print(sum(a) - 2 * a[0])
    print(a[0], a[1])
    b = a[0] - a[1]
    print(a[2], b)
    exit()

p = max(2, p)
print(sum(a[p:]) - sum(a[:p]))
b = a[0]
for i in range(p + 1, n):
    print(b, a[i])
    b -= a[i]
c = a[p]
for i in range(1, p):
    print(c, a[i])
    c -= a[i]
print(c, b)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

import sys
from collections import deque

n = int( sys.stdin.readline().strip() )
a = [ int(x) for x in sys.stdin.readline().strip().split() ]
a.sort()
# maximum value
h = n//2
print( sum(a[h:]) - sum(a[:h]) )
a = deque( a )
#
flag= n%2==0
if flag: t= a.popleft()
else: t= a.pop()

while a:
    if flag: tp = a.pop()
    else: tp = a.popleft()
    print( tp,t )
    #
    t= tp - t
    flag= not flag

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    A.sort()
    M = A.pop()
    heapq.heapify(A)

    ans = []
    while A:
        a = heapq.heappop(A)
        if a < 0:
            M -= a
            ans.append((M, a))
        elif A:
            b = heapq.heappop(A)
            ans.append((a, b))
            heapq.heappush(A, a-b)
        else:
            M -= a
            ans.append((M, a))

    print(M)
    for x, y in ans:
        print(x, y)


def main():
    N = read_int()
    A = read_int_n()
    (slv(N, A))


if __name__ == '__main__':
    main()

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

    if a[-1] <= 0:
        s = max(a)
        a.remove(s)

        X, Y = [], []
        for _ in range(n - 2):
            ai = a.pop()
            X.append(s), Y.append(ai)
            s -= ai

        ai = a.pop()
        X.append(s), Y.append(ai)

        print(s - ai)
        for x, y in zip(X, Y):
            print(x, y)
    elif a[0] >= 0:
        s = min(a)
        a.remove(s)

        X, Y = [], []
        for _ in range(n - 2):
            ai = a.pop()
            X.append(s), Y.append(ai)
            s -= ai

        ai = a.pop()
        X.append(ai), Y.append(s)

        print(ai - s)
        for x, y in zip(X, Y):
            print(x, y)
    else:
        s = min(a)
        a.remove(s)

        X, Y = [], []
        while a[-1] >= 0 and len(a) > 1:
            ai = a.pop()
            X.append(s), Y.append(ai)
            s -= ai

        a = [s] + a
        s = max(a)
        a.remove(s)

        while len(a) > 1:
            ai = a.pop()
            X.append(s), Y.append(ai)
            s -= ai

        ai = a.pop()
        X.append(s), Y.append(ai)

        print(s - ai)
        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.sort()
#print(A)
arr=[]
for i in range(n-1):
    if (n+i)%2!=0:
        arr.append([A[0],A[-1]])
        #print(A[0],A[-1])
        A.insert(0,A[0]-A[-1])
        del A[1]
        del A[-1]
    else:
        arr.append([A[-1],A[0]])
        #print(A[-1],A[0])
        A.insert(-1,A[-1]-A[0])
        del A[0]
        del A[-2]
print(arr[-1][0]-arr[-1][1])
for i in arr:
    print(i[0],i[1])

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = list(map(int, input().split()))
plus = 0
minus = 0
for i in range(N):
    if A[i] >= 0:
        plus += 1
    else:
        minus += 1
bb = deque(sorted(A))
x = [0]*(N-1)
y = [0]*(N-1)
for i in range(N-1):
    l = bb.popleft()
    r = bb.pop()
    if plus > minus and len(bb) > 2:
        ret = l - r
        x[i] = l
        y[i] = r
        plus -= 1
    else:
        ret = r - l
        x[i] = r
        y[i] = l
        minus -= 1
    if ret > 0:
        bb.append(ret)
    else:
        bb.appendleft(ret)
print(bb.pop())
for i in range(N-1):
    print(x[i], y[i])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

operation = []
for i in range(N-1):
    if i%2==0: # Negative
        x = A.pop()
        y = A.popleft()
        operation.append('{} {}'.format(y,x))
        A.appendleft(y + x * -1)
    else: # Positive
        x = A.popleft()
        y = A.pop() 
        operation.append('{} {}'.format(y,x))
        A.append(y + x * -1)

print(sum(list(A)))
for o in operation:
    print(o)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import itertools
import numpy as np
N = int(input())
A = list(map(int,input().split()))
A = sorted(A)
c = A.pop(0)
ans = []
while(len(A)):
	tmp = A.pop(0)
	if len(A)%2 == 1:
		x,y = c,tmp
	else:
		x,y = tmp,c
	c = x - y
	ans.append([x,y])
print (c)
for x,y in ans:
    print (x,y)

Submission status: Wrong Answer

Solution code:
N = int(input())
alist = list(map(int, input().split()))
alist.sort()
ans = 0
tmp = alist[0] - alist[N - 1]
anslist = []
anslist.append(str(alist[0]) + ' ' + str(alist[N - 1]))
tmp1 = alist[1]
for i in range(1, N - 2):
    tmp1 = alist[i] - alist[i + 1]
    anslist.append(str(alist[i]) + ' ' + str(alist[i + 1]))
if N != 2:
    ans = tmp1 - tmp
    anslist.append(str(tmp1) + ' ' + str(tmp))

print(ans)
for i in range(len(anslist)):
    print(anslist[i])

Submission status: Wrong Answer

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

nega = sorted(list(filter(lambda x: x < 0, a)))
posi = sorted(list(filter(lambda x: x >= 0, a)), reverse=True)

selected = []

for i in range(n - 1):
  if len(nega) == 0:
    (x, y) = posi[0], posi[-1]
    posi = posi[1:-1]
    selected.append((x, y))
    nega.append(x - y)
  elif len(posi) == 0:
    (x, y) = nega[0], nega[-1]
    nega = nega[1:-1]
    selected.append((x, y))
    posi.append(x - y)
  else:
    if len(nega) >= len(posi):
      (x, y) = posi.pop(), nega.pop()
      selected.append((x, y))
      posi.append(x - y)
    else:
      (x, y) = nega.pop(), posi.pop()
      selected.append((x, y))
      nega.append(x - y)

if len(nega) == 1:
  print(nega[0])
else:
  print(posi[0])

for (x, y) in selected:
  print(str(x) + ' ' + str(y))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
if l[0]>=0 or l[-1]<0:
  print(sum(l)-2*l[0])
  for i in range(1,n-1):
    print(l[0],l[i])
    l[0]-=l[i]
  print(l[-1],l[0])
else:
  cnt=0
  i=0
  while l[i]<0:
    cnt+=1
    if l[i+1]>=0:
      break
    i+=1
  ans=0
  for j in range(n):
    ans+=abs(l[j])
  print(ans)
  for j in range(n-2-i):
    print(l[0],l[i+j+1])
    l[0]-=l[i+j+1]
  for j in range(i+1):
    print(l[-1],l[j])
    l[-1]-=l[j]

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
A.sort()
pre = A[:N // 2]
post = A[N // 2:]
M = sum(post) - sum(pre)
print(M)
if N % 2:
  y = A.pop()
else:
  y = A.pop(0)
for i in range(N - 1):
  x = A.pop() if len(A) % 2 else A.pop(0)
  print('{} {}'.format(x, y))
  y = x - y

Submission status: Wrong Answer

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

tasug=[]
hikug=[]
for i in range(n):
  if a[i]<0:
    hikug.append(a[i])
  else:
    tasug.append(a[i])

if len(hikug)==0:
  tasug.sort()
  ans=sum(tasug)-2*tasug[0]
  print(ans)
  for i in range(len(tasug)-1):
    if i ==0:
      print(tasug[0],tasug[1])
      a=tasug[0]-tasug[i]
    elif i==1:
      continue
    else:
      print(a,tasug[i])
      a=a-tasug[i]
  if not n==2:
    print(tasug[len(tasug)-1],a)
  
  
elif len(tasug)==0:
  hikug.sort()
  ans=-sum(hikug)+2*hikug[n-1]
  print(ans)
  for i in range(n-1):
    if i ==0:
      a=hikug[n-1]

    print(a,hikug[i])
    a=a-hikug[i]

else:
  ans=sum(tasug)-sum(hikug)
  print(ans)
  if len(tasug)==1:
    for i in range(len(hikug)):
      if i ==0:
        print(tasug[0],hikug[i])
        a=tasug[0]-hikug[i]
      else:
        print(a,hikug[i])
  else:
    for i in range(len(tasug)-1):
      if i ==0:
        print(hikug[0],tasug[i])
        a=hikug[0]-tasug[i]
      else:
        print(a,tasug[i])
        a=a-tasug[i]

    print(tasug[len(tasug)-1],a)
    a=tasug[len(tasug)-1]-a
  
    for i in range(1,len(hikug)):
      print(a,hikug[i])
      a=a-hikug[i]

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
if len(set(A)) == 1 and A[0] == 0:
    if A[0] == 0:
        print(0)
        for i in range(N - 1):
            print(0, 0)
        exit()


if N == 2:
    m1 = A[0] - A[1]
    m2 = A[1] - A[0]
    if m1 >= m2:
        print(m1)
        print(A[0], A[1])

    else:
        print(m2)
        print(A[1], A[0])
    exit()

mi = A[0]
ma = A[-1]
if mi > 0:
    d = []
    tmp = mi
    for i in range(1, N - 1):
        d.append((tmp, A[i]))
        tmp -= A[i]
    d.append((A[-1], tmp))
    tmp = ma - tmp
    print(tmp)
    for a, b in d:
        print(a, b)

elif ma <= 0:
    d = []
    tmp = ma
    for i in range(N - 1):
        d.append((tmp, A[i]))
        tmp -= A[i]

    print(tmp)
    for a, b in d:
        print(a, b)

else:
    d = []
    for i in range(N):
        if A[i] >= 0:
            if i == N - 1:
                tmp = ma
                for j in range(N - 1):
                    d.append((tmp, A[j]))
                    tmp -= A[j]
                print(tmp)
                for a, b in d:
                    print(a, b)
                exit()
            tmp = A[i]
            for j in range(1, i):
                d.append((tmp, A[j]))
                tmp -= A[j]

            d.append((mi, tmp))
            tmp = mi - tmp
            for j in range(i + 1, N - 1):
                d.append((tmp, A[j]))
                tmp = tmp - A[j]

            d.append((ma, tmp))
            tmp = ma - tmp
            break
    print(tmp)
    for a, b in d:
        print(a, b)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

operation = []
for i in range(N-1):
    if i%2==0: # Negative
        x = A.pop()
        y = A.popleft()
        operation.append('{} {}'.format(x,y))
        A.appendleft(y + x * -1)
    else: # Positive
        x = A.popleft()
        y = A.pop() 
        operation.append('{} {}'.format(x,y))
        A.append(y + x * -1)

print(sum(list(A)))
for o in operation:
    print(o)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = list(map(int, input().split()))
plus = 0
minus = 0
for i in range(N):
    if A[i] >= 0:
        plus += 1
    else:
        minus += 1
bb = deque(sorted(A))
x = [0]*(N-1)
y = [0]*(N-1)
for i in range(N-1):
    l = bb.popleft()
    r = bb.pop()
    if plus > minus:
        ret = l - r
        x[i] = l
        y[i] = r
        plus -= 1
    else:
        ret = r - l
        x[i] = r
        y[i] = l
        minus -= 1
    if ret > 0:
        bb.append(ret)
    else:
        bb.appendleft(ret)
print(bb.pop())
for i in range(N-1):
    print(x[i], y[i])

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

from collections import deque
import bisect

n = ni()
a = na()

a = sorted(a)
b = n-bisect.bisect_right(a,0) #0↑の数

k = int((n+1)//2) #これだけプラスにできる
c = min(n-1,(b-1)*2)

dd = []

if c%2 == 1:
    d = deque(a)
    while d:
        dd.append(d.popleft())
        if d:
            dd.append(d.pop())
else:
    d = deque(a)
    while d:
        dd.append(d.pop())
        if d:
            dd.append(d.popleft())

ans = []
dd = deque(dd)
for i in range(n-1):
    if i < c:
        p = dd.popleft()
        q = dd.popleft()
        ans.append([q,p])
        dd.appendleft(q-p)
    else:
        p = dd.popleft()
        q = dd.popleft()
        ans.append([p,q])
        dd.appendleft(p-q)

print(list(dd)[0])
for i in ans:
    print(i[0],i[1])