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 = 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]
else:
  plus *= -1
  for k in range(plus):
    print(a[-1],a[0])
    a[-1] -= a[0]
    del a[0]
  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())
A = list(map(int,input().split()))
A.sort()
K = N-1
while(K>1):
    if A[K-1] < 0:
        break
    l = A[0] - A[K]
    print(A[0],A[K])
    A[0] = l
    K -= 1
if N == 2:
    print(A[1],A[0])
    print(A[1]-A[0])
else:
    b = A[K]
    for i in range(K-1,0,-1):
        c = A[i]
        print(b,c)
        b = b-c
    print(b,A[0])
    print(b-A[0])

Submission status: Wrong Answer

Solution code:
from sys import stdin

if __name__ == '__main__':
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]
    
    A.sort(reverse=True)
    k = []
    for i in range(N-1):
        if len(A) > 2:
            a = A[-1] - A[0]
            k.append([A[-1], A[0]])
            A = A[1:len(A)-1]
            A.append(a)
        else:
            a = A[0] - A[1]
            k.append([A[0], A[1]])
            print(a)
    for s in k:
        print("{} {}".format(s[0], s[1]))

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):
  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())
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.sort()
  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.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:
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)
    x = a[0]
    print(a)
    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()
    x = a[0]
    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())
a = list(map(int, input().split()))
a.sort()
nyu = []
i=0
while i!=N-2:
    if a[1]>=0:
        x=a.pop(0)
        y=a.pop(0)
        z=[x,y]
        nyu.append(z)
        a.insert(0, x-y)
    else:
        x=a.pop(2)
        y=a.pop(1)
        z=[x,y]
        nyu.append(z)
        a.append(x-y)
        a.sort()
    i += 1

last = [max(a), min(a)]
nyu.append(last)


print(nyu[N-2][0]-nyu[N-2][1])
for i in range(N-1):
    print(nyu[i][0], nyu[i][1])

Submission status: Wrong Answer

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

a.sort()

amax = a[-1]
amin = a[0]
a = a[1:-1]

left = amax
right = amin
pairs = []

while a:
    aa = a.pop()
    if aa < 0:
        pairs.append((left, aa))
        left = left - aa
    else:
        pairs.append((aa, right))
        right = right - aa

pairs.append((left, right))
m = left - right

print(m)
for left, right in pairs:
    print(left, right)

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[0]>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[N-1]
    for i in range(N-2):
        y = As[i]
        print(x,y)
        x = x - y

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

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]-a[-1]
    ans.append([a[0],a[-1] ])
    for i in range(1, n - 2):
        ans.append([c,a[i]])
        c = c - a[i]
    ans.append([a[-2],c])
    s = a[-2]-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)
# print(ans)
for v,w in ans:
    print(v,w)

Submission status: Wrong Answer

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

A.sort()
ans = A[N-1] - (A[0]-sum(A[1:N-1]))
print(ans)

sub = A[N-2]
min_A = A[0]
max_A = A[N-1]
for i in range(1,N-1):
  sub = A[N-i-1]
  print(str(min_A) + " " + str(sub))
  min_A = min_A - sub

print(str(max_A) + " " + str(min_A))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int, input().split()))
A.sort()
positive=[x for x in A if x >=0]
negative=[x for x in A if x <0]
ans=[]



if len(negative)>0 and len(positive)>0:
    y=negative[0]
    for i in positive[1:]:
        ans.append([y,i])
        y-=i
        
    x=positive[0]
    for i in negative[1:]:
        ans.append([x,i])
        x-=i

    M=x-y
    ans.append([x,y])

elif len(negative)==0:
    x=positive[N-1]
    y=positive[0]
    for i in positive[1:N-1]:
        ans.append([y,i])
        y-=i
    M=x-y
    ans.append([x,y])

elif len(positive)==0:
    y=negative[N-1]
    x=negative[0]
    for i in negative[1:N-1]:
        ans.append([x,i])
        x-=i
    M=x-y
    ans.append([x,y])
    
