Problem:


Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0



Sample input:
3
1 -1 2


Sample output:
4
-1 1
2 -2

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
t = 0
for m in range(N):
    if a[m] < 0:
        t += 1
if N > 2:
    if t == 0:
        c = a[0]
        for i in range(N - 2):
            c = c - a[N - 1 - i]
        d = a[1] - c
        print(d)
        e = a[0]
        f = a[N - 1]
        for j in range(N - 2):
            print(e, end = ' ')
            print(f)
            e = e - f
            f = a[N - 2 - j]
        print(a[1], end = ' ')
        print(e)
    elif t > 0 and t < N - 1:
        c = 0
        for i in range(t):
            c += a[i] 
        for j in range(N - t -1):
            c = c - a[t + 1 + j]
        d = a[t] - c
        print(d)
        e = a[0]
        f = a[N - 1]
        for k in range(N - t - 1):
            print(e, end = ' ')
            print(f)
            e = e - f
            f = a[N - 2 - k]
        a[0] = e
        g = a[t]
        for l in range(t):
            print(g, end = ' ')
            print(a[i])
            g = g - a[l]
    elif t == N - 1 or t == N:
        c = a[N - 1]
        for i in range(N - 1):
            c = c - a[i]
        print(c)
        d = a[N - 1]
        for j in range(N - 1):
            print(d, end = ' ')
            print(a[j])
            d -= a[j]
else:
    print(a[1] - a[0])
    print(a[1], end = ' ')
    print(a[0])

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
t = 0
for m in range(N):
    if a[m] < 0:
        t += 1
if N > 2:
    if t == 0:
        c = a[0]
        for i in range(N - 2):
            c = c - a[N - 1 - i]
        d = a[1] - c
        print(d)
        e = a[0]
        f = a[N - 1]
        for j in range(N - 2):
            print(e, end = ' ')
            print(f)
            e = e - f
            f = a[N - 2 - j]
        print(a[1], end = ' ')
        print(e)
    elif t > 0 and t < N - 1:
        c = 0
        for i in range(t):
            c += a[i] 
        for j in range(N - t -1):
            c = c - a[t + 1 + j]
        d = a[t] - c
        print(d)
        e = a[0]
        f = a[N - 1]
        for k in range(N - t - 1):
            print(e, end = ' ')
            print(f)
            e = e - f
            f = a[N - 2 - k]
        a[0] = e
        for l in range(t):
            print(a[t], end = ' ')
            print(a[i])
    elif t == N - 1 or t == N:
        c = a[N - 1]
        for i in range(N - 1):
            c = c - a[i]
        print(c)
        d = a[N - 1]
        for j in range(N - 1):
            print(d, end = ' ')
            print(a[j])
            d -= a[j]
else:
    print(a[1] - a[0])
    print(a[1], end = ' ')
    print(a[0])

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
t = 0
for m in range(N):
    if a[m] < 0:
        t += 1
if N > 2:
    if t == 0:
        c = a[0]
        for i in range(N - 2):
            c = c - a[N - 1 - i]
        d = a[1] - c
        print(d)
        e = a[0]
        f = a[N - 1]
        for j in range(N - 2):
            print(e, end = ' ')
            print(f)
            e = e - f
            f = a[N - 2 - j]
        print(a[1], end = ' ')
        print(e)
    elif t > 0 and t < N - 1:
        c = 0
        for i in range(t):
            c += a[i] 
        for j in range(N - t -1):
            c = c - a[t + 1 + j]
        d = a[t] - c
        print(d)
        e = a[0]
        f = a[N - 1]
        for k in range(N - t - 1):
            print(e, end = ' ')
            print(f)
            e = e - f
            f = a[N - 2 - k]
        a[0] = e
        for l in range(t):
            print(a[t], end = ' ')
            print(a[i])
    elif t == N - 1 or t == N:
        c = 0
        for i in range(N - 1):
            c = a[N - 1] - a[i]
        print(c)
        for j in range(N - 1):
            print(a[N - 1], end = ' ')
            print(a[j])
else:
    print(a[1] - a[0])
    print(a[1], end = ' ')
    print(a[0])

Submission status: Wrong Answer

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

p = A[-1]
n = A[0]

ans_list = []

