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(map(int, input().split()))
import copy
#全てせいor負の時は例外処理

ans_list = []
A.sort()

ans = 0
#正リストと負リストに分ける
plus_list = []
minus_list = []

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

pre_f = 0

if len(A)==2:
    if len(plus_list)==2 or len(plus_list)==0:
        if A[0]>A[1]:
            ans_list.append([A[0],A[1]])
        else:
            ans_list.append([A[1],A[0]])
        ans = abs(A[0]-A[1])
    
    else:
        if A[0]>=0:
            ans_list.append([A[0],A[1]])
            ans = A[0]-A[1]
        else:
            ans_list.append([A[1],A[0]])
            ans = A[1]-A[0]
    print(ans)
    for i in range(len(ans_list)):
        print(ans_list[i][0],ans_list[i][1])





else:



    if len(plus_list)==0:
        minus_list = copy.deepcopy(A)
        minus = minus_list.pop()
        plus_list.append(minus-minus_list[0])
        ans_list.append([minus,minus_list[0]])
        del(minus_list[0])

    if len(minus_list)==0:
        if plus_list[0]==0:
            minus_list.append(0)
            plus_list.remove(0)
        else:
            plus_list = copy.deepcopy(A)
            plus = plus_list.pop()
            minus_list.append(plus_list[0]-plus)
            ans_list.append([plus_list[0],plus])
            del(plus_list[0])



        
    


    f = 0
    if len(minus_list)==1:
        f = 1

    ans = plus_list[0]
    # print("lis",plus_list,minus_list)
    #負を正に
    while(1):
        if f== 1: break
        if len(minus_list)==1:
            plus_list.append(ans)
            break
        else:
            minus = minus_list.pop()
            ans_list.append([ans,minus])
            ans -= minus
            


    # print("lis",plus_list,minus_list)
    #性を負に

    f = 0
    if len(plus_list)==1:
        f = 1

    ans = minus_list[0]
    while(1):
        if f== 1: break
        if len(plus_list)==1:
            break
        else:
            plus = plus_list.pop()
            ans_list.append([ans,plus])
            ans -= plus

    # print("lis",plus_list,minus_list)

    ans_list.append([plus_list[0],ans])
    ans = plus_list[0]-ans

    print(ans)
    for i in range(len(ans_list)):
        print(ans_list[i][0],ans_list[i][1])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
if a[0] >= 0:
    print(sum(a) - 2*min(a))
    pr = 0
    for i in range(n-1):
        if i == n-2:
            print(a[i+1], pr)
        elif i == 0:
            print(a[i], a[i+1])
            pr = a[i] - a[i+1]
        else:
            print(pr, a[i+1])
            pr -= a[i+1]
elif a[-1] < 0:
    print(abs(sum(a)) - abs(2*max(a)))
    a.sort(reverse=True)
    pr = 0
    for i in range(n-1):
        if i == 0:
            print(a[i], a[i+1])
            pr = a[i] - a[i+1]
        else:
            print(pr, a[i+1])
            pr -= a[i+1]
else:
    print(sum([abs(i) for i in a]))
    pr = 0
    for i in range(n-1):
        if i == 0:
            print(a[i], a[i+1])
            pr = a[i] + a[i+1]
        else:
            print(pr, a[i+1])
            pr += a[i+1]

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
from sys import stdin

def main():
    N = int(input())
    A = list(map(int, input().split()))

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

    a = deque(A)
    xy = []
    while len(a) > 1:
        a1 = a.popleft()
        a2 = a.pop()
        xy.append((a1, a2))
        a.appendleft(a1 - a2)

    print(xy[-1][1] - xy[-1][0])
    for x, y in xy[:N-2]:
        print(x, y)
    print(xy[-1][1], xy[-1][0])

input = lambda: stdin.readline()
main()

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = list(map(int,input().split()))
lst = sorted(lst, reverse=True)

print( sum(lst[0:n-1])-(n-2)*lst[n-1] )

