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:
# -*- coding: utf-8 -*-
"""
AtCoder A
"""

import sys, math, random
import numpy as np

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column
# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']
# S.replace('ABC','X') # "testABCABC" -> "testXX"
 
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
 
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    N=I()
    A=LI()
    out=[]
    A.sort()
    print(sum(A)-2*A[0])
    x=A.pop(0)
    for n in range(N-1):
        if n==N-2:
            print("%d %d"%(A.pop(0),x))
        else:
            y=A.pop(-1)
            print("%d %d"%(x,y))
            x=x-y
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
xy = []
while(len(A)>1):
    x = A.pop(0)
    y = A.pop(-1)
    xy.append([x, y])
    A.append(x-y)
print(*A)
for out in xy:
    print(*out)

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)
    pa = itertools.combinations_with_replacement(range(n), 2)
    ans = inf
    for xi, yi in pa:
        bf = 0
        x1,y1 = xy[xi]
        x2,y2 = xy[yi]
        p, q = x1 - x2, y1 - y2
        for xa, ya in pa:
            if not (xa == xi and yi == ya):
                x1,y1 = xy[xi]
                x2,y2 = xy[yi]
        
    return

#C
def C():
    m = II()
    a = LI()
    a.sort()
    check = [0] * m
    ans = 0
    if m % 2:
        for i in range(m//2):
            ans += a[-1 - i] - a[i]
            print(a[i], a[-1 - i])
        print(a[m//2],-1*ans)
        ans += a[m//2]
        print(ans)
    else:
        for i in range(m//2):
            ans += a[-1 - i] - a[i]
            print(a[i],a[-1-i])
        print(ans)
    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()))

A.sort()

B = [0] * N
if N%2==1:
    for i in range(N//2):
        B[i*2+1]=A[i]

    for i in range(N//2,N):
        B[(i-N//2)*2]=A[i]
else:
    for i in range(N//2):
        B[i*2]=A[i]

    for i in range(N//2,N):
        B[(i-N//2)*2+1]=A[i]



temp = B[0]
ans = []
for i in range(N-1):
    ans.append(str(B[i+1])+" "+str(temp))
    temp = B[i+1]-temp
#print(B)
print(temp)
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=[]
for i in range(n-1):
    a.sort()
    num=a.pop()
    num2=a.pop(0)
    if i%2:
        ans.append((num,num2))
        a.append(num-num2)
    else:
        ans.append((num2,num))
        a.append(num2-num)
print(a[0])
for num,num2 in ans:
    print(num,num2)

Submission status: Wrong Answer

Solution code:
n = int(input())
v = list(map(int, input().split()))
v.sort()
h = n // 2
l = v[:h]
r = v[h:]
print(sum(r) - sum(l))
if n % 2 == 1:
    l, r = r, l
l.reverse()
r.reverse()
s = l.pop()
while l or r:
    if r:
        x = r.pop()
        print(x, s)
        s = x - s
    if l:
        x = l.pop()
        print(x, s)
        s = x - s

Submission status: Wrong Answer

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

N = int(input())

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

A = sorted(A)

print(sum(A[N//2:]) - sum(A[:N//2]))
l = 0
r = N - 1
if N % 2 == 0:
    for i in range(N-1):
        if i % 2 == 0:
            print(A[r],A[l])
            A[r] = A[r] - A[l]
            l += 1
        else:
            print(A[l],A[r])
            A[l] = A[l] - A[r]
            r -= 1
        
else:
    for i in range(N-1):
        if i % 2 == 1:
            print(A[r],A[l])
            A[r] = A[r] - A[l]
            l += 1
        else:
            print(A[l],A[r])
            A[l] = A[l] - A[r]
            r -= 1

Submission status: Wrong Answer

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

ans = a[0]
ans_l = []

for i in range(1,n-1):
    ans_l += [(ans, a[i])]
    ans -= a[i]

ans_l += [(a[-1],ans)]
ans = a[-1] - ans

print(ans)

for x, y in ans_l:
    print(x,y)

Submission status: Wrong Answer

Solution code:
import sys, math, collections, heapq, itertools
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b
  
def solve():
    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    N = int(file())
    A = [int(a) for a in file().split()]
    A.sort()
    ans = A[0] - A[1]
    out = [None] * (N-1)
    out[0] = [A[0], A[1]]
    for i in range(2, N - 1):
        out[i - 1] = [ans, A[i]]
        ans -= A[i]
    out[N - 2] = [A[N-1], ans]
    ans = A[N - 1] - ans
    print(ans)
    for k in out:
        print(" ".join(map(str, k)))

    return 0
  
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

print(sum(a) - 2 * a[0])

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
xy = []
while(len(A)>1):
    x = A.pop(-1)
    y = A.pop(0)
    xy.append([x, y])
    A.append(x-y)
print(A)

Submission status: Wrong Answer

Solution code:
INF = 10 ** 100

n = int(raw_input())
ns = map(int, raw_input().split())

ns.sort(reverse=True)
pos = ns[:(n + 1) / 2]
neg = ns[(n + 1) / 2:]

ans = sum(pos) - sum(neg)
print ans

res = INF
while pos and neg:
    if len(pos) >= 2 and len(neg) >= 2:
        a1, a2 = pos.pop(), pos.pop()
        b1, b2 = neg.pop(), neg.pop()

        c1 = a1 - b1
        print a1, b1

        c2 = b2 - a2
        print b2, a2

        pos.append(c1)
        neg.append(c2)
    elif len(pos) == 2 and len(neg) == 1:
        a1, a2 = pos.pop(), pos.pop()
        b1 = neg.pop()

        c = b1 - a2
        print b1, a2
        print a1, c

        res = a1 - c
    elif len(pos) == 1 and len(neg) == 2:
        a1 = pos.pop()
        b1, b2 = neg.pop(), neg.pop()

        c = b2 - a1
        print b2, a1
        print c, b1

        res = c - b1
    else:
        a1 = pos.pop()
        b1 = neg.pop()
        print a1, b1
        res = a1 - b1

assert res == ans
assert not pos
assert not neg

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
if min(A)>=0:
  A.sort()
  print(sum(A)-2*A[0])
  k=A[0]
  for i in range(1,N-1):
    print(k,A[i])
    k-=A[i]
  print(A[N-1],k)
  exit()
if max(A)<=0:
  A.sort()
  print(-sum(A)+2*A[N-1])
  k=A[N-1]
  for i in range(1,N-1)[::-1]:
    print(k,A[i])
    k-=A[i]
  print(k,A[0])
  exit()
A.sort()
A_=list(map(abs,A))
print(sum(A_))
for i in range(1,N-1):
  if A[i]<0:
    print(A[N-1],A[i])
    A[N-1]-=A[i]
  else:
    print(A[0],A[i])
    A[0]-=A[i]
  print(A[N-1],A[0])

Submission status: Wrong Answer

Solution code:
from itertools import*
from math import*
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = float("inf")
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)

N = int(input())
a = list(map(int,input().split()))
a.sort()
print(sum(a)-2*a[0])
heapify(a)
for i in range(N-1):
    x = heappop(a)
    y = heappop(a)
    print(x,y)
    heappush(a,x-y)

Submission status: Wrong Answer

Solution code:
n = int(input())
nums = list(map(int, input().split()))
nums.sort()
last_x = nums[-1]
tmp = nums[0]
buf = []
for m in nums[1:-1]:
    buf.append('{} {}'.format(tmp, m))
    tmp -= m
ans = last_x - tmp
buf.append('{} {}'.format(last_x, tmp))
print(ans)
print('\n'.join(buf))

Submission status: Time Limit Exceeded

Solution code:
def key_gen(i, j):
    return tuple([k for k in range(i, j + 1)])


def sub(A, D):
    A.sort()
    N = len(A)
    for i in range(N):
        key = key_gen(i, i)
        D[key] = (A[i], None), (A[i], None)
    for l in range(2, N + 1):
        for i in range(N - l + 1):
            j = i + l - 1
            key = key_gen(i, j)
            min_sub_list = []
            max_sub_list = []
            # lowest
            key_i = list(key)
            index = key_i.pop(0)
            ikey = tuple(key_i)
            res_min_i, res_max_i = D[ikey]
            a_i = A[index]
            b_i_min, pair_i_min = res_min_i
            b_i_max, pair_i_max = res_max_i
            min_sub_list.append((-abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
            min_sub_list.append((-abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
            max_sub_list.append((abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
            max_sub_list.append((abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
            # highest
            key_i = list(key)
            index = key_i.pop(-1)
            ikey = tuple(key_i)
            res_min_i, res_max_i = D[ikey]
            a_i = A[index]
            b_i_min, pair_i_min = res_min_i
            b_i_max, pair_i_max = res_max_i
            min_sub_list.append((-abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
            min_sub_list.append((-abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
            max_sub_list.append((abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
            max_sub_list.append((abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
            res_min = min(min_sub_list)
            res_max = max(max_sub_list)
            D[key] = res_min, res_max


def pair_extract(p, pair_list):
    while p is not None:
        a = p[0]
        b = p[1]
        p_next = p[2]
        pair_list.append((a, b))
        p = p_next


N = int(input())
A_temp = map(int, input().split())
A = list(A_temp)
D = {}
sub(A, D)
res_max = D[tuple([i for i in range(len(A))])][1]
max_val = res_max[0]
pair_list = []
pair_extract(res_max[1], pair_list)
pair_list.reverse()
print(max_val)
for p in pair_list:
    print(p[0], p[1])

Submission status: Time Limit Exceeded

Solution code:
import itertools


def kval_gen(A, key):
    return tuple(sorted([A[k] for k in key]))


def comb_gen(stuff, k, n):
    for L in range(k, min(n + 1, len(stuff) + 1)):
        for subset in itertools.combinations(stuff, L):
            yield subset


def sub(A, D):
    for i in range(len(A)):
        kval = kval_gen(A, [i])
        if kval not in D:
            D[kval] = (A[i], None), (A[i], None)
    stuff = [i for i in range(len(A))]
    for key in comb_gen(stuff, 2, len(stuff)):
        kval = kval_gen(A, key)
        if kval not in D:
            min_sub_list = []
            max_sub_list = []
            for i in range(len(key)):
                key_i = list(key)
                index = key_i.pop(i)
                ikval = kval_gen(A, key_i)
                res_min_i, res_max_i = D[ikval]
                a_i = A[index]
                b_i_min, pair_i_min = res_min_i
                b_i_max, pair_i_max = res_max_i
                min_sub_list.append((-abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
                min_sub_list.append((-abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
                max_sub_list.append((abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
                max_sub_list.append((abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
            res_min = min(min_sub_list)
            res_max = max(max_sub_list)
            D[kval] = res_min, res_max


def pair_extract(p, pair_list):
    if p is not None:
        a = p[0]
        b = p[1]
        p_next = p[2]
        pair_list.append((a, b))
        pair_extract(p_next, pair_list)


N = int(input())
A_temp = map(int, input().split())
A = list(A_temp)
D = {}
sub(A, D)
res_max = D[tuple(sorted(A))][1]
max_val = res_max[0]
pair_list = []
pair_extract(res_max[1], pair_list)
pair_list.reverse()
print(max_val)
for p in pair_list:
    print(p[0], p[1])

Submission status: Time Limit Exceeded

Solution code:
import itertools


def comb_gen(stuff, k, n):
    for L in range(k, min(n + 1, len(stuff) + 1)):
        for subset in itertools.combinations(stuff, L):
            yield subset


def sub(A, D):
    for i in range(len(A)):
        D[tuple([i])] = (A[i], None), (A[i], None)
    stuff = [i for i in range(len(A))]
    for key in comb_gen(stuff, 2, len(stuff)):
        min_sub_list = []
        max_sub_list = []
        for i in range(len(key)):
            key_i = list(key)
            index = key_i.pop(i)
            ikey = tuple(key_i)
            res_min_i, res_max_i = D[ikey]
            a_i = A[index]
            b_i_min, pair_i_min = res_min_i
            b_i_max, pair_i_max = res_max_i
            min_sub_list.append((-abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
            min_sub_list.append((-abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
            max_sub_list.append((abs(a_i - b_i_min), (a_i, b_i_min, pair_i_min)))
            max_sub_list.append((abs(a_i - b_i_max), (a_i, b_i_max, pair_i_max)))
        res_min = min(min_sub_list)
        res_max = max(max_sub_list)
        D[tuple(key)] = res_min, res_max


def pair_extract(p, pair_list):
    if p is not None:
        a = p[0]
        b = p[1]
        p_next = p[2]
        pair_list.append((a, b))
        pair_extract(p_next, pair_list)


N = int(input())
A_temp = map(int, input().split())
A = list(A_temp)
D = {}
sub(A, D)
res_max = D[tuple([i for i in range(len(A))])][1]
max_val = res_max[0]
pair_list = []
pair_extract(res_max[1], pair_list)
pair_list.reverse()
print(max_val)
for p in pair_list:
    print(p[0], p[1])

Submission status: Time Limit Exceeded

Solution code:
from bisect import insort_left

N = int(input())
*A, = map(int, input().split())
operations = []
pos = []; zero = []; neg = [];
for a in A:
    if a > 0: insort_left(pos, a)
    elif a == 0: zero.append(a)
    else: insort_left(neg, a)
if N == 2:
    a, b = max(A), min(A)
    ans = a - b
    operations.append((a, b))
else:
    if len(neg) == 0:
        pos = zero + pos
        operations.append((pos[0], pos[-1]))
        neg = [pos[0] - pos[-1]]
        pos = pos[1:-1]
    elif len(pos) == 0:
        neg = neg + zero
        operations.append((neg[-1], neg[0]))
        pos = [neg[-1] - neg[0]]
        neg = neg[1:-1]
    else:
        neg = neg + zero
    neg = neg[::-1]
    while len(pos) > 1 and len(neg) > 1:
        n, p = neg.pop(), pos.pop()
        operations.append((p, n))
        pos.append(p-n)
    if len(neg) == 1:
        while len(pos) > 1:
            n, p = neg.pop(), pos.pop()
            operations.append((n, p))
            neg.append(n-p)
    else:
        while len(neg) > 1:
            n, p = neg.pop(), pos.pop()
            operations.append((p, n))
            pos.append(p-n)
    operations.append((pos[0], neg[0]))
    ans = pos[0] - neg[0]
print(ans)
for x, y in operations:
    print(x, y)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
A=list(map(int,input().split()))
count=len(A)-2
A.sort()
process=[]

for _ in range(count):
    if A[-1]<=0:
        new=A[-1]-A[0]
        process.append((A[-1],A[0]))
        A=A[1:-1]+[new]
    elif A[0]>=0:
        new=A[0]-A[-1]
        process.append((A[0],A[-1]))
        A=[new]+A[1:-1]
    else:
        if A[1]>=0:
            new=A[0]-A[-1]
            process.append((A[0],A[-1]))
            A=[new]+A[1:-1]
        else:
            new=A[-1]-A[0]
            process.append((A[-1],A[0]))
            A=A[1:-1]+[new]

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

print(M)
for x,y in process:
    print(x,y)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
A=list(map(int,input().split()))
A.sort()
process=[]

while len(A)>=3:
    if A[0]<=0 and A[-1]>=0:
        if A[1]>=0:
            new=A[0]-A[-1]
            process.append((A[0],A[-1]))
            del A[0]
            del A[-1]
            A.insert(0,new)
        else:
            new=A[-1]-A[0]
            process.append((A[-1],A[0]))
            del A[0]
            del A[-1]
            A.append(new)
    elif A[-1]<=0:
        new=A[-1]-A[0]
        process.append((A[-1],A[0]))
        del A[0]
        del A[-1]
        A.append(new)
    else:
        new=A[0]-A[-1]
        process.append((A[0],A[-1]))
        del A[0]
        del A[-1]
        A.insert(0,new)

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

print(M)
for x,y in process:
    print(x,y)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
A=list(map(int,input().split()))
A.sort()
process=[]

while len(A)>=3:
    if A[0]<=0 and A[-1]>=0:
        if A[1]>=0:
            new=A[0]-A[-1]
            process.append(str(A[0])+" "+str(A[-1]))
            del A[0]
            del A[-1]
            A.insert(0,new)
        else:
            new=A[-1]-A[0]
            process.append(str(A[-1])+" "+str(A[0]))
            del A[0]
            del A[-1]
            A.append(new)
    elif A[-1]<=0:
        new=A[-1]-A[0]
        process.append(str(A[-1])+" "+str(A[0]))
        del A[0]
        del A[-1]
        A.append(new)
    else:
        new=A[0]-A[-1]
        process.append(str(A[0])+" "+str(A[-1]))
        del A[0]
        del A[-1]
        A.insert(0,new)

M=A[-1]-A[0]
process.append(str(A[-1])+" "+str(A[0]))

print(M)
for i in range(len(process)):
    print(process[i])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
A=list(map(int,input().split()))
A.sort()
process=[]

while len(A)>=3:
    if A[-1]<=0:
        process.append([A[-1],A[0]])
        A.append(A[-1]-A[0])
        del A[0]
        del A[-2]
    elif A[0]>=0:
        process.append([A[0],A[-1]])
        A.insert(0,A[0]-A[-1])
        del A[1]
        del A[-1]
    else:
        if A[1]>=0:
            process.append([A[0],A[-1]])
            A.insert(0,A[0]-A[-1])
            del A[1]
            del A[-1]          
        else:
            process.append([A[-1],A[0]])
            A.append(A[-1]-A[0])
            del A[0]
            del A[-2]

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

print(M)
for i in range(len(process)):
    print(" ".join([str(j) for j in process[i]]))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
A=list(map(int,input().split()))
A.sort()
process=[]

while len(A)>=3:
    if A[-1]<=0:
        new=A[-1]-A[0]
        process.append([A[-1],A[0]])
        del A[0]
        del A[-1]
        A.append(new)
    elif A[0]>=0:
        new=A[0]-A[-1]
        process.append([A[0],A[-1]])
        del A[0]
        del A[-1]
        A.insert(0,new)
    else:
        if A[1]>=0:
            new=A[0]-A[-1]
            process.append([A[0],A[-1]])
            del A[0]
            del A[-1]
            A.insert(0,new)
        else:
            new=A[-1]-A[0]
            process.append([A[-1],A[0]])
            del A[0]
            del A[-1]
            A.append(new)

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

print(M)
for i in range(len(process)):
    print(" ".join([str(j) for j in process[i]]))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
A=list(map(int,input().split()))
A.sort()
process=[]

while len(A)>=3:
    if A[0]<=0 and A[-1]>=0:
        if A[1]>=0:
            new=A[0]-A[-1]
            process.append([A[0],A[-1]])
            del A[0]
            del A[-1]
            A.insert(0,new)
        else:
            new=A[-1]-A[0]
            process.append([A[-1],A[0]])
            del A[0]
            del A[-1]
            A.append(new)
    elif A[-1]<=0:
        new=A[-1]-A[0]
        process.append([A[-1],A[0]])
        del A[0]
        del A[-1]
        A.append(new)
    else:
        new=A[0]-A[-1]
        process.append([A[0],A[-1]])
        del A[0]
        del A[-1]
        A.insert(0,new)

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

print(M)
for i in range(len(process)):
    print(" ".join([str(j) for j in process[i]]))

Submission status: Time Limit Exceeded

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

if a[0]>=0:
    g=sum(a[1:])-a[0]
    print(g)
    for i in range(n-1):
        if len(a)>2:
            print(str(a[0])+" "+str(a[-1]))
            a[0]-=a[-1]
            a.remove(a[-1])
        else:
            print(str(a[1])+" "+str(a[0]))
    
elif a[-1]<=0:
    g=a[-1]-sum(a[:-1])
    print(g)
    for i in range(n-1):
        print(str(a[-1])+" "+str(a[0]))
        a[-1]-=a[0]
        a.remove(a[0])
else:
    g=0 
    for i in range(n):
        g+=abs(a[i])
    print(g)
    for _ in range(n-1):
        if a[-2]>=0:
            print(str(a[0])+" "+str(a[-1]))
            a[0]-=a[-1]
            a.remove(a[-1])
        else:
            print(str(a[-1])+" "+str(a[0]))
            a[-1]-=a[0]
            a.remove(a[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
INPUT = list(map(int, input().split()))
POSs = []
NEGs = []
for e in INPUT:
    if e > 0:
        POSs.append(e)
    else:
        NEGs.append(e)

res = []
for i in range(N-2):
    if len(NEGs) == 0:
        pos_min = min(POSs)
        pos_max = max(POSs)
        POSs.remove(pos_min)
        POSs.remove(pos_max)
        res.append([pos_min, pos_max])
        new_val = pos_min - pos_max
    elif len(POSs) == 0:
        neg_min = min(NEGs)
        neg_max = max(NEGs)
        NEGs.remove(neg_min)
        NEGs.remove(neg_max)
        res.append([neg_max, neg_min])
        new_val = neg_max - neg_min
    elif len(POSs) > 1:
        pos_min = min(POSs)
        POSs.remove(pos_min)
        neg = NEGs.pop()
        res.append([neg, pos_min])
        new_val = neg - pos_min
    else:
        pos = POSs.pop()
        neg = NEGs.pop()
        res.append([pos, neg])
        new_val = pos - neg

    if new_val < 0:
        NEGs.append(new_val)
    else:
        POSs.append(new_val)

last_pair = POSs + NEGs
M = max(last_pair)
m = min(last_pair)
res.append([M, m])
print(M - m)
for el in res:
    print(el[0], el[1])

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
A = list(map(int, input().split()))
#print(N, A)
x = [0] * (N-1)
y = [0] * (N-1)
for i in range(N -1):
    amin = min(A)
    A.remove(min(A))
    amax = max(A)
    A.remove(max(A))
    #print(A, amin, amax)
    if i != N - 2:
        amin2 = abs(min(A))
        amax2 = abs(max(A))
        #print(amax2, amin2)
        if i == N - 3:
            if A[0] < 0:
                A.append(amax - amin)
                x[i] = amax
                y[i] = amin
            else:
                A.append(amin - amax)
                x[i] = amin
                y[i] = amax
        else:
            if amax2 > amin2:
                A.append(amin - amax)
                x[i] = amin
                y[i] = amax
            else:
                A.append(amax - amin)
                x[i] = amax
                y[i] = amin

    else:
        A.append(amax - amin)
        x[i] = amax
        y[i] = amin

    #print("A", A)

print(A[0])
#print(x, y)
for i in range(N - 1):
    print(x[i], y[i])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect

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

ans = []

if len(A) == 2 :
    ans.append(A[::-1])
    calc = A[1] - A[0]

else :
    ind = bisect(A,0)

    X = A[:ind]
    Y = A[ind:]
    
    if len(X) == 0 :
        ans.append([Y[0],Y[1]])
        X.append(Y[0]-Y[1])
        Y.pop(0)
        Y.pop(0)
    elif len(Y) == 0 :
        ans.append([X[-1],X[-2]])
        Y.append(X[-1]-X[-2])
        X.pop(-1)
        X.pop(-1)
    
    while True :
        if len(X) >= 2 :
            ans.append([Y[0],X[-1]])
            Y[0] -= X.pop(-1)
        elif len(Y) >= 2 :
            ans.append([X[-1],Y[0]])
            X[-1] -= Y.pop(0)
        else :
            ans.append([Y[0],X[0]])
            calc = Y.pop(0) - X.pop(0)
            break
    
print(calc)
for i in range(len(ans)) :
    print(*ans[i])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a = list(sorted(map(int,input().split())))
# a = [i for i in range(10**5)]

result = []
# all +
if a[0] > 0:
    if len(a) == 2:
        print(a[1]-a[0])
        print(a[1], a[0])
        exit()
    result.append((a[0], a[1]))
    t = a[0]-a[1]
    a.pop(0)
    a[0] = t
# all -
elif a[-1] < 0:
    if len(a) == 2:
        print(a[0]-a[1])
        print(a[0], a[1])
        exit()
    result.append((a[-1], a[-2]))
    t = a[-1]-a[-2]
    a.pop(-1)
    a[-1] = t

# print(a)
b = []
for i in range(len(a)-1):
    if a[i] > 0:
        result.append((b[0], a[i]))
        t = b[0]-a[i]
        b[0] = t
    else:
        b.append(a[i])
b.append(a[-1])
# print(b)
while len(b) != 1:
    result.append((b[-1], b[0]))
    b.append(b[-1]-b[0])
    b.pop(0)
    b.pop(-2)
# print(b)

print(b[0])
for r in result:
    print("{} {}".format(r[0],r[1]))

Submission status: Time Limit Exceeded

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

result = []
# all +
if a[0] > 0:
    if len(a) == 2:
        print(a[1]-a[0])
        print(a[1], a[0])
        exit()
    result.append((a[0], a[1]))
    t = a[0]-a[1]
    a.pop(0)
    a[0] = t
# all -
elif a[-1] < 0:
    if len(a) == 2:
        print(a[0]-a[1])
        print(a[0], a[1])
        exit()
    result.append((a[-1], a[-2]))
    t = a[-1]-a[-2]
    a.pop(-1)
    a[-1] = t

# print(a)
b = []
for i in range(len(a)-1):
    if a[i] > 0:
        result.append((b[0], a[i]))
        t = b[0]-a[i]
        b[0] = t
    else:
        b.append(a[i])
b.append(a[-1])
# print(b)
while len(b) != 1:
    result.append((b[-1], b[0]))
    b.append(b[-1]-b[0])
    b.pop(0)
    b.pop(-2)
# print(b)

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

Submission status: Time Limit Exceeded

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 A[-2] >= 0:
        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 len(A) != 2:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(atama, ketu)
        new = atama - ketu
        A.insert(0, new)
    atama = A.pop(0)
    ketu = A.pop(-1)
    print(ketu, atama)
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 len(A) != 1:
        atama = A.pop(0)
        ketu = A.pop(-1)
        print(ketu, atama)
        new = ketu - atama
        A.append(new)

Submission status: Time Limit Exceeded

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

xy = [0]*(N-1)
idx = 0

for i in range(N-1):
    if A[-2] > 0:
        y = A.pop()
        x = A[0]

        xy[idx] = [x,y]

        A[0] = x-y

    else:
        x = A.pop()
        y = A.pop(0)

        xy[idx] = [x,y]

        A.append(x-y)
    
    idx += 1

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