for a in A[1:-1]:
    if a >= 0:
        ans_list.append((n, a))
        n -= a
    else:
        ans_list.append((p, a))
        p -= a

M = p - n
ans_list.append((p, n))

print(M)
for ans in ans_list:
    print(ans)

Submission status: Wrong Answer

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

ans_list = []
p = []
n = []

# 最大にプラス、最小にマイナスを割り当てる
p.append(A[-1])
n.append(A[0])

# 正ならプラスを負ならマイナスを割り当てる
for a in A[1:-1]:
    if a >= 0:
        p.append(a)
    elif a < 0:
        n.append(a)

# プラスが１つになるまで消す
while len(p) != 1:
    x = n.pop()
    y = p.pop()
    n.append(x-y)
    ans_list.append((x, y))

# マイナスがなくなるまで消す
while len(n) != 0:
    x = p.pop()
    y = n.pop()
    p.append(x-y)
    ans_list.append((x,y))

# 残ったものが最大値
M = p[0]
print(M)
for ans in ans_list:
    print(ans)

Submission status: Wrong Answer

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

p = bisect.bisect_left(a, 0)
p = 1 if p < 2 and n == 3 else min(max(2, p), n - 1)
print(sum(a[p:]) - sum(a[:p]))
b = a[p - 1]
for i in range(p, n - 1):
    print(b, a[i])
    b -= a[i]
c = a[n - 1]
for i in range(p - 1):
    print(c, a[i])
    c -= a[i]
print(c, b)

Submission status: Wrong Answer

Solution code:
def c_successive_subtraction(N, A):
    a = sorted(A, reverse=True)

    operation = []
    while len(a) > 2:
        x = a.pop()
        y = a.pop()
        a.append(x - y)
        operation.append('{} {}'.format(x, y))
    operation.append('{} {}'.format(a[0], a[1]))
    return str(a[0] - a[1]) + '\n' + '\n'.join(operation)

N = int(input())
A = [int(i) for i in input().split()]
print(c_successive_subtraction(N, A))

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
A = sorted(list(map(int, input().split())))
if N == 2:
    x, y = A[1], A[0]
    print(x-y)
    print(x, y)
    exit()
idx = bisect.bisect_left(A, 0)
if idx:
    if idx < N:
        Q = A[:idx]
        P = A[idx:]
    else:
        Q = A[:-1]
        P = [A[-1]]
else:
    Q = [A[0]]
    P = A[1:]


XY = []
LP = len(P)
for i in range(LP-1):
    XY.append((Q[0], P[i]))
    Q[0] -= P[i]
LQ = len(Q)
for i in range(LQ):
    XY.append((Q[i], P[-1]))
    P[-1] = P[-1] - Q[i]
ans = P[-1]
print(ans)
for x, y in XY:
    print(x, y)

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    minus = []
    plus = []
    M = I()
    A = LI()
    zero = []
    ans = []
    for a in A:
        if a < 0:
            minus.append(a)
        elif a > 0:
            plus.append(a)
        else:
            zero.append(0)
    minus.sort()
    plus.sort(reverse = True)
    if not minus:
        #print(minus,plus,zero)
        if len(plus) >= 2:
            a = plus.pop()
            b = plus.pop()
            minus.append(a-b)
            ans.append("{} {}".format(a, b))
        elif zero and plus:
            a = zero.pop()
            b = plus.pop()
            minus.append(a-b)
            ans.append("{} {}".format(a, b))
        
    if not plus:
        if len(minus):
            a = minus.pop()
            b = minus.pop()
            plus.append(a-b)
            ans.append("{} {}".format(a, b))
        elif zero and minus:
            a = zero.pop()
            b = minus.pop()
            plus.append(a-b)
            ans.append("{} {}".format(a, b))
    while minus and plus:
        if len(minus) == len(plus) == 1:
            a = plus.pop()
            b = minus.pop()
            ans.append("{} {}".format(a,b))
            plus.append(a - b)
            break
        a = minus.pop()
        b = plus.pop()
        if len(minus) > len(plus):
            ans.append("{} {}".format(b,a))
            plus.append(b-a)
        else:
            ans.append("{} {}".format(a,b))
            minus.append(a - b)

    while zero:
        if minus:
            a = zero.pop()
            b = minus.pop()
            #print(a, b)
        elif plus:
            a = plus.pop()
            b = zero.pop()
            #print(a, b)
        elif len(zero) >= 2:
            a = zero.pop()
            b = zero.pop()
            #print(a,b)
        else:
            break
        plus.append(a - b)
        ans.append("{} {}".format(a,b))

    if zero:
        print(*zero)
    elif minus:
        print(*minus)
    else:
        print(*plus)
    print(*ans,sep="\n")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if (A[0] >= 0):
    print(sum(A)-A[0]*2)
