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())
lists=list(map(int,input().split()))
lists=sorted(lists)

M=max(lists)
m=min(lists)
if M<0:
    sums=0
    for i in range(n):
        if i!=n-1:
            sums-=lists[i]
        else:
            sums+=lists[i]
    print(sums)
    
    c=lists[n-1]
    for i in range(1,n):
            
            print(c,lists[n-1-i])
            c-=lists[i]

elif m>0:
    print(sum(lists)-2*lists[0])
    c=lists[0]
    for i in range(1,n):
        if i!=n-1:
            print(c,lists[i])
            c-=lists[i]
        else:
            print(lists[i],c)
        
else:
    index=0
    sums=0
    flag=True
    for i in range(n):
        sums+=abs(lists[i])
        if lists[i]==0 and flag:
            index=i
            flag=False
    print(sums)
    c=lists[index]
    for i in range(index+1,n-1):
        print(c,lists[i])
        c-=lists[i]
    print(lists[n-1],c)
    c=lists[n-1]-c
    for i in range(index):
        print(c,lists[i])
        c-=lists[i]

Submission status: Wrong Answer

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

M=max(lists)
m=min(lists)
if M<=0:
    sums=0
    for i in range(n):
        if i!=n-1:
            sums-=lists[i]
        else:
            sums+=lists[i]
    print(sums)
    
    c=lists[n-1]
    for i in range(1,n):
            
            print(c,lists[n-1-i])
            c-=lists[i]

elif m>=0:
    print(sum(lists)-2*lists[0])
    c=lists[0]
    for i in range(1,n):
        if i!=n-1:
            print(c,lists[i])
            c-=lists[i]
        else:
            print(lists[i],c)
        
else:
    index=0
    sums=0
    flag=True
    for i in range(n):
        sums+=abs(lists[i])
        if lists[i]==0 and flag:
            index=i
            flag=False
    print(sums)
    c=lists[index]
    for i in range(index+1,n-1):
        print(c,lists[i])
        c-=lists[i]
    print(lists[n-1],c)
    c=lists[n-1]-c
    for i in range(index):
        print(c,lists[i])
        c-=lists[i]

Submission status: Wrong Answer

Solution code:
n=int(input())
lists=list(map(int,input().split()))
lists=sorted(lists)
print(lists)
M=max(lists)
m=min(lists)
if M<=0:
    sums=0
    for i in range(n):
        if i!=n-1:
            sums-=lists[i]
        else:
            sums+=lists[i]
    print(sums)
    
    c=lists[n-1]
    for i in range(1,n):
            
            print(c,lists[n-1-i])
            c-=lists[i]

elif m>=0:
    print(sum(lists)-2*lists[0])
    c=lists[0]
    for i in range(1,n):
        if i!=n-1:
            print(c,lists[i])
            c-=lists[i]
        else:
            print(lists[i],c)
        
else:
    index=0
    sums=0
    flag=True
    for i in range(n):
        sums+=abs(lists[i])
        if lists[i]==0 and flag:
            index=i
            flag=False
    print(sums)
    c=lists[index]
    for i in range(index+1,n-1):
        print(c,lists[i])
        c-=lists[i]
    print(lists[n-1],c)
    c=lists[n-1]-c
    for i in range(index):
        print(c,lists[i])
        c-=lists[i]

Submission status: Wrong Answer

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
#https://atcoder.jp/contests/arc091/tasks/arc091_b
#D - Remainder Reminder
"""
if has negative

if all positive

"""

N = gi()
A = [0] * N
ans = 0
max_vi = -1
max_v = -1e4 - 1
min_vi = -1
min_v = 1e4 + 1
for i in range(N):
    a = gi()
    A[i] = a
    if (a >= max_v):
        max_vi = i
        max_v = a
    if (a < min_v):
        min_vi = i
        min_v = a

print(sum(A) - 2 * min_v)

for i in range(N):
    if i == max_vi or i == min_vi:
        continue
    a = A[i]
    if a >= 0:
        print(min_v, a)
        min_v -= a
    else:
        print(max_v, a)
        max_v += a
print(max_v, min_v)

Submission status: Wrong Answer

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
#https://atcoder.jp/contests/arc091/tasks/arc091_b
#D - Remainder Reminder
"""
if has negative

if all positive

"""

N = gi()
A = [0] * N
ans = 0
max_vi = -1
max_v = -1e4 - 1
min_vi = -1
min_v = 1e4 + 1
for i in range(N):
    a = gi()
    A[i] = a
    if (a >= max_v):
        max_vi = i
        max_v = a
    if (a < min_v):
        min_vi = i
        min_v = a

