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())
alist = list(map(int,input().split()))
ans = []

if len(alist) == 2:
    alist.sort(reverse = True)
    print(alist[0]-alist[1])
    print(' '.join(map(str,alist)))
else:
    plus =[w for w in alist if w>=0]
    minus =[x for x in alist if x<0]
    if minus == []:
        plus.sort()
        for y in range(len(plus)-2):
            ans.append(' '.join(map(str,plus[y:y+2])))
            plus[y+1] = plus[y]-plus[y+1]
        pplus = plus[::-1]
        ans.append(' '.join(map(str,pplus[0:2])))
        print(pplus[0]-pplus[1])
        for y in ans:
            print(y)

Submission status: Wrong Answer

Solution code:
N = int(input())
# -*- coding: utf-8 -*-
#hq = []
#hq_inv = []#大きいもの
#import heapq
l = list(map(int,input().split()))
l.sort()
import bisect
index = bisect.bisect_right(l, 0)
print(l[:0])
if index == 0:
    print(sum(l)-l[0])    
else:
    print(sum(l[index:])-sum(l[:index]))

Submission status: Wrong Answer

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

import copy

List.sort()

result_list = []
tmp_list = copy.deepcopy(List)

if List[0] >= 0:
    for i in range(N-1):
        x = tmp_list[1]
        y = tmp_list[0]
        result_list.append([x,y])
        del tmp_list[0]
        tmp_list[0] = x - y
        
elif List[N-1] <= 0:
    for i in range(N-1):
        x = tmp_list[N-i-1]
        y = tmp_list[N-i-2]
        result_list.append([x,y])
        del tmp_list[N-i-1]
        tmp_list[N-i-2] = x - y
        
else:
    i = 0
    while tmp_list[N-i-2] >= 0:
        x = tmp_list[0]
        y = tmp_list[N-i-1]
        result_list.append([x,y])
        del tmp_list[N-i-1]
        tmp_list[0] = x - y
        i += 1
    for j in range(N-i-1):
        x = tmp_list[N-i-j-1]
        y = tmp_list[0]
        result_list.append([x,y])
        del tmp_list[0]
        tmp_list[N-i-j-2] = x - y

result = x - y

print(result)

for i in range(len(result_list)):
    print(result_list[i][0], result_list[i][1])

Submission status: Wrong Answer

Solution code:
def main():

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

    a.sort()

    i = n // 2
    ans = sum(a[i:])-sum(a[:i])
    print(ans)

    ansi = a.pop()
    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:
n=int(input())
A=list(map(int,input().split()))
a=sorted(A)
d=a[0]
for i in range(n-2):
  d-=a[n-1-i]
D=a[1]-d
print(D)
e=a[0]
for j in range(n-2):
  print(e,a[n-1-i])
  e-=a[n-1-i]
print(a[1],e)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = []
C = []
D = []
for i in range(N):
    if A[i]<0:
        C.append(-A[i])
    else:
        D.append(A[i])
if(C is None or sum(C) < sum(D)):
    A.sort()
    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])
else:
    list.sort(A, reverse=True)
    while len(A) > 2:
        if(A[0] < A[1]):
            B.append([A[1],A[0]])
            x = A[1]
            y = A[0]
            A.pop(0)
            A.pop(0)
            A.insert(0, x-y)
        else:
            B.append([A[0],A[1]])
            x = A[0]
            y = A[1]
            A.pop(0)
            A.pop(0)
            A.insert(0, x-y)
    if(A[1] > A[0]):
        B.append([A[1],A[0]])
        print(A[1]-A[0])
    else:
        B.append([A[0],A[1]])
        print(A[0]-A[1])
    for i in range(N-1):
        print(B[i][0], B[i][1])

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = list(map(int,input().split()))
lst = sorted(lst, reverse=True)

print(sum(lst[0:n-1]) - lst[n-1])

for _ in range(n-2):
    a = lst.pop()
    b = lst.pop()
    print("%s %d" % (a,b))
    lst.append(a-b)
    lst = sorted(lst, reverse=True)