print(M)
for i in ans:
    print(i[0],i[1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

info.sort()
a=n//2
for i in range(a):
    info[i]=0-info[i]
    
print(sum(info))

k=info[0]

for i in range(n-1):
    if i%2==0:
        print("{} {}".format(info[i//2+a],k))
        k=k-info[i//2+a]
        
    else:
        print("{} {}".format(info[i//2],k))
        k=k+info[i//2]

Submission status: Wrong Answer

Solution code:
import numpy as np

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

A = np.array(A)

A = np.sort(A)

B = np.copy(A)

if A[0] <= 0:
    for i in range(n-1):
        A[i] = min(A[i], -A[i])

    print(np.sum(A[:-1]) * -1 + A[n-1])

    a = B[0]
    for i in range(1, n-1):
        print(a, B[i])
        a = a - B[i]
    print(B[n-1], a)

else:
    A[0] = A[0] - A[1]
    A[1] = 0
    for i in range(n-1):
        A[i] = min(A[i], -A[i])

    print(np.sum(A[:-1]) * -1 + A[n-1])

    a = B[0]
    for i in range(1, n-1):
        print(a, B[i])
        a = a - B[i]
    print(B[n-1], a)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = [int(i) for i in input().split(" ")]
l.sort()
if l[0]<0:
    ll = [abs(j) for j in l]
    ans = sum(ll)
    print(ans)
    for i in range(N-1):
        if i==0:
            k = l[i]

        if i==N-2:
            print(l[i+1],k)
        else:
            k2 = l[i+1]
            print(k,k2)
        k -= k2
else:
    ans = sum(l[1:]) - l[0]
    print(ans)
    k = l[0]
    for i in range(1,N):
        if i==N-1:
            print(l[i],k)
        else:
            print(k, l[i])
        k -= l[i]

Submission status: Wrong Answer

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

s = min(a)
a.remove(s)

X, Y = [], []
while len(a) > 1:
    ai = a.pop()
    X.append(s), Y.append(ai)
    s -= ai

ai = a.pop()
X.append(ai), Y.append(s)

print(ai - s)
for x, y in zip(X, Y):
    print(x, y)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
import itertools

INF = 10 ** 10


# N個整数A1～AN, N=10^5
N = int(stdin.readline().rstrip())
A = list(map(int, stdin.readline().rstrip().split()))
A_abs = [abs(x) for x in A]

# 2個の整数x, yを選んで消し、新たに1個の整数x-yを書く
# ただ1つ残る整数としてありうる値の最大値Mと、その最大値を達成する操作列を求める

# 答え
result = []

plus_num = []
minus_num = []

for Ai in A:
    if Ai > 0:
        plus_num.append(Ai)
    else:
        minus_num.append(Ai)

plus_num_cnt = len(plus_num)
minus_num_cnt = len(minus_num)

if minus_num_cnt == 0:
    # マイナスを作る
    x = plus_num.pop()
    y = plus_num.pop()
    if x > y:
        tmp = x
        x = y
        y = tmp
    
    result.append([x, y])
    minus_num.append(x-y)
    plus_num_cnt -= 2
    minus_num_cnt += 1

elif plus_num_cnt == 0:
    # プラスを作る
    x = minus_num.pop()
    y = minus_num.pop()
    if x < y:
        tmp = x
        x = y
        y = tmp

    result.append([x, y])
    plus_num.append(x-y)
    minus_num_cnt -= 2
    plus_num_cnt += 1

    


if plus_num_cnt > 0 and minus_num_cnt > 0:

    while(plus_num_cnt > 0):
        if plus_num_cnt == 1:
            break

        x = minus_num.pop()
        y = plus_num.pop()

        result.append([x, y])
        minus_num.append(x-y)
        plus_num_cnt -= 1


    while minus_num_cnt > 0:
        x = plus_num.pop()
        y = minus_num.pop()

        result.append([x, y])
        plus_num.append(x-y)
        minus_num_cnt -= 1

    print(plus_num[0])
    for i, j in result:
        print(i, j)

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 min_tmp > 0:
        max_tmp2 = q.pop()
        steps.append((min_tmp, max_tmp2))
        min_tmp -= max_tmp2
        q.appendleft(min_tmp)
        q.append(max_tmp)
        continue

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

A.sort()
max_n = 0

if A[0] >= 0:
    print(sum(A)-2*A[0])
    a = A[0]
    for i in range(N-2, 0, -1):
        print(a, A[i])
        a -= A[i]
    print(A[N-1], a)
elif A[N-1] <= 0:
    print(A[N-1]-sum(A))
    a = A[N-1]
    for i in range(N-1):
        print(A[i], a)
        a -= A[i]
else:
    B = [abs(a) for a in A]
    print(sum(B))
    a = A[0]
    i = N-2
    for i in range(N-2, 0, -1):
        if A[i] <= 0:
            break
        print(a, A[i])
        a -= A[i]
    print(A[N-1], a)
    b = A[N-1] - a
    for j in range(1, i):
        print(b, A[i])
        b -= A[i]

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
b = a[0] - a[N - 1]
d = a[0]
e = a[N - 1]
for i in range(N - 4):
    b = b - a[N - 2 - i] 
c = a[1] - b
print(c)
for j in range(N - 2):
    print(d, end = ' ')
    print(e)
    d = d - e
    e = a[N - 1 - j]
print(e, end = ' ')
print(d, end = ' ')

Submission status: Wrong Answer

Solution code:
inp = input()
large_n = int(inp)
inp = input()
split_input = inp.split()
a_list = [int(i) for i in split_input]
plus_list = []
minus_list = []
zero_count = 0
for n in a_list:
    if n > 0:
        plus_list.append(n)
    elif n < 0:
        minus_list.append(n)
    else:
        zero_count += 1
if zero_count == large_n:
    print(0)
    for _ in range(large_n-1):
        print("0 0")
    exit()
print_list = []
m = 0
if zero_count == 0:
    if len(plus_list) == 0:
        max_number = max(minus_list)
        hoge = minus_list.pop(minus_list.index(max_number))
        present_number = max_number
        for number in minus_list:
            print_list.append(str(present_number) + " " + str(number))
            present_number = present_number - number
        m = present_number
    elif len(minus_list) == 0:
        mini_number = plus_list.pop(plus_list.index(min(plus_list)))
        present_number = mini_number
        for number in plus_list[:-1]:
            print_list.append(str(present_number) + " " + str(number))
            present_number = present_number - number
        print_list.append(str(plus_list[-1]) + " " + str(present_number))
        m = plus_list[-1] - present_number
    else:
        present_number = minus_list.pop()
        for number in plus_list[:-1]:
            print_list.append(str(present_number) + " " + str(number))
            present_number -= number
        print_list.append(str(plus_list[-1]) + " " + str(present_number))
        present_number = plus_list[-1] - present_number
        for number in minus_list:
            print_list.append(str(present_number) + " " + str(number))
            present_number -= number
        m = present_number
else:
    for _ in range(zero_count-1):
        print_list.append("0 0")
    # 0 一つ残る
    plus_number = 0
    if len(plus_list) > 0:
        plus_number = plus_list.pop()
    present_number = 0
    # plus なくす
    for number in plus_list:
        print_list.append(str(present_number) + " " + str(number))
        present_number -= number
    print_list.append(str(plus_number) + " " + str(present_number))
    present_number = plus_number - present_number
    for number in minus_list:
        print_list.append(str(present_number) + " " + str(number))
        present_number -= number
    m = present_number


print(m)
for p in print_list:
    print(p)

Submission status: Wrong Answer

Solution code:
from sys import stdin

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

    A.sort()
    if N == 2:
        print(A[1] - A[0])
        print(A[1], A[0])
        return

    xy = []
    n = A[0]
    for i in range(N - 2):
        xy.append((n, A[i+1]))
        n = A[i] - A[i+1]
    xy.append((A[-1], n))

    print(xy[-1][0] - xy[-1][1])
    for x, y in xy:
        print(x, y)

input = lambda: stdin.readline()
main()

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(now, newlist[ll+2])
                now=-(abs(now)+abs(newlist[ll+2]))
            counter+=1
    elif seihu==-1:
        newlist=sorted(newlist, reverse=True)
        wa=0
        for nl in range(n):
            wa+=abs(newlist[nl])
        wa+=newlist[0]*2
        print(wa)
        print(newlist[0], newlist[1])
        now=newlist[0]-newlist[1]
        for ll in range(n-2):
            if counter==n-3:
                print(now, newlist[ll+2])
                now=(abs(now)+abs(newlist[ll+2]))
            else:
                print(now, newlist[ll+2])
                now=(abs(now)+abs(newlist[ll+2]))
            counter+=1
    
else:
    print(abs(mazu*(len(newlist)-2)))
    for i in range(len(newlist)-2):
        print(mazu*(1-i), mazu)
    if mazu>=0:
        print(mazu, mazu*(3-len(newlist)))
    else:
        print(mazu*(3-len(newlist)), mazu)

Submission status: Wrong Answer

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

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

ans = []
idx = 0
while len(X) > 1:
    Y = deque()
    while len(X) > 1:
        x = X.pop()
        y = X.pop()
        if idx % 2 == 0:
            ans.append((y, x))
            if len(Y) > 0 and Y[-1] < y - x:
                Y.append(y - x)
            else:
                Y.appendleft(y - x)
        else:
            ans.append((x, y))
            if len(Y) > 0 and Y[-1] < x - y:
                Y.append(x - y)
            else:
                Y.appendleft(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:
def main():

    n = int(input())
    a = list(map(int, input().split()))
    ansList = []
    ans = -10000000000

    a.sort()

    for i in a:
        tmpAns = -sum(ansList) + i + i
        if ans < tmpAns:
            ans = tmpAns
            ansi = i

    print(ans)
    # for ans in ansList:
    #     print(ans[0], ans[1])

    a.remove(ansi)
    while len(a) >= 2:
        x = a.pop()
        y = a.pop()
        print(y, x)
        a.append(y-x)
        a.sort()

    print(ansi, a[0])


if __name__ == '__main__':
    main()

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:
        tmp = [0, 0]
    heappush(p, tmp)
res = []
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:
            res.append([x,y])
            tmp = x-y
        else:
            res.append([y,x])
            tmp = y-x
        continue
    if p[0][0] == 0:
        tmp = 0
    elif 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 = [0,0]
    elif tmp > 0:
        tmp = [tmp, 1]
    else:
        tmp = [-tmp, -1]
    res.append([x,y])
    heappush(p, tmp)
print(tmp)
for i in res:
    print(*i)

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 = []
co = 0
while True:
    if len(a) == 3:
        if a[1] >= 0:
            z = a.pop(-1)
            zz = a.pop(0)
            bs.insort(a,zz-z)
            aa.append([zz,z])
        else:
            z = a.pop(-1)
            zz = a.pop(0)
            bs.insort(a,z-zz)
            aa.append([z,zz])
    elif len(a) == 2:
        aa.append([max(a),min(a)])
        bs.insort(a,max(a),min(a))
        break
    elif len(a) == 1:
        break
    if co % 2 == 0:
        z = a.pop(-1)
        zz = a.pop(0)
        bs.insort(a,z-zz)
        aa.append([z,zz])
        co += 1
    else:
        z = a.pop(-1)
        zz = a.pop(0)
        bs.insort(a,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:
from functools import reduce

n = int(input())
nums = list(map(int, input().split()))
#max_num = max(nums)
#print(max_num)
#nums = sorted(nums)

nums.sort()
smalls = nums[:-1]

small = reduce(lambda a, b: a - b, smalls)
ans = nums[-1] - small

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

Submission status: Wrong Answer

Solution code:
#c.py
from collections import deque

n = int(input())
lis = list(map(int,input().split()))
x = 0
ans_lis = []
while len(lis) != 1:
    lis = list(lis)
    lis.sort()
    lis = deque(lis)
    mi = lis.popleft()
    ma = lis.pop()
    lis.append(mi - ma)
    ans_lis.append([mi,ma])
    x += 1
print(abs(lis[0]))
for x in ans_lis:
    print(x[1],x[0])

Submission status: Wrong Answer

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

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())
A = list(map(int, input().split()))
A = sorted(A)
if N == 2:
    m1 = A[0] - A[1]
    m2 = A[1] - A[0]
    if m1 >= m2:
        print(m1)
        print(A[0], A[1])

    else:
        print(m2)
        print(A[1], A[0])
    exit()

mi = A[0]
ma = A[-1]
if mi > 0:
    d = []
    tmp = mi
    for i in range(1, N - 1):
        d.append((tmp, A[i]))
        tmp = mi - A[i]
    d.append((A[-1], tmp))
    tmp = ma - tmp
    print(tmp)
    for a, b in d:
        print(a, b)

elif ma < 0:
    d = []
    tmp = ma
    for i in range(N - 1):
        d.append((tmp, A[i]))
        tmp -= A[i]

    print(tmp)
    for a, b in d:
        print(a, b)

else:
    d = []
    for i in range(N):
        if A[i] >= 0:
            tmp = A[i]
            for j in range(1, i):
                d.append((tmp, A[j]))
                tmp -= A[j]

            d.append((mi, tmp))
            tmp = mi - tmp
            d.append((ma, tmp))
            tmp = ma - tmp
            break
    print(tmp)
    for a, b in d:
        print(a, b)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
INPUT = sys.stdin.readline

def SINGLE_INT(): return int(INPUT())
def MULTIPLE_INT_LIST(): return list(map(int, INPUT().split()))
def MULTIPLE_INT_MAP(): return map(int, INPUT().split())
def SINGLE_STRING(): return INPUT()
def MULTIPLE_STRING(): return INPUT().split()

N = SINGLE_INT()
arr = MULTIPLE_INT_LIST()
arr.sort()

if N == 2:
    print(arr[1] - arr[0])
    print(arr[1], arr[0])

else:
    left = 0
    right = N - 1

    positives = (N + 1) // 2

    print(sum(arr[-positives:]) - sum(arr[:-positives]))

    if N % 2 == 1:
        # init
        print(arr[0], arr[N-1])
        last = arr[0] - arr[N-1]
        right -= 1
        left += 1

        for i in range(1,N-1):
            if i % 2 == 1:
                print(arr[right], last)
                last = arr[right] - last
                right -= 1
            else:
                print(arr[left], last)
                last = arr[left] - last
                left += 1

    else:
        # init
        print(arr[N-1], arr[0])
        last = arr[N-1] - arr[0]
        right -= 1
        left += 1

        for i in range(1, N-1):
            if i % 2 == 1:
                print(arr[left], last)
                last = arr[left] - last
                left += 1
            else:
                print(arr[right], last)
                last = arr[right] - last
                right -= 1

Submission status: Wrong Answer

Solution code:
n = int(input())
ps = []
mis = []
for _ in input().split():
  item = int(_)
  if item >= 0:
    ps.append(item)
  else:
    mis.append(item)
  
mis.sort(reverse = True)
ps.sort()
if len(mis) == 0:
  mis = [ps[0]]
  ps = ps[1:]
if len(ps) == 0:
  ps = [mis[0]]
  mis = mis[1:]
print(sum(ps) - sum(mis))

pre = mis[0]
for p in ps[:len(ps) - 1]:
  print(pre, p)
  pre -= p
print(ps[-1], pre)
pre -= ps[-1]
for m in mis[1:]:
  print(pre, m)
  pre -= m

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
i=0
B = []
while len(A) > 2:
    if(A[0] < A[1]):
        B.append([A[0],A[1]])
        x = A[0]
        y = A[1]
        A.pop(0)
        A.pop(0)
        A.insert(0, x-y)
    else:
        B.append([A[1],A[0]])
        x = A[1]
        y = A[0]
        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())

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

arr = sorted(nums)

if len(arr) != n:
    raise RuntimeError("invalid input")

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:
from sys import stdin
from collections import deque

input = stdin.readline

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

count = 0
for x in a:
  if x >= 0:
    count += 1

plus = count >= (n // 2 + n % 2)

a.sort(reverse=(not plus))
q = deque(a)
# print(q)

r = []
for _ in range(n-1):
  x = q.popleft()
  y = q.pop()
  r.append((x, y))
  q.appendleft(x-y)
  # print(q)

print(q[0] if not plus else -q[0])
if plus:
  r[-1] = (r[-1][1], r[-1][0])

for t in r:
  print(*t)