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: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
Ans_list = [[] for i in range(N-1)]
A.sort(reverse = True)
count = 0

for i in A[1:-1]:
    if i >= 0:
        Ans_list[count] = [A[-1],i]
        A[-1] -= i
    else:
        Ans_list[count] = [A[0],i]
        A[0] -= i
    count += 1

Ans_list[-1] = [A[0],A[-1]]
print(A[0]-A[-1])
for i,j in Ans_list:
    print(i,j)

Submission status: Accepted

Solution code:
def all_positive(values):
  m = []
  x = values[0]

  for v in values[1:-1]:
    m.append((x, v))
    x = x - v

  m.append((values[-1], x))
  x = values[-1] - x

  return x, m


def all_negative(values):
  m = []
  x = values[-1]

  for v in values[:-1]:
    m.append((x, v))
    x = x - v

  return x, m


def run(values):
  m = []
  x1 = values[0]
  x2 = values[-1]

  for v in values[-2::-1]:
    if v < 0:
      break

    m.append((x1, v))
    x1 = x1 - v

  for v in values[1:]:
    if v >= 0:
      break

    m.append((x2, v))
    x2 = x2 - v

  m.append((x2, x1))

  return x2 - x1, m


def main():
  input()
  values = list(map(int, input().split()))
  values.sort()

  if values[1] >= 0:
    x, m = all_positive(values)
  elif values[-2] <= 0:
    x, m = all_negative(values)
  else:
    x, m = run(values)

  print(x)
  for v in m:
    print('{} {}'.format(*v))


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys
import bisect

input = sys.stdin.readline
N = int(input().strip())
A = sorted(map(int, input().strip().split()))

if A[0] < 0:
    zero_i = bisect.bisect_left(A, 0)
    if zero_i == N:
        print(A[N-1] - sum(A[0:N-1]))
        i = 0
        now = A[N-1]
        while i < N-1:
            print(now, A[i])
            now -= A[i]
            i += 1
    else:
        print(sum(A[zero_i:N])-sum(A[0:zero_i]))
        i = zero_i
        now = A[0]
        while i < N-1:
            print(now, A[i])
            now -= A[i]
            i += 1
        print(A[N-1], now)
        now = A[N-1] - now
        i = 1
        while i < zero_i:
            print(now, A[i])
            now -= A[i]
            i += 1
else:
    print(sum(A[1:N])-A[0])
    i = 1
    now = A[0]
    while i < N-1:
        print(now, A[i])
        now -= A[i]
        i += 1
    print(A[N-1], now)

Submission status: Accepted

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

A.sort(reverse = True)

Ans = []
for a in A[1:-1]:
    if a >= 0:
        Ans.append([A[-1],a])
        A[-1] -= a
    else:
        Ans.append([A[0],a])
        A[0] -= a
ans = [A[0],A[-1]]
Ans.append(ans)
print(ans[0] - ans[1])
for answ in Ans:
    print(*answ, sep = " ")

Submission status: Accepted

Solution code:
import bisect
N=int(input())
A=list(map(int,input().split()))
A.sort()
k=bisect.bisect_left(A,0)
a=-sum(A[:k])+sum(A[k:])
if N==2:
  a-=2*A[0]
elif k==0:
  a-=2*A[0]
elif k==N:
  a+=2*A[N-1]
print(a)
if N==2:
  print(A[1],A[0])
elif k>=N-1:
  x=A[N-1]
  for i in range(N-1):
    print(x,A[i])
    x-=A[i]
else:
  
    x=A[0]
    
    for i in range(max(k,1),N-1):
      print(x,A[i])
      x-=A[i]
    print(A[-1],x)
    x=A[-1]-x
    for i in range(1,k):
      print(x,A[i])
      x-=A[i]

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,k = LI()
    print(n%k)
    return

#B
def B():

    n = I()
    po = LIR(n)
    return

