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:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
arr = np.array(A)
minuscnt = np.count_nonzero(arr < 0)


ansxy = []
while 1:
    if minuscnt == 0 or minuscnt % 2 == 1: 
        x,y = min(A),max(A)
        A.remove(x)
        A.remove(y)
        A.append(x-y)
        ansxy.append([x,y])
        if len(A) == 1:
            print(abs(A[0]))
            for x,y in ansxy:
                print(x,y)
            exit()
    else:
        x,y = max(A),min(A)
        A.remove(x)
        A.remove(y)
        A.append(x-y)
        ansxy.append([x,y])
        if len(A) == 1:
            print(abs(A[0]))
            for x,y in ansxy:
                print(x,y)
            exit()

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()
a = cl.deque(a)
aa = []
co = 0
while True:
    if len(a) == 3:
        if a[1] >= 0:
            z = a.pop()
            zz = a.popleft()
            a.appendleft(zz - z)
            aa.append([zz,z])
        else:
            z = a.pop()
            zz = a.popleft()
            a.append(z - zz)
            aa.append([z,zz])
        continue
    elif len(a) == 2:
        z = a.pop()
        zz = a.popleft()
        aa.append([max(z,zz),min(z,zz)])
        a.append(max(z,zz) - min(z,zz))
        break
    elif len(a) == 1:
        break
    if co % 2 == 0:
        z = a.pop()
        zz = a.popleft()
        a.append(z - zz)
        aa.append([z,zz])
        co += 1
    else:
        z = a.pop()
        zz = a.popleft()
        a.appendleft(zz - z)
        aa.append([zz,z])
        co += 1

print(a[0])
for i in range(n-1):
    print(jo(aa[i]))

Submission status: Wrong Answer

Solution code:
def simulateBoard(posNums, negNums):
  P, N = len(posNums), len(negNums)
  out_str = ""
  
  
  #if N == 0:
    #last_neg = negNums.pop()
    
    #while len(negNums) != 0:
      #cur_neg = negNums.pop()
      #cur_diff = last_pos - cur_neg
      #print(last_pos, ' ', cur_neg, ' ')
      #last_pos = cur_diff
    
  
  if P>=1 and N>=1:
    print(abs(sum(negNums)) + sum(posNums)) 
    
    last_pos = posNums.pop()
    last_neg = negNums.pop()
    
    while len(negNums) != 0:
      cur_neg = negNums.pop()
      cur_diff = last_pos - cur_neg
      print(last_pos, ' ', cur_neg, ' ')
      last_pos = cur_diff
    
    while len(posNums) != 0:
      cur_pos = posNums.pop()
      cur_diff = last_neg - cur_pos
      print(last_neg, ' ', cur_pos, ' ')
      last_neg = cur_diff
   	
    cur_diff = last_pos - last_neg
    print(last_pos, ' ', last_neg, ' ')

N = int(input())
nums = input().split()
nums = [int(i) for i in nums]
pos_nums = []
neg_nums = []

for num in nums:
  if num >= 0: pos_nums.append(num)
  else: neg_nums.append(num)
    
simulateBoard(pos_nums, neg_nums)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(map(int, input().split()))
neg = 0
l = 1
for i in range(n):
  if a[i] < 0:
    neg += a[i]
    l = i
  else:
    break
s = sum(a[l:-1])
print(a[-1] - neg - a[0] + s)
for i in range(l):
  print(a[-1], a[i])
  a[-1] -= a[i]

x = a[l]
for i in range(l + 1, n - 1):
  y = a[i]
  print(x, y)
  x = x - y
print(a[-1], x)

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()
  a_s = A[1::2]
  b_s = A[0::2]

  ways = ""
  
  lastvalue = 0
  for i in range(N - 1):
    a, b = heappop(a_s), heappop(b_s)
    if ((N - i) % 2 == 0):
      ways += ("{b} {a}\n".format(**locals()))
      heappush(b_s, a - b)
      lastvalue = a - b
    else:
      ways += ("{a} {b}\n".format(**locals()))
      heappush(a_s, b - a)
      lastvalue = b - a

  print(abs(lastvalue))
  print(ways)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main2(A1, A2, N):
    logs = []
    y = A1[0]
    x = A2[0]
    logs.append('%d %d' % (x, y))
    for i in range(1, N):
        y = x - y
        x = A1[i]
        logs.append('%d %d' % (x, y))
        y = x - y
        x = A2[i]
        logs.append('%d %d' % (x, y))
    return x - y, logs