print(sum(A) - 2 * min_v)

for i in range(N):
    if i == max_vi or i == min_vi:
        continue
    print(min_v, A[i])
    min_v -= A[i]
print(max_v, min_v)

Submission status: Wrong Answer

Solution code:
def main():
    import sys

    sys.setrecursionlimit(10 ** 7)

    def make_integer(li, ri, max_):
        """[li,ri]から作れる
        最小値(max_=False)
        または最大値(max_=True)を返す"""

        if li == ri:
            return a[li]

        if max_:
            ma = a[ri]
            res = make_integer(li, ri - 1, not max_)
            ret.append((ma, res))
            return ma - res
        else:
            mi = a[li]
            res = make_integer(li + 1, ri, not max_)
            ret.append((mi, res))
            return mi - res

    n = int(input())
    *a, = sorted(map(int, input().split()))

    ret = []
    ma = make_integer(li=0, ri=n - 1, max_=True)

    print(ma)
    for x in ret:
        print(*x)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

A.sort(reverse=True)
P=[]
M=[]

for i in A:
    if i<0:
        M.append(i)
    else:
        P.append(i)

ans=[]
if len(M)==0:
    if P[0]==0:
        print(0)
        for i in range(N-1):
            print(0,0)
        exit()
    else:
        x=P.pop()
        y=P.pop()
        ans.append([x,y])
        M.append(x-y)
if len(P)==0:
    x=M.pop()
    y=M.pop()
    ans.append([x,y])
    P.append(x-y)


for i in range(len(P)-1):
    y=P.pop()
    x=M.pop()
    ans.append([x,y])
    M.append(x-y)

for i in range(len(M)):
    y=M.pop()
    x=P.pop()
    ans.append([x,y])
    P.append(x-y)

print(P[0])

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

Submission status: Wrong Answer

Solution code:
n = int(input())
A = sorted(list(map(int, input().split())))
m, M = A[0], A[-1]
print(sum(A)-2*m)
for i in range(1, n-1):
  print(m, A[i])
  m = m-A[i]
print(M, m)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

A.sort()

n=bisect_left(A,0)
ans=[]
if n<=N//2:
    for i in range(N-2):
        b=A.pop(len(A)-2)
        a=A.pop(0)
        ans.append([a,b])
        idx=bisect_left(A,a-b)
        A.insert(idx,a-b)
else:
    for i in range(N-2):
        b=A.pop()
        a=A.pop(0)
        print(a,b)
        idx=bisect_left(A,b-a)
        A.insert(idx,b-a)
b = A.pop()
a = A.pop()
ans.append([b, a])
ans1 =b-a
print(ans1)
for i in ans:
    print(*i)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())
L = [int(v) for v in input().split()]
L.sort()

# only + 2
# only - 1
# + - 3
c = 0
for v in L:
    if v < 0:
        c = c | 1
    elif v > 0:
        c = c | 2

if c == 0:
    print(0)
    for i in range(N - 1):
        print(0, 0)

if c == 2:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    print(L[-2] - p)
    for x, y in ans:
        print(x, y)