#C
def C():
    n = I()
    a = LI()
    a.sort()
    if a[0] >= 0:
        print(sum(a)-2*a[0])
        for i in range(1,n-1):
            print(a[0],a[i])
            a[0] -= a[i]
        print(a[n-1],a[0])
    else:
        if a[-1] < 0:
            print(2*a[-1]-sum(a))
            for i in range(n-1):
                print(a[-1],a[i])
                a[-1] -= a[i]
        else:
            m = bisect.bisect_left(a,0)
            p = n-m
            ans = -sum(a[:m])+sum(a[m:])
            print(ans)
            if p > m:
                l = a[-1]
                a = a[:-1]
                a = a[::-1]
                p -= 1
                p,m = m,p
                k = a[m]-a[m-1]
                print(a[m],a[m-1])
                for i in range(p-1):
                    print(a[m-i-2],k)
                    k = a[m-i-2]-k
                    print(a[m+i+1],k)
                    k = a[m+i+1]-k
                for i in range(m-p):
                    print(k,a[i])
                    k -= a[i]
                print(l,k)
            else:
                k = a[m]-a[m-1]
                print(a[m],a[m-1])
                for i in range(p-1):
                    print(a[m-i-2],k)
                    k = a[m-i-2]-k
                    print(a[m+i+1],k)
                    k = a[m+i+1]-k
                for i in range(m-p):
                    print(k,a[i])
                    k -= a[i]
    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#G
def G():
    n = I()

    return

#H
def H():
    n = I()

    return

#I
def I_():
    n = I()

    return

#J
def J():
    n = I()

    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

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

ans_list = list()

v_min = min(As)
v_max = max(As)
As.remove(v_max)
As.remove(v_min)
for a in As:
    if a >= 0:
        ans_list.append((v_min, a))
        v_min = v_min - a
    else:
        ans_list.append((v_max, a))
        v_max = v_max - a

ans_list.append((v_max, v_min))
ans = v_max - v_min

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]"):
    A.sort()

    if A[0] >= 0:
        ret = sum(A) - 2 * A[0]
        print(ret)
        tmp = A[0]
        for i in range(N - 2):
            print(tmp, A[i + 1])
            tmp -= A[i + 1]
        print(A[-1], tmp)

    elif A[-1] <= 0:
        ret = -sum(A[:-1]) + A[-1]
        print(ret)
        tmp = A[-1]
        for i in range(N - 1):
            idx = -1 - i - 1
            print(tmp, A[idx])
            tmp -= A[idx]

    else:
        ret = 0
        cnt = 0
        for a in A:
            if a < 0:
                ret += -a
                cnt += 1
            else:
                ret += a
        print(ret)
        idx = cnt - 1
        tmp = A[idx]
        for i in range(idx, N - 2):
            print(tmp, A[i + 1])
            tmp -= A[i + 1]
        tmp2 = A[-1]
        for i in range(0, cnt - 1):
            print(tmp2, A[i])
            tmp2 -= A[i]
        print(tmp2, tmp)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Anega = [a for a in A if a<=0]
Aposi = [a for a in A if a>0]

if Anega and Aposi:
  print(sum(Aposi)-sum(Anega))
  temp = Anega[-1]
  for a in Aposi[:-1]:
    print(*(temp,a))
    temp = temp-a
  print(*(Aposi[-1],temp))
  temp = Aposi[-1]-temp
  for a in Anega[:-1]:
    print(*(temp,a))
    temp = temp-a

elif Aposi:
  print(sum(A[1:]) - A[0])
  a = A[0]
  for i in range(1,N-1):
    print(*(a, A[i]))
    a = a-A[i]
  print(*(A[-1],a))

else:
  print(A[-1] - sum(A[:-1]))
  temp = A[-1]
  for a in A[:-1]:
    print(*(temp,a))
    temp = temp-a

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]

a.sort()

results = []
x_min = a[0]
x_max = a[-1]
if x_min < 0 and x_max >= 0:
    # -と+にまたがる
    for i in range(1, n-1):
        if a[i] < 0:
            results.append((x_max, a[i]))
            x_max -= a[i]
        else:
            results.append((x_min, a[i]))
            x_min -= a[i]
elif a[0] >= 0:
    # +側only
    for i in range(1, n-1):
        results.append((x_min, a[i]))
        x_min -= a[i]
else:
    # -側only
    for i in range(1, n-1):
        results.append((x_max, a[i]))
        x_max -= a[i]

results.append((x_max, x_min))
print(x_max - x_min)
for x in results:
    print(str(x[0]) + " " + str(x[1]))