elif (A[-1] <= 0):
    print(-sum(A)+A[-1]*2)
else:
    print(sum(list(map(abs, A))))
    
for i in range(1, N-1):
    if (A[i] < 0):
        print(A[N-1], A[i])
        A[N-1] -= A[i]
    else:
        print(A[0], A[i])
        A[0] -= A[i]
    print(A[N-1], A[0])

Submission status: Wrong Answer

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

if (A[0] >= 0):
    print(sum(A)-A[0]*2)
elif (A[-1] <= 0):
    print(-sum(A)+A[-1]*2)
else:
    print(sum(list(map(abs, A))))
    for i in range(1, N-1):
        if (A[i] < 0):
            print(A[N-1], A[i])
            A[N-1] -= A[i]
        else:
            print(A[0], A[i])
            A[0] -= A[i]
    print(A[N-1], A[0])

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
inf = float("inf")
A = sorted(list(map(int, input().split())))
if N == 2:
    x, y = A[1], A[0]
    print(x-y)
    print(x, y)
    exit()
idx = bisect.bisect_left(A, 0)
if A[:idx]:
    if A[idx:]:
        M = sum(A[:idx])
        P = sum(A[idx:])
        ans = P - M
    else:
        M = sum(A[:-1])
        M_mini = A[-1]
        ans = M_mini - M
else:
    P_mini = A[0]
    P = sum(A[1:])
    ans = P - P_mini
print(ans)

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    minus = []
    plus = []
    M = I()
    A = LI()
    zero = []
    ans = []
    for a in A:
        if a < 0:
            minus.append(a)
        elif a > 0:
            plus.append(a)
        else:
            zero.append(0)
    minus.sort()
    plus.sort(reverse = True)
    if not minus:
        #print(minus,plus,zero)
        if len(plus) >= 2:
            a = plus.pop()
            b = plus.pop()
            minus.append(a-b)
            ans.append("{} {}".format(a, b))
        elif zero and plus:
            a = zero.pop()
            b = plus.pop()
            minus.append(a-b)
            ans.append("{} {}".format(a, b))
        
    if not plus:
        if len(minus):
            a = minus.pop()
            b = minus.pop()
            plus.append(a-b)
            ans.append("{} {}".format(a, b))
        elif zero and minus:
            b = zero.pop()
            a = minus.pop()
            plus.append(a-b)
            ans.append("{} {}".format(a, b))
    while minus and plus:
        if len(minus) == len(plus) == 1:
            a = plus.pop()
            b = minus.pop()
            ans.append("{} {}".format(a,b))
            plus.append(a - b)
            break
        a = minus.pop()
        b = plus.pop()
        if len(minus) > len(plus):
            ans.append("{} {}".format(b,a))
            plus.append(b-a)
        else:
            ans.append("{} {}".format(a,b))
            minus.append(a - b)

    while zero:
        if minus:
            a = zero.pop()
            b = minus.pop()
            #print(a, b)
        elif plus:
            a = plus.pop()
            b = zero.pop()
            #print(a, b)
        elif len(zero) >= 2:
            a = zero.pop()
            b = zero.pop()
            #print(a,b)
        else:
            break
        plus.append(a - b)
        ans.append("{} {}".format(a,b))

    if zero:
        print(*zero)
    elif minus:
        print(*minus)
    else:
        print(*plus)
    print(*ans,sep="\n")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    minus = []
    plus = []
    M = I()
    A = LI()
    zero = []
    ans = []
    for a in A:
        if a < 0:
            minus.append(a)
        elif a > 0:
            plus.append(a)
        else:
            zero.append(0)
    minus.sort()
    plus.sort(reverse = True)
    if not minus:
        #print(minus,plus,zero)
        if len(plus) >= 2:
            a = plus.pop()
            b = plus.pop()
            minus.append(a-b)
            ans.append("{} {}".format(a, b))
        elif zero and plus:
            a = zero.pop()
            b = plus.pop()
            minus.append(a-b)
            ans.append("{} {}".format(a, b))
        
    if not plus:
        if len(minus):
            a = minus.pop()
            b = minus.pop()
            plus.append(a-b)
            ans.append("{} {}".format(a, b))
        elif zero and minus:
            b = zero.pop()
            a = minus.pop()
            plus.append(a-b)
            ans.append("{} {}".format(a, b))
    while minus and plus:
        #print(minus,plus,zero)
        if len(minus) == len(plus) == 1:
            a = plus.pop()
            b = minus.pop()
            ans.append("{} {}".format(a,b))
            plus.append(a - b)
            break
        a = minus.pop()
        b = plus.pop()
        if len(minus) > len(plus):
            ans.append("{} {}".format(b,a))
            plus.append(b-a)
        else:
            ans.append("{} {}".format(a,b))
            minus.append(a - b)

    while zero:
        if minus:
            a = zero.pop()
            b = minus.pop()
            #print(a, b)
        elif plus:
            a = plus.pop()
            b = zero.pop()
            #print(a, b)
        elif len(zero) >= 2:
            a = zero.pop()
            b = zero.pop()
            #print(a,b)
        else:
            break
        plus.append(a - b)
        ans.append("{} {}".format(a,b))

    if zero:
        print(*zero)
    elif minus:
        print(*minus)
    else:
        print(*plus)
    print(*ans,sep="\n")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
