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())
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])
        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(newlist[ll+2], now)
            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(newlist[ll+2], now)
            counter+=1
    
else:
    print(abs(mazu*(len(newlist)-2)))
    for i in range(len(newlist)-1):
        print(mazu*(1-i), mazu)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import defaultdict
N = int(input())
A = sorted((list(map(int, input().split()))))

if N == 2:
    y, x = A
    print(x-y)
    print(x, y)
    exit()

XY = []
for i in range(N-1):
    if i == 0:
        x, y = A[0], A[-1]
        tmp = x-y
        XY.append((x, y))
    elif i < N-2:
        x, y = tmp, A[N-1-i]
        tmp = x-y
        XY.append((x, y))
    else:
        x, y = A[1],tmp
        ans = x-y
        XY.append((x, y))

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a_l=[int(i) for i in input().split()]
a_l.sort()

asc=[0]*n
des=[0]*n
asc[0]=a_l[0]
des[n-1]=a_l[-1]
for i in range(1,n):
    asc[i] = asc[i-1]-a_l[i]
    des[n-i-1] = des[n-i]-a_l[n-i-1]
asc.insert(0,0)

index=1
max_diff=des[1]-asc[1]
for i in range(1,n):
    diff=des[i]-asc[i]
    if max_diff<diff:
        index=i
        max_diff=diff
print(max_diff)

tmpdiff=a_l[0]
if index>1:
    for i in range(index-1):
        print(tmpdiff, a_l[i+1])
        tmpdiff-=a_l[i+1]

for i in range(index,n):
    print(a_l[i],tmpdiff)
    tmpdiff=a_l[i]-tmpdiff

Submission status: Wrong Answer

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

A.sort()
M=-A[0]+sum(A[1:])
print(M)
x=A[0]
y=A[1]
for i in range(N-1):
    if i==N-1-1:
        y=x-y
        x=A[i+1]
    elif i==0:
        x=A[i]
        y=A[i+1]
    else:
        x=x-y
        y=A[i+1]
    print(x,y)

Submission status: Wrong Answer

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

