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:
from collections import deque
n = int(input()) -1
nums = list(map(int,input().split()))
nums.sort(reverse=True)
if nums[0] > 0 and nums[n] < 0:
  for i,j in enumerate(nums):
    if j < 0:
      mark = i
      break
  if mark == 1:
    cash = nums[0]
    minus = nums[mark:]
    print(cash - sum(minus))
    minus = deque(minus)
    while minus:
      x = minus.pop()
      print(cash,x)
      cash -= x
  else:
    cash = nums[n]
    plus = nums[1:mark]
    minus = nums[mark:]
    print(nums[0] + sum(plus) - sum(minus))
    plus = deque(plus)
    minus = deque(minus)
    while plus:
      x = plus.pop()
      print(cash,x)
      cash -= x
    minus.pop()
    minus.append(cash)
    cash = nums[0]
    while minus:
      y = minus.pop()
      print(cash,y)
      cash -= y
elif nums[0] < 0:
  cash = nums[0]
  minus = deque(nums[1:])
  print(cash - sum(nums[1:]))
  while minus:
    x = minus.pop()
    print(cash,x)
    cash -= x
else:
  if n == 1:
    print(nums[0]-nums[1])
    print(nums[0],nums[1])
  else:
    cash = nums[n]
    plus = deque(nums[1:n])
    print(sum(nums)-nums[n]*2)
    while plus:
      x = plus.pop()
      print(cash,x)
      cash -= x
    print(nums[0],cash)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n, k = LI()
    a = n - k
    if k == 1:
        print(0)
        return
    print(a)
    return

#B
def B():
    n = II()
    xy = LIR(n)
    if n == 1:
        print(1)
        return
    ans = inf
    for i in range(n):
        for k in range(n):
            if i != k:
                b = 1
                check = []
                check.append((i,k))
                p = xy[i][0] - xy[k][0]
                q = xy[i][1] - xy[k][1] 
                for l in range(n):
                    for m in range(n):
                        if not((l,m) in check) and p == xy[l][0] - xy[m][0] and q == xy[l][1] - xy[m][1]:
                            b += 1
                            check.append((l, m))
                ans = min(ans, n-b)
    print(ans)

    return

