Problem:


Score : 500 points

Problem Statement

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

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

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

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

Constraints

2 \leq N \leq 10^5

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

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

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

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

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

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

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

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

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0



Sample input:
3
1 -1 2


Sample output:
4
-1 1
2 -2

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
p = []
m = []
for a in A:
    if a < 0:
        m.append(a)
    else:
        p.append(a)
was = []
if N == 2:
    print(max(A)-min(A))
    print(max(A), min(A))
    quit()
if len(m) == 0:
    p.sort()
    m.append(p[-1] - p[0])
    was.append((p[-1], p[0]))
    p = p[1:-1]
elif len(p) == 0:
    m.sort()
    p.append(m[-1] - m[0])
    was.append((m[-1], m[0]))
    m = m[1:-1]

mm = m[0]
m = m[1:]
pp = p[0]
p = p[1:]
for x in p:
    was.append((mm,x))
    mm -= x
for x in m:
    was.append((pp,x))
    pp -= x
was.append((pp, mm))
print(pp-mm)
for p,m in was:
    print(p,m)

Submission status: Wrong Answer

Solution code:

N = int(input())
temp  = list(map(int, input().split(" ")))
temp.sort()
nums = temp[:]
nums[0] *= -1
for i in range(1,N-1):
    if nums[i] <0:
        nums[i] *= -1

print(sum(nums))

nums = temp[:]
part = nums[:-1]

if len(nums) == 2:
    before = nums[1] - nums[0]
    print(nums[1],nums[0])
    exit()
before = nums[0] - nums[1]
print(nums[0],nums[1])
for p in part[1:-1]:
    print(p,before)
print(nums[-1],before)

Submission status: Wrong Answer

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

ai.sort()

sort = ai.sort
append = ai.append

xy = []
flag = True
while len(ai) > 1:
  xy.append((ai.pop(0), ai.pop(-1)))
  append(xy[-1][0]-xy[-1][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:
n = int(input())
l = sorted(list(map(int,input().split())))
ans = l[0]
ans_list = []
for i in range(1,n):
    if i == n-1:
        ans_list.append(str(l[i])+' '+str(ans))
        ans = l[n-1] - ans
        break
    ans_list.append(str(ans)+' '+str(l[i]))
    ans -= l[i]

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

Submission status: Wrong Answer

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

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

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

    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())
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)
                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())
