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)-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())
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])
  
  
  
elif len(plus)!=1:
  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])
    
else:
  M=sum(plus)-sum(minus)
  print(M)
  a=plus[0]-minus[0]
  aa=0
  for i in range(0,len(minus)-1):
    aa=a
    a=aa-minus[i+1]
    print(aa,minus[i+1])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(map(int,input().split()))[::-1]
B = A[:(N+1)//2]
i = (N+1)//2
if B[-1]<=0:
    for i in range(1,(N+1)//2):
        if B[i]<=0:
            B = B[:i]
            break
C = A[i:2*i-1][::-1]
D = A[2*i-1:]
ans = []
if (len(B)+len(C))%2:
    M = B.pop()
else:
    M = C.pop()
while B:
    if len(B)>len(C):
        p = B.pop()
    else:
        p = C.pop()
    ans.append((p,M))
    M = p - M
for d in D:
    ans.append((M,d))
    M = M - d
print(M)
for o in ans:
    print(*o)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
log = []
for i in range(N-1):
  if len(A)%2 == 0:
    x = max(A)
    y = min(A)
    z = x-y
    A.remove(x)
    A.remove(y)
    A.append(z)
    log.append(str(x)+' '+str(y))
    
  elif len(A)%2 == 1:
    x = min(A)
    y = max(A)
    z = x-y
    A.remove(x)
    A.remove(y)
    A.append(z)
    log.append(str(x)+' '+str(y))
    
print(A[0])
for i in log:
  print(i)

Submission status: Wrong Answer

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

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

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)

Submission status: Wrong Answer

Solution code:

def main():
    n = input()
    aa = map(int, raw_input().split())
    if n % 2 == 0:
        aa.sort()
    else:
        aa.sort(reverse=True)
    xx = []
    i = 0
    d = n - 1
    for _ in xrange(n):
        xx.append(aa[i])
        i += d
        if d < 0:
            d = -1 - d
        else:
            d = 1 - d
    order = [[xx[1], xx[0]]]
    q = xx[1] - xx[0]
    for p in xx[2:]:
        order.append([p, q])
        q = p - q
    print q
    for p, q in order:
        print p, q


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = input().split()
b=[]
for i in range(len(a)):
    a[i]=int(a[i])
while len(a)>1:
    a = sorted(a)
    if len(a)>2:
        b.append(a[0])
        b.append(a[1])
        new = int(a[0])-int(a[1])
        a.remove(int(a[0]))
        a.remove(int(a[0]))
        a.append(new)

    else:
        b.append(a[1])
        b.append(a[0])
        new = int(a[1])-int(a[0])
    
        print(new)
        a.clear()
def split_list(l, n):
    for idx in range(0, len(l), n):
        yield l[idx:idx + n]
result = list(split_list(b, 2))
for j in range(len(result)):
    print(result[j][0],result[j][1])

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):
    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())