def main3(A1, A2, N):
    logs = []
    y = A2[0]
    for i in range(0, N):
        x = A1[i]
        logs.append('%d %d' % (x, y))
        y = x - y
        x = A2[i+1]
        logs.append('%d %d' % (x, y))
        y = x - y
    return y, logs

def main():
    N = i2n()
    A = i2nn()
    A.sort()
    i = bisect_left(A, 0)
    A1 = A[:i]
    A2 = A[i:]
    AL = min(len(A1), len(A2))
    result = 0
    if len(A1) >= len(A2):
        if AL == 0:
            A12 = A1[1:]
            x = A1[0]
            logs = []
        else:
            A11 = A1[:AL]
            A12 = A1[AL:]
            x, logs = main2(A11, A2, AL)
        for y in A12:
            logs.append('%d %d' % (x, y))
            x = x - y
        result = x
    else:
        A2T = len(A2)-AL-1
        A21 = A2[:A2T]
        A22 = A2[A2T:]
        x, logs = main3(A1, A22, AL)
        q = deque(A21 + [x])
        if len(q) % 2 == 0:
            y = q.popleft()
        else:
            y = q.pop()
        while q:
            if len(q) % 2 == 0:
                x = q.popleft()
            else:
                x = q.pop()
            logs.append('%d %d' % (x, y))
            y = x - y
        result = y
    print(result)
    for s in logs:
        print(s)

main()

Submission status: Wrong Answer

Solution code:
#c.py
from collections import deque

n = int(input())
lis = list(map(int,input().split()))
lis.sort()
lis = deque(lis)
x = 0
mx = lis.pop()
ans_lis = []
while len(lis) != 1:
    mi = lis.popleft()
    ma = lis.pop()
    if ma <= 0:
        lis.appendleft(mi - ma)
    else:
        lis.appendleft(ma - mi)
    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:
import bisect

N = int(input())
A = sorted(list(map(int,input().split())))
ans = []
c = 0
if min(A) > 0:
    while len(A) > 2:
        t, s = A.pop(-1),A.pop(0)
        ans.append([s,t])
        bisect.insort_left(A,s-t)
    print(A[1]-A[0])
    for e in ans:
        print(*e)
    print(A[1],A[0])
elif max(A) < 0:
    while len(A) > 2:
        s, t = A.pop(-1),A.pop(0)
        ans.append([s,t])
        bisect.insort_left(A,s-t)
    print(A[1]-A[0])
    for e in ans:
        print(*e)
    print(A[1],A[0])
else:
    while len(A) > 2:
        if c == 0:
            t, s = A.pop(-1),A.pop(0)
            ans.append([s,t])
            bisect.insort_left(A,s-t)
            c = 1
        else:
            s, t = A.pop(-1),A.pop(0)
            ans.append([s,t])
            bisect.insort_left(A,s-t)
            c = 0
    print(A[1]-A[0])
    for e in ans:
        print(*e)
    print(A[1],A[0])

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
H = list(map(int, input().split()))
H.sort()
negative_count = bisect.bisect_left(H, 0)
positive_count = N - negative_count

def f():
    # answer
    ans = ""

    if positive_count <= 1:
        ans += "{0} {1}\n".format(H[-1], H[0])
        sum_ans = H[-1] - H[0]
    else:
        ans += "{0} {1}\n".format(H[0], H[-1])
        sum_ans = - H[-1] + H[0]

        count = positive_count - 2 if negative_count > 0 else positive_count - 3
        for i in range(count):
            v = H[negative_count + i]
            ans += "{0} {1}\n".format(sum_ans, v)
            sum_ans = sum_ans - v

        v = H[-2]
        ans += "{1} {0}\n".format(sum_ans, v)
        sum_ans = v - sum_ans

    count = negative_count if positive_count > 0 else negative_count - 1
    for i in range(1, count):
        v = H[i]
        ans += "{0} {1}\n".format(sum_ans, v)
        sum_ans = sum_ans - v

    return ans, sum_ans