if c == 1:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    ans = []
    p = L[-1] - L[0]
    ans.append((L[-1], L[0]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((p, L[-2]))
    print(p - L[-2])
    for x, y in ans:
        print(x, y)

if c == 3:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    mc = 0
    for v in L:
        if v < 0:
            mc += 1

    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[mc:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    p = L[-2] - p
    for v in L[1:mc]:
        ans.append((p, v))
        p -= v
    print(p)
    for x, y in ans:
        print(x, y)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())
L = [int(v) for v in input().split()]
L.sort()

# only + 2
# only - 1
# + - 3
c = 0
for v in L:
    if v <= 0:
        c = c | 1
    else:
        c = c | 2

if c == 2:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    print(L[-2] - p)
    for x, y in ans:
        print(x, y)

if c == 1:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    ans = []
    p = L[-1] - L[0]
    ans.append((L[-1], L[0]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((p, L[-2]))
    print(p - L[-2])
    for x, y in ans:
        print(x, y)

if c == 3:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    mc = 0
    for v in L:
        if v < 0:
            mc += 1

    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[mc:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    p = L[-2] - p
    for v in L[1:mc]:
        ans.append((p, v))
        p -= v
    print(p)
    for x, y in ans:
        print(x, y)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())
L = [int(v) for v in input().split()]
L.sort()

# only + 2
# only - 1
# + - 3
c = 0
for v in L:
    if v < 0:
        c = c | 1
    else:
        c = c | 2

if c == 2:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    print(L[-2] - p)
    for x, y in ans:
        print(x, y)

if c == 1:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    ans = []
    p = L[-1] - L[0]
    ans.append((L[-1], L[0]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((p, L[-2]))
    print(p - L[-2])
    for x, y in ans:
        print(x, y)

if c == 3:
    if N == 2:
        print(max(L) - min(L))
        print(max(L), min(L))
        exit()

    mc = 0
    for v in L:
        if v < 0:
            mc += 1

    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[mc:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    p = L[-2] - p
    for v in L[1:mc]:
        ans.append((p, v))
        p -= v
    print(p)
    for x, y in ans:
        print(x, y)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())
L = [int(v) for v in input().split()]
L.sort()

# only + 2
# only - 1
# + - 3
c = 0
for v in L:
    if v < 0:
        c = c | 1
    else:
        c = c | 2

if c == 2:
    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    print(L[-2] - p)
    for x, y in ans:
        print(x, y)

if c == 1:
    ans = []
    p = L[-1] - L[0]
    ans.append((L[-1], L[0]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((p, L[-2]))
    print(p - L[-2])
    for x, y in ans:
        print(x, y)

if c == 3:
    mc = 0
    for v in L:
        if v < 0:
            mc += 1

    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[mc:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    p = L[-2] - p
    for v in L[1:mc]:
        ans.append((p, v))
        p -= v
    print(p)
    for x, y in ans:
        print(x, y)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())
L = [int(v) for v in input().split()]
L.sort()

# only + 2
# only - 1
# + - 3
c = 0
for v in L:
    if v < 0:
        c = c | 1
    else:
        c = c | 2

if c == 2:
    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    print(L[-2] - p)
    for x, y in ans:
        print(x, y)

if c == 1:
    ans = []
    p = L[-1] - L[0]
    ans.append((L[-1], L[0]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((p, L[-2]))
    print(p - L[-2])
    for x, y in ans:
        print(x, y)

if c == 3:
    ans = []
    p = L[0] - L[-1]
    ans.append((L[0], L[-1]))
    for v in L[1:-2]:
        ans.append((p, v))
        p -= v
    ans.append((L[-2], p))
    print(L[-2] - p)
    for x, y in ans:
        print(x, y)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019-2/tasks/diverta2019_2_c

# 引くのは - の大きい数字にすると良さそう。

# 負の数 - 正の数で数を大きくしていって最後にどんとブッパするのが良さそう


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

now = A[0]
ansls = []
for a in A[1:-1]:
    ansls.append((now, a))
    now = now - a

ansls.append((A[-1], now))
ans = A[-1] - now
print(ans)
for ans in ansls:
    print(*ans)

Submission status: Wrong Answer

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

flag=True
if A[0]==0:
    flag=False
else:
    for item in A:
        temp=A[0]*item
        if temp<=0:
            flag=False
            
A=list(map(lambda x:abs(x),A))

if flag:
    print(sum(A)-2*min(A))
else:
    print(sum(A))

Submission status: Wrong Answer

Solution code:
N = int(input())
posA, negA = [], []
for a in list(map(int,input().split())):
  if a>0:posA.append(a)
  else:negA.append(a)

posA.sort(reverse=True)
negA.sort()

if len(posA) >0:
  posi, negi = 1,0
  pros = []
  nums = []
  ans = 0
  while posi<len(posA) and negi<len(negA):
    ans += posA[posi] - negA[negi]
    pros.append([negA[negi],posA[posi]])
    nums.append(negA[negi]-posA[posi])
    posi += 1
    negi += 1
  
  ans = posA[0]
  prv_val = posA[0]
  for num in nums:
    ans -= num
    pros.append([prv_val, num])
    prv_val -= num
  
  if posi < len(posA):
    pos_nums = []
    si = posi
    ei = len(posA)-1
    while ei-si>=1:
      pos_nums.append(posA[ei]-posA[si])
      pros.append([posA[ei],posA[si]])
      si += 1
      ei -= 1
    
    if ei==si:
      ans -= posA[si]
      pros.append([prv_val, posA[si]])
      prv_val -= posA[si]
   
    for pos_num in pos_nums:
      ans -= pos_num
      pros.append([prv_val, pos_num])
      prv_val -= pos_num      
  else:
    for a in negA[negi:]:
      ans -= a
      pros.append([prv_val,a])
      prv_val -= a
else:
  ans = negA[-1]
  pros = []
  prv_val = negA[-1]
  for a in negA[:-1]:
    ans -=a
    pros.append([prv_val, a])
    prv_val -= a
  
print(ans)
[print(*pro) for pro in pros]

Submission status: Wrong Answer

Solution code:
N = int(input())
posA, negA = [], []
for a in list(map(int,input().split())):
  if a>0:posA.append(a)
  else:negA.append(a)

posA.sort(reverse=True)
negA.sort()

if len(posA) >0:
  posi, negi = 1,0
  pros = []
  nums = []
  ans = 0
  while posi<len(posA) and negi<len(negA):
    ans += posA[posi] - negA[negi]
    pros.append([negA[negi],posA[posi]])
    nums.append(negA[negi]-posA[posi])
    posi += 1
    negi += 1
  
  ans = posA[0]
  prv_val = posA[0]
  for num in nums:
    ans -= num
    pros.append([prv_val, num])
    prv_val -= num
  
  if posi < len(posA):
    pos_nums = []
    si = posi
    ei = len(posA)-1
    while si<ei:
      pos_nums.append(posA[ei]-posA[si])
      pros.append([posA[ei],posA[si]])
      si += 1
      ei -= 1
    
    if ei-si == 2:
      ans -= posA[si+1]
      pros.append(prv_val, posA[si+1])
      prv_val -= posA[si+1]
   
    for pos_num in pos_nums:
      ans -= pos_num
      pros.append([prv_val, pos_num])
      prv_val -= pos_num      
  else:
    for a in negA[negi:]:
      ans -= a
      pros.append([prv_val,a])
      prv_val -= a
else:
  ans = negA[-1]
  pros = []
  prv_val = negA[-1]
  for a in negA[:-1]:
    ans -=a
    pros.append([prv_val, a])
    prv_val -= a
  
print(ans)
[print(*pro) for pro in pros]

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
print(A[-1]-A[0]+sum(A[1:-1]))

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    import collections
    D = collections.deque(A)
    ans = []
    for i in range(N-1):
        if i % 2 != N % 2:
            x, y = D.popleft(), D.pop()
            D.appendleft(x - y)
        else:
            x, y = D.pop(), D.popleft()
            D.append(x - y)
        ans.append((x, y))
    print(D.pop())
    for x, y in ans:
        print(x, y)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
a = list(map(int, input().split()))
a.sort()
a = deque(a)

left = a.popleft()
right = a.pop()

ans = []
for i in a:
    if i > 0:
        ans.append((left, i))
        left -= i
    else:
        ans.append((right, i))
        right -= i
else:
    ans.append((right, left))

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A = list(map(int, input().split(' ')))

A.sort()
a = sum(A[:-1]) - A[-1]
b = sum(A[1:]) - A[0]
if a >= b:
    print(a)
    for i in range(N-2):
        print(A[N-1], A[i])
        A[N-1] -= A[i]
    print(A[N-2], A[N-1])

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

Submission status: Wrong Answer

Solution code:
def examC():
    N = I()
    A = LI()
    negative = []; positive = []
    ans = 0; ansC = []
    for i in A:
        if i<0:
            negative.append(i)
        else:
            positive.append(i)
    if negative==[]:
        positive.sort()
        cur = positive[0] - positive[1]
        ansC.append([positive[0],positive[1]])
        ans = sum(positive)-2*positive[0]
        for i in range(2,N-1):
            ansC.append([cur,positive[i]])
            cur -= positive[i]
        ansC.append([positive[N-1],cur])
    elif positive==[]:
        negative.sort(reverse=True)
        cur = negative[0] - negative[1]
        ansC.append([negative[0],negative[1]])
        ans = -sum(negative)+2*negative[0]
        for i in range(2,N):
            ansC.append([cur,negative[i]])
            cur -=negative[i]
    else:
        positive.sort()
        negative.sort(reverse=True)
        ans = sum(positive) - sum(negative)
        curN = negative[0]
        curP = positive[0]
        for i in range(len(positive)-1):
            ansC.append([curN,positive[i+1]])
            curN -=positive[i+1]
        for i in range(len(negative)-1):
            ansC.append([curP,negative[i+1]])
            curP -=negative[i+1]
        ansC.append([curP,curN])

    print(ans)
    for v in ansC:
        print(" ".join(map(str,v)))

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Submission status: Wrong Answer

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

S = sum(map(abs,A))

if all([a>0 for a in A]):
    print(S-2*min(A))
    A = sorted(A)
    B = [A[0]]
    for i in range(1,N):
        B.append(B[-1]-A[i])
    for i in range(N-2):
        print(B[i],A[i+1])
    print(A[N-1],B[N-2])

elif all([a<0 for a in A]):
    print(S+2*max(A))
    A = sorted(A)
    B = [A[-1]]
    for i in range(N-1):
        B.append(B[-1]-A[i])
    for i in range(N-1):
        print(B[i],A[i])

else:
    print(S)
    A = sorted(A)
    B = [A[0]]
    for i in range(N):
        if -2-i<0 or A[-2-i]<0:
            print(A[-1-i],B[-1])
            B.append(A[-1-i]-B[-1])
            break
        print(B[-1],A[-1-i])
        B.append(B[-1]-A[-1-i])
    C = [B[-1]]
    for i in range(1,N):
        if A[i]>=0:
            break
        print(C[-1],A[i])
        C.append(C[-1]-A[i])

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
A = list(map(int,input().split()))
if N == 2:
  print(max(A)-min(A))
  print(*A)
  exit()
A.sort()
#print(A)
plus = []
minus = []
for i in range(N):
  if A[i] < 0:
    minus.append(A[i])
  else:
    plus.append(A[i])
minus.sort()
plus.sort()
if len(plus) == 0:
  plus.append(minus.pop(max(minus)))
elif len(minus) == 0:
  minus.append(plus.pop())
#print(plus)
#print(minus)
tmp = plus[-1]
tmp2 = minus[-1]
ans = []
cnt = 0
for i in range(len(plus)-1):
  ans.append([tmp2,plus[i]])
  tmp2 -= plus[i]
minus[-1] = tmp2
for i in range(len(minus)):
  ans.append([tmp,minus[i]])
  tmp -= minus[i]
print(tmp)
for i in range(N-1):
  print(*ans[i])

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
A = list(map(int,input().split()))
if N == 2:
  print(max(A)-min(A))
  print(*A)
  exit()
A.sort()
#print(A)
plus = []
minus = []
for i in range(N):
  if A[i] < 0:
    minus.append(A[i])
  else:
    plus.append(A[i])
minus.sort(reverse = True)
plus.sort()
if len(plus) == 0:
  plus.append(minus.pop())
elif len(minus) == 0:
  minus.append(plus.pop())
#print(plus)
#print(minus)
tmp = plus[-1]
tmp2 = minus[-1]
ans = []
cnt = 0
for i in range(len(plus)-1):
  ans.append([tmp2,plus[i]])
  tmp2 -= plus[i]
minus[-1] = tmp2
for i in range(len(minus)):
  ans.append([tmp,minus[i]])
  tmp -= minus[i]
print(tmp)
for i in range(N-1):
  print(*ans[i])

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
A = list(map(int,input().split()))
if N == 2:
  print(max(A)-min(A))
  print(*A)
  exit()
A.sort()
#print(A)
plus = []
minus = []
for i in range(N):
  if A[i] < 0:
    minus.append(A[i])
  else:
    plus.append(A[i])
minus.sort()
plus.sort()
if len(plus) == 0:
  plus.append(minus.pop())
elif len(minus) == 0:
  minus.append(plus.pop())
#print(plus)
#print(minus)
tmp = plus[-1]
tmp2 = minus[-1]
ans = []
cnt = 0
for i in range(len(plus)-1):
  ans.append([tmp2,plus[i]])
  tmp2 -= plus[i]
minus[-1] = tmp2
for i in range(len(minus)):
  ans.append([tmp,minus[i]])
  tmp -= minus[i]
print(tmp)
for i in range(N-1):
  print(*ans[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
if N == 2:
  print(max(A)-min(A))
  print(*A)
  exit()
A.sort()
#print(A)
tmp = A[0]
ans = []
for i in range(1,N):
  if i == N-1:
    if tmp <= A[i]:
      ans.append([A[i],tmp])
      tmp = A[i] - tmp
    else:
      ans.append([tmp,A[i]])
      tmp -= A[i] 
  else:
    ans.append([tmp,A[i]])
    tmp -= A[i]
print(tmp)
for i in range(N-1):
  print(*ans[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
if N == 2:
  print(max(A)-min(A))
  print(*A)
  exit()
A.sort()
#print(A)
tmp = A[0]
ans = []
for i in range(1,N):
  if i == N-1:
    ans.append([A[i],tmp])
    tmp = A[i] - tmp
  else:
    ans.append([tmp,A[i]])
    tmp -= A[i]
print(tmp)
for i in range(N-1):
  print(*ans[i])

Submission status: Wrong Answer

Solution code:
#12:17
n = int(input())
a = list(map(int,input().split()))
plus = []
minus = []
for i in range(n):
  if a[i] >= 0:
    plus.append(a[i])
  else:
    minus.append(a[i])
plus.sort()
minus.sort(reverse=True)
lp = len(plus)
lm = len(minus)
if lp >= 1 and lm >= 1:
  ans = sum(plus) - sum(minus)
  print(ans)
  x = minus[0]
  lp = len(plus)
  for i in range(1,lp):
    print(x,plus[i])
    x = x - plus[i]
  minus[0] = x
  x = plus[0]
  for j in range(lm):
    print(x,minus[j])
    x = x - minus[j]
elif lp == 0:
  ans = - sum(minus) + 2 * max(minus)
  print(ans)
  x = minus[1]
  for j in range(2,lm):
    print(x,minus[j])
    x = x - minus[j]
  print(minus[0],x)
elif lm == 0:
  ans = sum(plus) - 2 * min(plus)
  print(ans)
  x = plus[0]
  for i in range(2,lp):
    print(x,plus[i])
    x = x - plus[i]
  print(plus[1],x)

Submission status: Wrong Answer

Solution code:
#12:17
n = int(input())
a = list(map(int,input().split()))
plus = []
minus = []
for i in range(n):
  if a[i] >= 0:
    plus.append(a[i])
  else:
    minus.append(a[i])
plus.sort()
minus.sort(reverse=True)
lp = len(plus)
lm = len(minus)
if lp >= 1 and lm >= 1:
  ans = sum(plus) - sum(minus)
  print(ans)
  x = minus[0]
  lp = len(plus)
  for i in range(1,lp):
    print(x,plus[i])
    x = x - plus[i]
  minus[0] = x
  x = plus[0]
  for j in range(lm):
    print(x,minus[j])
    x = x - minus[j]
elif lp == 0:
  ans = - sum(minus) - 2 * max(minus)
  print(ans)
  x = minus[1]
  for j in range(2,lm):
    print(x,minus[j])
    x = x - minus[j]
  print(minus[0],x)
elif lm == 0:
  ans = sum(plus) - 2 * min(plus)
  print(ans)
  x = plus[0]
  for i in range(2,lp):
    print(x,plus[i])
    x = x - plus[i]
  print(plus[1],x)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=0
tmp=a[-1]
for i in range(n):
    if i<n//2:
        ans-=a[i]
    else:
        ans+=a[i]
for i in range(n-1):
    print(str(a[i])+" "+str(tmp))
    tmp=a[i]-tmp
print(tmp)
# print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
XY = []
y = A[N//2] if N % 2 == 1 else A[-1]
smaller_count = 1
for i in reversed(range(N-1)):
  if i % 2 == 0:
    x = A[i//2]
  else:
    x = A[i+smaller_count]
    smaller_count += 1
  XY.append((x, y))
  y = x-y
print(y)
for xy in XY:
  print(xy[0], xy[1])

Submission status: Wrong Answer

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

if A[0] < 0 and A[-1] > 0:
    ans = 0
    for a in A:
        ans += abs(a)
    print(ans)
    now = A[0]
    for a in A[1:N - 1]:
        if a >= 0:
            print(now, a)
            now -= a
    print(A[-1], now)
    now = A[-1] - now
    for a in A[1:N - 1]:
        if a < 0:
            print(now, a)
elif A[0] >= 0:
    ans = sum(A) - A[0] * 2
    print(ans)
    now = A[0]
    for a in A[1: N - 1]:
        print(now, a)
        now -= a
    print(A[-1], now)
else:
    ans = -sum(A) + A[-1] * 2
    print(ans)
    now = A[-1]
    for a in A[: N - 1]:
        print(now, a)
        now -= a

Submission status: Wrong Answer

Solution code:
n=int(input())
L=list(map(int,input().split()))
L.sort()
ans=[]
for i in range(1,n-1):
  if L[i]>=0:
    ans.append((L[0],L[i]))
    L[0]-=L[i]
for i in range(n):
  if L[i]>0:
    break
  else:
    ans.append((L[n-1],L[i]))
    L[n-1]-=L[i]
print(L[n-1])
for a in ans:
  x,y=a
  print(x,y)