lst = list(map(int,input().split()))
so = sorted(lst)
if N % 2 == 0:	
	M = sum(so[len(so)//2:])-sum(so[:len(so)//2])
	print(M)
	for i in range(len(so)-1):
		if i % 2 == 0:
			print(so[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:
import sys
n = int(input())
a = list(map(int,sys.stdin.readline().split()))
a.sort()
minA = a[0]
mx = a[-1]
ans = []
t = ''
# ans.append(t)
s =0

if a[0]<0 and a[-1]>-1:
    l = a[0]
    h = a[-1]
    for i in range(1,n-1):
        if a[i]<0:
            ans.append([h,a[i]])
            h = h-a[i]
        else:
            ans.append([l,a[i]])
            l = l-a[i]
    ans.append([h,l])
    s = h-l
elif a[0]>-1 and a[-1]>-1:
    c=a[0]
    for i in range(1,n-1):
        ans.append([c,a[i]])
        c= c-a[i]
    ans.append([a[-1],c])
    s= a[1]-c
else:
    c = a[-1]-a[0]
    ans.append([a[-1],a[0]])
    for i in range(1, n-1):
        ans.append([c,a[i]])
        c= c-a[i]
    s=c

print(s)
for v,w in ans:
    print(v,w)

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]
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:
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
    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 = [int(i) for i in input().split()]

a.sort()
plus = -1
for i in range(len(a)-2):
    if a[i] * a[i+1] < 0:
        plus = i + 1

if plus >= 0:
    b = a[:plus]
    c = a[plus:]
    print(sum(c)-sum(b))

    tmp = a[0] - a[plus]
    print(a[0],a[plus])
    for i in range(1,len(a)-plus-1):
        print(tmp,a[plus+i])
        tmp = tmp - a[plus+i]
    
    print(a[-1],tmp)
    tmp = a[-1] - tmp
    for i in range(1,plus):
        print(tmp, a[i])

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

else:
    print((sum(a) - 2 * a[0])*(-1))

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):
    ans_list.append([l[i],ans])
    if i == n-1:
        ans = l[n-1] - ans
        break
    ans -= i

print(ans)
print(ans_list)

Submission status: Wrong Answer

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

numbers = {a: 0 for a in set(A)}
for a in A:
    numbers[a] += 1

operations = []

for _ in range(N - 2):
    numbers_keys = numbers.keys()
    x = min(numbers_keys)
    y = max(numbers_keys)

    operations.append((x, y))

    z = x - y
    if z in numbers:
        numbers[z] += 1
    else:
        numbers[z] = 1

    if numbers[x] == 1:
        del numbers[x]
    else:
        numbers[x] -= 1

    if numbers[y] == 1:
        del numbers[y]
    else:
        numbers[y] -= 1

A = []
for k, v in numbers.items():
    A.extend([k] * v)
x, y = max(A), min(A)
print(x - y)
operations.append((x, y))
for o in operations:
    print(*o, sep=" ")

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
i=0
B = []
while len(A) > 2:
    B.append([A[0],A[1]])
    x = A[0]
    y = A[1]
    A.pop(0)
    A.pop(0)
    A.insert(0, x-y)
if(A[1] > A[0]):
    B.append([A[1],A[0]])
    print(A[1]-A[0])
else:
    B.append([A[0],A[1]])
    print(A[0]-A[1])
for i in range(N-1):
    print(B[i][0], B[i][1])

Submission status: Wrong Answer

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

a.sort()
xy = []
while len(a) > 2:
    if a[len(a)-1] > 0:
        n = a[0] - a[len(a)-1]
        xy.append([a[0], a[len(a)-1]])
        a = a[1:len(a) - 1]
        a.insert(0, n)
    else:
        n = a[len(a)-1] - a[0]
        xy.append([a[len(a)-1], a[0]])
        a = a[1:len(a) - 1]
        a.append(n)

m = a[1] - a[0]
xy.append([a[1], a[0]])
print(m)
for _xy in xy:
    print(str(_xy[0]) + " " + str(_xy[1]))

Submission status: Wrong Answer

Solution code:
M=int(input())
A= list(map(int, input().split()))
b=A.copy()
while(len(b)>3):
    b.sort()
    C=b[1]-b[0]
    b.remove(b[1])
    b.remove(b[0])
    b.append(C)
print(max(b)-min(b))

while(len(A)>1):
    A.sort()
    C=A[1]-A[0]
    print(A[0],A[1])
    A.remove(A[1])
    A.remove(A[0])
    A.append(C)

Submission status: Wrong Answer

Solution code:
n=int(input())
a_list=list(map(int,input().split()))
a_list.sort()
import bisect
x=bisect.bisect_left(a_list,0)
y=bisect.bisect_right(a_list,0)
neg=a_list[:x]
pos=a_list[y:]
zero=y-x
if zero==n:
    print(0)
    for i in range(n-1):
        print(0,0)
else:
    if neg==[]:
        if zero==0:
            m=sum(pos[1:])-pos[0]
            print(m)
            now=pos[0]
            for i in pos[1:-1]:
                print(now,i)
                now=now-i
            print(pos[-1],now)
        else:
            m=sum(pos)
            print(m)
            for i in range(zero-1):
                print(0,0)
            now=0
            for i in pos[:-1]:
                print(now,i)
                now=now-i
            print(pos[-1],now)
    elif pos==[]:
        if zero==0:
            m=abs(sum(neg[:-1])-neg[-1])
            print(m)
            now=neg[-1]
            for i in neg[:-1]:
                print(now,i)
                now=now-i
        else:
            m=abs(sum(neg))
            print(m)
            for i in range(zero-1):
                print(0,0)
            now=0
            for i in neg:
                print(now,i)
                now=now-i
    else:
        if zero==0:
            m=-sum(neg)+sum(pos)
            print(m)
            now=neg[0]
            for i in pos[1:]:
                print(now,i)
                now=now-i
            print(pos[0],now)
            now=pos[0]-now
            for i in neg[1:]:
                print(now,i)
                now=now-i
        else:
            m=-sum(neg)+sum(pos)
            print(m)
            for i in range(zero-1):
                print(0,0)
            now=neg[0]
            for i in pos:
                print(now,i)
                now=now-i
            print(0,now)
            now=-now
            for i in neg[1:]:
                print(now,i)

Submission status: Wrong Answer

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

l.sort()

if l[0] >= 0:
    #all +
    m = sum(l[1:])-l[0]
    print(m)
    curr = l[0]
    for i in range(n-2):
        print(curr,l[i+1])
        curr -= l[i+1]
    print(l[-1],curr)
elif l[-1]<=0:
    m = l[-1] - sum(l[:-1])
    print(m)
    curr = l[-1]
    for i in range(n-1):
        print(curr,l[n-2-i])
        curr -= l[n-2-i]
else:
    m = sum([abs(item) for item in l])
    print(m)
    idx = 0
    for i in range(n):
        if l[i]>=0:
            idx=i
            break
    plus = l[idx:]
    minus = l[:idx]
    
    curr = minus[0] 
    for i in range(len(plus)-1):
        print(curr,plus[i])
        curr -= plus[i]
    for i in range(1,len(minus)):
        print(plus[-1],minus[i])
    print(plus[-1],curr)

Submission status: Wrong Answer

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

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

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())
aa = list(map(int,input().split()))
aa.sort()
minmam = min(aa)
anss = []

for i in range(len(aa)-1):
    if i > 0:
        minmam -= aa[i]

    if i < len(aa)-2:
        anss.append([minmam,aa[i+1]])
    else:
        anss.append([aa[-1],minmam])
        
print(max(aa)-minmam)
for ans in anss:
    print(*ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
xs=sorted(map(int,input().split()))
if len(xs) == 2:
    print(xs[1]-xs[0])
    print(xs[1],xs[0])
else:
    for i in range(n-2):
        if i == 0:
            a = xs[0] - xs[1]
        else:
            a = a - xs[i+1]
    print(xs[-1]-a)
    for i in range(n-2):
        if i == 0:
            print(xs[0],xs[1])
            a = xs[0] - xs[1]
        else:
            print(a,xs[i+1])
            a = a - xs[i+1]
    print(xs[-1],a)

Submission status: Wrong Answer

Solution code:

def main():
    n = input()
    aa = map(int, raw_input().split())
    if n % 2 == 0:
        aa.sort()
    else:
        aa.sort(reverse=True)
    xx = []
    i = 0
    d = n - 1
    for _ in xrange(n):
        xx.append(aa[i])
        i += d
        if d < 0:
            d = -1 - d
        else:
            d = 1 - d
    order = [[xx[0], xx[1]]]
    q = xx[1] - xx[0]
    for p in xx[2:]:
        order.append([p, q])
        q = p - q
    print q
    for p, q in order:
        print p, q


if __name__ == '__main__':
    main()

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)
else:
    a.sort()


while i!=n-1:
    if a[i]>0:

        if x<a[i]:
            ans.append([x,a[i]])
            x=x-a[i]
        else:
            ans.append([a[i],x])
            x=a[i]-x
    else:
        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

if x>a[i]:
    ans.append([x,a[i]])
    print(x-a[i])
else:
    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())