Submission status: Accepted

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

A_array.sort()

ans_array = []
if A_array[0] >= 0:
    m = A_array[0]
    for i in range(1, N - 1):
        ans_array.append((m, A_array[i]))
        m = m - A_array[i]
    ans_array.append((A_array[-1], m))
    m = A_array[-1] - m
    print(m)
    for ans in ans_array:
        print(ans[0], ans[1])
    exit()

elif A_array[-1] <= 0:
    m = A_array[-1]
    for i in range(N - 2, -1, -1):
        ans_array.append((m, A_array[i]))
        m = m - A_array[i]
    print(m)
    for ans in ans_array:
        print(ans[0], ans[1])
    exit()

else:
    change_index = 0
    m = A_array[-1]
    for i in range(N - 1):
        if A_array[i + 1] < 0:
            ans_array.append((m, A_array[i]))
            m -= A_array[i]
        else:
            change_index = i
            break
    n = A_array[change_index]

    for i in range(change_index + 1, N - 1):
        ans_array.append((n, A_array[i]))
        n -= A_array[i]

    ans_array.append((m, n))
    m = m - n
    print(m)
    for ans in ans_array:
        print(ans[0], ans[1])
    exit()

Submission status: Accepted

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

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

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


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


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


main()

Submission status: Accepted

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

li.sort()

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

Submission status: Accepted

Solution code:
import bisect

n = int(input())
a = list(map(int, input().split()))
b = [abs(i) for i in a]
a.sort()
if a[0] >= 0:
    print(sum(a[1:])-a[0])
    if n == 2:
        print(a[1],a[0])
    else:
        now = a[0]
        for i in range(n-2):
            print(now,a[i+1])
            now -= a[i+1]
        print(a[-1],now)
            
elif a[-1] <= 0:
    print(a[-1]-sum(a[:-1]))
    now = a[-1]
    for i in range(n-1):
        print(now, a[i])
        now -= a[i]
else:
    print(sum(b))
    zero = bisect.bisect_left(a,0)
    now1 = a[-1]
    now2 = a[0]
    for i in range(zero-1):
        print(now1,a[i+1])
        now1 -= a[i+1]
    for j in range(n-zero-1):
        print(now2,a[-(j+2)])
        now2 -= a[-(j+2)]
    print(now1,now2)

Submission status: Accepted

Solution code:
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    n = int(input())
    a_list = li()
    a_list.sort()

    mode = 1
    if a_list[0] <= 0 <= a_list[n - 1]:
        max_sum = sum(list(map(abs, a_list)))
        print(max_sum)
        min_index = 0
        max_index = n - 1
    elif a_list[0] < 0:
        max_value = a_list[n - 1]
        max2_index = n - 2
        max2_value = a_list[max2_index]

        while max2_index - 1 >= 0 and max_value == max2_value:
            max2_index = max2_index - 1
            max2_value = a_list[max2_index]

        max_sum = sum(list(map(abs, a_list))) - abs(max_value) * 2
        print(max_sum)
        min_index = 0
        max_index = n - 1

        print(a_list[max_index], a_list[max2_index])
        a_list[max_index] = a_list[max_index] - a_list[max2_index]
        a_list.pop(max2_index)
        max_index = max_index - 1
    else:
        min_value = a_list[0]
        min2_index = 1
        min2_value = a_list[min2_index]

        while min2_index + 1 < n and min_value == min2_value:
            min2_index = min2_index + 1
            min2_value = a_list[min2_index]

        max_sum = sum(list(map(abs, a_list))) - abs(min_value) * 2
        print(max_sum)
        min_index = 0
        max_index = n - 1

        print(a_list[min_index], a_list[min2_index])
        a_list[min_index] = a_list[min_index] - a_list[min2_index]
        a_list.pop(min2_index)
        max_index = max_index - 1

    while True:
        if min_index + 1 == max_index:
            print(a_list[max_index], a_list[min_index])
            break
        if a_list[min_index + 1] <= 0:
            print(a_list[max_index], a_list[min_index])
            a_list[max_index] = a_list[max_index] - a_list[min_index]
            min_index = min_index + 1
        else:
            print(a_list[min_index], a_list[max_index])
            a_list[min_index] = a_list[min_index] - a_list[max_index]
            max_index = max_index - 1

