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())
lst = list(map(int,input().split()))
so = sorted(lst)
if N % 2 == 0:	
	M = sum(so[len(so)//2-1 :])-sum(so[:len(so)//2])
	print(M)
	for i in range(len(so)-1):
		if i % 2 == 0:
			print(so[0],so[-1])
			so.append(so[0]-so[-1])
			del(so[-2])
			del(so[0])
		else:
			print(so[-2],so[-1])
			so.append(so[-2]-so[-1])
			del(so[-2])
			del(so[-3])
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:
# coding: utf-8

import heapq
import sys
def input(): return sys.stdin.readline().strip()

N = int(input())
minh = [int(x) for x in input().split()]
maxh = [-x for x in minh]
# N, M = map(int, input().split())
# S = input()

heapq.heapify(minh)
heapq.heapify(maxh)

anslist = []
ans = 0
for i in range(N - 1):
    if i == N-2:
        min_n = heapq.heappop(minh)
        max_n = (-1 * heapq.heappop(maxh))

        anslist.append([max_n, min_n])
        ans = max_n - min_n
    else:
        min_n = heapq.heappop(minh)
        max_n = (-1 * heapq.heappop(maxh))
        num = min_n - max_n

        anslist.append([min_n, max_n])
        heapq.heappush(minh, num)
        heapq.heappush(maxh, -num)

print(ans)

for x in anslist:
    print(x[0], x[1])

Submission status: Wrong Answer

Solution code:
import heapq
n=int(input())
a=list(map(int,input().split()))
heapq.heapify(a)
stack=[]
p=[]
neg=[]
for i in range(n):
    if(a[i]<0):
        neg.append(a[i])
    else:
        p.append(a[i])
heapq.heapify(neg)
heapq.heapify(p)
for i in range(n-1):
    if neg!=[]:
        if len(p)>1:
            b=heapq.heappop(p)
            c=heapq.heappop(neg)
            stack.append([c,b])
            heapq.heappush(neg,c-b)
        elif len(p)==1:
            b=heapq.heappop(p)
            c=heapq.heappop(neg)
            stack.append([b,c])
            heapq.heappush(p,b-c)
        else:
            neg.sort(reverse=True)
            b=neg[0]
            c=neg[-1]
            stack.append([b,c])
            heapq.heappush(p,b-c)
            heapq.heapify(neg[1:-1])
    else:
        if len(p)>2:
            b=heapq.heappop(p)
            c=heapq.heappop(p)
            stack.append([b,c])
            heapq.heappush(neg,b-c)
        else:
            b=heapq.heappop(p)
            c=heapq.heappop(p)
            stack.append([c,b])
            heapq.heappush(p,c-b)
if p!=[]:
    print(p[0])
else:
    print(neg[0])
for i in range(n-1):
    print(stack[i][0],stack[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(len(B)>2):
    L = len(B)
    if B[L-2] < 0:
        break
    l = B[0] - B[L-1]
    print(B[0],B[L-1])
    C = [b for b in B[1:L-1]]
    C.insert(0,l)
    B = [c for c in C]
if len(B) == 0:
    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:
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(2, n):
        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:
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()
    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:
#!/usr/bin/env python

import sys

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

Submission status: Wrong Answer

Solution code:
from sys import stdin

if __name__ == '__main__':
    N = int(stdin.readline())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    A.sort(reverse=True)
    A_max = A.pop(0)

    A_minus = [i for i in A if i < 0]
    A_plus = [j for j in A if j >= 0]

    if len(A_minus) > 0:
        print(A_max - sum(A_minus) + sum(A_plus))

        A_minus.sort()
        tmp = A_minus.pop(0)

        for j in range(len(A_plus)):
            print(str(tmp) + " " + str(A_plus[j]))
            tmp = tmp - A_plus[j]

        print(str(A_max) + " " + str(tmp))
        tmp = A_max - tmp

        for i in range(len(A_minus)):
            print(str(tmp) + " " + str(A_minus[i]))
            tmp = tmp - A_minus[i]

    else:
        A_plus.sort
        tmp = A_plus.pop(0)
        print(A_max - tmp + sum(A_plus))
        for i in range(N-2):
            print(str(tmp) + " " + str(A_plus[i]))
            tmp = tmp - A_plus[i]

        print(str(A_max) + ' ' + str(tmp))

Submission status: Wrong Answer

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

A = sorted(A)

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

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)):
    print(removeNums[i][0], removeNums[i][1])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
print(sum(a) - 2*min(a))
pr = 0
for i in range(n-1):
    if i == n-2:
        print(a[i+1], pr)
    elif i == 0:
        print(a[i], a[i+1])
        pr = a[i] - a[i+1]
    else:
        print(pr, a[i+1])
        pr -= a[i+1]

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted([int(x) for x in input().split()])
ans = []

for i in range(N-1):
    if (N-i) % 2 == 0:
        ans += [A[-1],A[0]]
        n = A[-1] - A[0]
        if A[0] <= 0:    A = A[1:(N-i)-1] + [n] 
        else:
            j = (N-i)-2
            while n < A[j]:
                j -= 1
                if j == 0:    break
            A = A[1:j] + [n] + A[j:(N-i)-1]

    else:
        ans += [A[0],A[-1]]
        n = A[0] - A[-1]
        if A[-1] >= 0:    A = [n] + A[1:(N-i)-1] 
        else:
            j = 1
            while n > A[j]:
                j += 1
                if j == (N-i)-1:    break
            A = A[1:j] + [n] + A[j:(N-i)-1]

print(A[0])          
for i in range(N-1):
    print(ans[2*i],ans[2*i+1])

Submission status: Wrong Answer

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

a.sort()
xy = []
while len(a) > 2:
    n = a[0] - a[len(a)-1]
    xy.append([a[0], a[len(a)-1]])
    a.pop(0)
    a.pop(len(a)-1)
    a.insert(0, 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:
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[1]-plus[0]
  aa=0
  print(plus[1],plus[0])
  for i in range(0,N-3):
    aa=a
    a=plus[i+2]-a
    print(aa,plus[i+2])
  print(plus[N-1],aa)
    
    
    
  
elif len(plus)==0:
  M=-sum(minus)+2*minus[len(minus)-1]
  print(M)
  aa=0
  a=minus[N-1]-minus[N-2]
  print(minus[N-1],minus[N-2])
  for i in range(0,N-2):
    aa=a
    a=-minus[N-i-3]+a
    print(aa,minus[N-i-3])
    
  
  
elif len(plus) ==0:
  M=sum(plus)-sum(minus)
  print(M)
  for i in range(0,len(minus)):
    aa=a
    a=aa-minus[i]
    print(aa,minus[i])
  
  
  
else:
  M=sum(plus)-sum(minus)
  print(M)
  aa=0
  a=minus[0]-plus[0]
  print(minus[0],plus[0])
  for i in range(0,len(plus)-2):
    if len(plus)-2==0: break
    aa=a
    a=a-plus[i+1]
    print(aa,plus[i+1])
  aa=a
  a=-a+plus[len(plus)-1]
  print(plus[len(plus)-1],aa)
    
  for j in range(0,len(minus)-1):
    if len(minus)-1==0: break
    aa=a
    a=a-minus[j+1]
    print(aa,minus[j+1])

Submission status: Wrong Answer

Solution code:
input()
y=list(map(int,input().split()))
s=min(y)
y.remove(s)
print(sum(y)-s)
x=y.pop()
for i in y:
    print(str(s)+" "+str(i))
    s=s-i
print(str(x)+" "+str(s))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
res=[]
if n!=2:
    for i in range(n-2):
      a.sort()
      # -がない
      if a[0]>=0 or a[1] -a[0]>=0:
        b = a[0]-a[1]
        res.append([a[0],a[1]])
        del a[1]
        del a[0]
        a.append(b)
      # +がない
      else:
        b = a[1]-a[0]
        res.append([a[1],a[0]])
        del a[1]
        del a[0]
        a.append(b)

a.sort()
res.append([a[1],a[0]])
print(a[1]-a[0])
for i in res:
  print(i[0],i[1])

Submission status: Wrong Answer

Solution code:
import heapq
n=int(input())
a=list(map(int,input().split()))
heapq.heapify(a)
stack=[]
p=[]
neg=[]
for i in range(n):
    if(a[i]<0):
        neg.append(a[i])
    else:
        p.append(a[i])
heapq.heapify(neg)
heapq.heapify(p)
for i in range(n-1):
    if neg!=[]:
        if len(p)>1:
            b=heapq.heappop(p)
            c=heapq.heappop(neg)
            stack.append([c,b])
            heapq.heappush(neg,c-b)
        elif len(p)==1:
            b=heapq.heappop(p)
            c=heapq.heappop(neg)
            stack.append([b,c])
            heapq.heappush(p,b-c)
        else:
            neg.sort(reverse=True)
            b=neg[0]
            c=neg[-1]
            stack.append([b,c])
            heapq.heappush(p,c-b)
            heapq.heapify(neg[1:-1])
    else:
        if len(p)>2:
            b=heapq.heappop(p)
            c=heapq.heappop(p)
            stack.append([b,c])
            heapq.heappush(neg,b-c)
        else:
            b=heapq.heappop(p)
            c=heapq.heappop(p)
            stack.append([c,b])
            heapq.heappush(p,c-b)
if p!=[]:
    print(p[0])
else:
    print(neg[0])
for i in range(n-1):
    print(stack[i][0],stack[i][1])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(s) for s in input().split(' ')]
A.sort()
print(sum(A[1:]) - A[0])
answer = ''
for i in range(1, len(A) - 1):
    answer += "{} {}\n".format(A[0], A[i])
    A[0] -= A[i]
answer += "{} {}\n".format(A[N - 1], A[0])
print(answer)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

N = int(input())
A = [int(i) for i in input().split()]

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

A.reverse()
max_num = A[0]
min_num = A[-1]-A[1]
exchanges = [() for i in range(N-1)]
exchanges[0] = (A[-1],A[1])
for i in range(2,N-1):
    if A[i] < 0:
        exchanges[i-1] = (max_num,A[i])
        max_num = max_num - A[i]
    else:
        exchanges[i-1] = (min_num,A[i])
        min_num = min_num - A[i]

exchanges[-1] = (max_num,min_num)
ans = max_num-min_num
print(ans)
for i in range(N-1):
    a,b = exchanges[i]
    print(a,b)

Submission status: Wrong Answer

Solution code:


n=int(input())
i=0
a=(input().rstrip().split(" "))
cnt=[]
for i in range(n):
    a[i]=int(a[i])
  
    
a.sort()

for i in range(n-2):
    if a[n-2-i] >=0:
        cnt.append(str(a[n-2-i])+ " " + str(a[0]))
        a[0]=a[0]-a[n-2-i]
        del a[n-2-i]
    else:
        cnt.append(str(a[n-1-i])+ " " + str(a[0]))
        a[0]=a[0]-a[n-1-i]
        del a[n-1-i]
      
    a.sort()   
   
cnt.append(str(a[1])+ " " + str(a[0]))
print(a[1]-a[0]);
for i in range(n-1):
    print(cnt[i])

Submission status: Wrong Answer

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

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

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

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
if a[-1]>=0:
    m=sum(a)-a[-1]*2
    print(m)
    for i in range(n-1):
        if i==0:
            y=a[-2]
            x=a[-1]
        elif i==n-2:
            y=x-y
            x=a[0]
        else:
            x=x-y
            y=a[-2-i]
        print(x,y)
elif  a[0]<=0:
    m=-sum(a)+a[0]*2
    print(m)
    for i in range(n-1):
        if i==0:
            y=a[1]
            x=a[0]
        else:
            x=x-y
            y=a[i+1]
        print(x,y)
else:
    for i in range(n):
        if a[i]<=0:
            mem=i
            break
    m=0
    for j in range(n):
        m+=abs(a[j])
    print(m)
    for k in range(n-1):
        if k==0:
            x=a[mem]
            y=a[k]
        elif k==mem-1:
            y=x-y
            x=a[k]
        elif k<mem-1:
            x=x-y
            y=a[k]
        elif k>mem-1:
            x=x-y
            y=a[k+1]
        print(x,y)

Submission status: Wrong Answer

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

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

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

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

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


main()

Submission status: Wrong Answer

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

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

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

    X = Y

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

Submission status: Wrong Answer

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

a_max = max(a_list)

a_list.remove(a_max)
a_list.sort()

ans_list = []

for i in range(N-2):
    a = a_list[0] - a_list[-1]
    ans_list.append([a_list[0], a_list[-1]])
    a_list.remove(a_list[0])
    a_list.remove(a_list[-1])
    a_list.append(a)
    a_list.sort()
    
ans = a_max - a_list[0]
ans_list.append([a_max, a_list[0]])

print(ans)
for i in ans_list:
    print(*i)

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()
for i in range(n-2):
  print(a[0],a[-1])
  a[0] -= a[-1]
  del a[-1]
print(a[-1],a[0])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

#include <stdio.h>
#include <stdlib.h>//必須


n=int(input())
i=0
a=(input().rstrip().split(" "))
cnt=[]
for i in range(n):
    a[i]=int(a[i])
  
    
a.sort()

for i in range(n-2):
    if a[n-2-i] >=0:
        cnt.append(str(a[n-2-i])+ " " + str(a[0]))
        a[0]=a[0]-a[n-2-i]
        del a[n-2-i]
    else:
        cnt.append(str(a[n-1-i])+ " " + str(a[0]))
        a[0]=a[0]-a[n-1-i]
        del a[n-1-i]
         
   
cnt.append(str(a[1])+ " " + str(a[0]))
print(a[1]-a[0]);
for i in range(n-1):
    print(cnt[i])

Submission status: Wrong Answer

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

aLength=len(listA)
current = 0
index = 0
answerL=[]
while index < aLength-1:
    if index == 0:
        current+=listA[index]
    else:
        if current-listA[index] < listA[index]-current:
            answerL.append(str(current)+" "+str(listA[index]))
            current = current-listA[index]
        else:
            answerL.append(str(listA[index])+" "+str(current))
            current = listA[index]-current
    index+=1
if current-listA[-1] > listA[-1]-current:
    answerL.append(str(current)+" "+str(listA[-1]))
    current = current-listA[-1]
else:
    answerL.append(str(listA[-1])+" "+str(current))
    current = listA[-1]-current

print(current)
for ans in answerL:
    print(ans)

Submission status: Wrong Answer

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

tmp = []

m_li = []
p_li = []
for i in sorted(li):
    if i <= 0:
        m_li.append(i)
    else:
        p_li.append(i)

for i in range(n - 1):
    if len(m_li) == 0:
        x = min(p_li)
        y = max(p_li)
        p_li.remove(x)
        p_li.remove(y)
        m_li.append(x-y)
    elif len(p_li) == 0:
        x = max(m_li)
        y = min(m_li)
        m_li.remove(x)
        m_li.remove(y)
        p_li.append(x-y)
    else:
        if i == n-2:
            x = max(p_li)
            y = min(m_li)
            p_li.remove(x)
            m_li.remove(y)
            p_li.append(x-y)
        else:
            x = min(m_li)
            y = max(p_li)
            p_li.remove(y)
            m_li.remove(x)
            m_li.append(x-y)
    
    tmp.append(str(x) + ' ' + str(y))
    

print(p_li[0])
for i in tmp:
    print(i)

Submission status: Wrong Answer

Solution code:
import math


def main():
    N = int(input())
    X = sorted([int(a) for a in input().split()])

    # print(Xs)
    nn = N // 2
    minus = X[:nn]
    p = X[nn:]
    print(sum(p) - sum(minus))
    s = minus.pop() if N % 2 == 0 else p.pop()
    for i in range(N - 1):
        if i % 2 == N % 2:
            v = p.pop()
            print(v, s)
            s = v - s

        else:
            v = minus.pop()
            print(v, s)
            s = v - s


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq
n=int(input())
a=list(map(int,input().split()))
heapq.heapify(a)
stack=[]
p=[]
neg=[]
for i in range(n):
    if(a[i]<0):
        neg.append(a[i])
    else:
        p.append(a[i])
heapq.heapify(neg)
heapq.heapify(p)
for i in range(n-1):
    if neg!=[]:
        if len(p)>1:
            b=heapq.heappop(p)
            c=heapq.heappop(neg)
            stack.append([c,b])
            heapq.heappush(neg,c-b)
        elif len(p)==1:
            b=heapq.heappop(p)
            c=heapq.heappop(neg)
            stack.append([b,c])
            heapq.heappush(p,b-c)
        else:
            neg.sort(reverse=True)
            b=neg[0]
            c=neg[-1]
            stack.append([b,c])
            heapq.heappush(p,c-b)
            heapq.heapify(neg[1:-1])
    else:
        b=heapq.heappop(p)
        c=heapq.heappop(p)
        stack.append([b,c])
        heapq.heappush(neg,b-c)
if p!=[]:
    print(p[0])
else:
    print(neg[0])
for i in range(n-1):
    print(stack[i][0],stack[i][1])

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(int(i) for i in input().split()) 

ls = []
for i in range(n-2):
    x, y= min(a), max(a)
    a.remove(x)
    a.remove(y)
    a.append(x - y)
    ls.append((x, y))

x, y = max(a), min(a)
ls.append((x, y))

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))