ans, sum_ans = f()
ans = "{}\n".format(sum_ans) + ans
print(ans)

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 or mn > 0:
            # 両方+
            diff.append((mx,mn))
        else:
            # 両方-
            diff.append((mn,mx))

# print(same)
# print(diff)
result = []
cs = []
c = True
if len(a) == 1 and a[0] > 0:
    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
    if x < 0 or y < 0:
        x,y = y,x
        c = False
    result.append((y,x))
    cs.append(y-x)

if len(a) == 1 and a[0] < 0:
    cs.append(a[0])

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:
from functools import reduce

def print_diff(a, b):
    print(a, b)
    return a - b

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

nums.sort()
biggest = nums[-1]

smalls = nums[:-1]

small = reduce(lambda a, b: a - b, smalls)
if biggest > 0:
    ans = biggest - small
else:
    ans = small - biggest

# print(nums)
# print(smalls)
# print(small)
print(ans)

idx = 0
temp = smalls[0]

while True:
    if len(smalls) == 1:
        break
    print(temp, smalls[1])
    temp = smalls[0] - smalls[1]
    del smalls[0]
    smalls[0] = temp

if biggest > 0:
    print(nums[-1], smalls[0])
else:
    print(smalls[0], biggest)

Submission status: Wrong Answer

Solution code:
n=int(input())
lst=[int(i) for i in input().split()]
lst.sort()
cnt=0
for i in range(n):
    if lst[i]>0:
        break
    if lst[i]<0:
        cnt+=1
ans=0
for i in lst:
    if i<0:
        ans-=i
    else:
        ans+=i
if cnt!=n and cnt!=0:
    print(ans)
    if n>2:
        if n-cnt>1:
            for i in range(cnt+1,n):
                if i==cnt+1:
                    print(lst[0],lst[i])
                    res=lst[0]-lst[i]
                else:
                    print(res,lst[i])
                    res-=lst[i]
            if cnt>1:
                for i in range(1,cnt):
                    print(lst[cnt],lst[i])
                    res-=lst[i]
            print(lst[cnt],res)
        else:
            for i in range(cnt):
                if i==0:
                    print(lst[-1],lst[0])
                    res=lst[-1]-lst[0]
                else:
                    print(res,lst[i])
                    res-=lst[i]
    else:
        print(lst[1],lst[0])
            
elif cnt==n:
    ans=ans+2*lst[-1]
    print(ans)
    for i in range(n-1):
        if i==0:
            print(lst[-1],lst[i])
            res=lst[-1]-lst[i]
        else:
            print(res,lst[i])
            res-=lst[i]
else:
    ans=ans-2*lst[0]
    print(ans)
    if n>2:
        for i in range(1,n-1):
            if i==1:
                print(lst[0],lst[i])
                res=lst[0]-lst[i]
            else:
                print(res,lst[i])
                res-=lst[i]
        print(lst[-1],res)
    
    else:
        print(lst[1],lst[0])

Submission status: Wrong Answer

Solution code:
from sys import stdin

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

A.sort()
point = A[0]
xlist=[]
ylist=[]
for i in range(1, N-1, 1):
  if point < 0:
    xlist.append(A[N-1])
    ylist.append(point)
    A[N-1]=A[N-1]-point
    point=A[i]
  else:
    xlist.append(point)
    ylist.append(A[i])
    point = point - A[i]
  
print(A[N-1]-point)
for i in range(len(xlist)):
  print("{} {}".format(xlist[i],ylist[i]))
print("{} {}".format(A[N-1],point))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
a = sorted(A, key=abs)
ans = ""
for i in range(N-1):
    a = sorted(A, key=abs)
    if A[0] < 0 and A[1] < 0:
        x = A[0]
        y = A[1]
        ans += str(x) + " " + str(y) + "\n"
        xy = y-x
        A[1] = xy
        A = A[1:]
    elif A[0] > 0 and A[1] > 0:
        x = A[0]
        y = A[1]
        ans += str(x) + " " + str(y) + "\n"
        xy = y-x
        A[1] = xy
        A = A[1:]
    elif A[0] > 0 and A[1] < 0:
        x = A[0]
        y = A[1]
        ans += str(x) + " " + str(y) + "\n"
        xy = y-x
        A[1] = xy
        A = A[1:]
    else:
        x = A[0]
        y = A[1]
        ans += str(x) + " " + str(y) + "\n"
        xy = y-x
        A[1] = xy
        A = A[1:]