# 正整数と不正数が両方ある場合は、絶対値和にできる
# 負　正と選んで正を１つ減らすか、正　負と選んで正を１つ減らす操作を行い
# 最後に正　負とできるようにすればよい

# 正整数だけの場合
# なるべくロスが少なくなるように負の数を作る
# １番小さい数と２番目に小さい数を使って　1-2とし、負の数を作る
# 全て同じ数の場合は、0を作って再試行する
# 後は同様の手順

# 負整数だけの場合
# なるべくロスが少なくなるように正の数を作る
# １番大きい数と２番目に大きい数を使って　1-2とし、正の数を作る
# 後は同様の手順

Submission status: Accepted

Solution code:
n=int(input())
a=sorted(map(int, input().split()))
a1=[i for i in a if i>=0]
a2=[i for i in a if i<0]
if a1 and a2:
    print(sum(a1)-sum(a2))
    mx,mn=a1[-1], a2[0]
    for i in a1[:-1]:
        print(mn,i)
        mn-=i
    for i in a2[1:]:
        print(mx,i)
        mx-=i
    print(mx,mn)
elif not a2:
    print(sum(a1[1:])-a1[0])
    mx,mn=a1[-1], a1[0]
    for i in a1[1:-1]:
        print(mn,i)
        mn-=i
    print(mx,mn)
else:
    print(a2[-1]-sum(a2[:-1]))
    mx,mn=a2[-1], a2[0]
    for i in a2[1:-1]:
        print(mx,i)
        mx-=i
    print(mx,mn)

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
A = [int(i) for i in input().split()]
p = deque()
m = deque()

A.sort(reverse=True)
for i in A:
    if i > 0:
        p.append(i)
    else:
        m.append(i)

ans = []
for _ in range(N-2):
    if p and m:
        if len(p) < len(m):
            tt = p.popleft()
            zz = m.pop()
            p.appendleft(tt-zz)
            ans.append([tt,zz])
        else:
            tt = m.pop()
            zz = p.popleft()
            m.append(tt-zz)
            ans.append([tt,zz])
    elif p:
        tt = p.pop()
        zz = p.popleft()
        m.append(tt-zz)
        ans.append([tt,zz])

    else:
        tt = m.popleft()
        zz = m.pop()
        p.append(tt-zz)
        ans.append([tt,zz])

if p and m:
    tt = p.pop()
    zz = m.pop()
    ans.append([tt,zz])
    print(tt-zz)

elif p:
    tt = p.popleft()
    zz = p.pop()
    ans.append([tt,zz])
    print(tt-zz)

else:
    tt = m.popleft()
    zz = m.pop()
    ans.append([tt,zz])
    print(tt-zz)

for i in range(N-1):
    tt,zz = ans[i]
    print(tt,zz)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
b.sort()
if n == 2:
    print(b[-1]-b[0])
    print(b[-1], b[0])
    exit()
if b[0] == 0 and b[-1] == 0:
    print(0)
    for i in range(n-1):
        print(0,0)
elif b[0] > 0 and b[-1] > 0:
    ans = sum(b) - 2*b[0]
    print(ans)
    print(b[1], b[0])
    left = b[1] - b[0]
    right = b[-1]
    for i in range(2, n-1):
        x = left
        y = b[i]
        print(x, y)
        left = x - y
    print(right, left)
elif b[0] < 0 and b[-1] < 0:
    ans = -sum(b) + 2*b[-1]
    print(ans)
    b = b[::-1]
    print(b[0], b[1])
    left = 0
    right = b[0] - b[1]
    for i in range(2, n):
        x = right
        y = b[i]
        print(x, y)
        right = x - y
else:
    ans = abs(sum(abs(i) for i in b))
    print(ans)
    now = 1
    left = b[0]
    right = b[-1]
    while b[now] < 0:
        x = right
        y = b[now]
        print(x, y)
        right = x - y
        now += 1
    for i in range(now, n-1):
        x = left
        y = b[i]
        print(x, y)
        left = x - y
    print(right, left)

Submission status: Accepted

Solution code:
from copy import deepcopy
N=int(input())
A=list(map(int,input().split()))
if N==2:
    print(max(A[0]-A[1],A[1],A[0]))
    print(max(A[0],A[1]),min(A[0],A[1]))
    quit()