v = list(map(int, input().split()))
v.sort()
h = n // 2
l = v[:h]
r = v[h:]
print(sum(r) - sum(l))
s = 0
for i in range(n):
    if i % 2 == n % 2:
        x = l[i // 2]
    else:
        x = r[i // 2]
    if i:
        print(x, s)
    s = x - s
assert s == sum(r) - sum(l)

Submission status: Wrong Answer

Solution code:
import heapq
N = int(input())
A = [int(item) for item in input().split()]
# N = 10**5
# A = [item for item in range(N)]
heapq.heapify(A)
res = []
while len(A) > 2:
  min_ = A[0]
  heapq.heappop(A)
  min_2 = A[0]
  heapq.heappop(A)
  new = min_ - min_2
  res.append([min_, min_2])
  heapq.heappush(A, new)
min_ = A[0]
heapq.heappop(A)
min_2 = A[0]
heapq.heappop(A)
res.append([min_2, min_])
print(min_2 - min_)
for item in res:
  print (item[0], item[1])

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(map(int, input().strip().split(' ')))
 
count = 0
 
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):
    if (i != n -2):
        s[i + 1] = s[i] - s[i + 1]
        print(s[i], end =' ')
        print(s[i + 1])
    else:
        ans = s[i + 1] - s[i]
        print(s[i + 1], end =' ')
        print(s[i])

Submission status: Wrong Answer

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

#print(a)
s=[]
ans=[]

i=1
x=a[0]
if abs(min(a))>abs(max(a)):
    a.sort(reverse=True)
    while i != n - 1:

        if x > a[i]:
            ans.append([x, a[i]])
            x = x - a[i]
        else:
            ans.append([a[i], x])
            x = a[i] - x
        i += 1
    ans.append([x, a[i]])
    print(x - a[i])
else:
    a.sort()
    while i != n - 1:
        if x < a[i]:
            ans.append([x, a[i]])
            x = x - a[i]
        else:
            ans.append([a[i], x])
            x = a[i] - x
        i += 1
    ans.append([a[i], x])
    print(a[i] - x)


i=0
while i!=len(ans):
    print(ans[i][0],ans[i][1])
    i+=1

Submission status: Wrong Answer

Solution code:
n = int(input())
li = list(map(int,input().split()))
lipos = [i for i in li if i>0]
lineg = [i for i in li if i<0]
li0 = [i for i in li if i==0]
np = len(lipos)
ng = len(lineg)
n0 = len(li0)

if ng == 0:
  lipos = lipos + li0
  np = len(lipos)
  print(sum(lipos)-2*min(lipos))
  lipos.sort()
  tmp = lipos[0]
  for i in range(1,np-1):
    print("{} {}".format(tmp,lipos[i]))
    tmp -= lipos[i]
  print("{} {}".format(lipos[np-1],tmp))
elif np == 0:
  lineg = lineg + li0
  ng = len(lineg)
  print(2*max(lineg)-sum(lineg))
  lineg.reverse()
  tmp = lineg[ng-1]
  for i in range(ng-1):
    print("{} {}".format(tmp,lineg[i]))
    tmp -= lineg[i]
else:
  lipos = lipos + li0
  np = len(lipos)
  print(sum(list(map(abs,lineg))) + sum(lipos))
  lipos.sort()
  lineg.reverse()
  tmp = lineg[0]
  for i in range(1,np):
    print("{} {}".format(tmp,lipos[i]))
    tmp -= lipos[i]
  print("{} {}".format(lipos[0],tmp))
  tmp = lipos[0] - tmp
  for i in range(1,ng):
    print("{} {}".format(tmp,lineg[i]))
    tmp -= lineg[i]

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort()
d = deque(A)
if A[0] <= 0 and A[-1] >= 0:
    print(sum(map(abs, A)))
    while len(d) > 2:
        while d[1] < 0:
            x = d.pop()
            y = d.popleft()
            print(x, y)
            d.append(x - y)
        x = d.popleft()
        y = d.pop()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])

elif A[0] > 0:
    print(sum(A) - 2 * A[0])
    x = d.popleft()
    y = d.pop()
    if not d:
        print(y, x)
    else:
        print(x, y)
    d.appendleft(x - y)
    while len(d) > 2:
        x = d.popleft()
        y = d.pop()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])
else:
    print(-sum(A) + 2 * A[-1])
    x = d.pop()
    y = d.popleft()
    print(x, y)
    d.append(x - y)
    while len(d) > 2:
        while d[1] < 0 and len(d) > 2:
            x = d.pop()
            y = d.popleft()
            print(x, y)
            d.append(x - y)
        x = d.pop()
        y = d.popleft()
        print(x, y)
        d.appendleft(x - y)
    if len(d) >= 2:
        print(d[1], d[0])

Submission status: Wrong Answer

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

man = []

posi = 0
nega = 0
zero = 0
for e in a:
  if e > 0:
    posi += 1
  elif e < 0:
    nega += 1
  else:
    zero += 1

for i in range(n-1):
  a = sorted(a)
  max = a.pop(-1)
  min = a.pop(0)
  if len(a) % 2 == 0:  # maxmin
    x, y = max, min
  elif len(a) % 2 != 0:  # minmax
    x, y = min, max
  
  a.append(x-y)
  man.append([x,y])

print(a[0])
for x,y in man:
  print(x, y)

Submission status: Wrong Answer

Solution code:


def input_from_console():
    n = int(input())
    a_list = list(map(int, input().split()))
    return n, a_list


import bisect


def solve(n, a_list):
    a_list.sort()

    steps = []
    m = 0

    while len(a_list) > 2:
        minimum = a_list.pop(0)
        maximum = a_list.pop(len(a_list) - 1)
        steps.append("{} {}".format(minimum, maximum))
        m = minimum - maximum
        bisect.insort(a_list, m)

    steps.append("{} {}".format(a_list[1], a_list[0]))
    result = str(a_list[1] - a_list[0]) + "\n" + "\n".join(steps)
    return result


def main():
    n, a_list = input_from_console()
    print(solve(n, a_list))