ANS = str(A[0]) + "\n" + ans
print(ANS)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(int(i) for i in input().split()) 
  
ls = []
for i in range(n-2):
    x, y= max(a), min(a)
    a.remove(x)
    a.remove(y)
    c1 = (x - y) - min(a)
    c2 = max(a) - (y -x)
    if c1>c2:
      a.append(x - y)
      ls.append((x, y))
    else:
      a.append(y - x)
      ls.append((y, x))
      
print(max(a) - min(a))
print(max(a), min(a))
for l in ls:
  print(l[0], l[1])

Submission status: Wrong Answer

Solution code:
import numpy as np

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

xlog = []
ylog = []
minus = 0
for i in range(N):
	if A[i] < 0:
		minus += 1

i = 0
a = A[0]


for i in range(N-1):
	if i == 1:
		x = A[N-1-i]
		y = A[i]
		a = x - y

	elif i == N-1:
		if a < A[i+1]:
			x = A[i+1]
			y = a
		else:
			x = a
			y = A[i+1]
		a = x - y

	else:
		if a > A[i+1]:
			x = A[i+1]
			y = a
		else:
			x = a
			y = A[i+1]
		a = x - y

	xlog.append(x)
	ylog.append(y)


m = a
print(m)
for i in range(N-1):
	print(xlog[i], ylog[i])

Submission status: Wrong Answer

Solution code:
#c.py
from collections import deque

n = int(input())
lis = list(map(int,input().split()))
lis.sort()
lis = deque(lis)
x = 0
mx = lis.pop()
ans_lis = []
while len(lis) != 1:
    mi = lis.popleft()
    ma = lis.pop()
    if mi < ma:
        lis.appendleft(mi - ma)
    else:
        lis.appendleft(ma - mi)
    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())
alist = list(map(int, input().split()))
alist.sort()
ans = 0
tmp = alist[0] - alist[N - 1]
anslist = []
anslist.append(str(alist[0]) + ' ' + str(alist[N - 1]))
for i in range(1, N - 2):
    tmp1 = alist[i] - alist[i + 1]
    anslist.append(str(alist[i]) + ' ' + str(alist[i + 1]))
if N != 2:
    ans = alist[N - 2] - tmp
    anslist.append(str(alist[N - 2]) + ' ' + 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()))
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[N-2] >= 0 else False
    if hasPlus:
        # make minus
        ans.append([A[f], A[l]])
        s = A[f] - A[l]
        f += 1
        l -= 1
        # choose plus make plus
        while f <= l and A[l] >= 0:
            ans.append([A[l], s])
            s = A[l] - s
            l -= 1
    else:
        # make plus
        ans.append([A[l], A[f]])
        s = A[l] - A[f]
        f += 1
        l -= 1

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

if (len(ans) >= N):
    l[0] = 0
print(s)
for x, y in ans:
    print(x, y)

Submission status: Wrong Answer

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

sort = ai.sort
append = ai.append

sort()

xy = [0]*(n-1)

for i in range(n-1):
  xy[i] = (ai.pop(0), ai.pop(-1))
  ai.insert(0, xy[i][0]-xy[i][1])
  sort()
    
print(-ai[0])
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:
def sol():
    n=int(input())
    #n,k=map(int,input().split())
    l=[int(i) for i in input().split()]
    a=[(abs(l[i]),i) for i in range(n)]
    x=l[a[0][1]]
    d=[]
    for i in range(1,n):
        y=l[a[i][1]]
        #d.append((max(x,y),min(x,y)))
        d.append((min(x,y),max(x,y)))
        #x=max(x,y)-min(x,y)
        x=min(x,y)-max(x,y)
        #if x<y:
        #    d.append((x,y))
        #    x=x-y
        #else:
        #    d.append((y,x))
        #    x=(y-x)
    #print(d)
    print(abs(d[-1][0]-d[-1][1]))
    for x,i in enumerate(d):
        if x+1==n-1:
            print(max(i),min(i))
        else:
            print(i[0],i[1])

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
N = int(input())

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