plus=[a[i] for i in range(n) if a[i]>=0]
l_plus=len(plus)
minus=[a[i] for i in range(n) if a[i]<0]
l_minus=len(minus)
x_cal=[]
y_cal=[]
if n==2:
  a.sort()
  print(a[1]-a[0])
  print(a[0],a[1])
else:
  if plus!=[] and minus!=[]:
    x_cal.append(minus[0])
    for j in range(1,l_plus-1):
      x_cal.append(x_cal[-1]-plus[j])
    for k in range(1,l_plus):
      y_cal.append(plus[k])
    if y_cal!=[]:
      y_last=x_cal[-1]-y_cal[-1]
    else:
      y_last=0
    x_cal.append(plus[0])
    for j in range(1,l_minus):
      x_cal.append(x_cal[-1]-minus[j])
    for k in range(1,l_minus):
      y_cal.append(minus[k])
    y_cal.append(y_last)
    print(x_cal[-1]-y_cal[-1])
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])
  elif minus==[]:
    a.sort()
    print(sum(a)-2*a[0])
    x_cal.append(a[0])
    for ij in range(1,n-2):
      x_cal.append(x_cal[-1]-a[ij])
    for jj in range(0,n-2):
      y_cal.append(a[jj+1])
    y_cal.append(x_cal[-1]-y_cal[-1])
    x_cal.append(a[-1])
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])
  else:
    a.sort()
    print(a[-1]*2-sum(a))
    x_cal.append(a[-1])
    for ij in range(1,n-2):
      x_cal.append(x_cal[-1]-a[ij])
    for jj in range(0,n-2):
      y_cal.append(a[jj+1])
    x_cal.append(x_cal[-1]-y_cal[-1])
    y_cal.append(a[0])
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])

Submission status: Wrong Answer

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    print(A[-1], tmp)

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

elif count_p + count_m == 0:
    print(0)
    for i in range(N-1):
        print(0, 0)

else:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
plus=[a[i] for i in range(n) if a[i]>=0]
l_plus=len(plus)
minus=[a[i] for i in range(n) if a[i]<0]
l_minus=len(minus)
x_cal=[]
y_cal=[]
if n==2:
  a.sort()
  print(a[1]-a[0])
  print(a[0],a[1])