A.sort()

nminus = len([i for i in A if i < 0])
nzero = len([i for i in A if i == 0])
nplus = N - nminus - nzero



if nminus == N:
    M = 2*A[N-1] - np.sum(A)
    print(M)

    caled = A[N-1]
    for i in range(N-1):
        print(caled,A[i])
        caled -= A[i]
elif nplus == N:
    M = np.sum(A) - 2*A[0]
    print(M)

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

    caled = A[0]
    for i in range(nplus-1):
        print(caled,A[nminus+i])
        caled -= A[nminus+i]
    caled_2 = A[N-1]
    for i in range(nminus-1):
        print(caled_2,A[i+1])
    print(caled_2,caled)

Submission status: Wrong Answer

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


def answer(A, ans):
    if A[1] < A[0]:
        A = sorted(A)
    ans.append([A[0], A[-2]])
    A[0] = A[0]-A[-2]
    del A[-2]
    return A, ans

A = sorted(A)
for i in range(N-2):
    A, ans = answer(A, ans)

M = A[1] - A[0]
ans.append([A[1], A[0]])

print(M)
for d in ans:
    print(d[0], d[1])

Submission status: Wrong Answer

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

pos = []
neg = []
zero = []
for i in range(n):
    if a[i] < 0:
        neg += [i]
    elif a[i] == 0:
        zero += [i]
    else:
        pos += [i]