ans = ''
stack_neg = []
stack_pos_neg = []
pos_neg_flag = True
ans_flag = False
answer_list = []

while A:
    tmp = A.pop()
    
    if not ans_flag:
        ans = tmp
        ans_flag = True
        continue
        
    if ans_flag and ans < 0 and tmp >= 0:
        tmp2 = ans
        ans = tmp
        tmp = tmp2
    
    if tmp >= 0 and pos_neg_flag and ans_flag:
        stack_pos_neg.append(tmp)

    elif tmp < 0 and pos_neg_flag:
        stack_pos_neg.append(tmp)

        while stack_pos_neg:
            a = stack_pos_neg.pop()
            if not stack_pos_neg:
                stack_pos_neg.append(a)
                pos_neg_flag = False
                break
            b = stack_pos_neg.pop()
            answer_list.append((a, b))
            stack_pos_neg.append(a - b)

    elif tmp >= 0 and not pos_neg_flag:
        a = stack_pos_neg.pop()
        answer_list.append((a, tmp))
        stack_pos_neg.append(a - tmp)

    elif tmp < 0 and not pos_neg_flag:
        stack_neg.append(tmp)
        
if pos_neg_flag:
    stack_pos_neg.append(ans)
    stack_pos = sorted(stack_pos_neg)
    
    ans = stack_pos.pop()
    
    if stack_pos:
        target = stack_pos.pop(0)
    while stack_pos:
        tmp = stack_pos.pop()
        answer_list.append((target, tmp))
        target = target - tmp
    answer_list.append((ans, target))
    ans = ans - target

else:
    tmp = stack_pos_neg.pop()
    answer_list.append((ans, tmp))
    ans = ans - tmp
    
    while stack_neg:
        tmp = stack_neg.pop()
        answer_list.append((ans, tmp))
        ans = ans - tmp

print(ans)

for answer in answer_list:
    i, j = answer
    print(i, j)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
delim = 0

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

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 == 0:
			print(so[-1],so[0])
			so.append(so[-1]-so[0])
			del(so[-2])
			del(so[0])
		elif i % 2 == 1:
			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=sorted(list(map(int,input().split())),reverse=True)
if n==2:
    print(a[0]-a[1])
    print(a[0],a[1])
    exit()
if a[-1]>=0:
    m=sum(a)-a[-1]*2
    print(m)
    for i in range(n-1):
        if i==0:
            y=a[-2]
            x=a[-1]
        elif i==n-2:
            y=x-y
            x=a[0]
        else:
            x=x-y
            y=a[-2-i]
        print(x,y)
elif  a[0]<=0:
    m=-sum(a)+a[0]*2
    print(m)
    for i in range(n-1):
        if i==0:
            y=a[1]
            x=a[0]
        else:
            x=x-y
            y=a[i+1]
        print(x,y)
else:
    for i in range(n):
        if a[i]<=0:
            mem=i
            break
    m=0
    for j in range(n):
        m+=abs(a[j])
    print(m)
    for k in range(n-1):
        if k==0:
            x=a[mem]
            y=a[k]
        elif k==mem-1:
            y=x-y
            x=a[k]
        elif k<mem-1:
            x=x-y
            y=a[k]
        else:
            x=x-y
            y=a[k+1]
        print(x,y)

Submission status: Wrong Answer

Solution code:
import bisect
import random

#n = int(input())
#a = [20, -31, 27, 40, -99, -68, 71, 58, 97, -10, -85, -59, 40, 42]
#for i in range(n):
    #a.append(random.randint(-100,100))
n = int(input())
a = list(map(int,input().split()))
##print(n)
#print(a)

minus = 0
plus = 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    else:
        plus += 1

a.sort()
procedure = []

