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

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

Submission status: Wrong Answer

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

info.sort()
a=n//2
for i in range(a):
    info[i]=0-info[i]
    
print(sum(info))

k=info[0]

for i in range(n-1):
    if i%2==0:
        print("{} {}".format(info[i//2+a],k))
        k=k-info[i//2+a]
        
    else:
        print("{} {}".format(0-info[i//2],k))
        k=k+info[i//2]

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
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 = mi - 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:
            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:
N = int(input())
A = list(map(int, input().split()))

BB = sorted(A)

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

Submission status: Wrong Answer

Solution code:
def main():

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

    a.sort()

    for i in a:
        tmpAns = sum(a) - i - i
        if ans < tmpAns:
            ans = tmpAns
            ansi = i

    print(ans)

    a.remove(ansi)
    while len(a) >= 2:
        x = a.pop()
        y = a.pop()
        print(y, x)
        a.append(y-x)
        a.sort()

    print(ansi, a[0])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

info.sort()
a=n//2
for i in range(a):
    info[i]=0-info[i]
    
print(sum(info))

k=info[0]

for i in range(n-1):
    if i%2==0:
        print("{} {}".format(info[i//2+a],k))
        k=k-info[i//2+a]
        
    else:
        print("{} {}".format(-info[i//2],k))
        k=k+info[i//2]

Submission status: Wrong Answer

Solution code:
N = int(input())
alist = list(map(int, input().split()))
alist.sort()
ans = 0
tmp = alist[0] - alist[1]
anslist = []
anslist.append(str(alist[0]) + ' ' + str(alist[1]))
for i in range(2, N - 2):
    tmp = alist[i] - alist[i + 1]
    anslist.append(str(alist[i]) + ' ' + str(alist[i + 1]))
ans = alist[N  - 1] - tmp
anslist.append(str(alist[N - 1]) + ' ' + 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()))
a.sort()
b = a[0] - a[N - 1]
d = a[0]
e = a[N - 1]
if N > 2:
    for i in range(N - 4):
        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 - 1 - j]
    print(e, end = ' ')
    print(d, end = ' ')
else:
    print(a[1] - a[0])
    print(a[1], end = ' ')
    print(a[0], end = ' ')

Submission status: Wrong Answer

Solution code:
import fractions
from heapq import *

if __name__ == "__main__":
  N = int(input())
  A = list(map(int, input().split()))
  a_s = A[1::2]
  b_s = A[0::2]

  A.sort()
  
  print(sum(A[(len(A)) // 2:]) - sum(A[0:(len(A)) // 2]))

  for i in range(N - 1):
    a, b = heappop(a_s), heappop(b_s)
    if ((N - i) % 2 == 1):
      print(a, b)
      heappush(a_s, a - b)
    else:
      print(b, a)
      heappush(b_s, b - a)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())))
sequencelist = []

while (len(A)>1):
    x = A.pop(0)
    y = A.pop(-1)
    sequencelist.append([x,y])
    A.append(x-y)

print(sequencelist[-1][0]-sequencelist[-1][1])
for each in sequencelist:
    print(each[0], each[1])

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
a = list(map(int,input().split()))
a = sorted(a)
a.reverse()
c = []
p = 0
if(n==2):
    print(max(a[0]-a[1],a[1]-a[0]))
    exit()
for i in range(len(a)-1):
    if(a[i]>=0):
        c.append([a[-1],a[i]])
        a[-1]-=a[i]
    else:
        if(a[-1]<a[i]):
            c.append([a[i],a[-1]])
            a[-1] = a[i]-a[-1]
        else:
            c.append([a[-1],a[i]])
            a[-1] = a[-1]-a[i]
if(c[-1][1]-c[-1][0]>c[-1][0]-c[-1][1]):
    c[-1] = [c[-1][1],c[-1][0]]
else:
    c[-1] = [c[-1][0],c[-1][1]]
print(c[-1][0]-c[-1][1])
for i in range(len(c)):
    print(*c[i])

Submission status: Wrong Answer

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

man = []

for i in range(n-1):
  mx = a.pop(0)
  mn = a.pop(0)
  if mn > mx:
    mx, mn = mn, mx

  if len(a) % 2 == 0:  # maxmin
    x, y = mx, mn
  elif len(a) % 2 != 0:  # minmax
    x, y = mn, mx
  
  a.append(x-y)
  man.append([x,y])

print(a[0])
for x,y in man:
  print(x, y)

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
A = list(map(int, input().split()))
A.sort()
x = [0]*(N-1)
y = [0]*(N-1)
start_bool = 1 if N % 2 == 0 else -1
for i in range(N-1):
    l = A.pop(0)
    r = A.pop(-1)
    if start_bool < 0:
        if l > r:
            ret = r - l
            x[i] = r
            y[i] = l
        else:
            ret = l - r
            x[i] = l
            y[i] = r
    else:
        if l > r:
            ret = l - r
            x[i] = l
            y[i] = r
        else:
            ret = r - l
            x[i] = r
            y[i] = l
    if ret > 0:
        bisect.insort(A, ret)
    else:
        bisect.insort(A, ret)
    start_bool *= -1
print(A[0])
for i in range(N-1):
    print(x[i], y[i])

Submission status: Wrong Answer

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

from collections import deque

deq = deque(a_list)
res = []
while len(deq) > 2:
    min_num = deq.popleft()
    max_num = deq.pop()
    if min_num < 0 and max_num > 0:
        ne = deq.pop()
        if ne >= 0:
            res.append((min_num, ne))
            min_num -= ne
            deq.appendleft(min_num)
            deq.append(max_num)
        else:
            res.append((max_num, ne))
            max_num -= ne
            deq.append(max_num)
            deq.appendleft(min_num)
    else:
        if max_num < 0:
            res.append((max_num, min_num))
            max_num = max_num - min_num
            deq.append(max_num)
        else:
            res.append((min_num, max_num))
            min_num = min_num - max_num
            deq.appendleft(min_num)

res.append((deq[1], deq[0]))
ans = deq[1] - deq[0]
print(ans)
for x,y in res:
    print(x, y)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# input
n = int(input())
a = list(map(int, input().split()))
# calc
ap = [i for i in a if i > 0]
am = [i for i in a if i < 0]
a0 = [i for i in a if i == 0]
ans = 0
t = 0
t2 = 0
lap = len(ap)
lam = len(am)
la0 = len(a0)
if lap > 0 and lam > 0:
    ans = sum(ap)+abs(sum(am))
    print(ans)
    for i in range(la0):
        print(am[0], 0)

    if lam == 1 and lap > 1:
        print(am[0], ap[0])
        t = am[0]-ap[0]
        for i in range(1, lap-1):
            print(t, ap[i])
            t -= ap[i]
        print(ap[lap-1], t)
    elif lam > 1 and lap == 1:
        print(ap[0], am[0])
        t = ap[0]-am[0]
        for i in range(1, lam):
            print(t, am[i])
            t -= am[i]
    elif lam == 1 and lap == 1:
        print(ap[0], am[0])
    else:
        t = am[0]
        t2 = ap[0]
        for i in range(1, lap):
            print(t, ap[i])
            t -= ap[i]
        for i in range(1, lam):
            print(t2, am[i])
            t2 -= am[i]
        print(t2, t)
elif lap == 0 and lam > 0:
    am.sort()
    for i in range(lam-1):
        t += am[i]
    ans = t*(-1) + am[lam-1]
    print(ans)
    for i in range(la0):
        print(am[0], 0)
    t2 = am[lam-1]
    for i in range(lam-1):
        print(t2, am[i])
        t2 -= am[i]
elif lap > 0 and lam == 0:
    ap.sort()
    for i in range(1, lap):
        t += ap[i]
    ans = t - ap[0]
    print(ans)
    for i in range(la0):
        print(ap[0], 0)
    t2 = ap[0]
    for i in range(1, lap-1):
        print(t2, ap[i])
        t2 -= ap[i]
    print(ap[lap-1], t2)
elif lap == 0 and lam == 0:
    print(0)
    for i in range(la0-1):
        print('0 0')

Submission status: Wrong Answer

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

a_max = max(a_list)

a_list.remove(a_max)
a_list.sort()

ans_list = []

for i in range(N-2):
    a = a_list[0] - a_list[-1]
    ans_list.append([a_list[0], a_list[-1]])
    a_list.remove(a_list[0])
    a_list.remove(a_list[-1])
    a_list.append(a)
    
ans = a_max - a_list[0]
ans_list.append([a_max, a_list[0]])

print(ans)
for i in ans_list:
    print(*i)

Submission status: Wrong Answer

Solution code:
import fractions
from heapq import *

if __name__ == "__main__":
  N = int(input())
  A = list(map(int, input().split()))
  a_s = A[1::2]
  b_s = A[0::2]

  A.sort()
  
  print(sum(A[(len(A)) // 2:]) - sum(A[0:(len(A)) // 2]))

  for i in range(N - 1):
    a, b = heappop(a_s), heappop(b_s)
    if (i % 2 == 0):
      print(a, b)
      heappush(a_s, a - b)
    else:
      print(b, a)
      heappush(b_s, b - a)

Submission status: Wrong Answer

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

s=[]
ans=[]

i=1
x=a[0]
if abs(min(a))>abs(max(a)):
    a.sort(reverse=True)
    x = a[0]
    #print(a)
    while i != n - 1:

        if x > a[i]:
            ans.append([x, a[i]])
            x = x - a[i]
        else:
            ans.append([a[i], x])
            x = a[i] - x
        i += 1
    ans.append([x, a[i]])
    print(x - a[i])
else:
    a.sort()
    x = a[0]
    while i != n - 1:
        if x < a[i]:
            ans.append([x, a[i]])
            x = x - a[i]
        else:
            ans.append([a[i], x])
            x = a[i] - x
        i += 1
    ans.append([a[i], x])
    print(a[i] - x)


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

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
import itertools

INF = 10 ** 10


# N個整数A1～AN, N=10^5
N = int(stdin.readline().rstrip())
A = list(map(int, stdin.readline().rstrip().split()))
A_abs = [abs(x) for x in A]

# 2個の整数x, yを選んで消し、新たに1個の整数x-yを書く
# ただ1つ残る整数としてありうる値の最大値Mと、その最大値を達成する操作列を求める

# 答え
result = []

plus_num = []
minus_num = []

for Ai in A:
    if Ai > 0:
        plus_num.append(Ai)
    else:
        minus_num.append(Ai)

plus_num_cnt = len(plus_num)
minus_num_cnt = len(minus_num)

if minus_num_cnt == 0:
    # マイナスを作る
    plus_num.sort(reverse=True)
    x = plus_num.pop()
    y = plus_num.pop()
    if x > y:
        tmp = x
        x = y
        y = tmp
    
    result.append([x, y])
    minus_num.append(x-y)
    plus_num_cnt -= 2
    minus_num_cnt += 1

elif plus_num_cnt == 0:
    # プラスを作る
    minus_num.sort()
    x = minus_num.pop()
    y = minus_num.pop()
    if x < y:
        tmp = x
        x = y
        y = tmp

    result.append([x, y])
    plus_num.append(x-y)
    minus_num_cnt -= 2
    plus_num_cnt += 1

    


if plus_num_cnt > 0 and minus_num_cnt > 0:

    while(plus_num_cnt > 0):
        if plus_num_cnt == 1:
            break

        x = minus_num.pop()
        y = plus_num.pop()

        result.append([x, y])
        minus_num.append(x-y)
        plus_num_cnt -= 1


    while minus_num_cnt > 0:
        x = plus_num.pop()
        y = minus_num.pop()

        result.append([x, y])
        plus_num.append(x-y)
        minus_num_cnt -= 1

    print(plus_num[0])
    for i, j in result:
        print(i, j)

Submission status: Wrong Answer

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

k = N // 2
M = 0
Op = []
if N % 2 == 0:
    pos = A[0:k]
    neg = A[k:N]
    Op.append((pos[0], neg[0]))
    M = pos[0] - neg[0]
    for i in range(1, k):
        Op.append((neg[i], pos[i]))
        tmp = neg[i] - pos[i]
        Op.append((M, tmp))
        M = M - tmp
elif A[k] >= 0:
    pos = A[0:k + 1]
    neg = A[k + 1:N]
    Op.append((neg[0], pos[0]))
    M = neg[0] - pos[0]
    Op.append((pos[k], M))
    M = pos[k] - M
    for i in range(1, k):
        Op.append((neg[i], pos[i]))
        tmp = neg[i] - pos[i]
        Op.append((M, tmp))
        M = M - tmp
else:
    pos = A[0:k]
    neg = A[k:N]
    Op.append((pos[0], neg[0]))
    M = pos[0] - neg[0]
    Op.append((M, neg[k]))
    M = M - neg[k]
    for i in range(1, k):
        Op.append((neg[i], pos[i]))
        tmp = neg[i] - pos[i]
        Op.append((M, tmp))
        M = M - tmp

print(M)
for O in Op:
    print(O[0], O[1])

Submission status: Wrong Answer

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

import copy

List.sort()

result_list = []
tmp_list = copy.deepcopy(List)

if List[0] >= 0:
    for i in range(N-1):
        x = tmp_list[0]
        y = tmp_list[1]
        result_list.append([x,y])
        del tmp_list[0]
        tmp_list[0] = x - y
        
elif List[N-1] <= 0:
    for i in range(N-1):
        x = tmp_list[N-i-1]
        y = tmp_list[N-i-2]
        result_list.append([x,y])
        del tmp_list[N-i-1]
        tmp_list[N-i-2] = x - y
        
else:
    i = 0
    while tmp_list[N-i-2] > 0:
        x = tmp_list[0]
        y = tmp_list[N-i-1]
        result_list.append([x,y])
        del tmp_list[N-i-1]
        tmp_list[0] = x - y
        i += 1
    for j in range(N-i-1):
        x = tmp_list[N-i-j-1]
        y = tmp_list[0]
        result_list.append([x,y])
        del tmp_list[0]
        tmp_list[N-i-j-2] = x - y

result = result_list[N-2][0] - result_list[N-2][1]

print(result)

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

Submission status: Wrong Answer

Solution code:
n=int(input())
xs=sorted(map(int,input().split()))
if len(xs) == 2:
    print(xs[1]-xs[0])
    print(xs[1],xs[0])
elif xs[-1] >= -xs[0]:
    for i in range(n-2):
        if i == 0:
            a = xs[0] - xs[1]
        else:
            a = a - xs[i+1]
    print(xs[-1]-a)
    for i in range(n-2):
        if i == 0:
            print(xs[0],xs[1])
            a = xs[0] - xs[1]
        else:
            print(a,xs[i+1])
            a = a - xs[i+1]
    print(xs[-1],a)
else:
    xs=list(reversed(xs))
    for i in range(n-2):
        if i == 0:
            a = xs[0] - xs[1]
        else:
            a = a - xs[i+1]
    print(a-xs[-1])
    for i in range(n-2):
        if i == 0:
            print(xs[0],xs[1])
            a = xs[0] - xs[1]
        else:
            print(a,xs[i+1])
            a = a - xs[i+1]
    print(a,xs[-1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
N = int(input())
A = [int(i) for i in input().split()]
if N == 2:
    print(max(A) - min(A))
    print(max(A), min(A))
else:
    A.sort()
    As = []
    t = A[0]
    for a in A[1:-1]:
        As.append([t, a])
        t = t - a
    ans = A[-1] - t
    As.append([A[-1], t])
    print(ans)
    for a in As:
        print(*a)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
ansxy = []
A.sort()
while 1:
    x,y = A.pop(0),A.pop(-1)
    A.insert(0,x-y)
    ansxy.append([x,y])
    if len(A) == 1:
        print(abs(A[0]))
        for x,y in ansxy:
            print(x,y)
        exit()

Submission status: Wrong Answer

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

ans = 0
for a in a_list:
    ans += abs(a)
print(ans)

a_list.sort()

from collections import deque

deq = deque(a_list)
res = []
while len(deq) > 2:
    min_num = deq.popleft()
    max_num = deq.pop()
    if min_num < 0 and max_num > 0:
        ne = deq.pop()
        if ne >= 0:
            res.append((min_num, ne))
            min_num -= ne
            deq.appendleft(min_num)
            deq.append(max_num)
        else:
            res.append((max_num, ne))
            max_num -= ne
            deq.append(max_num)
            deq.appendleft(min_num)
    else:
        if max_num < 0:
            res.append((max_num, min_num))
            max_num = max_num - min_num
            deq.append(max_num)
        else:
            res.append((min_num, max_num))
            min_num = min_num - max_num
            deq.appendleft(min_num)

res.append((deq[1], deq[0]))

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

Submission status: Wrong Answer

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

switch=0
counter=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
            numba=newlist[numb]
    if konzai==1:
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        print(wa)
        now=newlist[numb]
        #print(newlist)
        newlist.remove(numba)
        #print(newlist)
        #print(now,newlist[0])
        if newlist[1]>0:
            if now>=0:
                print(newlist[0], now)
            else:
                print(now,newlist[0])
        else:
            if now>=0:
                print(now, newlist[0])
            else:
                print(newlist[0], now)
            
        now=abs(now)+abs(newlist[0])
        for xi in range(n-3):
            #print('d', now, newlist[xi+1], newlist[xi+2])
            if newlist[xi+2]>0:
                if now>=0:
                    print(newlist[xi+1], now)
                else:
                    print(now, newlist[xi+1])
                now=-abs(now)-abs(newlist[xi+1])
            else:
                if now>=0:
                    print(now, newlist[xi+1])
                else:
                    print(newlist[xi+1], now)
                now=abs(now)+abs(newlist[xi+1])
        if newlist[-1]>0:
            print(newlist[-1], now)
        else:
            print(now, newlist[-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):
            if counter==n-3:
                print(newlist[ll+2], now)
                now=-(abs(now)+abs(newlist[ll+2]))
            else:
                print(now, newlist[ll+2])
                now=-(abs(now)+abs(newlist[ll+2]))
            counter+=1
    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):
            if counter==n-3:
                print(now, newlist[ll+2])
                now=(abs(now)+abs(newlist[ll+2]))
            else:
                print(now, newlist[ll+2])
                now=(abs(now)+abs(newlist[ll+2]))
            counter+=1
    
else:
    print(abs(mazu*(len(newlist)-2)))
    for i in range(len(newlist)-2):
        print(mazu*(1-i), mazu)
    if mazu>=0:
        print(mazu, mazu*(3-len(newlist)))
    else:
        print(mazu*(3-len(newlist)), mazu)

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 = []
while len(numint) >= 3:
    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)
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())
li = list(map(int,input().split()))
lipos = [i for i in li if i>0]
lineg = [i for i in li if i<0]
li0 = [i for i in li if i==0]
np = len(lipos)
ng = len(lineg)
n0 = len(li0)

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

Submission status: Wrong Answer

Solution code:
#!/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()]

    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())
a=sorted(list(map(int,input().split())),reverse=True)
if n==2:
    print(a[0]-a[1])
    print(a[0],a[1])
    exit()

if a[-1]>=0:
    m=sum(a)-a[-1]*2
    print(m)
    for i in range(n-1):
        if i==0:
            y=a[-2]
            x=a[-1]
        elif i==n-2:
            y=x-y
            x=a[0]
        else:
            x=x-y
            y=a[-2-i]
        print(x,y)
elif  a[0]<=0:
    m=-sum(a)+a[0]*2
    print(m)
    for i in range(n-1):
        if i==0:
            y=a[1]
            x=a[0]
        else:
            x=x-y
            y=a[i+1]
        print(x,y)
else:
    for i in range(n):
        if a[i]<=0:
            mem=i
            break
    m=0
    for j in range(n):
        m+=abs(a[j])
    print(m)
    for k in range(n-1):
        if k==0:
            x=a[mem]
            y=a[k]
        elif k==mem-1:
            y=x-y
            x=a[k]
        elif k<mem-1:
            x=x-y
            y=a[k]
        elif k>mem-1:
            x=x-y
            y=a[k+1]
        print(x,y)

Submission status: Wrong Answer

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

a.sort()

ans = []
for i in range(n-1):
    ma = a[1]
    mi = a[0]
    if len(a)%2 == 0:
        ans.append([ma,mi])
        a = [ma - mi] + a[2:]
    else:
        ans.append([mi,ma])
        a = [mi - ma] + a[2:]
    
print(a[0])
for i in range(n-1):
    print(ans[i][0],ans[i][1])

Submission status: Wrong Answer

Solution code:
import heapq

N = int(input())
A = [int(x) for x in input().split()]
heapq.heapify(A)
X = []

for i in range(N-2):
    a = heapq.heappop(A)
    b = heapq.heappop(A)
    heapq.heappush(A, a-b)
    X.append([a, b])

print(A[1] - A[0])
for a, b in X:
    print(a, b)
print(A[1], A[0])

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
a = list(map(int,input().split()))
a = sorted(a)
a.reverse()
c = []
p = 0
for i in range(len(a)-1):
    if(a[i]>=0):
        c.append([a[-1],a[i]])
        a[-1]-=a[i]
    else:
        if(a[-1]<a[i]):
            c.append([a[i],a[-1]])
            a[-1] = a[i]-a[-1]
        else:
            c.append([a[-1],a[i]])
            a[-1] = a[-1]-a[i]
if(c[-1][1]-c[-1][0]>c[-1][0]-c[-1][1]):
    c[-1] = [c[-1][1],c[-1][0]]
else:
    c[-1] = [c[-1][0],c[-1][1]]
print(c[-1][0]-c[-1][1])
for i in range(len(c)):
    print(*c[i])

Submission status: Wrong Answer

Solution code:
from collections import Counter
from collections import deque
from functools import reduce
from pprint import pprint
import bisect
import copy
import fractions
import itertools
import math
import queue
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) 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 MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers): return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers): return reduce(LCM, numbers)
def LCM(m, n): return (m * n // fractions.gcd(m, n))


n = II()
a = sorted(LI(), key=abs)
for i in range(n - 1):
    if abs(a[i]) == abs(a[i+1]) and a[i] > a[i+1]:
        hoge = a[i+1]
        a[i+1] = a[i]
        a[i] = hoge
# print(a)
newa = []
zerocnt = 0
for i in a:
    if i != 0:
        newa.append(i)
    else:
        zerocnt += 1
s = 0
ans = []
if len(newa) >= 2:
    tmp = newa[0]
elif len(newa) == 0:
    tmp = 0
    print(0)
elif len(newa) == 1:
    tmp = newa[0]
    print(tmp)
    for i in range(zerocnt):
        print(str(tmp)+' '+str(0))
    exit()

for i in range(len(newa)-1):
    if i == len(newa) - 2:
        if tmp < newa[i+1]:
            ans.append((newa[i+1], tmp))
            tmp = newa[i+1] - tmp
            print(tmp)
        else:
            ans.append((tmp, newa[i+1]))
            tmp = tmp - newa[i+1]
            print(tmp)
    elif i == 0:
        tmp = newa[i] - newa[i+1]
        ans.append((newa[i], newa[i+1]))
    else:
        tmp = tmp - newa[i+1]
        ans.append((tmp, newa[i+1]))
for i in ans:
    print(str(i[0])+' '+str(i[1]))
for i in range(zerocnt):
    print(str(tmp)+' '+str(0))