A.sort()
ans=10**(-50)
S=sum(A)
for i in range(N-1):
    S=S-A[i]+(-A[i])
    if ans<S:
        ans=S
        j=i
print(ans)
for i in range(j+1,N-1):
    print(A[0],A[i])
    A[0]-=A[i]
for i in range(j+1):
    print(A[N-1],A[i])
    A[N-1]-=A[i]

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  l.sort()

  ans=[]
  # +オンリー
  if l[0]>=0:
    a=l[0]
    for i in range(1,n-1):
      b=l[i]
      ans.append([a,b])
      a-=b
    ans.append([l[-1],a])

    print(l[-1]-a)
    for x in ans:
      print(x[0],x[1])

  # +と-がある
  elif l[0]<0 and l[-1]>=0:
    a=l[0] #最低
    b=l[-1] #最高

    for x in l[1:-1]:
      if x<=0:
        ans.append([b,x])
        b-=x
      else:
        ans.append([a,x])
        a-=x
    ans.append([b,a])

    print(b-a)
    for x in ans:
      print(x[0],x[1])

  # -オンリー
  else:
    l.reverse()
    a=l[0]
    for i in range(1,n-1):
      b=l[i]
      ans.append([a,b])
      a-=b
    ans.append([a,l[-1]])

    print(a-l[-1])
    for x in ans:
      print(x[0],x[1])

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N=int(input())
A=[int(i) for i in input().split()]
Aabs = [abs(a) for a in A]
zeroarray = []
plusarray = []
minusarray =[]
for n,a in enumerate(A):
    if a==0:
        zeroarray.append(a)
    elif a>0:
        plusarray.append(a)
    else:
        minusarray.append(a)
zeros =len(zeroarray)
plus = len(plusarray)
minus = N-zeros-plus
if zeros :#どんな正負も実現可能
    firstzero = 0
    print(sum(Aabs))
    if plus:#マイナスならfirstplusから、プラスならfirstzeroから、最後にfirsrtplusからfirstzeroを引く
        firstplus = plusarray[0]
        for i in range(1,plus):
            print(firstzero, plusarray[i])
            firstzero-=plusarray[i]
        if minus:
            for m in minusarray:
                print(firstplus,m)
                firstplus-=m
        if zeros>1:
            for i in range(1,zeros):
                print(firstzero,0)
        print(firstplus,firstzero)
    else:#全部firstzeroから引く
        if zeros>1:
            for z in range(zeros-1):
                print(0,0)
        for m in minusarray:
            print(firstzero,m)
            firstzero-=m
    sys.exit()
    
if (plus and minus):#どんな正負も実現可能
    firstminus = minusarray[0]
    print(sum(Aabs))
    firstplus = plusarray[0]
    firstminus = minusarray[0]
    for i in range(1,plus):
        print(firstminus, plusarray[i])
        firstminus-=plusarray[i]
    for i in range(1,minus):
        print(firstplus,minusarray[i])
        firstplus-=minusarray[i]
    print(firstplus,firstminus)
    sys.exit()
    
    
if plus:#全部プラスなのでminplusから一個以外引いた上でfirstplusから引く
    plusarray.sort()
    minplus = plusarray[0]
    firstplus = plusarray[1]
    print(sum(Aabs)-2*minplus)
    for i in range(2,plus):
        print(minplus, plusarray[i])
        minplus-=plusarray[i]
    print(firstplus,minplus)
    sys.exit()
   
else:#全部マイナスなのでmaxminusから一個以外引く
    minusarray.sort(reverse = True)
    maxminus = minusarray[0]
    print(sum(Aabs)+2*maxminus)
    for i in range(1,minus):
        print(maxminus, minusarray[i])
        maxminus-=minusarray[i]
    sys.exit()

Submission status: Accepted

Solution code:
import sys

cin = sys.stdin
N, = map(int, cin.readline().split())
A = list(map(int, cin.readline().split()))

pos = []
neg = []
for a in A:
    if a>0:
        pos.append(a)
    else:
        neg.append(a)
if len(neg)>0:
    if len(pos)>0:
        ttl = sum(pos) - sum(neg)
    else:
        ttl = - sum(neg) + 2 * max(neg)
