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:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
import time
from bisect import bisect_left, insort_left, bisect_right
import sys
import random

input = sys.stdin.readline
_NUMINT_ALL = list(range(10))
_START_TIME = time.time()


def main():
    ans = solve()

    if ans is True or ans is False:
        YesNo(ans)
    elif ans is not None:
        print(ans)


def solve():
    N = iip()
    A = iip()
    dp_max = [A[0]]
    dp_min = [A[0]]

    for i, s in enumerate(A[1:]):
        #      x        y
        t1 = dp_max[i] - s
        t2 = dp_min[i] - s
        t3 = s - dp_max[i]
        t4 = s - dp_min[i]

        mn = min(t1, t2, t3, t4)
        mx = max(t1, t2, t3, t4)

        dp_max.append(mx)
        dp_min.append(mn)


    ans1 = dp_max[-1]

    dp_max.reverse()
    dp_min.reverse()
    A.reverse()

    ans2 = []
    cur = ans1
    for i in range(len(dp_max)-1):
        target = cur
        newface = A[i]
        mx = dp_max[i+1]
        mn = dp_min[i+1]

        if target == newface - mx:
            ans2.append((newface, mx))
            cur = mx
        elif target == mx - newface:
            ans2.append((mx, newface))
            cur = mx
        elif target == newface - mn:
            ans2.append((newface, mn))
            cur = mn
        elif target == mn - newface:
            ans2.append((mn, newface))
            cur = mn
        else:
            print("wrong")
            print(target, mn, mx, newface)

    #print(ans2)
    ans2.reverse()

    print(ans1)
    for i, j in ans2:
        print(i, j)




#####################################################ライブラリ集ここから

def kiriage_warizan(a, b):
    return -(-a//b)


def iip(listed=True):  # 数字のinputをlistで受け取る
    d = input().rstrip("\n").split()
    try:
        ret = [int(i) for i in d]
    except:
        ret = [int(i) if i in _NUMINT_ALL else i for i in d]
        if len(ret) == 1 and listed is False:
            return ret[0]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iipt(l, listed=True, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed))
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m

def time_debug_10percent():
    a = random.randint(0, 9)
    if a == 9:
        sys.exit()

def time_debug(t):
    time.sleep(_START_TIME + t - time.time())
    sys.exit()


def make_graph_edge_flat(N):  # グラフ作成のための辺をリストで返す
    ret = []
    for i in range(N-1):
        a, b, c = iip()
        a -= 1
        b -= 1
        ret[a].append((b, c))
        ret[b].append((a, c))
    return ret


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


def nibutan_func(func, target, left, right, side="left"): # 関数を二分探索
    l = left
    r = right
    x = (l + r) // 2
    while r-l > 1:
        x = (l+r)//2
        if func(x) == target:
            return x
        elif func(x) > target:
            r = x
        else:
            l = x

    if side == "left" or func(x) == target:
        return l
    else:
        return r


def nibutan_list(list_, target, side="left"):  # リストを二分探索
    if not isinstance(list_, list):
        list_ = list(list_)

    l = 0
    r = len(list_)
    x = (l + r) // 2
    while r-l > 1:
        x = (l+r)//2
        if list_[x] == target:
            return x
        elif list_[x] > target:
            r = x
        else:
            l = x

    if side == "left" or list_[x] == target:
        return l
    else:
        return r