print("%s %d" % (lst[0],lst[1]))

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]
if N > 2:
    for i in range(N - 3):
        b = b - a[N - 2 - i] 
    if b > a[1]:
        print(b - a[1])
    else:
        print(a[1] - b)
    for j in range(N - 2):
        print(d, end = ' ')
        print(e)
        d = d - e
        e = a[N - 2 - j]
    if e >= 0:
        print(e, end = ' ')
        print(d, end = ' ')
    else:
        print(d, end = ' ')
        print(e, end = ' ')
else:
    print(a[1] - a[0])
    print(a[1], end = ' ')
    print(a[0], end = ' ')

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
if l[0]>=0 or l[-1]<0:
  print(sum(l)-2*l[0])
  for i in range(1,n-1):
    print(l[0],l[i])
    l[0]-=l[i]
else:
  cnt=0
  i=0
  while l[i]<0:
    cnt+=1
    if l[i+1]>=0:
      break
    i+=1
  ans=0
  for j in range(n):
    ans+=abs(l[j])
  print(ans)
  for j in range(n-2-i):
    print(l[0],l[i+j+1])
    l[0]-=l[i+j+1]
  for j in range(i+1):
    print(l[-1],l[j])
    l[-1]-=l[j]

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def main():
    from collections import deque

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

    for ai in a:
        d.append(ai)

    ans = list()
    count = n

    while count > 1:
        left = d.popleft()
        right = d.pop()
        ans.append((left, right))

        tmp = left - right
        d.append(tmp)
        count -= 1

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

    for i in range(len(ans)):
        print(ans[i][0], ans[i][1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque
import bisect

input = stdin.readline

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

a.sort(reverse=True)
q = deque(a)
# print(q)

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

print(abs(q[0] - q[1]))
r.append((max(q),min(q)))

for t in r:
  print(*t)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
a.reverse()
b=a[0]
c=a[0]
d=a[-1]

for i in range(1,n):
  b-=a[i]
for i in range(1,n//2+1):
  c+=a[i]
for i in range(n//2):
  c-=a[-1*(i+1)]

if b>c:
  for i in range(1,n):
    print(b,a[i])
  print(b)
else:
  for i in range(1,n-1):
    print(d,a[-1*i-1])
    d-=a[-1*i-1]
  print(a[0],d)
  print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
a = sorted(a)
x = a[0]
for i in range(1, N-1):
  x = x - a[i]
print(a[N-1] - x)

x = a[0]

for i in range(1, N-1):
  print(x,a[i],sep=' ')
  x = x - a[i]
print(a[N-1],x,sep=' ')

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a_2 = [abs(a[i]) for i in range(n)]
a.sort()

if a[0] >= 0:
    print([sum(a_2) -2, sum(a_2)][a[0] == 0])
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(aa, a[i + 1])
            aa = aa - a[i + 1]
        elif i == n - 2:
            print(a[i + 1], aa)
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

elif a[-1] <= 0:
    print([sum(a_2) -2, sum(a_2)][a[-1] == 0])
    if a[-1] == 0:
        a = [a[0]] + [a[-1]] + a[1:]
    aa = a[0]
    for i in range(n-1):
        if i == 0:
            print(a[i+1], aa)
            aa = a[i + 1] - aa
        elif i == n - 2:
            print(aa, a[i + 1])
        else:
            print(aa, a[i+1])
            aa = aa - a[i + 1]

else:
    print(sum(a_2))
    minus = []
    plus = []
    for i in range(n):
        if a[i] < 0:
            minus.append(a[i])
        else:
            plus.append(a[i])
    minus2 = minus.pop()
    plus2 = plus.pop()
    for i in range(len(minus)):
        print(plus2 ,minus[i])
        plus2 -= minus[i]
    for i in range(len(plus)):
        print(minus2, plus[i])
        minus2 -= plus[i]
    print(plus2, minus2)

Submission status: Wrong Answer

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

import copy

List.sort()

result_list = []
tmp_list = copy.deepcopy(List)

if List[0] >= 0:
    for i in range(N-1):
        x = tmp_list[1]
        y = tmp_list[0]
        result_list.append([x,y])
        del tmp_list[0]
        tmp_list[0] = x - y
        
elif List[N-1] <= 0:
    for i in range(N-1):
        x = tmp_list[N-i-1]
        y = tmp_list[N-i-2]
        result_list.append([x,y])
        del tmp_list[N-i-1]
        tmp_list[N-i-2] = x - y
        
else:
    i = 0
    while tmp_list[N-i-2] >= 0:
        x = tmp_list[0]
        y = tmp_list[N-i-1]
        result_list.append([x,y])
        del tmp_list[N-i-1]
        tmp_list[0] = x - y
        i += 1
    for j in range(N-i-1):
        x = tmp_list[N-i-j-1]
        y = tmp_list[0]
        result_list.append([x,y])
        del tmp_list[0]
        tmp_list[N-i-j-2] = x - y

result = tmp_list[0]

print(result)

for i in range(len(result_list)):
    print(result_list[i][0], result_list[i][1])

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
data = [int(s) for s in stdin.readline().rstrip().split()]

xy = []
data = sorted(data)
while len(data) > 0:
  if len(data) == 2:
    xy.append([max(data[0], data[1]), min(data[0], data[1])])
    data = [abs(data[0] - data[1])]
    break
  mx = data.pop(-1)
  mn = data.pop(0)
  if mx >= 0 and mn >= 0:
    data.append(mn - mx)
    xy.append([mn, mx])
  if mx < 0 and mn < 0:
    data.append(mx - mn)
    xy.append([mx, mn])
  if mx >= 0 and mn < 0:
    data.append(mx - mn)
    xy.append([mx, mn])
  if mx < 0 and mn >= 0:
    data.append(mn - mx)
    xy.append([mn, mx])
  data = sorted(data)

print(data[0])
for d in xy:
  print("%d %d" % (d[0], d[1]))

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A = sorted(A)
    A_M = sorted(A)
    A_M[0] *= -1
    start = 0
    if N == 2:
        print(max(A) - min(A))
        print('{} {}'.format(max(A), min(A)))
        return
    for i in range(1, N-1):
        if A_M[i] < 0:
            A_M[i] *= -1
            start = i
    M = sum(A_M)
    print(M)
    if start == 0:
        for i in range(1, N):
            if i < N-1:
                if i == 1:
                    x = A[0]
                    y = A[1]
                else:
                    x = tmp
                    y = A[i]
            else:
                x = A[-1]
                y = tmp
            tmp = x - y
            print('{} {}'.format(x, y))
    elif start == N - 2:
        for i in range(1, N):
            if i == 1:
                x = A[-1]
                y = A[i-1]
            else:
                x = tmp
                y = A[i-1]
            tmp = x - y
            print('{} {}'.format(x, y))
    else:
        for i in range(1, N):
            if i == 1:
                x = A[start]
                y = A[start+1]
            elif 1 < i < N - 1 - start:
                x = tmp
                y = A[start+i+1]
            elif i == N - 1 - start:
                x = A[-1]
                y = tmp
            else:
                x = tmp
                y = A[i-(N-1-start)-1]
            tmp = x - y
            print('{} {}'.format(x, y))
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
print("時間が足りませんでした")
n = int(input())
lis = list(map(int,input().split()))
lis.sort(reverse = True)
for i in range(n):
  if lis[i] < 0:
    lia = lis[:i]
    lib = lis[i:]
    break
  if i == n-1:
    lia = lis
    lib = []
num = 0
print(lia,lib)
m = len(lia)
if m > 1:
  if m % 2 == 0:
    print(abs(sum(lia[:m//2]))-abs(sum(lia[m//2:]))-sum(lib))
    print(lia[0],lia[-1])
    num = lia[0]-lia[-1]
    for i in range(1,m//2):
      print(lia[-(i+1)],num)
      num = lia[-(i+1)]-num
      print(lia[i],num)
      num = lia[i]-num

  else:
    print(sum(lia[:m//2])-sum(lia[m//2+1:])+abs(lia[m//2])-sum(lib))
    print(lia[-1],lia[0])
    num = lia[-1]-lia[0]
    for i in range(1,m//2):
      print(lia[i],num)
      num = lia[i]-num
      print(lia[-(i+1)],num)
      num = lia[-(i+1)]-num     
    print(lia[m//2],num)
  if len(lib) > 0:
    print(num,lib[0])
    num -= lib[0]
elif m == 1:
  print(lia[0] -sum(lib))
  print(lia[0],lib[0])
  num = lia[0]-lib[0]
else:
  print(-sum(lib)+lib[0] * 2)
  num = lib[0]
for i in range(len(lib)-1):
  print(num,lib[i+1])
  num -= lib[i+1]

Submission status: Wrong Answer

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

def positive_negative(target_list):
    positive = 0
    negative = 0
    zero = 0
    for i in target_list:
        if i > 0:
            positive = 1
        elif i < 0:
            negative = 1
        else:
            zero = 1
            
    if zero == 1:
        return 0
    elif positive == 1 and negative == 0:
        return 1
    elif positive == 1 and negative == 1:
        return 2
    else: # positive == 0 and negative == 1
        return 3
      
print_list = []

while(1):
    if len(A) == 2:
        break

    p_n = positive_negative(A)
    if p_n == 0:
        A.remove(0)
        A.append(A[0])
        print_list.append(str(A[0])+' '+str(0))
        A.remove(A[0])
    
    elif p_n == 1:
        min1 = min(A)
        A.remove(min1)
        min2 = min(A)
        A.remove(min2)
    
        print_list.append(str(min1)+' '+str(min2))
        A.append(min1 - min2)
    
    elif p_n == 2:
        min1 = min(A)
        A.remove(min1)
        max1 = max(A)
        A.remove(max1)
    
        print_list.append(str(min1)+' '+str(max1))
        A.append(min1 - max1)
    
    else: #p_n == 3
        min1 = min(A)
        A.remove(min1)
        max1 = max(A)
        A.remove(max1)

        print_list.append(str(max1)+' '+str(min1))
        A.append(max1 - min1)

min1 = min(A)
A.remove(min1)
max1 = max(A)
A.remove(max1)

print_list.append(str(max1)+' '+str(min1))
A.append(max1 - min1)


print(A[0])
for s in print_list:
    print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

for i in range(N-1):
    if i%2==0: # Negative
        x = A.pop()
        y = A.popleft()
        print(x,y)
        A.appendleft(y + x * -1)
    else: # Positive
        x = A.popleft()
        y = A.pop() 
        print(x,y)
        A.append(y + x * -1)

print(sum(list(A)))

Submission status: Wrong Answer

Solution code:
import sys

def main():
    n = int(input())
    a = [int(x) for x in input().split()]

    diff_idx = -1
    min_idx = -1
    _min = 10*6
    for i in range(len(a)-1):
        if a[i] * a[i+1] <= 0 and (a[i] != 0 or a[i+1] != 0):
            diff_idx = i
            break
        tmp_min = abs(a[i]) + abs(a[i+1])
        if tmp_min < _min:
            _min = tmp_min
            min_idx = i

    curr = 0
    start_idx = diff_idx if diff_idx != -1 else tmp_min
    ops = []

    if diff_idx != -1:
        curr = abs(a[diff_idx]) + abs(a[diff_idx+1])
        start_idx = diff_idx
    else:
        curr = abs(a[min_idx] - a[min_idx+1])
        start_idx = min_idx

    a2 = a[start_idx:]
    a2.extend(a[:start_idx])

    for i in range(0,len(a2)-2):
        b = a[i]
        s = a[i+1]

        if a[i] < a[i+1]:
            b,s = s,b

        if a2[i+2] < 0:
            curr = b - s
            ops.append((b,s))
        else:
            curr = s - b
            ops.append((s,b))

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
m = 0
p = 0
for i in a:
    if i < 0:
        m += 1
    elif i > 0:
        p += 1
if p == 0 and m == 0:
    print(0)
    exit()
ans = []
while len(a) > 1:
    if p > m:
        ans.append([a[0], a[-1]])
        a.append(a[0] - a[-1])
        del a[0]
        del a[-1]
    else:
        ans.append([a[-1], a[0]])
        a.append(a[-1] - a[0])
        del a[0]
        del a[-1]
    a = sorted(a)
    m = 0
    p = 0
    for i in a:
        if i < 0:
            m += 1
        elif i > 0:
            p += 1
print(a[0])
for i in ans:
    print("{} {}".format(i[0], i[1]))

Submission status: Wrong Answer

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


if a[0] >= 0:
    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]
elif a[-1] < 0:
    print(abs(sum(a)) - abs(2*max(a)))
    a.sort(reverse=True)
    pr = 0
    for i in range(n-1):
        if i == 0:
            print(a[i], a[i+1])
            pr = a[i] - a[i+1]
        else:
            print(pr, a[i+1])
            pr -= a[i+1]
else:
    print(sum([abs(i) for i in a]))
    pr = 0
    for i in range(n-1):
        if 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 = 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 = []

while len(numint) >= 2:
    numint = sorted(numint)
    x = numint.pop(0)
    y = numint.pop(0)
    if x < y:
        a, b = x, y
        x, y = b,a
    word.append(str(x) + " " + str(y))
    numint.append(x - y)

print(numint[0])
for i in range(len(word)):
    print(word[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = list(map(int,input().split()))
lst = sorted(lst, reverse=True)

print(lst[0]+lst[n-1]-sum(lst[1:n-1]))

for _ in range(n-2):
    a = lst.pop()
    b = lst.pop()
    print("%s %d" % (a,b))
    lst.append(a-b)
    lst = sorted(lst, reverse=True)

print("%s %d" % (lst[0],lst[1]))

Submission status: Wrong Answer

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

A.sort()

if (A[0] <= 0 and A[n-1] > 0) or (A[0] < 0 and A[n-1] >= 0):
    print(sum(list(map(abs, A))))
    while sum(a>=0 for a in A) > 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(atama, ketu)
        new = atama - ketu
        A.insert(0, new)
    while len(A) != 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(ketu, atama)
        new = ketu - atama
        A.append(new)
elif A[0] > 0:
    atama = A.pop(0)
    tugi = A.pop(0)
    new = atama - tugi
    A.insert(0, new)
    print(sum(list(map(abs, A))))
    print(atama, tugi)
    while sum(a>=0 for a in A) > 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(atama, ketu)
        new = atama - ketu
        A.insert(0, new)
    while len(A) != 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(ketu, atama)
        new = ketu - atama
        A.append(new)
else:
    ketu = A.pop(-1)
    tugi = A.pop(-1)
    new = ketu - tugi
    A.append(new)
    print(sum(list(map(abs, A))))
    print(ketu, tugi)
    while sum(a>=0 for a in A) > 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(atama, ketu)
        new = atama - ketu
        A.insert(0, new)
    while len(A) != 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(ketu, atama)
        new = ketu - atama
        A.append(new)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
b = []
a = sorted(a)
center = a[N//2]
while len(a) > 2:
    xp = a.pop(0)
    yp = a.pop(-1)

    if xp > yp:
        y = xp
        x = yp
    else:
        x = xp
        y = yp

    if center < 0:
        b.append([y, x])
        a.insert(-1, y-x)
    else:
        b.append([x, y])
        a.insert(1, x-y)

if a[0] > a[1]:
    print(a[0]-a[1])
else:
    print(a[1]-a[0])

for e in b:
    print(e[0], e[1])

if a[0] > a[1]:
    print(a[0], a[1])
else:
    print(a[1], a[0])

Submission status: Wrong Answer

Solution code:
N = int(input())
numl = [int(arr) for arr in input().split()]
numl.sort()
root = []

if N > 2:
	for i in range(N-2):
		m = len(numl)-2
		root.append([numl.pop(0),numl.pop(m-1)])
		tmp = root[-1][0] - root[-1][1]
		numl.insert(0,tmp)
root.append([numl[1],numl[0]])
print(root[-1][0]-root[-1][1])
for j in range(len(root)):
	print("{} {}".format(root[j][0],root[j][1]))

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.pop(0)
    y = numint.pop(0)
    if x < y:
        a, b = x, y
        x, y = b,a
    word.append(str(x) + " " + str(y))
    numint.append(x - y)

print(numint[0])
for i in range(len(word)):
    print(word[i])

Submission status: Wrong Answer

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

a_sorted = sorted(a_list)
b_list = []
while True:
  b_list.append(a_sorted.pop(-1))
  if len(a_sorted) == 0:
    break
  b_list.append(a_sorted.pop(0))
  if len(a_sorted) == 0:
    break

sousa_list = []
if len(b_list) % 2 == 1: 
  dai = b_list.pop(-1)
  syou = b_list.pop(-1)
  sousa_list.append(syou)
  sousa_list.append(dai)
  while True:
    sa = syou-dai
    dai = b_list.pop(-1)
    sousa_list.append(dai)
    sousa_list.append(sa)
    if len(b_list) == 0:
      break
    sa = dai-sa
    syou = b_list.pop(-1)
    sousa_list.append(syou)
    sousa_list.append(sa)

else: 
  syou = b_list.pop(-1)
  dai = b_list.pop(-1)
  sousa_list.append(dai)
  sousa_list.append(syou)
  while True:
    if len(b_list) == 0:
      break
    sa = dai-syou
    syou = b_list.pop(-1)
    sousa_list.append(sa)
    sousa_list.append(syou)

    sa = sa - syou
    dai = b_list.pop(-1)
    sousa_list.append(sa)
    sousa_list.append(dai)

print(sousa_list[-2] - sousa_list[-1])
for i in range(len(sousa_list)//2):
  print(str(sousa_list[i])+" "+str(sousa_list[i+1]))

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],k))
        k=k-info[i//2+a]
        
    else:
        print("{} {}".format(-info[i//2+a],k))
        k+=info[i//2]

Submission status: Wrong Answer

Solution code:
import numpy as np
xlist = []
ylist = []
N = input()
N = int(N)
li = list(map(int,input().split()))
linp = np.array(li)
next_flag = 0
sellist = []
for i in range(0,100000):
    if(len(li) ==2):
        next_flag = 2
        break
    else:
        linp = np.array(li)
        minus_idx_size = (np.where(linp < 0))[0].size
        if(minus_idx_size==1):
            next_flag = 1
            break
        elif(minus_idx_size==0):
            sa = min(li) - max(li)
            li.remove(min(li))
            li.remove(max(li))
            sellist.append([min(li),max(li)])
            li.append(sa)
        else:
            sa = max(li) - min(li)
            li.remove(min(li))
            li.remove(max(li))
            sellist.append([max(li),min(li)])
            li.append(sa)

if(next_flag==2):
    sellist.append([max(li),min(li)])
    print(max(li)-min(li))
elif(next_flag==1):
    while(len(li)==2):
        sa = min(li) - max(li)
        li.remove(min(li))
        li.remove(max(li))
        li.append(sa)
        sellist.append([min(li),max(li)])
    sellist.append([max(li),min(li)])
    print(max(li)-min(li))
    for i in range(0,len(sellist)):
        print(sellist[i][0],sellist[i][1])

Submission status: Wrong Answer

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

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

elif A[0]<0 and A[-1]<0:
  tmpA = A.copy()
  print(-sum(A[:-2]) + (A[-1]-A[-2]))

  for i in range(N-1):
    if i==0:
      print(A[-1], A[-2])
      tmp = A[-1] - A[-2]
    elif i==N-2:
      print(tmp, A[-i-1])
    else:
      print(tmp, A[-i-1])
      tmp = tmp - A[i-1]

elif A[0]>0 and A[-1]>0:
  tmpA = A.copy()
  print(sum(A[2:]) + (A[1] - A[0]))

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

else:
  tmpA = A.copy()
  len_of_Apos = len([a for a in A if a>=0])
  len_of_Aneg = len([a for a in A if a<0])
  zero_in = 0 in A
  minv = min(len_of_Aneg, len_of_Apos)
  print(sum([abs(a) for a in A]))

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
        if num[-1] >= 0 or len(num) <= 2:
            ans.append([num[0], num[-1]])
            tmp = num[0] - num[-1]
            num.popleft()
            num.appendleft(tmp)
            num.pop()
        else:
            ans.append([num[1], num[2]])
            tmp = num[1] - num[2]
            min_num = num[0]
            num.popleft()
            num.popleft()
            num.popleft()
            num.appendleft(min_num)
            num.append(tmp)


    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)
if len(set(A)) == 1 and A[0] == 0:
    if A[0] == 0:
        print(0)
        for i in range(N - 1):
            print(0, 0)
        exit()


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(N - 1):
        d.append((tmp, A[i]))
        tmp -= 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()))

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

if N != 1:
    for answer in answer_list:
        i, j = answer
        print(i, j)