if minus != 0:
    if plus != 0:
        for i in range(plus): #全部マイナスにする
            procedure.append([a[0],a[-1]])
            a[0] = a[0] - a[-1]
            a.pop(-1)

    d = len(a)
    for i in range(d-2):
        procedure.append([a[-1],a[0]])
        a[-1] = a[-1] - a[0]
        a.pop(0)
    if d != 1:
        procedure.append([a[-1],a[0]])
        a[-1] = a[-1] - a[0]
    else:
        temp = procedure[-1][0]
        procedure[-1][0] = procedure[-1][1]
        procedure[-1][1] = temp
        a[-1] = -a[-1]
    print(a[-1])

else:
    d = len(a)
    for i in range(d-2):
            procedure.append([a[0],a[1]])
            a[0] = a[0] - a[1]
            a.pop(1)
    procedure.append([a[1],a[0]])
    a[0] = a[1] - a[0]
    print(a[0])

for i in range(n-1):
    print(procedure[i][0],procedure[i][1])

Submission status: Wrong Answer

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

A.sort()

if (A[0] <= 0 and A[n-1] > 0) or (A[0] < 0 and A[n-1] >= 0):
    print(sum(list(map(abs, A))))
    while sum(a>=0 for a in A) > 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(atama, ketu)
        new = atama - ketu
        A.insert(0, new)
    while len(A) != 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(ketu, atama)
        new = ketu - atama
        A.append(new)
elif A[0] > 0:
    atama = A.pop(0)
    tugi = A.pop(0)
    print(atama, tugi)
    new = atama - tugi
    A.insert(0, new)
    print(sum(list(map(abs, A))))
    while sum(a>=0 for a in A) > 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(atama, ketu)
        new = atama - ketu
        A.insert(0, new)
    while len(A) != 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(ketu, atama)
        new = ketu - atama
        A.append(new)
else:
    ketu = A.pop(-1)
    tugi = A.pop(-1)
    print(ketu, tugi)
    new = ketu - tugi
    A.append(new)
    print(sum(list(map(abs, A))))
    while sum(a>=0 for a in A) > 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(atama, ketu)
        new = atama - ketu
        A.insert(0, new)
    while len(A) != 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(ketu, atama)
        new = ketu - atama
        A.append(new)

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:
                plus_first = (tmp, ans_li)
                break

        minus = deque(A[:N//2+1])
        plus = deque(A[N//2+1:])
        m = minus.popleft()
        p = plus.popleft()
        tmp = p - m
        ans_li = ['{} {}'.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:
                if -tmp > plus_first[0]:
                    print(-tmp)
                    for i in ans_li: print(i)
                else:
                    print(plus_first[0])
                    for i in plus_first[1]: print(i)
                return

    else:
        if -sum(minus) > sum(plus):
            minus, plus = plus,minus
        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())
A = [int(i) for i in input().split()]

A.sort()

L = [a for a in A if a <= 0]
R = [a for a in A if a > 0]

if L != [] and R != []:
    low = L[0]
    for r in R[:-1]:
        print(low, r)
        low -= r

    up = R[-1]
    for l in L[1:]:
        print(up, l)
        up -= l

    print(up, low)
    print(up - low)

elif L == []:
    low, up = R[0], R[-1]
    for r in R[1:-1]:
        print(low, r)
        low -= r
    print(up, low)
    print(up - low)

elif R == []:
    low, up = L[0], L[-1]
    for l in L[1:-1]:
        print(up, l)
        up -= l
    print(up, low)
    print(up - low)

Submission status: Wrong Answer

Solution code:
N = int(input())
numbers = list(map(int,input().split()))
# print(numbers)
numbers.sort()
# print(numbers)

if N % 2 == 0:
    print(sum(numbers[int(N/2):])-sum(numbers[:int(N/2)]))

    x = numbers[-1]
    y = numbers[0]
    print(x, y)
    for step in range(1,N-1):
        y = x-y
        x = numbers[step] if step%2 != 0 else numbers[-int(step/2)-1]
        print(x, y)

else:
    print(sum(numbers[int(N / 2):]) - sum(numbers[:int(N / 2)]))

    x = numbers[0]
    y = numbers[-1]
    print(x, y)
    for step in range(1, N - 1):
        y = x - y
        x = numbers[int(step/2)] if step % 2 == 0 else numbers[-int((step+3)/2)]
        print(x, y)