for _ in range(n-2):
    a = lst.pop()
    b = lst.pop()
    print("%s %d" % (a,b))
    lst.append(a-b)
    lst = sorted(lst, reverse=True)

print("%s %d" % (lst[0],lst[1]))

Submission status: Wrong Answer

Solution code:
N=int(input())

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

A.sort()

if N%2==1:
    x0=sum(A[N//2:])-sum(A[:N//2])
    x1=sum(A[N//2+1:])-sum(A[:N//2+1])
    if x0>=x1:
        print(x0)
        t=A[N//2]
        for i in range(N-1):
            if i%2==0:
                print(str(A[i//2])+' '+str(t))
                t=A[i//2]-t
            else:
                print(str(A[N//2+i//2+1])+' '+str(t))
                t=A[N//2+i//2+1]-t
    else:
        print(x1)
        t=A[0]
        for i in range(N-2):
            if i%2==0:
                print(str(A[N//2+i//2+1])+' '+str(t))
                t=A[N//2+i//2+1]-t
            else:
                print(str(A[i//2+1])+' '+str(t))
                t=A[i//2+1]-t
        print(str(t)+' '+str(A[N//2]))
        
        
else:
    print(sum(A[N//2:])-sum(A[:N//2]))
    t=A[0]
    for i in range(N-1):
        if i%2==0:
            print(str(A[N//2+i//2])+' '+str(t))
            t=A[N//2+i//2]-t
        else:
            print(str(A[i//2+1])+' '+str(t))
            t=A[i//2+1]-t

Submission status: Wrong Answer

Solution code:
#c.py
from collections import deque

n = int(input())
lis = list(map(int,input().split()))
lis.sort()
lis = deque(lis)
x = 0
mx = lis.pop()
ans_lis = []
while len(lis) != 1:
    mi = lis.popleft()
    ma = lis.pop()
    lis.appendleft(mi - ma)
    ans_lis.append([mi,ma,mi-ma])
    x += 1
print(mx - lis[0])
for x in ans_lis:
    print(x[0],x[1])
print(mx,lis[0])

Submission status: Wrong Answer

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

plus=[]
minus=[]

if min(A)>=0:
    tmp=min(A)
    print(sum(A)-tmp*2)
    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))
    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:
N = int(input())

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

result = []


while(len(li)>2):
    li.sort()
    cnt = len(li)

    num1 = li[cnt-1] - li[0]
    num2 = li[0] - li[cnt-1]
    if abs(num1 - li[1]) >= abs(li[cnt-2]- num2):
        num = num1
        result.append([li[cnt-1], li[0]])
    else:
        num = num2
        result.append([li[0], li[cnt-1]])

    li.pop(cnt-1)
    li.pop(0)
    li.append(num)

result.append([li[1],li[0]])

print(abs(li[1]- li[0]))
for i in range(N-1):
    print(str(result[i][0]) + " " + str(result[i][1]))

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):
    heapq.heapify(A)
    M = 0
    ans = []
    while len(A) > 2:
        y = heapq.heappop(A)
        x = heapq.heappop(A)
        ans.append((y, x))
        heapq.heappush(A, y-x)
    print(max(A) - min(A))
    for x, y in ans:
        print(x, y)
    print(max(A), min(A))


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = input()
a_ls = list(map(int, input().split()))
a_ls.sort()
min_value = a_ls.pop(0)
max_value = a_ls.pop(len(a_ls)-1)
output_ls = []
for i in range(len(a_ls)):
    hoge = []
    if a_ls[i] > 0:
        hoge.append(min_value)
        hoge.append(a_ls[i])
        output_ls.append(hoge)
        min_value = min_value - a_ls[i]
    else:
        hoge.append(max_value)
        hoge.append(a_ls[i])
        output_ls.append(hoge)
        max_value = max_value + a_ls[i]
hoge = []
hoge.append(max_value)
hoge.append(min_value)
output_ls.append(hoge)
print(max_value - min_value)
for j in range(len(output_ls)):
    print(str(output_ls[j][0]) + ' ' + str(output_ls[j][1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
A.sort()

B = [None] * N
B[::2] = A[::-1][: N - (N // 2)]
B[1::2] = A[:N // 2]

print(sum(B[::2]) - sum(B[1::2]))
tmp = B[-1]
for i in range(N - 1):
    print(B[N - 2 - i], tmp)
    tmp = B[N - 2 - i] - tmp

Submission status: Wrong Answer

Solution code:
import bisect

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

i = bisect.bisect_left(A, 0)
j = bisect.bisect_right(A, 0)

if i == j:
    if i == 0:
        negatives = A[:1]
        positives = A[1:]
    elif i == N:
        negatives = A[:-1]
        positives = A[-1:]
    else:
        negatives = A[:i]
        positives = A[i:]

else:
    m = max((i+j)//2, 1)
    negatives = A[:m]
    positives = A[m:]


score = sum(positives) - sum(negatives)
print(score)

def it():

    def balanced(pos, neg):
        y = neg.pop()
        x = pos.pop()
        yield (x,y)
        y = x-y
        while pos and neg:
            x = neg.pop()
            yield (x,y)
            y = x-y
            x = pos.pop()
            yield (x,y)
            y = x-y

    if len(positives) < len(negatives):

        x = positives.pop()

        while len(positives)+1 < len(negatives):
            y = negatives.pop()
            yield (x,y)
            x -= y
        positives.append(x)
        for x,y in balanced(positives, negatives):
            yield x,y

    elif len(positives) > len(negatives):
        x = negatives.pop()

        while len(negatives)+1 < len(positives):
            y = positives.pop()
            yield (x,y)
            x -= y
        negatives.append(x)
        for x,y in balanced(positives, negatives):
            yield x,y


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

Submission status: Wrong Answer

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

A.sort()
if A[-1]<0:
    A.reverse()
M=abs(-A[0]+sum(A[1:]))
print(M)
x=A[0]
y=A[1]
for i in range(N-1):
    if i==N-1-1:
        y=x-y
        x=A[i+1]
        if A[-1]<0:
            x,y=y,x
    elif i==0:
        x=A[i]
        y=A[i+1]
    else:
        x=x-y
        y=A[i+1]
    print(x,y)

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


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

An_list = map(int, input().split() )
"""
An_negative_list = [i for i in An_list if i < 0]
list_            = [l for l in An_list]
An_positive_list = [j for j in An_list if j > 0]
An_zero_list     = [k for k in An_list if k == 0]
"""

An_negative_list = []
An_positive_list = []
An_zero_list     = []
for An in An_list:
    if An < 0:
        An_negative_list.append(An)
    elif An > 0:
        An_positive_list.append(An)
    else:
        An_zero_list.append(An)
  


## print(str(An_negative_list))
## print(str(An_positive_list))
## print(str(An_zero_list))


DEL_num = max(abs(len(An_negative_list) - len(An_positive_list)) - len(An_zero_list) , 0)


if DEL_num == 0:
    ## print(str("DEL_num:0"))
    result = abs(sum(An_negative_list)) + sum(An_positive_list)
elif len(An_negative_list) - len(An_positive_list)>0:
    ## print(str("An_negative_list"))
    DEL_list = sorted(An_negative_list, reverse=True)[:DEL_num-1]
    result = abs(sum(An_negative_list)) + sum(An_positive_list) - sum(DEL_list)
else:
    ## print(str("An_positive_list"))
    DEL_list = sorted(An_positive_list)[:DEL_num-1]
    result = abs(sum(An_negative_list)) + sum(An_positive_list) - sum(DEL_list)

print(str(result))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a = sorted(a)

for i in range(len(a)-2):
    x = a[i+1]
    y = a[i]
    xy = x-y
    if xy == 0:
        print(str(a[i])+" "+str(a[i+1]))
        print(str(a[i+2])+ " " + str(xy))
    else:
        print(str(a[i])+" "+str(a[i+1]))
        print(str(a[i+2])+ " -" + str(xy))

Submission status: Wrong Answer

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

A.sort()
A_MAX = A[-1]
X = A[:-1]
x = X[0]
ans = []
for v in X[1:]:
    ans.append((x, v))
    x -= v

if x <= 0:
    print(A_MAX - x)
    ans.append((A_MAX, x))
else:
    print(x - A_MAX)
    ans.append((x, A_MAX))
for i in range(N - 1):
    print(*ans[i])

Submission status: Wrong Answer

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

ans = []
f = 0
l = N-1
s = 0

if N == 2:
    ans.append([A[l], A[f]])
    s = A[l] - A[f]
    f += 1
    l -= 1
else:
    hasPlus = True if A[N-2] >= 0 else False
    if hasPlus:
        # make minus
        ans.append([A[f], A[l]])
        s = A[f] - A[l]
        f += 1
        l -= 1
        # choose plus make plus
        while f <= l and A[l] >= 0:
            ans.append([A[l], s])
            s = A[l] - s
            l -= 1
    else:
        # make plus
        ans.append([A[l], A[f]])
        s = A[l] - A[f]
        f += 1
        l -= 1

# choose minus make plus
while f <= l:
    ans.append([s, A[l]])
    s = s - A[l]
    l -= 1

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
data = [int(s) for s in stdin.readline().rstrip().split()]

xy = []
data = sorted(data)
while len(data) > 0:
  mx = data.pop(-1)
  mn = data.pop(0)
  if mx >= 0 and mn >= 0:
    data.append(mn - mx)
    xy.append([mn, mx])
  if mx < 0 and mn < 0:
    data.append(mx - mn)
    xy.append([mx, mn])
  if mx >= 0 and mn < 0:
    data.append(mx - mn)
    xy.append([mx, mn])
  if mx < 0 and mn >= 0:
    data.append(mn - mx)
    xy.append([mn, mx])
  if len(data) == 1:
    break
  data = sorted(data)

print(data[0])
for d in xy:
  print("%d %d" % (d[0], d[1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
a = sorted(a)
x = a[0]
for i in range(1, N-2):
  x = x - a[i]
print(a[N-1] - x)

x = a[0]

for i in range(1, N-2):
  print(x,a[i],sep=' ')
  x = x - a[i]
print(a[N-1],x,sep=' ')

Submission status: Wrong Answer

Solution code:
n = input()
n = int(n)
num = []*n
numint = []
num = input().split(' ')
for i in range(len(num)):
    numint.append(int(num[i]))
i = 0
x, y = int, int
word = []
numint = sorted(numint)
x = numint[0]
y = numint[1]
word.append(str(x) + " " + str(y))
numint.append(x - y)
numint.pop(0)
numint.pop(0)
while len(numint) >= 2:
    numint = sorted(numint)
    x = numint[0]
    y = numint[1]
    if x < y:
        a, b = x, y
        x, y = b,a
    word.append(str(x) + " " + str(y))
    numint.append(x - y)
    numint.pop(0)
    numint.pop(0)
print(numint[0])
for i in range(len(word)):
    print(word[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
lst = list(map(int,input().split()))
so = sorted(lst)
if N % 2 == 0:	
	M = sum(so[len(so)//2:])-sum(so[:len(so)//2])
	print(M)
	for i in range(len(so)-1):
		if i == 0:
			print(so[-1],so[0])
			so.append(so[-1]-so[0])
			del(so[-2])
			del(so[0])
		elif i % 2 == 0:
			print(so[0],so[-1])
			so.append(so[0]-so[-1])
			del(so[-2])
			del(so[0])
		else:
			print(so[-2],so[-1])
			so.append(so[-2]-so[-1])
			del(so[-3])
			del(so[-2])
else:
	M = sum(so[(len(so)-1)//2:]) - sum(so[:(len(so)-1)//2])
	print(M)
	for i in range(len(so)-1):
		if i % 2 == 0:
			print(so[0],so[-1])
			so.append(so[0]-so[-1])
			del(so[-2])
			del(so[0])
		else:
			print(so[-2],so[-1])
			so.append(so[-2]-so[-1])
			del(so[-3])
			del(so[-2])

Submission status: Wrong Answer

Solution code:
#c.py
from collections import deque

n = int(input())
lis = list(map(int,input().split()))
lis.sort()
lis = deque(lis)
x = 0
#mx = lis.pop()
ans_lis = []
while len(lis) != 1:
    mi = lis.popleft()
    ma = lis.pop()
    lis.append(mi - ma)
    ans_lis.append([mi,ma,mi-ma])
    x += 1
print(lis[0])
for x in ans_lis:
    print(x[0],x[1])
#print(mx,lis[0])

Submission status: Wrong Answer

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

BB = sorted(A)


if BB[-1] >= 0:
    x = BB[0]
    ope = []
    for y in BB[1:-1]:
        x, y = max(BB[-1], x), min(BB[-1], x)
        ope.append((x, y))
        x = x - y

    x, y = max(BB[-1], x), min(BB[-1], x)
    ans = x - y
    ope.append((x, y))
    print(ans)
    for x, y in ope:
        print(x, y)
else:
    end = BB[0]
    x = BB[1]
    ope = []
    for y in BB[2:]:
        x, y = max(BB[-1], x), min(BB[-1], x)
        ope.append((x, y))
        x = x - y

    x, y = max(end, x), min(end, x)
    ans = x - y
    ope.append((x, y))
    print(ans)
    for x, y in ope:
        print(x, y)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
A_a=sorted(A)
A_b=sorted(A,reverse=True)
#print(A_a)
#print(A_b)

hikae=[]
flag=0
ans_list=[]
if len(A)%2==1:
    for i in range(len(A)-1):
        if A_a[-1]==A_b[-1]:
            if flag==0:
                if len(A_a)+len(A_b)==len(A):
                    flag=1
        if A_a[-1]>=max(hikae+[-10**10]) and flag==0:
            M=A_a.pop(-1)
        else:
            M=max(hikae)
            hikae.remove(M)
        if A_b[-1]<=min(hikae+[10**10]) and flag==0:
            m=A_b.pop(-1)
        else:
            m=min(hikae)
            hikae.remove(m)

        if i%2==0:
            ans_list.append([m,M])
            hikae.append(m-M)
        else:
            ans_list.append([M,m])
            hikae.append(M-m)
else:
    for i in range(len(A)-1):
        if A_a[-1]==A_b[-1]:
            if flag==0:
                if len(A_a)+len(A_b)==len(A):
                    flag=1
        if A_a[-1]>=max(hikae+[-10**10]) and flag==0:
            M=A_a.pop(-1)
        else:
            M=max(hikae)
            hikae.remove(M)
        if A_b[-1]<=min(hikae+[10**10]) and flag==0:
            m=A_b.pop(-1)
        else:
            m=min(hikae)
            hikae.remove(m)
            
        if i%2==0:
            ans_list.append([M,m])
            hikae.append(M-m)
        else:
            ans_list.append([m,M])
            hikae.append(m-M)

print(hikae[0])
for i in range(N-1):
    print(ans_list[i][0],ans_list[i][1])

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
ps = []
ms = []

cnt = 0
ans = 0
anss = []
for e in a:
  if e<0:
    cnt += 1
    ms.append(e)
  else:
    ps.append(e)
  ans += abs(e)
if cnt==n:
  ans += a[n-1]*2
elif cnt==0:
  ans -= a[0]*2
print(ans)

if cnt==0:
  cur = a[0]
  a.remove(cur)
  for i in range(n-2):
    print(cur, a[i])
    cur = cur-a[i]
  print(a[n-2], cur)
elif cnt==n:
  cur = a[n-1]
  a.remove(cur)
  for i in range(n-1):
    print(cur, a[i])
    cur = cur-a[i]
else:
  flag = True
  if len(ps)>=len(ms):
    cur = ps.pop()
  else:
    cur = ms.pop()
  while len(ps) or len(ms):
    while len(ms) and len(ps)>=len(ms):
      t = ms.pop()
      print(t, cur)
      cur = t-cur
    while len(ps) and len(ps)>=len(ms):
      t = ps.pop()
      print(t, cur)
      cur = t-cur

for e in anss:
  print(*e)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted([int(x) for x in input().split()])
ans = []
if N % 2==0:   a = True
else:  a = False

for i in range(N-1):
    if a:
        ans += [A[-1],A[0]]
        n = A[-1] - A[0]
        if A[0] <= 0:    A = A[1:(N-i)-1] + [n] 
        else:
            j = (N-i)-2
            while n < A[j]:
                j -= 1
                if j == 0:    break
            A = A[1:j] + [n] + A[j:(N-i)-1]
        a = False

    else:
        ans += [A[0],A[-1]]
        n = A[0] - A[-1]
        if A[-1] >= 0:    A = [n] + A[1:(N-i)-1] 
        else:
            j = 1
            while n > A[j]:
                j += 1
                if j == (N-i)-1:    break
            A = A[1:j] + [n] + A[j:(N-i)-1]
        a = True

print(A[0])          
for i in range(N-1):
    print(ans[2*i],ans[2*i+1])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main():
    N = II()
    A = LI()

    A.sort()
    from collections import deque
    minus = deque(A[:N//2])
    plus = deque(A[N//2:])

    ans_li = []
    if N%2 != 0:
        p = plus.popleft()
        m = minus.popleft()
        tmp = m - p
        ans_li.append('{} {}'.format(m, p))
        while True:
            try:
                p = plus.popleft()
                ans_li.append('{} {}'.format(p, tmp))
                tmp = p - tmp
                m = minus.popleft()
                ans_li.append('{} {}'.format(m, tmp))
                tmp = m - tmp
            except:
                plus_first = (tmp, ans_li)
                break

        minus = deque(A[:N//2+1])
        plus = deque(A[N//2+1:])
        m = minus.popleft()
        p = plus.popleft()
        tmp = p - m
        ans_li = ['{} {}'.format(m, p)]
        while True:
            try:
                m = minus.popleft()
                ans_li.append('{} {}'.format(m, -tmp))
                tmp = m - tmp
                p = plus.popleft()
                ans_li.append('{} {}'.format(p, -tmp))
                tmp = p - tmp
            except:
                if -tmp > plus_first[0]:
                    print(-tmp)
                    for i in ans_li: print(i)
                else:
                    print(plus_first[0])
                    for i in plus_first[1]: print(i)
                return

    else:
        m = plus.popleft()
        p = minus.popleft()
        tmp = m - p
        ans_li.append('{} {}'.format(m, p))
        while True:
            try:
                m = minus.popleft()
                ans_li.append('{} {}'.format(m, tmp))
                tmp = m - tmp
                p = plus.popleft()
                ans_li.append('{} {}'.format(p, tmp))
                tmp = p - tmp
            except:
                print(tmp)
                for i in ans_li: print(i)
                return


main()

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
b = a[0] - a[N - 1]
d = a[0]
e = a[N - 1]
if N > 2:
    for i in range(N - 3):
        b = b - a[N - 2 - i] 
    c = a[1] - b
    print(c)
    for j in range(N - 2):
        print(d, end = ' ')
        print(e)
        d = d - e
        e = a[N - 2 - j]
    print(e, end = ' ')
    print(d, end = ' ')
else:
    print(a[1] - a[0])
    print(a[1], end = ' ')
    print(a[0], end = ' ')