class UfTree():
    def __init__(self, maxnum):
        self.parent = list(range(maxnum))
        self._size = [1] * maxnum
        self.rank = [0] * maxnum

    def size(self, a):
        return self._size[self.root(a)]

    def root(self, a):
        rank = 0
        cur = a
        while True:
            if self.parent[cur] == cur:
                #for i in path: # 経路圧縮
                #    self.parent[i] = cur
                return cur
            else:
                self.parent[cur] = self.parent[self.parent[cur]]
                cur = self.parent[cur]
                rank += 1

    def unite(self, a, b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb:
            return self

        self._size[ra] += self._size[rb]
        self.parent[rb] = ra
        return self

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,*a = map(int,open(0).read().split())
xy = []
xyap = xy.append
ap = max(a)
a.remove(ap)
am = min(a)
a.remove(am)
for i in a:
  if i>0:
    xyap((am,i))
    am -= i
  else:
    xyap((i,ap))
    ap += i
xyap((ap,am))
print(ap-am)
for i in xy:
  print(*i)

Submission status: Wrong Answer

Solution code:
n,*a = map(int,open(0).read().split())
xy = []
xyap = xy.append
ap = max(a)
am = min(a)
a.remove(ap)
a.remove(am)
for i in a:
  if i>0:
    xyap((am,i))
    am -= i
  else:
    xyap((i,ap))
    ap += 1
xyap((ap,am))
print(ap-am)
for i in xy:
  print(*i)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
A.sort()
ans = []

# 全て正
if min(A)>=0:
    print(sum(A)-2*A[0])
    for i in range(n):
        if i==0:
            x = A[i]
        elif i==n-1:
            print(A[i], x)
        else:
            print(x, A[i])
            x -= A[i]

# 全て負
elif max(A)<=0:
    A = A[::-1]
    print(sum(A)*(-1) + 2*A[0])
    for i in range(n):
        if i==0:
            x = A[i]
        elif i==n-1:
            print(A[i], x)
        else:
            print(x, A[i])
            x -= A[i]

# 正負
else:
    m,p = [], []
    for a in A:
        if a<0:
            m.append(a)
        else:
            p.append(a)
    print(sum(p)-sum(m))
    m.sort()
    p.sort()
    x = m[0]
    for i in range(len(p)-1):
        print(x, p[i])
        x -= p[i]
    else:
        print(p[-1], x)
        x = p[-1] - x
        
    for j in range(1, len(m)):
        print(x, m[j])
        x -= m[j]

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
A.sort()
ans = []

x = A[0]
for i in range(1, n-1):
    ans.append((x, A[i]))
    x -= A[i]
if x<0:
   ans.append((A[-1], x))
else:
    ans.append((x, A[-1]))
print(abs(A[-1] - x))
for a in ans:
    print(*a)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
A.sort()
ans = []
x = A[0]
for i in range(1, n-1):
    ans.append((x, A[i]))
    x -= A[i]
ans.append((A[-1], x))
print(A[-1] - x)
for a in ans:
    print(*a)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
A.sort()
x = A[0]
ans = []
for i in range(1, n-1):
    y = A[i]
    if x <= 0:
        ans.append((x, y))
    else:
        ans.append((y, x))
    x = x - y
else:
    ans.append((A[-1], x))
print(A[-1] - x)
for x,y in ans:
    print(x,y)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
A.sort()
x = A[0]
for i in range(1, n-1):
    y = A[i]
    if x <= 0:
        print(x, y)
    else:
        print(y, x)
    x = x - y
else:
    print(A[-1], x)

Submission status: Wrong Answer

Solution code:
from collections import deque
n=int(input())
a=list(map(int,input().split()))
a.sort()
a=deque(a)
max_a=a.pop()
min_a=a.popleft()
ans=[]
while(len(a)>0):
    ai=a.popleft()
    ans.append([min_a,ai])
    min_a-=ai
ans.append([max_a,min_a])
print(max_a-min_a)
for i in range(len(ans)):
    print("{0} {1}".format(ans[i][0],ans[i][1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
n=int(input())
a=list(map(int,input().split()))
a.sort()
a=deque(a)
max_a=a.pop()
min_a=a.popleft()
while(len(a)>0):
    ai=a.popleft()
    print(min_a,ai)
    min_a-=ai
print(max_a,min_a)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)
from collections import defaultdict
from bisect import *
mod = 10**9+7

N = int(input())
A = [int(x) for x in input().split()]
A.sort()
k = max(bisect_left(A, 0), 1)
print(sum(A[k:])-sum(A[:k]))
for a in A[k:-1]:
    print(A[0], a)
    A[0] -= a
for a in A[1:k]:
    print(A[-1], a)
print(A[-1], A[0])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)
from collections import defaultdict
mod = 10**9+7

N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse = 1)
if N & 1:
    ans = sum(A[:N//2+1])-sum(A[N//2+1:])
    print(ans)
    lst = []
    l, r = 1, N-1
    while l < r:
        print(A[r], A[l])
        lst.append(A[r]-A[l])
        l += 1
        r -= 1
    x = A[0]
    for a in lst:
        print(x, a)
        x -= a
else:
    ans = sum(A[:N//2])-sum(A[N//2:])
    print(ans)
    lst = []
    l, r = 1, N-2
    while l < r:
        print(A[r], A[l])
        lst.append(A[r]-A[l])
        l += 1
        r -= 1
    x = A[0]-A[-1]
    for a in lst:
        print(x, a)
        x -= a

Submission status: Wrong Answer

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

if n==2:
    print(max(al)-min(al))
    exit()

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

pos.sort()
neg.sort(reverse=True)
res = []

if len(pos)==0:
    x = neg[0]
    for a in neg[1:]:
        res.append((x,a))
        x -= a
elif len(neg)==0:
    x = pos[0]
    for a in pos[1:-1]:
        res.append((x,a))
        x -= a
    res.append((pos[-1],x))
    x = pos[-1]-x
else:
    p = pos[0]
    pos = pos[1:]

    x = neg[0]
    neg = neg[1:]

    for a in pos:
        res.append((x,a))
        x -= a
    for a in neg:
        res.append((x,a))
        x -= a

    res.append((p,x))
    x = p - x

print(x)
for a,b in res:
    print(a,b)

Submission status: Wrong Answer

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

if n==2:
    print(max(al)-min(al))
    exit()

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

pos.sort()
neg.sort(reverse=True)
res = []

if len(pos)==0:
    x = neg[0]
    for a in neg[1:]:
        res.append((x,a))
        x -= a
elif len(neg)==0:
    x = pos[0]
    for a in pos[1:-1]:
        res.append((x,a))
        x -= a
    res.append((pos[-1],x))
    x = pos[-1]-x
else:
    p = pos[0]
    pos = pos[1:]

    x = neg[0]
    neg = neg[1:]

    for a in pos:
        res.append((x,a))
        x -= a

    res.append((p,x))
    x = p - x
    for a in neg:
        res.append((x,a))
        x -= a

print(x)
for a,b in res:
    print(a,b)

Submission status: Wrong Answer

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

if n==2:
    print(max(al)-min(al))
    exit()

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

pos.sort()
neg.sort(reverse=True)
res = []

if len(pos)==0:
    res.append((neg[0],neg[-1]))
    pos.append(neg[0]-neg[-1])
    neg = neg[1:-1]
if len(neg)==0:
    res.append((pos[0],pos[-1]))
    neg.append(pos[0]-pos[-1])
    pos = pos[1:-1]

p = pos[0]
pos = pos[1:]

x = neg[0]
neg = neg[1:]

for a in pos:
    res.append((x,a))
    x -= a

res.append((p,x))
x = p - x
for a in neg:
    res.append((x,a))
    x -= a

print(x)
for a,b in res:
    print(a,b)

Submission status: Wrong Answer

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

if n==2:
    print(max(al)-min(al))
    exit()

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

pos.sort()
neg.sort(reverse=True)
res = []

if len(pos)==0:
    res.append((neg[0],neg[1]))
    pos.append(neg[0]-neg[1])
    neg = neg[2:]
if len(neg)==0:
    res.append((pos[0],pos[-1]))
    neg.append(pos[0]-pos[-1])
    pos = pos[1:-1]

p = pos[0]
pos = pos[1:]

x = neg[0]
neg = neg[1:]

for a in pos:
    res.append((x,a))
    x -= a

res.append((p,x))
x = p - x
for a in neg:
    res.append((x,a))
    x -= a

print(x)
for a,b in res:
    print(a,b)

Submission status: Wrong Answer

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

if n==2:
    print(max(al)-min(al))
    exit()

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

pos.sort(reverse=True)
neg.sort(reverse=True)
res = []

if len(pos)==0:
    res.append((neg[0],neg[1]))
    pos.append(neg[0]-neg[1])
    neg = neg[2:]
if len(neg)==0:
    res.append((pos[1],pos[0]))
    neg.append(pos[1]-pos[0])
    pos = pos[2:]

p = pos[0]
pos = pos[1:]

x = neg[0]
neg = neg[1:]

for a in pos:
    res.append((x,a))
    x -= a

res.append((p,x))
x = p - x
for a in neg:
    res.append((x,a))
    x -= a

print(x)
for a,b in res:
    print(a,b)

Submission status: Wrong Answer

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

if n==2:
    print(max(al)-min(al))
    exit()

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

pos.sort()
neg.sort(reverse=True)
res = []

if len(pos)==0:
    res.append((neg[0],neg[1]))
    pos.append(neg[0]-neg[1])
    neg = neg[2:]
if len(neg)==0:
    res.append((pos[0],pos[1]))
    neg.append(pos[0]-pos[1])
    pos = pos[2:]

pos.sort(reverse=True)
p = pos[-1]
pos = pos[:-1]

x = neg[0]
neg = neg[1:]

for a in pos:
    res.append((x,a))
    x -= a

res.append((p,x))
x = p - x
for a in neg:
    res.append((x,a))
    x -= a

print(x)
for a,b in res:
    print(a,b)

Submission status: Wrong Answer

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

if n==2:
    print(max(al)-min(al))
    exit()

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

pos.sort()
neg.sort(reverse=True)
res = []

if len(pos)==0:
    res.append((neg[0],neg[1]))
    pos.append(neg[0]-neg[1])
    neg = neg[2:]
if len(neg)==0:
    res.append((pos[0],pos[1]))
    neg.append(pos[0]-pos[1])
    pos = pos[2:]

pos.sort(reverse=True)
p = pos[0]
pos = pos[1:]

x = neg[0]
neg = neg[1:]

for a in pos:
    res.append((x,a))
    x -= a

res.append((p,x))
x = p - x
for a in neg:
    res.append((x,a))
    x -= a

print(x)
for a,b in res:
    print(a,b)

Submission status: Wrong Answer

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

if n==2:
    print(max(al)-min(al))
    exit()

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

pos.sort()
neg.sort(reverse=True)
res = []

if len(pos)==0:
    res.append((neg[0],neg[1]))
    pos.append(neg[0]-neg[1])
    neg = neg[2:]
if len(neg)==0:
    res.append((pos[0],pos[1]))
    neg.append(pos[0]-pos[1])
    pos = pos[2:]

p = pos[-1]
pos = pos[:-1]

x = neg[0]
neg = neg[1:]

for a in pos:
    res.append((x,a))
    x -= a

res.append((p,x))
x = p - x
for a in neg:
    res.append((x,a))
    x -= a

print(x)
for a,b in res:
    print(a,b)

Submission status: Wrong Answer

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

if n==2:
    print(max(al)-min(al))
    exit()

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

pos.sort()
neg.sort(reverse=True)
res = []

if len(pos)==0:
    res.append((neg[0],neg[1]))
    pos.append(neg[0]-neg[1])
    neg = neg[2:]
if len(neg)==0:
    res.append((pos[0],pos[1]))
    neg.append(pos[0]-pos[1])
    pos = pos[2:]

p = pos[0]
pos = pos[1:]

x = neg[0]
neg = neg[1:]

for a in pos:
    res.append((x,a))
    x -= a

res.append((p,x))
x = p - x
for a in neg:
    res.append((x,a))
    x -= a

print(x)
for a,b in res:
    print(a,b)

Submission status: Wrong Answer

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

pos = []
neg = []
for a in al:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)

if len(pos)<2:
    l = pos + neg
    x = l[0]
    res = []
    for a in l[1:]:
        res.append((x,a))
        x = x - a
    print(x)
    for a,b in res:
        print(a,b)
    exit()

if len(neg)>0:
    res = []
    pos.sort()
    p = pos[-1]
    pos = pos[:-1]
    x = neg[0]
    for a in pos:
        res.append((x,a))
        x = x - a
    for a in neg[1:]:
        res.append((x,a))
        x -= a
    res.append((p,x))
    x = p - x
    print(x)
    for a,b in res:
        print(a,b)
    exit()

res = []
pos.sort()
p = pos[-1]
pos = pos[:-1]
x = pos[0]
for a in pos[1:]:
    res.append((x,a))
    x -= a
res.append((p,x))
x = p - x
print(x)
for a,b in res:
    print(a,b)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
import sys
sys.setrecursionlimit(10000000)

#const
# my functions here!
def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():
    N,=pin()
    A=sorted(list(pin()))
    p=(N+1)//2
    q=N-p
    A1,A2=A[q:N],A[:p]
    A2=A2 if N%2==0 else A[:p-1]  
    M=sum(A1)-sum(A2)
    #print(A1,A2)
    #Max value aftter operations
    print(M)
    
    #Restore operation 
    if N%2==0:
        print(A2[0],A1[0])
        a,b=A2[0],A1[0]
        temp=a-b
        for i in range(p-1):
            a,b=A1[i],temp
            print(a,b)
            temp=a-b
            a,b=temp,A2[i]
            print(a,b)
            temp=a-b
    else:
        print(A2[0],A1[0])
        a,b=A2[0],A1[0]
        temp=a-b
        a,b=A1[1],temp
        print(a,b)
        temp=a-b
        for i in range(p-2):
           
            a,b=temp,A2[i]
            print(a,b)
            temp=a-b   
            a,b=A1[i],temp
            print(a,b)
            temp=a-b
    
"""         
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""
if __name__=="__main__":resolve()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
a = A[0]
print(sum(A) - 2*A[0])
for i in range(1, N-1):
    print(a, A[i])
    a -= A[i]
print(A[-1], a)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
minus = A[:N//2]
plus = A[N//2:]
print(sum(plus) - sum(minus))
nums = []
while minus:
    nums.append(plus.pop())
    nums.append(minus.pop())
if N % 2 == 1:
    nums.append(plus.pop())
while len(nums) > 1:
    y = nums.pop()
    x = nums.pop()
    nums.append(x - y)
    print(x, y)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
minus = A[:N//2]
plus = A[N//2:]
nums = []
while minus:
    nums.append(plus.pop())
    nums.append(minus.pop())
if N % 2 == 1:
    nums.append(plus.pop())
xy = []
while len(nums) > 1:
    y = nums.pop()
    x = nums.pop()
    xy.append((x, y))
    nums.append(x - y)
print(nums[0])
for x, y in xy:
    print(x, y)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    neg = []
    pos = []
    for v in a:
        if v < 0:
            neg.append(v)
        else:
            pos.append(v)
    neg.sort()
    ans = []
    for i in range(len(neg)//2):
        x = neg[len(neg)-i-1] - neg[i]
        ans.append([neg[len(neg)-i-1], neg[i]])
        pos.append(x)
    if len(neg) % 2 == 1:
        pos.append(neg[len(neg)//2])
    pos.sort()
    for i in range(len(pos)-2):
        ans.append([pos[i], pos[i+1]])
        pos[i+1] = pos[i] - pos[i+1]
    if len(pos) > 1:
        ans.append([pos[len(pos)-1], pos[len(pos)-2]])
        pos[len(pos)-1] = pos[len(pos)-1] - pos[len(pos)-2]
    print(pos[len(pos)-1])
    for p, q in ans:
        print(p, q)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    neg = []
    pos = []
    for v in a:
        if v < 0:
            neg.append(v)
        else:
            pos.append(v)
    neg.sort()
    ans = []
    for i in range(len(neg)//2):
        x = neg[len(neg)-i-1] - neg[i]
        ans.append([neg[len(neg)-i-1], neg[i]])
        pos.append(x)
    if len(neg) % 2 == 1:
        pos.append(neg[len(neg)//2])
    pos.sort()
    for i in range(len(pos)-2):
        ans.append([pos[i], pos[i+1]])
        pos[i+1] = pos[i] - pos[i+1]
    ans.append([pos[len(pos)-1], pos[len(pos)-2]])
    pos[len(pos)-1] = pos[len(pos)-1] - pos[len(pos)-2]
    print(pos[len(pos)-1])
    for p, q in ans:
        print(p, q)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()
    ans = []
    for i in range(n-2):
        ans.append([a[i], a[i+1]])
        a[i+1] = a[i] - a[i+1]
    ans.append([a[n-1], a[n-2]])
    m = a[n-1] - a[n-2]
    print(m)
    for p, q in ans:
        print(p, q)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heapify,heappop,heappush
n = int(input())
a = list(map(int,input().split()))
if max(a) > 0 and min(a) < 0:
  hp = []
  hn = []
  heapify(hp)
  heapify(hn)
  ans = 0
  for i in range(n):
    if a[i] >= 0:
      heappush(hp,a[i])
      ans += a[i]
    else:
      ans -= a[i]
      heappush(hn,a[i])
  print(ans)
  while hp and hn:
    p = heappop(hp)
    q = heappop(hn)
    if hn:
      print(p,q)
      heappush(hp,p-q)
    elif hp:
      print(q,p)
      heappush(hn,q-p)
    else:
      print(p,q)
elif min(a) >= 0:
  ans = sum(a)-2*min(a)
  print(ans)
  heapify(a)
  while a:
    p = heappop(a)
    if a:
      q = heappop(a)
      if a:
        print(p,q)
        heappush(a,p-q)
      else:
        print(q,p)
    else:
      break
elif max(a) <= 0:
  ans = -sum(a)+2*max(a)
  a = [-a[i] for i in range(n)]
  heapify(a)
  print(ans)
  while a:
    p = heappop(a)
    if a:
      q = heappop(a)
      if a:
        print(-q,-p)
        heappush(a,-q+p)
      else:
        print(-p,-q)
    else:
      break

Submission status: Wrong Answer

Solution code:
import heapq
from collections import defaultdict
N = int(input())
nums  = list(map(int, input().split(" ")))
# 正の数、負の数の個数で操作をコントロール？
p_count = 0
m_count = 0
pos = []
minus = []
for num in nums:
    if num < 0:
        m_count += 1
        minus.append(num)
    else:
        p_count += 1
        pos.append(num)
pos.sort()
minus.sort() #小さい順番
途中式 = []
if N == 2:
    print(max(nums) - min(nums))
else:
    #正の数を一つにして、そこから引きまくる
    if m_count == 0: #マイナスが無かったら作る
        x = min(pos)
        y = max(pos)
        途中式.append((x, y))
        minus.append(x - y)
        pos.remove(x)
        pos.remove(y)
        p_count -=2
        m_count += 1

    if p_count > 1: # 正が複数あったら、マイナスから引くことで減らしていく
        for i in range(p_count - 1):
            #マイナスから正が1個になるまで引いていく
            x = minus.pop()
            y = pos.pop()
            途中式.append((x,y))
            minus.append(x- y)
    elif p_count == 0 : #正が一つも無かったら、作る
        x = minus.pop()
        y = minus.pop()
        途中式.append((x, y))
        pos.append(x - y)

    x = pos.pop()
    for i in minus:
        y = i
        途中式.append((x, y))
        x -=y
    print(x)
    for temp in 途中式:
        x,y = temp
        print(x,y)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import defaultdict
N = int(input())
nums  = list(map(int, input().split(" ")))

Xs = []
Ys = []
S = defaultdict(lambda :0)
途中式 = []
for num in nums:
    heapq.heappush(Xs,-num)
    heapq.heappush(Ys, num)
    S[num] += 1
min_max = N % 2
for i in range(N - 1):
    while True:
        X = heapq.heappop(Xs)
        X = -X
        if S[X] > 0:
            S[X] -= 1
            break

    while True:
        Y = heapq.heappop(Ys)
        if S[Y] > 0:
            S[Y] -= 1
            break
    if i % 2 != min_max:
        途中式.append((Y,X))
        temp  = Y -X
    else:
        途中式.append((X, Y))
        temp = X - Y

    S[temp] += 1
    heapq.heappush(Xs, -temp)
    heapq.heappush(Ys, temp)


print(temp)
for temp in 途中式:
    X,Y = temp
    print(X, Y)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import defaultdict
N = int(input())
nums  = list(map(int, input().split(" ")))
Xs = []
Ys = []
S = defaultdict(lambda :0)
途中式 = []
for num in nums:
    heapq.heappush(Xs,-num)
    heapq.heappush(Ys, num)
    S[num] += 1
for _ in range(N - 2):
    while True:
        X = heapq.heappop(Xs)
        X = -X
        if S[X] > 0:
            S[X] -= 1
            break

    while True:
        Y = heapq.heappop(Ys)
        if S[Y] > 0:
            S[Y] -= 1
            break
    途中式.append((Y,X))
    temp  = Y -X
    S[temp] += 1
    heapq.heappush(Xs, -temp)
    heapq.heappush(Ys, temp)
anss = []
for k,v in S.items():
    if v > 0:
        anss.append(k)
Y = max(anss)
X = min(anss)
ans1 = Y - X
途中式.append((Y,X))


Xs = []
Ys = []
S = defaultdict(lambda :0)
途中式2 = []
for num in nums:
    heapq.heappush(Xs,-num)
    heapq.heappush(Ys, num)
    S[num] += 1
for _ in range(N - 2):
    while True:
        X = heapq.heappop(Xs)
        X = -X
        if S[X] > 0:
            S[X] -= 1
            break

    while True:
        Y = heapq.heappop(Ys)
        if S[Y] > 0:
            S[Y] -= 1
            break
    途中式2.append((X,Y))
    temp  = X - Y
    S[temp] += 1
    heapq.heappush(Xs, -temp)
    heapq.heappush(Ys, temp)
anss = []
for k,v in S.items():
    if v > 0:
        anss.append(k)
Y = min(anss)
X = max(anss)
ans2 = X- Y
途中式2.append((X,Y))

if ans1 < ans2:
    途中式  = 途中式2
    ans1 = ans2


print(ans1)

for temp in 途中式:
    Y,X = temp
    print(Y, X)

Submission status: Wrong Answer

Solution code:
import heapq
from collections import defaultdict
N = int(input())
nums  = list(map(int, input().split(" ")))

Xs = []
Ys = []
S = defaultdict(lambda :0)
途中式 = []
for num in nums:
    heapq.heappush(Xs,-num)
    heapq.heappush(Ys, num)
    S[num] += 1

for _ in range(N - 2):
    while True:
        X = heapq.heappop(Xs)
        X = -X
        if S[X] > 0:
            S[X] -= 1
            break

    while True:
        Y = heapq.heappop(Ys)
        if S[Y] > 0:
            S[Y] -= 1
            break

    途中式.append((Y,X))
    temp  = Y -X
    S[temp] += 1
    heapq.heappush(Xs, -temp)
    heapq.heappush(Ys, temp)

anss = []
for k,v in S.items():
    if v > 0:
        anss.append(k)
Y = max(anss)
X = min(anss)
ans = Y - X
途中式.append((Y,X))
print(ans)

for temp in 途中式:
    Y,X = temp
    print(Y, X)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

A  = sorted(A)
ans = []
# print(A)
for index in range(N - 1):
    # print(A)
    if ((N - index) % 2) == 1:
        ans.append([A[index], A[index + 1]])
        A[index + 1] = A[index] - A[index + 1]
    else:
        ans.append([A[index + 1], A[index]])
        A[index + 1] = A[index + 1] - A[index]


print(A[N - 1])
for l in ans:
    print(str(l[0]) + " " + str(l[1]))

Submission status: Wrong Answer

Solution code:
N = int(input())

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

A  = sorted(A)
ans = []
# print(A)
for index in range(N - 1):
    # print(A)
    if ((N - index) % 2) == 1:
        ans.append([A[index], A[index + 1]])
        A[index + 1] = A[index] - A[index + 1]
    else:
        ans.append([A[index + 1], A[index]])
        A[index + 1] = A[index + 1] - A[index]


print(A[N - 1])
for l in ans:
    print(*l)