if zero != [] or (pos != [] and neg != []):
    best = 0
    for i in range(n):
        best += abs(a[i])
    print(best)
else:
    best = 0
    for i in range(n):
        best += abs(a[i])
    if pos == []:
        best -= abs(max(a)) * 2
    else:
        best -= abs(min(a)) * 2
    print(best)

if len(pos) < len(neg):
    pos += zero
    pos.sort()
else:
    neg += zero
    neg.sort()

np = len(pos)
nn = len(neg)
if np == 0 or nn == 0:
    if np == 0:
        index = a.index(max(a))
        x = a[index]
        for i in range(n):
            if i == index:
                continue
            y = a[i]
            print(x, y)
            x -= y
    else:
        index = a.index(min(a))
        x = a[index]
        flag = 0
        if index == 0:
            flag = 1
            
        for i in range(n):
            if i == index or i == flag:
                continue
            y = a[i]
            print(x, y)
            x -= y
        x, y = a[flag], x
        print(x, y)
else:
    i = 0
    j = 0
    x = a[pos[j]]
    for k in range(np-1):
        y = a[pos[k]]
        print(x, y)
        x -= y
    y = x
    x = a[pos[np-1]]
    print(x, y)
    x -= y
    for k in range(1, nn):
        y = a[neg[k]]
        print(x, y)
        x -= y

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(0,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(0,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)
            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,pos)
                now=now-i
            print(0,now)
            for i in neg[1:]:
                print(now,i)