else:
  if plus!=[] and minus!=[]:
    x_cal.append(minus[0])
    for j in range(1,l_plus-1):
      x_cal.append(x_cal[-1]-plus[j])
    for k in range(1,l_plus):
      y_cal.append(plus[k])
    if y_cal!=[]:
      y_last=x_cal[-1]-y_cal[-1]
    else:
      y_last=0
    x_cal.append(plus[0])
    for j in range(1,l_minus):
      x_cal.append(x_cal[-1]-minus[j])
    for k in range(1,l_minus):
      y_cal.append(minus[k])
    y_cal.append(y_last)
    print(x_cal[-1]-y_cal[-1])
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])
  elif minus==[]:
    a.sort()
    print(sum(a)-2*a[0])
    x_cal.append(a[0])
    for ij in range(1,n-2):
      x_cal.append(x_cal[-1]-a[ij])
    for jj in range(0,n-2):
      y_cal.append(a[jj+1])
    y_cal.append(x_cal[-1]-y_cal[-1])
    x_cal.append(a[-1])
    print(x_cal,y_cal)
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])
  else:
    a.sort()
    print(a[-1]*2-sum(a))
    x_cal.append(a[-1])
    for ij in range(1,n-2):
      x_cal.append(x_cal[-1]-a[ij])
    for jj in range(0,n-2):
      y_cal.append(a[jj+1])
    x_cal.append(x_cal[-1]-y_cal[-1])
    y_cal.append(a[0])
    print(x_cal,y_cal)
    for ii in range(n-1):
      print(x_cal[ii],y_cal[ii])

Submission status: Wrong Answer

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

A = sorted(A)

count_m = 0
count_z = 0
count_p = 0
sum_abs = 0

for i in range(N):
    if A[i] < 0:
        count_m += 1
        sum_abs -= A[i]
    elif A[i] == 0:
        count_z += 1
    else:
        count_p += 1
        sum_abs += A[i]

if count_m + count_z == 0:
    print(sum_abs-2*min(A))
    tmp = A[0]
    for i in range(N-2):
        print(tmp, A[i+1])
        tmp -= A[i+1]
    print(A[-1], tmp)

elif count_p + count_z == 0:
    print(sum_abs+2*max(A))
    tmp = A[-1]
    for i in range(N-1):
        print(tmp, A[i])
        tmp -= A[i]

else:
    print(sum_abs)
    cmz = count_m + count_z
    tmp = A[-1]
    for i in range(cmz - 1):
        print(tmp, A[i])
        tmp -= A[i]
    tmp_2 = A[cmz-1]
    for i in range(count_p-1):
        print(tmp_2, A[i+cmz])
        tmp_2 -= A[i+cmz]
    print(tmp, tmp_2)

Submission status: Wrong Answer

Solution code:
import sys

def bigsmall(x,y):
    return (x,y) if x>y else (y,x)

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

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    if a[0] < 0 and a[-1] > 0:
        first = a.pop(-1)
        a.insert(0,first)

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[1])
    if a[2] <= 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    for i in range(2, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] <= 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    if a[-1] <= 0:
        ops.append((curr, a[-1]))
        curr = curr - a[-1]
    else:
        ops.append((a[-1], curr))
        curr = a[-1] - curr

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
A_plus = [a for a in A if a >= 0]
A_minus = [a for a in A if a < 0]

if len(A_minus) == 0:
    M = A[-1] - (A_plus[0] - sum(A_plus[1:-1]))
elif len(A_plus) == 0:
    M = A[-1] - sum(A_plus[:-1])
else:
    M = (A_plus[-1] - sum(A_minus[1:])) - (A_minus[0] - sum(A_plus[:-1]))
print(M)

opes = []
n_max = A[-1]
n_min = A[0]
for a in A[1:-1]:
    if a >= 0:
        print(n_min, a)
        n_min -= a
    else:
        print(n_max, a)
        n_max -= a

print(n_max, n_min)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

log = []

# すべて正
if A[-1] > 0 :
    a = A.pop()
    b = A.pop()
    log.append([a, b])
    A.append(a - b)
# すべて負
elif A[0] < 0 :
    a = A.pop(0)
    b = A.pop()
    log.append([a, b])
    A = [a-b] + A
    
i = 1
while A[i] > 0 :
    a = A.pop()
    log.append([a, A[i]])
    A.append(a-A[i])
    i += 1
    
log.append([A[0], A[-1]])
print(A[0]-A[-1])
for l in log :
    print(l[0], l[1])

Submission status: Wrong Answer

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