#C
def C():
    n = II()
    a = LI()
    a.sort()
    anslis = []
    f = 1
    if n % 2:
        bf = a[n // 2]
        for i in range(n // 2):
            anslis.append((a[i], a[-i - 1]))
            anslis.append((bf,a[i]-a[-i - 1]))
            bf += - a[i] + a[-i - 1]
    else:
        bf = -a[0] + a[- 1]
        anslis.append((a[-1], a[0]))
        for i in range(1,n // 2):
            anslis.append((a[i], a[-i - 1]))
            anslis.append((bf,a[i]-a[-i - 1]))
            bf += - a[i] + a[-i - 1]
    print(bf)
    for a, b in anslis:
        print(a,b)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Wrong Answer

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

BB = sorted(A)


if BB[-1] >= 0:
    x = BB[0]
    ope = []
    for y in BB[1:-1]:
        ope.append((x, y))
        x = x - y

    x, y = max(BB[-1], x), min(BB[-1], x)
    ans = x - y
    ope.append((x, y))
    print(ans)
    for x, y in ope:
        print(x, y)
else:
    end = BB[0]
    x = BB[1]
    ope = []
    for y in BB[2:]:
        ope.append((x, y))
        x = x - y

    x, y = max(end, x), min(end, x)
    ans = x - y
    ope.append((x, y))
    print(ans)
    for x, y in ope:
        print(x, y)

Submission status: Wrong Answer

Solution code:
# 2019 6/15
# diverta 2019 C Successive Subtraction

import sys
from collections import deque


if __name__ == '__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])
        sys.exit()

    max_num = A[-1]
    num = deque(A)
    num.pop()

    ans = []

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

    for i in range(0, N - 1):
        if i == N - 2:
            tmp = max_num - num[0]
            ans.append([max_num, num[0]])
            print(tmp)
            break
        ans.append([num[0], num[-1]])
        tmp = num[0] - num[-1]
        num.popleft()
        num.appendleft(tmp)
        num.pop()

    for i, item in enumerate(ans):
        print(item[0], item[1])

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a = sorted(a)

for i in range(len(a)-2):
    x = a[1]
    y = a[0]
    xy = x-y
    if xy == 0:
        print(str(a[i])+" "+str(a[i+1]))
        print(str(a[i+2])+ " " + str(xy))
    else:
        print(str(a[i])+" "+str(a[i+1]))
        print(str(a[i+2])+ " -" + str(xy))

Submission status: Wrong Answer

Solution code:
n = input()
n = int(n)
num = []*n
numint = []
num = input().split(' ')
for i in range(len(num)):
    numint.append(int(num[i]))
i = 0
x, y = int, int
word = []

numint = sorted(numint)
x = numint[0]
y = numint[1]
word.append(str(x) + " " + str(y))
numint.append(x - y)
numint.pop(0)
numint.pop(0)
while len(numint) > 2:
    numint = sorted(numint)
    x = numint[0]
    y = numint[1]
    if x < y:
        a, b = x, y
        x, y = b,a
    word.append(str(x) + " " + str(y))
    numint.append(x - y)
    numint.pop(0)
    numint.pop(0)
print(numint[0])
for i in range(len(word)):
    print(word[i])

Submission status: Wrong Answer

Solution code:
#c.py
from collections import deque

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

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

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

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

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
def sol():
    n=int(input())
    #n,k=map(int,input().split())
    l=[int(i) for i in input().split()]
    a=[(abs(l[i]),i) for i in range(n)]
    x=l[a[0][1]]
    d=[]
    for i in range(1,n):
        y=l[a[i][1]]
        if x<y:
            d.append((x,y))
            x=x-y
        else:
            d.append((y,x))
            x=(y-x)
    #print(d)
    print(abs(d[-1][0]-d[-1][1]))
    for x,i in enumerate(d):
        if x+1==n-1:
            print(max(i),min(i))
        else:
            print(i[0],i[1])

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

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

plus=[]
minus=[]

if min(A)>=0:
    tmp=min(A)
    print(sum(A)-tmp*2)
    tmpind=A.index(tmp)
    for i in range(len(A)-1):
        if i==tmpind:continue
        print(tmp,A[i])
        tmp-=A[i]
    print(A[-1],tmp)
elif max(A)<=0:
    tmp=max(A)
    print(-(sum(A)-tmp*2))
    tmpind=A.index(tmp)
    for i in range(len(A)-1):
        if i==tmpind:continue
        print(tmp,A[i])
        tmp-=A[i]
    print(tmp,A[-1])
else:
    print(sum(map(abs,A)))
    for i in range(len(A)):
        if A[i]>0:
            plus.append(A[i])
        else:
            minus.append(A[i])
    if len(plus)==1 and len(minus)==1:
        print(plus[0],minus[0])
    elif len(minus)==1:
        tmp=minus[0]
        for i in range(len(plus)-1):
            print(tmp,plus[i])
            tmp-=A[i]
        print(plus[-1],tmp)
    else:
        for i in range(1,len(minus)):
            print(plus[-1],minus[i])
            plus[-1]-=minus[i]
        tmp=minus[0]
        for i in range(len(plus)-1):
            print(tmp,plus[i])
            tmp-=A[i]
        print(plus[-1],tmp)

Submission status: Wrong Answer

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

a.sort()
ope = []
max_num = a[-1]
min_num = a[0]
p = bisect.bisect_left(a, 0)
for i in range(1, p):
    ope.append([max_num, a[i]])
    max_num -= a[i]
for i in range(max(p, 1), n - 1):
    ope.append([min_num, a[i]])
    min_num -= a[i]
ope.append([max_num, min_num])
print(max_num - min_num)
for i in range(n - 1):
    print(" ".join(map(str, ope[i])))

Submission status: Wrong Answer

Solution code:
import heapq

N = int(input())

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

plus = []
minus = []

for i, ai in enumerate(A):
    if ai <= 0:
        heapq.heappush(minus, -ai)
    else:
        heapq.heappush(plus, ai)

if len(minus) == 0:
    m = heapq.heappop(plus)
    print(sum(plus) - m)
    for i in range(N-2):
        k = heapq.heappop(plus)
        print(m, k)
        m -= k
    k = heapq.heappop(plus)
    print(k, m)
elif len(minus) == 1:
    m = -heapq.heappop(minus)
    print(sum(plus) - m)
    for i in range(N-2):
        k = heapq.heappop(plus)
        print(m, k)
        m -= k
    k = heapq.heappop(plus)
    print(k, m)
else:
    m = -heapq.heappop(minus)
    print(sum(plus) + sum(minus) + m)
    for i in range(len(minus)-1):
        k = -heapq.heappop(minus)
        print(m, k)
        m -= k
    n = -heapq.heappop(minus)
    for i in range(len(plus)):
        k = heapq.heappop(plus)
        print(n, k)
        n -= k
    print(m, n)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]
data = sorted(data)
ans = [0] * (N-1)
for i in range(1,N):
    if i % 2 == 0:
        val = data[-1] - data[0]
        ans[i-1] = (data[-1],data[0])
    else:
        val = data[0] - data[-1]
        ans[i-1] = (data[0],data[-1])
        
    data = sorted(data[i:N-i] + [val])
print(sum(data))
for j,k in ans:
    print(j,k)

Submission status: Wrong Answer

Solution code:
from functools import reduce

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


n = int(input())
nums = list(map(int, input().split()))
#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)

#(lambda a, b: print(a, b), smalls))
#[lambda a, b: print(a, b), smalls]
#reduce(print_diff, smalls)

idx = 0
temp = smalls[0]

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

#print(smalls[0], nums[-1])
print(nums[-1], smalls[0])

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=0-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(k,-info[i//2]))
        k=k+info[i//2]

Submission status: Wrong Answer

Solution code:
N = int(input())

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

output_list = [0] * (N - 1)

def get_maximum_diff(some_list):
    tmp_max = max(some_list)
    some_list.pop(some_list.index(tmp_max))
    tmp_min = min(some_list)
    some_list.pop(some_list.index(tmp_min))
    some_list.append(tmp_max - tmp_min)
    return some_list, tmp_min, tmp_max
    
def get_minimum_diff(some_list):
    tmp_max = max(some_list)
    some_list.pop(some_list.index(tmp_max))
    tmp_min = min(some_list)
    some_list.pop(some_list.index(tmp_min))
    some_list.append(tmp_min - tmp_max)
    return some_list, tmp_min, tmp_max

for i in range(N - 1):
    if i % 2 == 1:
        A_list, tmp_min, tmp_max = get_maximum_diff(A_list)
    else:
        A_list, tmp_min, tmp_max = get_minimum_diff(A_list)
    output_list[i] = [tmp_min, tmp_max]
print(abs(sum(A_list)))
for item in output_list:
    print(item[0], item[1])

Submission status: Wrong Answer

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

A_MAX = max(A)
X = sorted(A)[1:]
x = X[0]
ans = []
for v in X[1:]:
    ans.append((x, v))
    x -= v

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# input
n = int(input())
a = list(map(int, input().split()))
# calc
ap = [i for i in a if i > 0]
am = [i for i in a if i < 0]
a0 = [i for i in a if i == 0]
ans = 0
t = 0
t2 = 0
lap = len(ap)
lam = len(am)
la0 = len(a0)
if lap > 0 and lam > 0:
    ans = sum(ap)+abs(sum(am))
    print(ans)
    for i in range(la0):
        print(am[0], 0)

    if lam == 1 and lap > 1:
        print(am[0], ap[0])
        t = am[0]-ap[0]
        for i in range(1, lap-1):
            print(t, ap[i])
            t -= ap[i]
        print(ap[lap-1], t)
    elif lam > 1 and lap == 1:
        print(ap[0], am[0])
        t = ap[0]-am[0]
        for i in range(1, lam):
            print(t, am[i])
            t -= am[i]
    elif lam == 1 and lap == 1:
        print(ap[0], am[0])
    else:
        t = am[0]
        t2 = ap[0]
        for i in range(1, lap):
            print(t, ap[i])
            t -= ap[i]
        for i in range(1, lam):
            print(t2, am[i])
            t2 -= am[i]
        print(t2, t)
elif lap == 0 and lam > 1:
    am.sort()
    for i in range(lam-1):
        t += am[i]
    ans = t*(-1) + am[lam-1]
    print(ans)
    for i in range(la0):
        print(am[0], 0)
    t2 = am[lam-1]
    for i in range(lam-1):
        print(t2, am[i])
        t2 -= am[i]
elif lap > 1 and lam == 0:
    ap.sort()
    for i in range(1, lap):
        t += ap[i]
    ans = t - ap[0]
    print(ans)
    for i in range(la0):
        print(ap[0], 0)
    t2 = ap[0]
    for i in range(1, lap-1):
        print(t2, ap[i])
        t2 -= ap[i]
    print(ap[lap-1], t2)
elif lap == 1 and lam == 0:
    print(ap[0])
    for i in range(la0):
        print(ap[0], 0)
elif lap == 0 and lam == 1:
    print(abs(am[0]))
    for i in range(la0-1):
        print('0 0')
    print(0, am[0])
elif lap == 0 and lam == 0:
    print(0)
    for i in range(la0-1):
        print('0 0')

Submission status: Wrong Answer

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

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

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

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

        minus = deque(A[:N//2+1])
        plus = deque(A[N//2+1:])
        m = minus.popleft()
        p = plus.popleft()
        tmp = p - m
        ans_li = ['{} {}'.format(-p, -m)]
        while True:
            try:
                m = minus.popleft()
                ans_li.append('{} {}'.format(-m, -tmp))
                tmp = m - tmp
                p = plus.popleft()
                ans_li.append('{} {}'.format(-p, -tmp))
                tmp = p - tmp
            except:
                if -tmp > plus_first[0]:
                    print(-tmp)
                    for i in ans_li: print(i)
                else:
                    print(plus_first[0])
                    for i in plus_first[1]: print(i)
                return

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


main()

Submission status: Wrong Answer

Solution code:
N = int(input())

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

result = []


while(len(li)>2):
    li.sort()
    cnt = len(li)

    result.append([li[0], li[cnt-1]])
    num1 = li[cnt-1] - li[0]
    num2 = li[0] - li[cnt-1]
    if abs(num1 - li[1]) >= abs(li[cnt-2]- num2):
        num = num1
    else:
        num = num2

    li.pop(cnt-1)
    li.pop(0)
    li.append(num)

result.append([li[0],li[1]])

print(abs(li[1]- li[0]))
for i in range(N-1):
    print(str(result[i][0]) + " " + str(result[i][1]))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
a1 = [i for i in a if i >= 0]
a2 = [i for i in a if i < 0]
if a1 and a2:
  print(sum(a1)-sum(a2))
  for i in range(len(a1)-1):
    print(a2[0],a1[i])
    a2[0] = a2[0] - a1[i]
  acur = a1[len(a1)-1] - a2[0]
  print(a1[len(a1)-1],a2[0])
  for i in range(1,len(a2)):
    print(acur,a2[i])
    acur = acur - a2[i]
elif a1:
  a1.sort()
  print(sum(a1)-2*a[0])
  for i in range(1,n-1):
    print(a1[0],a1[i])
    a1[0] -= a1[i]
  print(a1[n-1],a1[0])
else:
  a2.sort()
  print(2*a2[n-1]-sum(a2))
  for i in range(n-1):
    print(a2[n-1],a2[i])
    a2[n-1] -= a2[i]

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

import sys
import numpy as np

n = int( sys.stdin.readline().strip() )
a = np.asarray( [ np.int64(x) for x in sys.stdin.readline().strip().split() ],dtype=np.int64)
a.sort()
#
h = n // 2
m = a[0:h]
p = a[h:]
#
print( p.sum() - m.sum() )
#
flag= n%2==0
if flag: t= m[0]
else: t= p[0]
#
h1= h -1
fmt= "{:>d} {:>d}"
for i in range(h):
    if flag:
        print( fmt.format( p[i],t ) )
        t = p[i] - t
        if i==h1: break
        print( fmt.format( m[i+1],t ) )
        t = m[i+1] - t
    else:
        print( fmt.format( m[i], t ) )
        t = m[i] - t
        #
        print( fmt.format( p[i+1], t ) )
        t = p[i+1] - t

Submission status: Wrong Answer

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

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

# np.random.seed(1234)
# N = 5
# A = [np.random.randint(-10, 1) for _ in range(N)]

A.sort(reverse=True)


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

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

B = []

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

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

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

from collections import Counter

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

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

Submission status: Wrong Answer

Solution code:
from functools import reduce

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


n = int(input())
nums = list(map(int, input().split()))
#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)

#(lambda a, b: print(a, b), smalls))
#[lambda a, b: print(a, b), smalls]
#reduce(print_diff, smalls)

idx = 0
temp = smalls[0]

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

print(smalls[0], nums[-1])

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
L2=[i*-1 for i in L[1:-1]]
L2.sort()
print(L[-1]-(L[0]+sum(L2)))
n=0
while n<=N-2:
  s=0
  if n==0:
    print(L[0], end=" ")
    print(L[1])
    n=n+1
  else:
    print(L[n+1], end=" ")
    print(L[0]+sum(L2[:n+1]))
    n=n+1

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
pos, neg = [], []
for num in a:
    if num >= 0:
        pos.append(num)
    else:
        neg.append(num)

def maximizeNeg():
    cur = neg[0]
    for i in range(1, len(neg)):
        ret.append((cur, a[i]))
        cur -= a[i]
    return cur

def maximizePos():
    cur = pos[0]
    for i in range(1, len(pos) - 1):
        ret.append((cur, pos[i]))
        cur -= pos[i]
    ret.append((pos[-1], cur))
    cur = pos[-1] - cur
    return cur

pos.sort()
neg.sort(reverse=True)
ret = []
if not neg:
    cur = maximizePos()
elif not pos:
    cur = maximizeNeg()
else:
    cur = neg[-1]
    for i in range(len(neg) - 1):
        ret.append((cur, neg[i]))
        cur -= neg[i]
    for i in range(len(pos) - 1):
        ret.append((cur, pos[i]))
        cur -= pos[i]
    ret.append((pos[-1], cur))
    cur = pos[-1] - cur

print(cur)
print('\n'.join('%d %d' % (xy[0], xy[1]) for xy in ret))

Submission status: Wrong Answer

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]
data = sorted(data)
ans = [0] * (N-1)
for i in range(1,N):
    if i % 2 == 0:
        val = data[-1] - data[0]
        ans[i-1] = (data[-1],data[0])
    else:
        val = data[0] - data[-1]
        ans[i-1] = (data[0],data[-1])
        
    data = sorted(data[i:N-i] + [val])
for j,k in ans:
    print(j,k)
print(sum(data))

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:
            if i == N - 1:
                tmp = ma
                for j in range(N - 1):
                    d.append((tmp, A[j]))
                    tmp -= A[j]
                print(tmp)
                for a, b in d:
                    print(a, b)
                exit()
            tmp = A[i]
            for j in range(1, i):
                d.append((tmp, A[j]))
                tmp -= A[j]

            d.append((mi, tmp))
            tmp = mi - tmp
            for j in range(i + 1, N - 1):
                d.append((tmp, A[j]))
                tmp = tmp - A[j]

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

for i in range(N-2):
    a = A.pop(0)
    b = A.pop(-2)
    X.append([a, b])
    A.insert(0, a-b)

print(A[1] - A[0])
for a, b in X:
    print(a, b)
print(A[1], A[0])

Submission status: Wrong Answer

Solution code:
# div2019-2C - Successive Subtraction
import sys
input = sys.stdin.readline

n = int(input())
lst = sorted(list(map(int, input().rstrip().split())))
log = []

if lst[-1] >= 0:
    ans = lst[0]
    for i in lst[1:-1]:
        log += [(ans, i)]
        if i > 0:
            ans -= i
        else:
            ans += i
    log += [(lst[-1], ans)]
    print(lst[-1] - ans)
else:
    lst = lst[::-1]
    ans = lst[0]
    for i in lst[1:-1]:
        log += [(ans, i)]
        ans -= i
    log += [(ans, lst[-1])]
    print(ans - lst[-1])
    
for i, j in log:
    print(i, j)