if __name__ == "__main__":
    import sys

    if sys.gettrace():  # Check if the python interpreter is in debug mode or not.
        check_cases()
    else:
        main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
add = a.append
way = []
wadd = way.append

for i in range(n, 1, -1):
    mn, mx = a.pop(0), a.pop(-1)
    if i%2:
        wadd([mn, mx])
        add(mn-mx)
    else:
        wadd([mx, mn])
        add(mx-mn)
    a.sort()

print(a[0])
for i, j in way: print(i, j)

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
# answer
ans = ""
sum_ans = 0
if positive_count <= 1:
    for i in range(N - 1, 0, -1):
        if i == N - 1:
            ans += "{0} {1}\n".format(H[-1], H[0])
            sum_ans = H[-1] - H[0]

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

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

print(ans)

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

if N == 2:
    ans.append([A[l], A[f]])
    prev = A[l] - A[f]
    f += 1
    l -= 1
# plus max 1
elif 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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
B = [a for a in A]
while(N-1>1):
    if B[N-2] < 0:
        break
    l = B[0] - B[N-1]
    print(B[0],B[N-1])
    B[0] = l
    N -= 1
if N == 2:
    print(B[1],B[0])
    print(B[1]-B[0])
else:
    b = B[N-1]
    for i in range(N-2,0,-1):
        c = B[i]
        print(b,c)
        b = b-c
    print(b,B[0])
    print(b-B[0])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
plus = 0
for i in range(n):
  plus += ( 2 * int( a[i] > 0 ) - 1 )
score = 0
for i in range(n):
  score += abs(a[i])
if plus != n:
  print(score)
else:
  print(score-2*abs(a[0]))
a.sort()
if plus >=0:
  for k in range(plus):
    print(a[0],a[-1])
    a[0] -= a[-1]
    del a[-1]
  half = ( n - plus ) // 2
  if half != 0:
    for k in range(half-1):
      print(a[k],a[-1])
      a[k] -= a[-1]
      del a[-1]
    for k in range(half):
      print(a[-1],a[0])
      a[-1] -= a[0]
      del a[0]

Submission status: Wrong Answer

Solution code:
N = int( input())
B = list( map( int, input().split()))
B.sort(reverse=True)
plus = 0
for i in range(N):
    if B[i] > 0:
        plus += 1
minus = N - plus
adding = 0
if plus <= minus:
    adding -= sum(B[plus*2:])
    A = B[:plus*2]
else:
    A = B

if plus == 0 or plus == 1:
    print(-sum(B)+B[0]*2)
    now = B[0]
    for a in B[1:]:
        print(now, a)
        now -= a