else:
    # N >= 2 => len(pos)>=2
    ttl = sum(pos) - 2 * min(pos) - sum(neg)
print(ttl)

acc = 0
if len(neg)>0:
    if len(pos)>0:
        for i in range(len(pos)-1):
            print(neg[0], pos[i])
            neg[0] -= pos[i]
        print(pos[-1], neg[0])
        pos[-1] -= neg[0]
        for i in range(1, len(neg)):
            print(pos[-1], neg[i])
            pos[-1] -= neg[i]
    else:
        neg.sort()
        for i in range(len(neg)-2, -1, -1):
            print(neg[-1], neg[i])
            neg[-1] -= neg[i]
else:
    # N >= 2 => len(pos)>=2
    pos.sort()
    if N == 2:
        print(pos[1], pos[0])
    else:
        for i in range(1, len(pos)-1):
            print(pos[0], pos[i])
            pos[0] -= pos[i]
        print(pos[-1], pos[0])

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int, input().split()))
A.sort()
pA = []
nA = []
for a in A:
    if a < 0:
        nA.append(a)
    else:
        pA.append(a)

ans=[]
def calc():
    # pAもnAも長さが2以上ある場合
    if len(pA) > 0 and len(nA) > 0: 
        for i in range(len(pA) -1):
            ans.append([nA[0], pA[i]])
            nA[0] -=  pA[i]

        for i in range(len(nA) -1):
            ans.append([pA[-1], nA[-i-1]])
            pA[-1] -= nA[-i-1]

        ans.append([pA[-1], nA[0]])
        return pA[-1] - nA[0]
    elif not nA:
        for i in range(1,len(pA) -1):
            ans.append([pA[0], pA[i]])
            pA[0] -=  pA[i]

        ans.append([pA[-1], pA[0]])
        return pA[-1] - pA[0]
    elif not pA:
        for i in range(1,len(nA) -1):
            ans.append([nA[-1], nA[-i-1]])
            nA[-1] -=  nA[-i-1]

        ans.append([nA[-1], nA[0]])
        return nA[-1] - nA[0]


x = calc()

print(x)
for a in  ans:
    print(*a)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
a = list(map(int, input().split()))
a.sort()
max_a = max(a)
min_a = min(a)

if max(a) <= 0:
    count = max_a
    for i in range(n - 1):
        count += abs(a[i])
    print(count)
    for i in range(n - 1):
        print(max_a, a[i])
        max_a -= a[i]
elif min(a) >= 0:
    count = -min_a
    for i in range(n - 1):
        count += a[i + 1]
    print(count)
    for i in range(n - 2):
        print(min_a, a[i + 1])
        min_a -= a[i + 1]
    print(max_a, min_a)
else:
    count = 0
    for i in range(n):
        count += abs(a[i])
    print(count)
    num = bisect.bisect(a, 0)
    for i in range(num + 1, n):
        print(min_a, a[i])
        min_a -= a[i]
    a_num = a[num]
    a[0] = min_a
    for i in range(num):
        print(a_num, a[i])
        a_num -= a[i]

Submission status: Accepted

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


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

from collections import deque
X=deque(A)

while X[0]>=0 and len(X)>=3:
    x=X.popleft()
    z=X.pop()
    ANS.append([x,z])
    X.appendleft(x-z)

if len(X)==2:
    x=X[0]
    z=X[-1]

    ANS.append([z,x])
    print(z-x)
    for ans in ANS:
        print(*ans)
    sys.exit()
    