# すべて正
if A[-1] > 0 :
    a = A.pop()
    b = A.pop()
    print(a, b)
    A.append(a - b)
# すべて負
elif A[0] < 0 :
    a = A.pop(0)
    b = A.pop()
    print(a, b)
    A = [a-b] + A
    
i = 1
while A[i] > 0 :
    a = A.pop()
    print(a, A[i])
    A.append(a-A[i])
    i += 1
    
print(A[0], A[-1])
print(A[0]-A[-1])

Submission status: Wrong Answer

Solution code:
N=int(input())
listA=list(map(int,input().split()))
listB=[abs(i) for i in listA]
MAX=max(listA)
MIN=min(listA)
ans=sum(listB)
if MAX<=0:
  print(ans+MAX)
elif MIN<=0:
  print(ans)  
else:
  print(ans-MIN)
listC=sorted(listA)  
for i in range(len(listA)-2):
  if listC[i+1]>=0:
    print(MIN,listC[i+1])
    MIN=MIN-listC[i+1]
  else:
    print(MAX,listC[i+1])
    MAX=MAX-listC[i+1]
print(MAX,MIN)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
A_plus = [a for a in A if a >= 0]
A_minus = [a for a in A if a < 0]

if len(A_minus) == 0:
    M = A[-1] - (A_plus[0] - sum(A_plus[1:-1]))
elif len(A_plus) == 0:
    M = A[-1] - sum(A_plus[:-1])
else:
    M = (A_plus[-1] - sum(A_minus[1:])) - (A_minus[0] - sum(A_plus[:-1]))
print(M)

Submission status: Wrong Answer

Solution code:
import sys

def bigsmall(x,y):
    if x > y:
        return x,y
    else:
        return y,x

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

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    if a[0] < 0 and a[-1] > 0:
        first = a.pop(-1)
        a.insert(0,first)

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[1])
    if a[2] < 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    for i in range(2, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] < 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    if a[-1] < 0:
        ops.append((curr, a[-1]))
        curr = curr - a[-1]
    else:
        ops.append((a[-1], curr))
        curr = a[-1] - curr

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
listA=list(map(int,input().split()))
listB=[abs(i) for i in listA]
MAX=max(listA)
MIN=min(listA)
ans=sum(listB)
if MIN<=0:
  print(ans)
else:
  print(ans-MIN)
listC=sorted(listA)  
for i in range(len(listA)-2):
  if listC[i+1]>=0:
    print(MIN,listC[i+1])
    MIN=MIN-listC[i+1]
  else:
    print(MAX,listC[i+1])
    MAX=MAX-listC[i+1]
print(MAX,MIN)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 29 18:22:16 2019

@author: 479909
"""
import numpy as np
import math
#A = str(input())
#A = "ABCABC"
#N = int(input())
#[N,K] = list(map(int,input().split()))
N = int(input())
A = list(map(int,input().split()))
#N = 4
#A = [4,3,2,1]
num_plus = 0
num_minus = 0
A.sort(reverse = True)
for i in range(len(A)):
    if A[i] <= 0:
        num_minus += 1
    else:
        num_plus += 1
min_i = math.floor((N)/2)
if num_minus >= min_i:
    ans = 0
    B = A[:]
    for i in range(len(A)):
        ans += abs(A[i])
    print(ans)
    for  i in range(num_plus-1):
        print(A[num_plus+i],A[i+1])
        b = A[num_plus+i] - A[i+1]
        B.pop(i+1-i)
        B.pop(num_plus+i-1-2*i)
        B.append(b)
    for i in range(1,len(B)):
        print(B[0],B[1])
        B[0] = B[0] - B[1]
        B.pop(1)
else:
    ans = 0
    B = A
    for i in range(N):
        if i < N - min_i:
            ans += A[i]
        else:
            ans += -A[i]
    print(ans)
    B = A[:]
    for i in range(N-1-min_i):
        print(A[N-1-min_i+1+i],A[i+1])
        b = A[N-1-min_i+1+i] - A[i+1]
        B.pop(1)
        B.pop(N-1-min_i+1+i-1-2*i)
        B.append(b)
    for i in range(1,len(B)):
        print(B[0],B[1])
        B[0] = B[0] - B[1]
        B.pop(1)