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

import copy

List.sort()

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

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

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

print(result)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
A = sorted(A)
removeNums = []
for i in range(len(A)-1):
    newValue = A[0] - A[-1]
    removeNums.append((A[0], A[-1]))
    A.remove(A[0])
    A.remove(A[-1])
    A.insert(0, newValue)

print(-1*A[0])
for i in range(len(removeNums)-1):
    print(removeNums[i][0], removeNums[i][1])

print(removeNums[-1][1], removeNums[-1][0])

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L2=[i*-1 for i in L[1:-1]]
L2.sort()
print(L[-1]-(L[0]+sum(L2)))
n=0
while n<=N-2:
  s=0
  if n==0:
    print(L[0], end=" ")
    print(L[1])
    n=n+1
  else:
    print(L[n+1], end=" ")
    print(L[0]+sum(L2[:n+1]))
    n=n+1

Submission status: Wrong Answer

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

A = sorted(A)
l = []

if A[len(A)//2] > 0:
    for i in range(N-1):
        l.append([A[0],A[-1]])
        diff = A[0]-A[-1]
        A = [diff]+A[1:len(A)-1]

        #print(A)
else:
    for i in range(N-1):
        l.append([A[-1],A[0]])
        diff = A[-1]-A[0]
        A = A[1:len(A)-1] + [diff]

        #print(A)

print(-A[0])
for i in range(len(l)-1):
    print(l[i][0],l[i][1])

print(l[len(l)-1][1],l[len(l)-1][0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#c.py
from collections import deque

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

Submission status: Wrong Answer

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

switch=0
counter=0
mazu=newlist[0]
if newlist[0]>=0:
    seihu=1
else:
    seihu=-1
konzai=0

for nl in newlist:
    if mazu!=nl:
        switch=1

if switch==1:
    wa=0
    list2=[]
    for nl in range(n):
        list2.append([newlist[nl], abs(newlist[nl])])
        if seihu*newlist[nl]<0:
            konzai=1
            numb=nl
            numba=newlist[numb]
    if konzai==1:
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        print(wa)
        now=newlist[numb]
        #print(newlist)
        newlist.remove(numba)
        #print(newlist)
        #print(now,newlist[0])
        #print('d', now, newlist[0], newlist[1])
        if newlist[1]>0:
            if now>=0:
                #now*=-1
                print(newlist[0], now)
            else:
                #now*=-1
                print(now,newlist[0])
        else:
            if now>=0:
                print(now, newlist[0])
                now=abs(now)+abs(newlist[0])
            else:
                print(newlist[0], now)
            
        now=abs(now)+abs(newlist[0])
        if newlist[1]>0:
            now=now*(-1)
        for xi in range(n-3):
            #print('d', now, newlist[xi+1], newlist[xi+2])
            if newlist[xi+2]>0:
                if now>=0:
                    print(newlist[xi+1], now)
                else:
                    print(now, newlist[xi+1])
                now=-abs(now)-abs(newlist[xi+1])
            else:
                if now>=0:
                    print(now, newlist[xi+1])
                else:
                    print(newlist[xi+1], now)
                now=abs(now)+abs(newlist[xi+1])
        if newlist[-1]>0:
            print(newlist[-1], now)
        else:
            print(now, newlist[-1])
    elif seihu==1:
        newlist=sorted(newlist)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa-=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            if counter==n-3:
                print(newlist[ll+2], now)
                now=-(abs(now)+abs(newlist[ll+2]))
            else:
                print(now, newlist[ll+2])
                now=-(abs(now)+abs(newlist[ll+2]))
            counter+=1
    elif seihu==-1:
        newlist=sorted(newlist, reverse=True)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa+=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            if counter==n-3:
                print(now, newlist[ll+2])
                now=(abs(now)+abs(newlist[ll+2]))
            else:
                print(now, newlist[ll+2])
                now=(abs(now)+abs(newlist[ll+2]))
            counter+=1
    
else:
    print(abs(mazu*(len(newlist)-2)))
    for i in range(len(newlist)-2):
        print(mazu*(1-i), mazu)
    if mazu>=0:
        print(mazu, mazu*(3-len(newlist)))
    else:
        print(mazu*(3-len(newlist)), mazu)

Submission status: Wrong Answer

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

A.sort()
max_n = 0

S = []
if A[0] >= 0:
    a = A[0]
    for i in range(N-2, 0, -1):
        S.append([a, A[i]])
        a -= A[i]
    S.append([A[N-1], a])
    max_n = A[N-1] - a
elif A[N-1] <= 0:
    a = A[N-1]
    for i in range(N-1):
        S.append([A[i], a])
        max_n = A[i] - a
        a -= A[i]
else:
    a = A[0]
    i = N-2
    for i in range(N-2, 0, -1):
        if A[i] <= 0:
            break
        S.append([a, A[i]])
        a -= A[i]
    S.append([A[N-1], a])
    b = A[N-1] - a
    for j in range(1, i):
        S.append([b, A[i]])
        max_n = b - A[i]
        b -= A[i]

print(S[len(S)-1][0] - S[len(S)-1][1])
for s in S:
    print(s[0], s[1])

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(map(int, input().strip().split(' ')))
t = [0] * (n)
t[n - 1] = s[n - 1]
 
for i in range(n):
    minj = i
    for j in range(i + 1, n):
        if s[j] < s[minj]:
            minj = j
     
    s[i], s[minj] = s[minj], s[i]

for i in range(n - 1):
    t[i] = s[i]
    if (i != n -2):
        s[i + 1] = s[i] - s[i + 1]
    else:
        ans = s[i + 1] - s[i]

print(ans)
for i in range(n - 1):
    if (i != n -2):
        t[i + 1] = t[i] - t[i + 1]
        print(t[i], end =' ')
        print(t[i + 1])
    else:
        t[i] = t[i - 1] - t[i]
        print(t[i + 1], end =' ')
        print(t[i])

Submission status: Wrong Answer

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
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n, k = LI()
    a = n - k
    if k == 1:
        print(0)
        return
    print(a)
    return

#B
def B():
    n = II()
    xy = LIR(n)
    if n == 1:
        print(1)
        return
    ans = inf
    for i in range(n):
        for k in range(n):
            if i != k:
                b = 1
                check = []
                check.append((i,k))
                p = xy[i][0] - xy[k][0]
                q = xy[i][1] - xy[k][1] 
                for l in range(n):
                    for m in range(n):
                        if not((l,m) in check) and p == xy[l][0] - xy[m][0] and q == xy[l][1] - xy[m][1]:
                            b += 1
                            check.append((l, m))
                ans = min(ans, n-b)
    print(ans)

    return

#C
def C():
    n = II()
    a = LI()
    a.sort()
    anslis = []
    f = 1
    if n % 2:
        bf = a[n // 2]
        for i in range(n // 2):
            if f:
                anslis.append((a[i], a[-i - 1]))
                anslis.append((bf,a[i]-a[-i - 1]))
                bf += - a[i] + a[-i - 1]
                f ^= 1
            else:
                anslis.append((a[-i - 1], a[i]))
                anslis.append((bf, -a[i] + a[-i - 1]))
                bf += a[i] - a[-i - 1]
                f ^= 1
    else:
        bf = -a[0] + a[- 1]
        anslis.append((a[-1], a[0]))
        for i in range(1,n // 2):
            if f:
                anslis.append((a[i], a[-i - 1]))
                anslis.append((bf,a[i]-a[-i - 1]))
                bf += - a[i] + a[-i - 1]
                f ^= 1
            else:
                anslis.append((a[-i - 1], a[i]))
                anslis.append((bf, -a[i] + a[-i - 1]))
                bf += a[i] - a[-i - 1]
                f ^= 1
    print(bf)
    for a, b in anslis:
        print(a,b)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

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

Submission status: Wrong Answer

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

BB = sorted(A)

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
m = 0
p = 0
for i in a:
    if i < 0:
        m += 1
    elif i > 0:
        p += 1
if p == 0 and m == 0:
    print(0)
    exit()
ans = []
if p == m:
    while p == m:
        flag = 0
        b = []
        for i in range(len(a) // 2):
            if flag % 2:
                ans.append([a[i], a[-i - 1]])
                b.append(a[i] - a[-i - 1])
            else:
                ans.append([a[-i - 1], a[i]])
                b.append(a[-i - 1] - a[i])
            flag += 1
        a = sorted(b)
        p = 0
        m = 0
        for i in a:
            if i < 0:
                m += 1
            elif i > 0:
                p += 1
if p > m:
    flag = 1
    while len(a) > 1:
        b = []
        for i in range(len(a) // 2):
            if flag % 2:
                ans.append([a[i], a[-i - 1]])
                b.append(a[i] - a[-i - 1])
            else:
                ans.append([a[-i - 1], a[i]])
                b.append(a[-i - 1] - a[i])
        if len(a) % 2:
            b.append(a[i + 1])
        a = b
        flag += 1
elif p < m:
    flag = 0
    while len(a) > 1:
        b = []
        for i in range(len(a) // 2):
            if flag % 2:
                ans.append([a[i], a[-i - 1]])
                b.append(a[i] - a[-i - 1])
            else:
                ans.append([a[-i - 1], a[i]])
                b.append(a[-i - 1] - a[i])
        if len(a) % 2:
            b.append(a[i + 1])
        a = b
        flag += 1
print(a[0])
for i in ans:
    print("{} {}".format(i[0], i[1]))

Submission status: Wrong Answer

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

same = []
diff = []

while len(a) > 1:
    mx = a.pop(0)
    mn = a.pop(-1)
    if mx > 0 and mn < 0:
        same.append((mx,mn))
    else:
        if mx > 0:
            # 両方+
            diff.append((mx,mn))
        else:
            # 両方-
            diff.append((mn,mx))

# print(same)
# print(diff)
result = []

cs = []
c = True
if len(a) == 1:
    cs.append(a[0])
    c = False

for x,y in same:
    if c:
        x,y = y,x
        c = False
    result.append((y,x))
    cs.append(y-x)

for x,y in diff:
    if c:
        x,y = y,x
        c = False
    result.append((y,x))
    cs.append(y-x)

sm = cs.pop(0)
while len(cs) > 0:
    y = cs.pop(0)
    result.append((sm, y))
    sm += -1*y

print(sm)

for r in result:
    print(r[0],r[1])

Submission status: Wrong Answer

Solution code:
N = int(input())
alist = list(map(int, input().split()))
alist.sort()
ans = 0
tmp = alist[0] - alist[1]
anslist = []
anslist.append(str(alist[0]) + ' ' + str(alist[1]))
for i in range(2, N - 2):
    tmp = alist[i] - alist[i + 1]
    anslist.append(str(alist[i]) + ' ' + str(alist[i + 1]))
if N != 2:
    ans = alist[N  - 1] - tmp
    anslist.append(str(alist[N - 1]) + ' ' + str(tmp))

print(ans)
for i in range(len(anslist)):
    print(anslist[i])

Submission status: Wrong Answer

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

P.sort(reverse=True)
M.sort()
if N % 2 == 0:
    if len(P) > N // 2:
        M += P[N // 2:]
        M.sort(reverse=True)
        P = P[:N // 2]
        P.sort()
    else:
        P += M[N // 2:]
        P.sort()
        M = M[:N // 2]
        M.sort(reverse=True)
    x = 0
    y = 0
    print(sum(P)-sum(M))
    for i in range(N - 1):
        if i % 2 == 0:
            y = x-y if i!=0 else M[0]
            x = P[i // 2]
        else:
            y = x - y
            x = M[i//2+1]
        print(x,y)
else:
    # Pの方を1つ増やすパターン
    if len(M) > N // 2:
        P += M[N // 2:]
        P.sort()
        M = M[:N // 2]
        M.sort(reverse=True)
    else:
        M += P[N // 2+1:]
        M.sort(reverse=True)
        P = P[:N // 2+1]
        P.sort()
    x = 0
    y = 0
    print(sum(P)-sum(M))
    for i in range(N - 1):
        if i % 2 == 0:
            y = x-y if i!=0 else P[0]
            x = M[i // 2]
        else:
            y = x - y
            x = P[i//2+1]
        print(x, y)

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

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

a.sort()
ope = []
max_num = a[-1]
min_num = a[0]
p = bisect.bisect_left(a, 0)
for i in range(1, p):
    ope.append([max_num, a[i]])
    max_num -= a[i]
for i in range(p, n - 1):
    ope.append([min_num, a[i]])
    min_num -= a[i]
ope.append([max_num, min_num])
print(max_num - min_num)
for i in range(n - 1):
    print(" ".join(map(str, ope[i])))

Submission status: Wrong Answer

Solution code:
import heapq

N = int(input())

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

plus = []
minus = []

for i, ai in enumerate(A):
    if ai < 0:
        heapq.heappush(minus, -ai)
    else:
        heapq.heappush(plus, ai)

if len(minus) == 0:
    m = heapq.heappop(plus)
    print(sum(plus) - m)
    for i in range(N-2):
        k = heapq.heappop(plus)
        print(m, k)
        m -= k
    k = heapq.heappop(plus)
    print(k, m)
elif len(minus) == 1:
    m = -heapq.heappop(minus)
    print(sum(plus) - m)
    for i in range(N-2):
        k = heapq.heappop(plus)
        print(m, k)
        m -= k
    k = heapq.heappop(plus)
    print(k, m)
else:
    m = -heapq.heappop(minus)
    print(sum(plus) + sum(minus) + m)
    for i in range(len(minus)-1):
        k = -heapq.heappop(minus)
        print(m, k)
        m -= k
    n = -heapq.heappop(minus)
    for i in range(len(plus)):
        k = heapq.heappop(plus)
        print(n, k)
        n -= k
    print(m, n)

Submission status: Wrong Answer

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

a_max = max(a_list)

a_list.remove(a_max)
a_list.sort()

ans_list = []

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
operations = []
for _ in range(N - 2):
    x, y = A[0], A[-1]
    operations.append((x, y))
    A[0] -= A.pop()
x, y = A.pop(), A.pop()
print(x - y)
operations.append((x, y))
for o in operations:
    print(*o, sep=" ")

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(map(int, input().split()))
s = sum(a[1:-1])
print(a[-1] - a[0] + sum(a[1:-1]))
x = a[0]
for i in range(1, n - 1):
  y = a[i]
  print(x, y)
  x = x - y
print(a[-1], x)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

result = []


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

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

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


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

Submission status: Wrong Answer

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())
###template###

N = int(input())
As = list(mi())

TL = [[], [], []]
#[0] : zeroList
#[1] : minusList
#[2] : plusList

for a in As:
  if a == 0: TL[0].append(a)
  elif a < 0: TL[1].append(a)
  else: TL[2].append(a)

#print(TL)

ans = 0
printlist = []
#全てxxの場合（3パターン）
if len(TL[0])==N: #全てゼロの場合
  print(0)
  for i in range(N-1):
    print(0, 0)
elif len(TL[1])==N: #全てminusの場合
  TL[1] = sorted(TL[1], reverse=True) #降順に並べる（-値の低いものから）
  printlist.append((TL[1][0],TL[1][1])) #痛みを最小に
  hand = TL[1][0]-TL[1][1] #これでプラスになった
  idx = 2
  while idx < N:
    printlist.append((hand, TL[1][idx]))
    hand -= TL[1][idx]
    idx += 1
  ans = hand
elif len(TL[2])==N: #全てplusの場合
  TL[2] = sorted(TL[2]) #昇順に並べる
  printlist.append((TL[2][0],TL[2][1])) #痛みを最小に
  hand = TL[2][0]-TL[2][1] #これで-になった
  idx = 2
  while idx < N-1:
    printlist.append((hand, TL[2][idx]))
    hand -= TL[2][idx]
    idx += 1
  #idx==N-1
  printlist.append((TL[2][idx], hand))
  hand = TL[2][idx]-hand
  ans = hand
#print(printlist)

#ゼロがある場合を考える
if len(TL[0])>=1:
  print('')

#答えを出力する
print(ans)
for x, y in printlist:
  print(x, y)

Submission status: Wrong Answer

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

switch=0
counter=0
mazu=newlist[0]
if newlist[0]>=0:
    seihu=1
else:
    seihu=-1
konzai=0

for nl in newlist:
    if mazu!=nl:
        switch=1

if switch==1:
    wa=0
    list2=[]
    for nl in range(n):
        list2.append([newlist[nl], abs(newlist[nl])])
        if seihu*newlist[nl]<0:
            konzai=1
            numb=nl
            numba=newlist[numb]
    if konzai==1:
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        print(wa)
        now=newlist[numb]
        #print(newlist)
        newlist.remove(numba)
        #print(newlist)
        #print(now,newlist[0])
        #print('d', now, newlist[0], newlist[1])
        if newlist[1]>0:
            if now>=0:
                #now*=-1
                print(newlist[0], now)
                now=-abs(now)-abs(newlist[0])
            else:
                #now*=-1
                print(now,newlist[0])
                now=-abs(now)-abs(newlist[0])
        else:
            if now>=0:
                print(now, newlist[0])
                now=abs(now)+abs(newlist[0])
            else:
                print(newlist[0], now)
                now=abs(now)+abs(newlist[0])
            
        now=abs(now)+abs(newlist[0])
        for xi in range(n-3):
            #print('d', now, newlist[xi+1], newlist[xi+2])
            if newlist[xi+2]>0:
                if now>=0:
                    print(newlist[xi+1], now)
                else:
                    print(now, newlist[xi+1])
                now=-abs(now)-abs(newlist[xi+1])
            else:
                if now>=0:
                    print(now, newlist[xi+1])
                else:
                    print(newlist[xi+1], now)
                now=abs(now)+abs(newlist[xi+1])
        if newlist[-1]>0:
            print(newlist[-1], now)
        else:
            print(now, newlist[-1])
    elif seihu==1:
        newlist=sorted(newlist)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa-=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            if counter==n-3:
                print(newlist[ll+2], now)
                now=-(abs(now)+abs(newlist[ll+2]))
            else:
                print(now, newlist[ll+2])
                now=-(abs(now)+abs(newlist[ll+2]))
            counter+=1
    elif seihu==-1:
        newlist=sorted(newlist, reverse=True)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa+=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            if counter==n-3:
                print(now, newlist[ll+2])
                now=(abs(now)+abs(newlist[ll+2]))
            else:
                print(now, newlist[ll+2])
                now=(abs(now)+abs(newlist[ll+2]))
            counter+=1
    
else:
    print(abs(mazu*(len(newlist)-2)))
    for i in range(len(newlist)-2):
        print(mazu*(1-i), mazu)
    if mazu>=0:
        print(mazu, mazu*(3-len(newlist)))
    else:
        print(mazu*(3-len(newlist)), mazu)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

output_list = [0] * (N - 1)

def get_maximum_diff(some_list):
    tmp_max = max(some_list)
    some_list.pop(some_list.index(tmp_max))
    tmp_min = min(some_list)
    some_list.pop(some_list.index(tmp_min))
    some_list.append(tmp_max - tmp_min)
    return some_list, tmp_min, tmp_max
    
def get_minimum_diff(some_list):
    tmp_max = max(some_list)
    some_list.pop(some_list.index(tmp_max))
    tmp_min = min(some_list)
    some_list.pop(some_list.index(tmp_min))
    some_list.append(tmp_min - tmp_max)
    return some_list, tmp_min, tmp_max

for i in range(N - 1):
    if i % 2 == 1:
        A_list, tmp_min, tmp_max = get_maximum_diff(A_list)
    else:
        A_list, tmp_min, tmp_max = get_minimum_diff(A_list)
    output_list[i] = [tmp_min, tmp_max]
print(-sum(A_list))
for item in output_list:
    print(item[0], item[1])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
check = -1
for i in range(N):
    if A[i] > 0:    
        break
    check += 1

ans = 0
if 0 <= check < N-1:
    ans = 0
    for i in range(N):
        if i <= check:
            ans -= A[i]
        else:
            ans += A[i]
else:
    ans = abs(sum(A)) - 2*min(abs(A[0]),abs(A[N-1]))
print(ans)
tmp = A[0]
i = 0
while N-i-2 > max(0,check):
    print(str(tmp) + " " + str(A[N-i-1]))
    tmp -= A[N-i-1]
    i += 1
if N-i-2 <= check:
    print(str(A[N-i-1]) + " " + str(tmp))
    tmp = A[N-i-1] - tmp
    i += 1
while N-i-1 > 0:
    print(str(tmp) + " " + str(A[N-i-1]))
    tmp -= A[N-i-1]
    i += 1

Submission status: Wrong Answer

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

switch=0
counter=0
mazu=newlist[0]
if newlist[0]>=0:
    seihu=1
else:
    seihu=-1
konzai=0

for nl in newlist:
    if mazu!=nl:
        switch=1

if switch==1:
    wa=0
    list2=[]
    for nl in range(n):
        list2.append([newlist[nl], abs(newlist[nl])])
        if seihu*newlist[nl]<0:
            konzai=1
            numb=nl
            numba=newlist[numb]
    if konzai==1:
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        print(wa)
        now=newlist[numb]
        #print(newlist)
        newlist.remove(numba)
        #print(newlist)
        #print(now,newlist[0])
        print('d', now, newlist[0], newlist[1])
        if newlist[1]>0:
            if now>=0:
                #now*=-1
                print(newlist[0], now)
                now=-abs(now)-abs(newlist[0])
            else:
                #now*=-1
                print(now,newlist[0])
                now=-abs(now)-abs(newlist[0])
        else:
            if now>=0:
                print(now, newlist[0])
                now=abs(now)+abs(newlist[0])
            else:
                print(newlist[0], now)
                now=abs(now)+abs(newlist[0])
            
        now=abs(now)+abs(newlist[0])
        for xi in range(n-3):
            #print('d', now, newlist[xi+1], newlist[xi+2])
            if newlist[xi+2]>0:
                if now>=0:
                    print(newlist[xi+1], now)
                else:
                    print(now, newlist[xi+1])
                now=-abs(now)-abs(newlist[xi+1])
            else:
                if now>=0:
                    print(now, newlist[xi+1])
                else:
                    print(newlist[xi+1], now)
                now=abs(now)+abs(newlist[xi+1])
        if newlist[-1]>0:
            print(newlist[-1], now)
        else:
            print(now, newlist[-1])
    elif seihu==1:
        newlist=sorted(newlist)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa-=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            if counter==n-3:
                print(newlist[ll+2], now)
                now=-(abs(now)+abs(newlist[ll+2]))
            else:
                print(now, newlist[ll+2])
                now=-(abs(now)+abs(newlist[ll+2]))
            counter+=1
    elif seihu==-1:
        newlist=sorted(newlist, reverse=True)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa+=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            if counter==n-3:
                print(now, newlist[ll+2])
                now=(abs(now)+abs(newlist[ll+2]))
            else:
                print(now, newlist[ll+2])
                now=(abs(now)+abs(newlist[ll+2]))
            counter+=1
    
else:
    print(abs(mazu*(len(newlist)-2)))
    for i in range(len(newlist)-2):
        print(mazu*(1-i), mazu)
    if mazu>=0:
        print(mazu, mazu*(3-len(newlist)))
    else:
        print(mazu*(3-len(newlist)), mazu)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
a = 0
if N%2 == 1:
  a = max(A)
  A.remove(a)
b = max(A)
A.remove(b)
c = 0
ans = [[A[0],b]]
com = A[0]-b
c += com
for i in range(1,(N//2)*2-1):
  ans.append([c,A[i]])
  com = c-A[i] 
  c = com
print(a-c)
print(ans[0][0],ans[0][1])
for j in range(1,(N//2)*2-1):
  print(ans[j][0],ans[j][1])
if N%2 == 1:
  print(a,c)