A = list(map(int,input().split()))
A.sort()
B = [a for a in A]
while(N>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 len(B) == 2:
    print(B[1],B[0])
    print(B[1]-B[0])
else:
    L = len(B)
    b = B[L-1]
    for i in range(L-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:
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(int(i) for i in input().split()) 

print(sum(a) - 2*min(a))
for i in range(n-2):
    x, y= min(a), max(a)
    a.remove(x)
    a.remove(y)
    a.append(x - y)
    print(x, y)

print(max(a), min(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a_s = [int(v) for v in input().split()]

a_s.sort()
#max_a = a_s[len(a_s) - 1]
#min_a = a_s[0]
m = sum(a_s[1:]) - a_s[0]

print(m)
current = a_s[0]

for a in a_s[1:-1]:
  print("{} {}".format(current, a))
  current = current - a
print("{} {}".format(a_s[-1], current))

Submission status: Wrong Answer

Solution code:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict
import math


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def mod_inv(x):
    # available only when mod is prime
    global mod
    return pow(x, mod - 2, mod)


def gcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    ret = math.factorial(x)
    ret = ret // (math.factorial(x - y) * math.factorial(y))

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)


###############################################################################


def main():
    n = intin()
    alist = intina()

    alist.sort()

    if alist[0] >= 0:
        ans = -alist[0]
        for a in alist[1:]:
            ans += a
        print(ans)
        x = alist[0]
        for a in alist[1:(n-1)]:
            print('%d %d' % (x, a))
            x = x - a
        print('%d %d' % (alist[-1], x))

    if alist[-1] <= 0:
        ans = alist[-1]
        for a in alist[:(n-1)]:
            ans += (-a)
        print(ans)
        x = alist[-1]
        for a in alist[:(n-1)]:
            print('%d %d' % (x, a))
            x = x - a

    if alist[0] < 0 and alist[-1] > 0:
        ans = 0
        for a in alist:
            ans += abs(a)
        print(ans)
        x = alist[0]
        for a in alist[:(n-1)]:
            if a < 0:
                continue
            print('%d %d' % (x, a))
            x = x - a
        alist[0] = x
        x = alist[-1]
        for a in alist[:(n-1)]:
            if a >= 0:
                break
            print('%d %d' % (x, a))
            x = x - a


if __name__ == '__main__':
    main()

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
A = sorted(A)
removeNums = []
while 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:
num = int(input())

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

max_A = A[num-1]
B = A[:num-1]
X = []
for i in range(num-2):
    X.append(B[0])
    X.append(B[len(B)-1])
    new = [B[0]-B[len(B)-1]]
    B = new + B[1:len(B)-1]
X.append(max_A)
X.append(B[0])

print(max_A - B[0])
for i in range(0,len(X),2):
    print(X[i],X[i+1])

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush, heapify
N = int(input())
A = list(map(int, input().split()))
p = []
for i in A:
    if i > 0:
        tmp = [i, 1]
    elif i < 0:
        tmp = [-i, -1]
    else:
        N -= 1
        continue
    heappush(p, tmp)

for i in range(N-1):
    x, y = heappop(p), heappop(p)
    x = x[0] * x[1]
    y = y[0] * y[1]
    if p == []:
        if x-y > y-x:
            print(x,y)
        else:
            print(y,x)
        continue
    if p[0][1] > 0:
        tmp = min(x-y, y-x)
    else:
        tmp = max(x-y, y-x)
    if tmp == y-x:
        x,y=y,x
    if tmp > 0:
        tmp = [tmp, 1]
    else:
        tmp = [-tmp, -1]
    print(x,y)
    heappush(p, tmp)