elif plus <= minus:
    N, K = plus*2, N
    print( sum(A[:N//2]) - sum(A[N//2:]) + adding)
    now = A[0]
    print(now, A[N-1])
    now -= A[N-1]
    for i in range(2,N):
        if i%2 == 0:
            print(A[N-1-i//2], now)
            now = A[N-1-i//2] - now
        else:
            print(A[i//2], now)
            now = A[i//2] - now
    for a in B[plus*2:]:
        print(now, a)
        now -= a
    
elif N%2 == 0:
    print( sum(A[:N//2]) - sum(A[N//2:]))
    now = A[0]
    print(now, A[N-1])
    now -= A[N-1]
    for i in range(2,N):
        if i%2 == 0:
            print(A[N-1-i//2], now)
            now = A[N-1-i//2] - now
        else:
            print(A[i//2], now)
            now = A[i//2] - now
else:
    k = N//2
    t = 1
    print( sum(A[:k+t]) - sum(A[k+t:]))
    now = A[N-1]
    print(now, A[0])
    now = now - A[0]
    for i in range(2, N):
        if i%2 == 1:
            print(A[i//2], now)
            now = A[i//2] - now
        else:
            print(A[N-1-i//2], now)
            now = A[N-1-i//2] - now

Submission status: Wrong Answer

Solution code:
import fractions
from heapq import *

if __name__ == "__main__":
  N = int(input())
  A = list(map(int, input().split()))
  a_s = A[0::2]
  b_s = A[1::2]

  A.sort()
  
  print(sum(A[(len(A)) // 2:]) - sum(A[0:(len(A)) // 2]))

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

Submission status: Wrong Answer

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

ans = a[0]
ans_l = []
ans_e = a[-1]


if n > 2:
    if ans_e >= 0:
        for i in range(1,n-1):
            if a[i] < 0:
                ans_l += [(ans_e, a[i])]
                ans_e -= a[i]

            if a[i] >= 0:
                ans_l += [(ans, a[i])]
                ans -= a[i]

        ans_l += [(ans_e,ans)]
        ans = ans_e - ans

    else:
        for i in range(1,n):
            ans_l += [(ans, a[i])]
            ans -= a[i]

else:
    # if ans_e >= 0:
    ans_l += [(ans_e,ans)]
    ans = ans_e - ans
    # else:
    #     ans_l += [(ans,ans_e)]
    #     ans = ans - ans_e


print(ans)

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

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
    s=(L[0]-L[1])
  else:
    print(L[n+1], end=" ")
    print(s-L[n+1])
    s=(s-L[n+1])
    n=n+1

Submission status: Wrong Answer

Solution code:
N=int(input())

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

A.sort()

x=[]
y=[]
for i in range(N-2):
    #print(str(A[i])+" "+str(A[i+1]))
    x.append(A[0])
    y.append(A[0+1])
    app=A[0]-A[0+1]
    del A[0:2]
    
    A.insert(0,app)

x.append(A[1])
y.append(A[0])

print(A[1]-A[0])

for i in range(len(x)):
    print(str(x[i])+" "+str(y[i]))

Submission status: Wrong Answer

Solution code:
import heapq
N = int(input())
A = [int(item) for item in input().split()]
# N = 10**5
# A = [item for item in range(N)]
heapq.heapify(A)
res = []
while len(A) > 2:
  min_ = A[0]
  heapq.heappop(A)
  min_2 = A[0]
  heapq.heappop(A)
  new = min_ - min_2
  res.append([min_, min_2])
  heapq.heappush(A, new)
min_ = A[0]
heapq.heappop(A)
min_2 = A[0]
heapq.heappop(A)
res.append([min_, min_2])
print(min_2 - min_)
for item in res:
  print (item[0], item[1])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=[i for i in a if i<0]
t=sorted([i for i in a if i>=0])
if len(s)==0:
    print(t[-1]-t[0]+sum(t[1:-1]))
    k=t[0]
    if n!=2:
        for i in range(1,n-1):
            print(k,t[i])
            k-=t[i]
    print(t[-1],k)
elif len(t)==0:
    d=s[0]
    print(s[0]-sum(s[1:]))
    for i in range(1,n):
        print(d,s[i])
        d-=s[i]
else:
    print(t[-1]-sum(s)+sum(t[:-1]))
    p=t[-1]
    for i in range(1,len(s)):
        print(p,s[i])
        p-=s[i]
    g=s[0]
    for i in range(len(t)-1):
        print(g,t[i])
        g-=t[i]
    print(p,g)

Submission status: Wrong Answer

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

a.sort()
if a[0] >= 0:
    m = sum(a[1:]) - a[0]
    print(m)
    tmp = a[0]
    for i in range(1, n-1):
        print(tmp, a[i])
        tmp -= a[i]
    print(a[n-1], tmp)
elif a[-1] <= 0:
    m = -sum(a[:-1]) + a[-1]
    print(m)
    tmp = a[n-1]
    for i in range(0, n-1, -1):
        print(tmp, a[i])
        tmp -= a[i]
else:
    m = 0
    pos = []
    neg = []
    for ai in a:
        m += abs(ai)
        if ai >= 0:
            pos.append(ai)
        else:
            neg.append(ai)
    print(m)
    tmp = 0
    ni = 0
    pi = 0

    if len(pos) > 1:
        print(neg[ni], pos[pi])
        tmp = neg[ni] - pos[pi]
        ni += 1
        pi += 1

        while pi < len(pos) - 1:
            print(tmp, pos[pi])
            tmp = tmp - pos[pi]
            pi += 1
        print(pos[pi], tmp)
        tmp = pos[pi] - tmp
        while ni < len(neg):
            print(tmp, neg[ni])
            tmp -= neg[ni]
            ni += 1
    else:
        print(pos[pi], neg[ni])
        tmp = pos[pi] - neg[ni]
        ni += 1
        while ni < len(neg):
            print(tmp, neg[ni])
            tmp -= neg[ni]
            ni += 1

Submission status: Wrong Answer

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

def printAns(array1,array2=[]):
    curr1 = array1[0]
    for i in range(1, len(array1)):
        print(curr1, array1[i])
        curr1 -= array1[i]

    if len(array2)>0:
        curr2 = array2[0]
        for i in range(1, len(array2)):
            print(curr2, array2[i])
            curr2 -= array2[i]
            
        print(curr1, curr2)
        curr1 -= curr2
        
    return curr1

plus = []
minus = []
zero = []
for it in a:
    if it == 0:
        zero.append(it)
    elif it > 0:
        plus.append(it)
    else:
        minus.append(it)

# solve problem
if len(plus) == 0:
    minus += zero
    minus.sort(key=lambda x:abs(x))
    max_value = abs(sum(minus[1:]))-abs(minus[0])
    print(max_value)
    printAns(minus)
elif len(minus) == 0:
    plus += zero
    plus.sort(key=lambda x:abs(x))  
    max_value = sum(plus[1:])-plus[0]
    print(max_value)
    printAns([plus[-1]], plus[:-1])
else:
    array1 = [plus.pop()]
    array2 = []
    for i, it in enumerate(minus):
        if i==0:
            array2.append(it)
        else:
            array1.append(it)
    # plus
    array2+=plus
    
    max_value = sum(map(abs, a))
    print(max_value)
    tmp = printAns(array1, array2)

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
ans_lis = []
while len(lis) != 1:
    mi = lis.popleft()
    ma = lis.pop()
    lis.append(mi - ma)
    ans_lis.append([mi,ma])
    x += 1
print(lis[0])
for x in ans_lis:
    print(x[0],x[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()


step = n-1

out = []

if step%2 ==0:
    A = arr[-1]
    dist = 0
    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],end="")
    print(" ",end="")
    print(out[i][1])

Submission status: Wrong Answer

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

a.sort()

ans = []
for i in range(n-1):
    ma = a[1]
    mi = a[0]
    if len(a)%2 == 0:
        ans.append([mi,ma])
        a = [ma - mi] + a[2:]
    else:
        ans.append([ma,mi])
        a = [mi - ma] + a[2:]
    a.sort()
print(a[0])
for i in range(n-1):
    print(ans[i][0],ans[i][1])

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import defaultdict
from functools import reduce

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

# np.random.seed(1234)
# N = 1000
# A = [np.random.randint(-100, 100) for _ in range(N)]
# print(A)

A.sort(reverse=True)

k = max(N//2, len(list(filter(lambda x: x < 0, A)))) # マイナスをつける個数
t = N-k
s = set(range(N))
res = []

if N%2 == 0:
    M = A[0]-A[N-1]
    res.append((A[0], A[N-1]))
    t -= 1
    s.remove(0)
    s.remove(N-1)
else:
    M = A[0]
    t -= 1
    s.remove(0)

B = []

for i in range(t):
    if N%2 == 0:
        j = N-1-i-1
    else:
        j = N-1-i
    B.append(A[j]-A[i+1])
    res.append((A[j], A[i+1]))
    s.remove(j)
    s.remove(i+1)

for b in B:
    res.append((M,b))
    M = M - b

for i in s:
    res.append((M,A[i]))
    M = M - A[i]

from collections import Counter

c = Counter()
for a in A:
    c[a] += 1

print(M)
for r in res:
    c[r[0]] -= 1
    c[r[1]] -= 1
    if c[r[0]] < 0 or c[r[1]] < 0:
        raise
    c[(r[0]-r[1])] += 1
    print(r[0], r[1])
# print(c)
# print(M)
# print(M == res[-1][0] - res[-1][1])

Submission status: Wrong Answer

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

A = sorted(A)

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

        #print(A)

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

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

        #print(A)

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

Submission status: Wrong Answer

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

M = A[-1] - (A[0] - sum(A[1:-1]))
print(M)

acc = A[0]
for i in range(1, N-1):
    print(acc, A[i])
    acc -= A[i]

print(A[-1], acc)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = sorted(list(map(int,input().split())))
ans = l[0]
ans_list = []
for i in range(1,n):
    if i == n-1:
        ans_list.append(str(l[i])+' '+str(ans))
        ans = l[n-1] - ans
        break
    ans_list.append(str(ans)+' '+str(l[i]))
    ans -= i

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

Submission status: Wrong Answer

Solution code:
N=int(input())

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

A.sort()

print(sum(A[N//2:])-sum(A[:N//2]))

if N%2==1:
    t=A[N//2]
    for i in range(N-1):
        if i%2==0:
            print(str(A[i//2])+' '+str(t))
            t=A[i//2]-t
        else:
            print(str(A[N//2+i//2+1])+' '+str(t))
            t=A[N//2+i//2+1]-t
else:
    t=A[0]
    for i in range(N-1):
        if i%2==0:
            print(str(A[N//2+i//2])+' '+str(t))
            t=A[N//2+i//2]-t
        else:
            print(str(A[i//2+1])+' '+str(t))
            t=A[i//2+1]-t

Submission status: Wrong Answer

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

a = sorted(a)

for i in range(n-1):
    x = a[0]
    y = a[1]
    if len(a) > 2:
        a = a[2:]
    a.append(x+y)

if a[0] < 0:
    ans = a[1] - a[0]
elif a[1] < 0:
    ans = a[0] - a[1]
else:
    ans = max(a) - min(a)
print(ans)

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]))

  a_s = A[0::2]
  b_s = A[1::2]

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

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:
import bisect
n = int(input())
a = list(map(int,input().split()))
a = sorted(a)
a.reverse()
c = []
p = 0
for i in range(len(a)-1):
    if(a[i]>=0):
        c.append([a[-1],a[i]])
        a[-1]-=a[i]
    else:
        if(p==0):
            c.append([a[i],a[-1]])
            a[-1] = a[i]-a[-1]
            p+=1
        else:
            c.append([a[-1],a[i]])
            a[-1] = a[-1]-a[i]
            p+=1
if(c[-1][1]-c[-1][0]>c[-1][0]-c[-1][1]):
    c[-1] = [c[-1][1],c[-1][0]]
else:
    c[-1] = [c[-1][0],c[-1][1]]
print(c[-1][0]-c[-1][1])
for i in range(len(c)):
    print(*c[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
nums = list(map(int, input().split()))
patterns = []
for i in range(n - 1):
    nums.sort()
    a = nums.pop(0)
    b = nums.pop(len(nums) - 1)
    patterns.append("{} {}".format(a, b))
    nums.append(a - b)
print(abs(nums[0]))
for pattern in patterns:
    print(pattern)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
A_a=sorted(A)
A_b=sorted(A,reverse=True)
#print(A_a)
#print(A_b)

hikae=[]

ans_list=[]
if len(A)%2==1:
    for i in range(len(A)-1):
        if A_a[-1]>=max(hikae+[-10**10]) and A_a[-1]>=A_b[-1]:
            M=A_a.pop(-1)
        else:
            M=max(hikae)
            hikae.remove(M)
        if A_b[-1]<=min(hikae+[10**10]) and A_a[-1]>=A_b[-1]:
            m=A_b.pop(-1)
        else:
            m=min(hikae)
            hikae.remove(m)

        if i%2==0:
            ans_list.append([m,M])
            hikae.append(m-M)
        else:
            ans_list.append([M,m])
            hikae.append(M-m)
else:
    for i in range(len(A)-1):
        if A_a[-1]>=max(hikae+[-10**10]) and A_a[-1]>=A_b[-1]:
            M=A_a.pop(-1)
        else:
            M=max(hikae)
            hikae.remove(M)
        if A_b[-1]<=min(hikae+[10**10]) and A_a[-1]>=A_b[-1]:
            m=A_b.pop(-1)
        else:
            m=min(hikae)
            hikae.remove(m)
            
        if i%2==0:
            ans_list.append([M,m])
            hikae.append(M-m)
        else:
            ans_list.append([m,M])
            hikae.append(m-M)

print(hikae[0])
for i in range(N-1):
    print(ans_list[i][0],ans_list[i][1])