if X[0]<0:
    
    
    z=X.popleft()

    Y=[]
    while len(X)>=2:
        x=X.popleft()
        if x<0:
            Y.append(x)
        else:
            ANS.append([z,x])
            z=z-x

    x=X.pop()
    for y in Y:
        ANS.append([x,y])
        x-=y

    ANS.append([x,z])
    x-=z

    print(x)
    for ans in ANS:
        print(*ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
n=int(input())
a=list(map(int,input().split()))
a.sort()
if(a[0]>=0):
    print(sum(a[1:])-a[0])
    ans=a[0]
    for i in a[1:n-1]:
        print(ans,i)
        ans-=i
    print(a[-1],ans)
elif(a[-1]<=0):
    print(a[-1]-sum(a[:n-1]))
    ans=a[-1]
    for i in a[:n-1]:
        print(ans,i)
        ans-=i
else:
    print(sum([abs(i) for i in a]))
    b=bisect_left(a,0)
    ans=a[0]
    tmp=a[-1]
    for i in a[b:n-1]:
        print(ans,i)
        ans-=i
    for i in a[1:b]:
        print(tmp,i)
        tmp-=i
    print(tmp,ans)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
arr.sort()
if(arr[0]>=0):
	sumx=sum(arr)-2*(arr[0])
	ansarr=[]
	if(n==2):
		ansarr.append(arr[1])
		ansarr.append(arr[0])
		val=arr[1]-arr[0]
	else:
		ansarr.append(arr[0])
		ansarr.append(arr[1])
		val=arr[0]-arr[1]
		for i in range(2,n-1):
			ansarr.append(val)
			ansarr.append(arr[i])
			val=val-arr[i]
		ansarr.append(arr[n-1])
		ansarr.append(val)
		val=arr[n-1]-val
	print(val)
	print(*ansarr)

elif(arr[-1]<=0):
	sumx=0
	for i in range(n-1):
		sumx+=abs(arr[i])
	sumx-=abs(arr[n-1])
	ansarr=[]
	if(n==2):
		ansarr.append(arr[1])
		ansarr.append(arr[0])
		val=arr[1]-arr[0]
	else:
		ansarr.append(arr[n-1])
		ansarr.append(arr[n-2])
		val=arr[n-1]-arr[n-2]
		for i in range(n-3,-1,-1):
			ansarr.append(val)
			ansarr.append(arr[i])
			# ansarr.append(val,arr[i])
			val=val-arr[i]

	print(val)
	print(*ansarr)
else:
	val=0
	index=-1
	for i in range(n):
		if(arr[i]>0):
			index=i
			break
	val=arr[0]
	ansarr=[]
	for i in range(index,n-1):
		ansarr.append(val)
		ansarr.append(arr[i])
		# ansarr.append(val,arr[i])
		val-=arr[i]

	ansarr.append(arr[n-1])
	ansarr.append(val)
	val=arr[n-1]-val
	for i in range(1,index):
		ansarr.append(val)
		ansarr.append(arr[i])
		# ansarr.append(val,arr[i])
		val-=arr[i]

	print(val)
	print(*ansarr)

Submission status: Accepted

Solution code:
def getN():
    return int(input())

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

def getlist():
    return list(map(int, input().split()))
import sys

n = getN()
nums = getlist()
nums.sort()

from bisect import bisect_left
insert = bisect_left(nums, 0)


if insert == 0:
    print(sum(nums) - 2 * nums[0])
    tmp = nums[0]
    for num in nums[1:-1]:
        print(tmp, num)
        tmp = tmp - num
    print(nums[-1], tmp)
    sys.exit()

if insert == len(nums):
    nums.sort(reverse=True)
    print(abs(sum(nums)) + 2 * nums[0])
    tmp = nums[0]
    for num in nums[1:]:
        print(tmp, num)
        tmp = tmp - num
    sys.exit()

neg = nums[:insert]
pos = nums[insert:]
neg_end = neg[0]
pos_end = pos[-1]
print(abs(sum(neg)) + sum(pos))
for ne in neg[1:]:
    print(pos_end, ne)
    pos_end -= ne

for po in pos[:-1]:
    print(neg_end, po)
    neg_end -= po

print(pos_end, neg_end)




"""
5
8 9 -2 -3 10
"""

Submission status: Accepted

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

# 全部+,-は無理だが、それ以外は任意に符号を決められる
plus = []
minus = []
for a in A:
  if a >= 0:
    plus.append(a)
  else:
    minus.append(a)
plus.sort()
plus.reverse()
minus.sort()
if len(plus) == 0:
  plus.append(minus.pop())
if len(minus) == 0:
  minus.append(plus.pop())

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

tmp = minus[0]
for p in plus[1:]:
  print(tmp, p)
  tmp = tmp - p
print(plus[0], tmp)
tmp = plus[0] - tmp
for m in minus[1:]:
  print(tmp, m)
  tmp = tmp - m