B = [None] * N
B[::2] = A[::-1][: N - (N // 2)]
B[1::2] = A[:N // 2]

print(sum(B[::2]) - sum(B[1::2]))
tmp = B[-1]
for i in range(N - 2):
    print(B[N - 2 - i], tmp)
    tmp = B[N - 2 - i] - tmp
print(A[-1], tmp)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

import sys

n = int( input() )
a = [ int(x) for x in sys.stdin.readline().split() ]
a.sort()
#
h = n // 2
m = a[0:h]
p = a[h:]
#
print( sum(p) - sum(m) )
#
flag= n%2==0
if flag: t= m[0]
else: t= p[0]
#
for i in range(h):
    if flag:
        print( p[i],t )
        t = p[i] - t
        if i==h-1: break
        print( m[i+1],t )
        t = m[i+1] - t
    else:
        print( m[i], t )
        t = m[i] - t
        #
        print( p[i+1], t )
        t = p[i+1] - t

Submission status: Wrong Answer

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

A = sorted(A)

print(sum(A)-2*A[0])
x = A[0]-A[1]
print(A[0], A[1])
for i in range(2, N-1):
    print(x, A[i])
    x -= A[i]
print(A[-1], x)

Submission status: Wrong Answer

Solution code:
n = int(input())

nums = map(int, input().split(' '))

arr = sorted(nums)

num_zeros = 0
num_pos = 0
num_neg = 0

for num in arr:
    if num > 0:
        num_pos += 1
    elif num < 0:
        num_neg += 1
    else:
        num_zeros += 1

# all pos or zero
if num_neg == 0:
    print(sum(arr) - arr[0] * 2)
    lhs = arr[0]

    for i in range(1, n-1):
        rhs = arr[i]
        cal = lhs - rhs
        print(str(lhs) + " " + str(rhs))
        lhs = cal

    print(str(arr[n-1]) + " " + str(lhs))
    exit(0)

# all neg or zero
elif num_pos == 0:
    print(-sum(arr) + arr[n-1] * 2)
    lhs = arr[n-1]

    for i in reversed(range(0, n-1)):
        rhs = arr[i]
        cal = lhs - rhs
        print(str(lhs) + " " + str(rhs))
        lhs = cal
    exit(0)

# 0 and pos / 0 and neg / pos and neg (and 0)
else:
    plus_index = 0
    for index, num in enumerate(arr):
        if num < 0:
            continue
        else:
            plus_index = index
            break

    print(-sum(arr[:plus_index]) + sum(arr[plus_index:]))

    if len(arr) == 2:
        print(str(arr[1]) + " " + str(arr[0]))
        exit(0)

    lhs = arr[plus_index]
    for i in reversed(range(1, plus_index)):
        rhs = arr[i]
        cal = lhs - rhs
        print(str(lhs) + " " + str(rhs))
        lhs = cal

    print(str(arr[0]) + " " + str(lhs))
    lhs = arr[0] - lhs

    for i in range(plus_index+1, n-1):
        rhs = arr[i]
        cal = lhs - rhs
        print(str(lhs) + " " + str(rhs))
        lhs = cal

    print(str(arr[n-1]) + " " + str(lhs))
    exit(0)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
b=[]
if n%2==0:
    for k in range(n//2):
        b.append(a[n//2+k])
        b.append(a[n//2-k-1])
    print(sum(a[n//2:])-sum(a[:n//2]))
    for i in range(n-1):
        print(str(b[-2])+" "+str(b[-1]))
        tmp=b[-1]
        b.pop(-1)
        b[-1]=b[-1]-tmp

else:
    if a[n//2+1]>=0:
        for k in range(n//2):
            b.append(a[n//2+k])
            b.append(a[n//2-k-1])
        b.append(a[-1])
        print(sum(a[n//2:])-sum(a[:n//2]))
    else:
        for k in range(n//2):
            b.append(a[n//2-k])
            b.append(a[n//2+k+1])
        b.append(a[0])
        print(sum(a[n//2+1:])-sum(a[:n//2+1]))
    for i in range(n-1):
        print(str(b[-2])+" "+str(b[-1]))
        tmp=b[-1]
        b.pop(-1)
        b[-1]=b[-1]-tmp

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import collections
sys.setrecursionlimit(10**7)
def lmi(): return list(map(int, input().split()))

n = int(input())
a = lmi()
a.sort()
# print(a)
ans = sum([abs(i) for i in a[1:]]) - min(a)
print(ans)
for i in range(n):
    if a[i] >= 0:
        b = a[:i]
        c = a[i:]
        break
else:
    b = a[:]
    c = []
# print(b,c)

if b == []:
    l = c[0]
    c.pop(0)
else:
    l = b[0]
if c == []:
    r = b[-1]
    b.pop(-1)
else:
    r = c[-1]

# print('#',l,r)

for i in range(len(c)-1):
    print(l,c[i])
    l -= c[i]
for i in range(len(b)-1):
    print(r,b[-i-1])
    r -= b[-i-1]
print(r, l)

Submission status: Wrong Answer

Solution code:
import fractions
from heapq import *

if __name__ == "__main__":
  N = int(input())
  A = list(map(int, input().split()))
  A.sort()
  
  print(sum(A[(len(A)) // 2:]) - sum(A[0:(len(A)) // 2]))

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

Submission status: Wrong Answer

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

ans = []
f = 0
l = N-1
prev = 0
# plus max 1
if A[-2] < 0:
    # make plus
    ans.append([A[l], A[f]])
    prev = A[l]-A[f]
    f += 1
    l -= 1
else:
    # make minus
    ans.append([A[f], A[l]])
    prev = A[f]-A[l]
    f += 1
    l -= 1
    # choose plus make plus
    while True:
        if f > l or A[l] < 0:
            break
        ans.append([A[l], prev])
        prev = A[l] - prev
        l -= 1

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

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

Submission status: Wrong Answer

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

if n == 2:
    print(a[1] - a[0])
    print("{} {}".format(a[1], a[0]))
    exit()

if a[0] <= 0 and 0 <= a[-1]:
    import bisect
    print(sum(map(lambda x: abs(x), a)))
    x0 = bisect.bisect_left(a,0)

    # 0以下から0以上をひいていく（一番大きいものを除く）
    if a[-2] >= 0:
        if x0 == 0:
            x0 += 1
        print("{} {}".format(a[0], a[x0]))
        res = a[0] - a[x0]

        for x in a[x0+1:-1]:
            print("{} {}".format(res, x))
            res -= x
    else:
        res = a[0]

    # 一番大きいものから上のものと負の数を引いていく
    print("{} {}".format(a[-1], res))
    res = a[-1] - res
    for x in a[1:x0]:
        print("{} {}".format(res,x))
        res -= x

elif a[0] > 0:
    print(sum(a) - 2*a[0])

    print("{} {}".format(a[0],a[1]))
    res = a[0] - a[1]
    for x in a[2:-2]:
        print("{} {}".format(res, x))
        res -= x
    
    print("{} {}".format(a[-1], res))
else:
    print(-sum(a) + 2*a[-1])

    print("{} {}".format(a[-1],a[-2]))
    res = a[-1] - a[-2]
    for x in a[:-2]:
        print("{} {}".format(res, x))
        res -= x

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

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

queue = deque(sorted(a))
xs = list()
ys = list()

length = len(queue)

while length > 1:
    min_val = queue.popleft()  # 0
    max_val = queue.pop()  # -1

    if length % 2 == 0:
        new_val = max_val - min_val
        xs.append(max_val)
        ys.append(min_val)
        queue.append(new_val)
    else:
        new_val = min_val - max_val
        xs.append(min_val)
        ys.append(max_val)
        queue.appendleft(new_val)

    length -= 1

print(queue[0])
for x, y in zip(xs, ys):
    print(x, y)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main():
    N = II()
    A = LI()

    A.sort()
    from collections import deque
    minus = deque(A[:N//2])
    plus = deque(A[N//2:])

    ans_li = []
    if N%2 != 0:
        p = plus.popleft()
        m = minus.popleft()
        tmp = m - p
        ans_li.append('{} {}'.format(m, p))
        while True:
            try:
                p = plus.popleft()
                ans_li.append('{} {}'.format(p, tmp))
                tmp = p - tmp
                m = minus.popleft()
                ans_li.append('{} {}'.format(m, tmp))
                tmp = m - tmp
            except:
                print(tmp)
                for i in ans_li: print(i)
                return

    else:
        m = plus.popleft()
        p = minus.popleft()
        tmp = m - p
        ans_li.append('{} {}'.format(m, p))
        while True:
            try:
                m = minus.popleft()
                ans_li.append('{} {}'.format(m, tmp))
                tmp = m - tmp
                p = plus.popleft()
                ans_li.append('{} {}'.format(p, tmp))
                tmp = p - tmp
            except:
                print(tmp)
                for i in ans_li: print(i)
                return


main()

Submission status: Wrong Answer

Solution code:
N = int(input())
tmp = input().split(" ")

A = []
DP = []
out = []

for n in range(N):
    A.append(int(tmp[n]))
    DP.append([0,0])

A.sort()

DP[0][0] = A[0]
DP[0][1] = A[0]
for n in range(1,N):
    p0 = DP[n-1][0] - A[n]
    p1 = A[n] - DP[n-1][0]
    p2 = DP[n-1][1] - A[n]
    p3 = A[n] - DP[n-1][1]

    DP[n][0] = max(p0,p1,p2,p3)
    DP[n][1] = min(p0,p1,p2,p3)

    if n != N-1:
        if p0 == min(p0,p1,p2,p3):
            out.append([DP[n-1][0],A[n]])
        elif p1 == min(p0,p1,p2,p3):
            out.append([A[n],DP[n-1][0]])
        elif p2 == min(p0,p1,p2,p3):
            out.append([DP[n-1][1],A[n]])
        elif p3 == min(p0,p1,p2,p3):
            out.append([A[n],DP[n-1][1]])
        else:
            out.append([DP[n-1][0],A[n]])
    else:
        if p0 == max(p0,p1,p2,p3):
            out.append([DP[n-1][0],A[n]])
        elif p1 == max(p0,p1,p2,p3):
            out.append([A[n],DP[n-1][0]])
        elif p2 == max(p0,p1,p2,p3):
            out.append([DP[n-1][1],A[n]])
        elif p3 == max(p0,p1,p2,p3):
            out.append([A[n],DP[n-1][1]])
        else:
            out.append([DP[n-1][0],A[n]])
                       
print(DP[N-1][0])

for n in out:
    print(n[0],n[1])

Submission status: Wrong Answer

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

minus_flag = 1
max_value = a[N - 1]
min_value = a[0]
ans = []

while a[minus_flag] <=  0 and minus_flag < N - 1:
    ans.append(str(max_value) + " " + str(minus_flag))
    max_value = max_value - a[minus_flag]
    minus_flag += 1

plus_flag = minus_flag

while  plus_flag < N - 1:
    ans.append(str(min_value) + " " + str(plus_flag))
    min_value = min_value - a[plus_flag]
    plus_flag += 1

print(max_value - min_value)
for i in range(len(ans)):
    print(ans[i])
print(str(max_value)  + " " + str(min_value))

Submission status: Wrong Answer

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

ai.sort()

xy = []
flag = True
while flag:
  xy.append((ai.pop(0), ai.pop(0)))
  output = xy[-1][0]-xy[-1][1]
  if len(ai) is 0:
    flag = False
  else:
    ai.append(output)
    ai.sort()
    
print(-output)
for i in range(len(xy)):
  if i < len(xy) - 1:
    print(xy[i][0], xy[i][1])
  else:
    print(xy[i][1], xy[i][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-1 :])-sum(so[:len(so)//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])
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:
N = int(input())
A = list(map(int, input().split()))
ans = list()
i = 0

A = sorted(A)

print(sum(A)-2*A[0])
x = A[0]-A[1]
print(A[0], A[1])
for i in range(2, N-2):
    print(x, A[i])
    x -= A[i]
print(A[-1], x)

Submission status: Wrong Answer

Solution code:
N=int(input())
li=list(map(int,input().split()))
plus=[] 
minus=[]
for i in range(0,N):
  if li[i]>0:
    plus.append(li[i])
  else:
    minus.append(li[i])
    
plus.sort()
minus.sort()
#print(plus)
if len(minus)==0:
  M=sum(plus)-2*plus[0]
  print(M)
  a=plus[0]-plus[1]
  aa=0
  print(plus[0],plus[1])
  for i in range(0,N-3):
    aa=a
    a=-plus[i+2]+a
    print(aa,plus[i+2])
  print(plus[N-1],a)
    
    
    
  
elif len(plus)==0:
  M=-sum(minus)+2*minus[len(minus)-1]
  print(M)
  aa=0
  a=minus[N-1]-minus[N-2]
  print(minus[N-1],minus[N-2])
  for i in range(0,N-2):
    aa=a
    a=-minus[N-i-3]+a
    print(aa,minus[N-i-3])
    
  
  
elif len(plus) ==0:
  M=sum(plus)-sum(minus)
  print(M)
  for i in range(0,len(minus)):
    aa=a
    a=aa-minus[i]
    print(aa,minus[i])
  
  
  
else:
  M=sum(plus)-sum(minus)
  print(M)
  aa=0
  a=minus[0]-plus[0]
  print(minus[0],plus[0])
  for i in range(0,len(plus)-2):
    if len(plus)-2==0: break
    aa=a
    a=a-plus[i+1]
    print(aa,plus[i+1])
  aa=a
  a=-a+plus[len(plus)-1]
  print(plus[len(plus)-1],aa)
    
  for j in range(0,len(minus)-1):
    if len(minus)-1==0: break
    aa=a
    a=a-minus[j+1]
    print(aa,minus[j+1])

Submission status: Wrong Answer

Solution code:


def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g


n = int(input())
arr = list(map(int,input().split()))
arr.sort()

# print(arr)

step = n-1

out = []

if step%2 ==0:
    A = arr[-1]
    dist = 0
    # print(A)
    for i in range(step):
        if i%2==0:
            out.append([arr[0+dist],A])
            A = arr[0+dist]-A
            dist+=1

        else:
            out.append([arr[-1-dist],A])
            A = arr[-1-dist]-A

else:
    A = arr[0]
    dist = 0
    for i in range(step):
        if i%2==0:
            out.append([arr[-1-dist],A])
            A = arr[-1-dist]-A
            dist+=1

        else:
            out.append([arr[0+dist],A])
            A = arr[0+dist]-A

print(A)
for i in range(step):
    print(out[i][0],out[i][1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3

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

  max_a = max(a)
  min_a = min(a)

  if max_a >= 0 and min_a <= 0:
    result, process = has_pos_and_neg(a)
    print(result)
    for line in process:
      print(line)

  elif min_a > 0:
    a.remove(max_a)
    a.remove(min_a)
    a.append(min_a - max_a)

    result, process = has_pos_and_neg(a)
    print(result)
    print('{} {}'.format(min_a, max_a))
    for line in process:
      print(line)

  elif max_a < 0:
    a.remove(max_a)
    a.remove(min_a)
    a.append(min_a - max_a)

    result, process = has_pos_and_neg(a)
    print(result)
    print('{} {}'.format(min_a, max_a))
    for line in process:
      print(line)


def has_pos_and_neg(a):
  if len(a) == 1:
    return a[0], []

  max_a = max(a)
  min_a = min(a)

  a.remove(max_a)
  a.remove(min_a)

  process = []

  for elem in a:
    if elem <= 0:
      process.append('{} {}'.format(max_a, elem))
      max_a -= elem
    else:
      process.append('{} {}'.format(min_a, elem))
      min_a -= elem

  process.append('{} {}'.format(max_a, min_a))
  result = max_a - min_a

  return result, process

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

A = sorted(A)
q = deque(A)

while len(q) > 2:
    max_tmp = q.pop()
    min_tmp = q.popleft()
    if max_tmp > 0 and min_tmp > 0:
        steps.append((min_tmp, max_tmp))
        min_tmp -= max_tmp
        q.appendleft(min_tmp)
        continue
    if max_tmp < 0 and min_tmp < 0:
        steps.append((max_tmp, min_tmp))
        max_tmp -= min_tmp
        q.append(max_tmp)
        continue

    if abs(max_tmp) >= abs(min_tmp):
        steps.append((min_tmp, max_tmp))
        min_tmp -= max_tmp
        q.appendleft(min_tmp)
    else:
        steps.append((max_tmp, min_tmp))
        max_tmp -= min_tmp
        q.append(max_tmp)

max_tmp = q.pop()
min_tmp = q.popleft()
steps.append((max_tmp, min_tmp))
m = max_tmp - min_tmp
print(m)
for x, y in steps:
    print('{} {}'.format(x, y))

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
A = list(map(int, input().split()))
bb = deque(sorted(A))
x = [0]*(N-1)
y = [0]*(N-1)
start_bool = 1 if N % 2 == 0 else -1
for i in range(N-1):
    l = bb.popleft()
    r = bb.pop()
    if start_bool < 0:
        if l > r:
            ret = r - l
            x[i] = r
            y[i] = l
        else:
            ret = l - r
            x[i] = l
            y[i] = r
    else:
        if l > r:
            ret = l - r
            x[i] = l
            y[i] = r
        else:
            ret = r - l
            x[i] = r
            y[i] = l
    if ret > 0:
        bb.append(ret)
    else:
        bb.appendleft(ret)
    start_bool *= -1
print(bb.pop())
for i in range(N-1):
    print(x[i], y[i])

Submission status: Wrong Answer

Solution code:
N, *A = map(int, open(0).read().split())

A.sort()
A_MAX = max(abs(A[0]), abs(A[-1]))
X = sorted(A)[:-1]

ans = []
while len(X) > 1:
    Y = []
    while len(X) > 1:
        x = X.pop()
        y = X.pop()
        if y >= 0:
            Y.append(x - y)
            ans.append((x, y))
        else:
            Y.append(y - x)
            ans.append((y, x))
            
    if len(X) == 1:
        Y.append(X[0])

    X = Y

if X[0] <= 0:
    print(A_MAX - X[0])
    ans.append([A_MAX, X[0]])
else:
    print(X[0] - A_MAX)
    ans.append([X[0], A_MAX])
for i in range(N - 1):
    print(*ans[i])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
minus=sum(a[0:n//2])
plus=sum(a)-minus
print(plus-minus)

if n%2==0:
 s=a[0]
 for i in range(1,n//2):
  print(a[n-i],s)
  s=a[n-i]-s
  print(a[i],s)
  s=a[i]-s
 print(a[n//2],s)

else:
 s=a[n-1]
 for i in range(n//2):
  print(a[i],s)
  s=a[i]-s
  print(a[n-(i+2)],s)
  s=a[n-(i+2)]-s

Submission status: Wrong Answer

Solution code:
# C - Successive Subtraction
n = int(input())
a = list(map(int, input().split()))

a.sort()
ans = []
for i in range(n-2):
    ans.append([a[n-i-2], a[n-i-1]])
    a[n-i-2] = a[n-i-2] - a[n-i-1]

print(a[0] - a[1])
for i in range(n-2):
    print('%d %d' %(ans[i][0], ans[i][1]))
print('%d %d' %(a[0], a[1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(int(i) for i in input().split())

ans = max(A)
A.pop(A.index(max(A)))

for i in range(N-2):
  min_A = min(A)
  max_A = max(A)
  A.pop(A.index(min(A)))
  A.pop(A.index(max(A)))
  print(str(min_A) + " " + str(max_A))
  A.append(min_A - max_A)

print(str(ans) + " " + str(A[0]))
print(ans - A[0])

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from collections import deque

N = int(input())
A = [int(s) for s in input().split()]
A.sort()
if len(A) == 2:
    print(A[1] - A[0])
    exit()

i = bisect_left(A, 0)
left = deque(A[:i])
right = deque(A[i:])
ans = []
if not len(left):
    r0 = right.popleft()
    r1 = right.pop()
    ans.append((r0, r1))
    left.append(r0 - r1)
if not len(right):
    l0 = left.popleft()
    l1 = left.pop()
    ans.append((l1, l0))
    right.append((l1 - l0))

while len(left) > 1:
    l = left.popleft()
    r = right[0]
    ans.append((r, l))
    right[0] = r - l

while len(right) > 1:
    r = right.pop()
    l = left[0]
    ans.append((l, r))
    left[0] = l - r

ans.append((right[0], left[0]))
print(right[0] - left[0])
for x, y in ans:
    print(x, y)

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    n = int(input())
    num = np.array(list(map(int, input().split(' '))))
    print(num)
    res = []
    for i in range(n-1):
        mindex = np.argmin(num)
        minv = num[mindex]
        num = np.delete(num, mindex)
        near = np.argmin(num-minv)
        nerv = num[near]
        num = np.delete(num, near)
        if i == n-2:
            num = np.append(num, nerv-minv)
            res.append([nerv, minv])
        else:
            num = np.append(num, minv-nerv)
            res.append([minv, nerv])
    print(num[0])
    for i in res:
        print(i[0], i[1])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
    return list(map(int,input().split()))
def m():
    return map(int,input().split())
def onem():
    return int(input())
def s(x):
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x):
    return " ".join(map(str,x))

n = onem()

a = l()
a.sort()
aa = []
al = cl.deque()
co = 0
if n % 2 == 0:
    for i in range(n//2):
        if i % 2 == 0:
            al.append([a[-(1+i)],a[i]])
            aa.append(a[-(1+i)]-a[i])
        else:
            al.append([a[i],a[-(1+i)]])
            aa.append(a[i]-a[-(1+i)])
    su = sum(map(abs,aa))
    if (n//2) % 2 == 0:
        while True:
            if len(aa) == 1:
                break
            if len(aa) == 2:
                al.append([max(aa),min(aa)])
                break
            else:
                if co % 2 == 0:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([max(z,zz),min(z,zz)])
                    aa.append(max(z,zz)-min(z,zz))
                    co += 1
                else:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([min(z,zz),max(z,zz)])
                    aa.append(min(z,zz)-max(z,zz))
                    co += 1
    else:
        while True:
            if len(aa) == 1:
                break
            if len(aa) == 2:
                al.append([max(aa),min(aa)])
                break
            else:
                if co % 2 == 0:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([min(z,zz),max(z,zz)])
                    aa.append(min(z,zz)-max(z,zz))
                    co += 1
                else:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([max(z,zz),min(z,zz)])
                    aa.append(max(z,zz)-min(z,zz))
                    co += 1
    print(su)
    for i in range(len(al)):
        print(jo(al[i]))

else:
    if a[n//2] <= 0:
        for i in range(n//2):
            if i % 2 == 0:
                al.append([a[-(1+i)],a[i]])
                aa.append(a[-(1+i)]-a[i])
            else:
                al.append([a[i],a[-(1+i)]])
                aa.append(a[i]-a[-(1+i)])
        aa.append(a[n//2])
        su = sum(map(abs,aa))
        while True:
            if len(aa) == 1:
                break
            if len(aa) == 2:
                al.append([max(aa),min(aa)])
                break
            else:
                if co % 2 == 0:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([max(z,zz),min(z,zz)])
                    aa.append(max(z,zz)-min(z,zz))
                    co += 1
                else:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([min(z,zz),max(z,zz)])
                    aa.append(min(z,zz)-max(z,zz))
                    co += 1
    else:
        for i in range(n//2):
            if i % 2 == 0:
                al.append([a[i],a[-(1+i)]])
                aa.append(a[i]-a[-(1+i)])
            else:
                al.append([a[-(1+i)],a[i]])
                aa.append(a[-(1+i)]-a[i])
        aa.append(a[n//2])
        su = sum(map(abs,aa))
        while True:
            if len(aa) == 1:
                break
            if len(aa) == 2:
                al.append([max(aa),min(aa)])
                break
            else:
                if co % 2 == 0:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([min(z,zz),max(z,zz)])
                    aa.append(min(z,zz)-max(z,zz))
                    co += 1
                else:
                    z = aa.pop()
                    zz = aa.pop()
                    al.append([max(z,zz),min(z,zz)])
                    aa.append(max(z,zz)-min(z,zz))
                    co += 1
    print(su)
    for i in range(len(al)):
        print(jo(al[i]))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [0]*N
A = list(map(int, input().split()))
ans = int(0)
x = int(0)
y = int(0)

As = sorted(A)

if As[1]>0:
    for i in range(N):
        ans += As[i]

    ans -=As[0]*2
    print(ans)

    x = As[0]
    for i in range(N-2):
        y = As[i+1]
        print(x,y)
        x = x - y

    y = x
    x = As[N-1]
    print(x,y)

elif As[N-1]<0:
    for i in range(N):
        As[i] *= -1
    for i in range(N):
        ans += As[i]

    ans -=As[N-1]*2
    print(ans)

    As = sorted(As)

    x = As[0]
    for i in range(N-2):
        y = As[i+1]
        print(-x,-y)
        x = x - y

    y = x
    x = As[N-1]
    print(-y,-x)


else:
    for i in range(N):
        ans += abs(As[i])

    print(ans)

    x = As[0]
    for i in range(N-2):
        y = As[i+1]
        print(x,y)
        x = x - y

    y = x
    x = As[N-1]
    print(x,y)

Submission status: Wrong Answer

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

a.sort()
score = 0
for i in range(0,n//2):
    score -= a[i]
for i in range(n//2,n):
    score += a[i]
print(score)
if len(a) % 2 == 0:
    print(a[-1],a[0])
    ttl = a[-1] - a[0]
    for i in range(1,n//2):
        print(a[i], ttl)
        ttl = a[i] - ttl
        print(a[-1*(i+1)], ttl)
        ttl = a[-1*(i+1)] - ttl
else:
    while True:
        a = 1
    print(a[0], a[-1])
    ttl = a[0] - a[-1]
    for i in range(1,n//2):
        print(a[-1*(i+1)], ttl)
        ttl = a[-1*(i+1)] - ttl
        print(a[i], ttl)
        ttl = a[i] - ttl
    print(a[n//2], ttl)