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

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.IO;

namespace Sandbox
{
    internal class ProgramC
    {
        private static void Main(string[] args)
        {
            var scanner = new Scanner();
            var n = scanner.GetNextAsInt();
            var a = scanner.ToIntArrayFromNextLine();

            var sorted = new List<int>();
            foreach (var ai in a)
            {
                sorted.Add(ai);
            }

            sorted.Sort();

            var isSmall = n % 2 == 1;

            var result = new List<int[]>();

            for (var i = 0; i < n - 1; i++)
            {
                var xIndex = 0;
                var yIndex = 0;

                var listCount = sorted.Count;
                if (isSmall)
                {
                    xIndex = 0;
                    yIndex = listCount > 2 ? listCount - 2 : listCount - 1;
                }
                else
                {
                    xIndex = listCount - 1;
                    yIndex = listCount > 2 ? 1 : 0;
                }

                var x = sorted[xIndex];
                var y = sorted[yIndex];
                var newNum = x - y;

                if (xIndex > yIndex)
                {
                    sorted.RemoveAt(xIndex);
                    sorted.RemoveAt(yIndex);
                }
                else
                {
                    sorted.RemoveAt(yIndex);
                    sorted.RemoveAt(xIndex);
                }

                var index = BinarySearch.LowerBound(sorted, newNum);
                sorted.Insert(index, newNum);

                result.Add(new []{x, y});

                isSmall = !isSmall;
            }

            Console.WriteLine(sorted[0]);

            foreach (var item in result)
            {
                Console.WriteLine(string.Join(" ", item));
            }
        }

        #region String Scanner

        public class Scanner
        {
            private const char Separator = ' ';
            private readonly StreamReader _Stream = new StreamReader(Console.OpenStandardInput());
            private int _Pos;
            private string[] _Line = new string[0];

            #region get a element

            public string GetNext()
            {
                // ReSharper disable once InvertIf
                if (this._Pos >= this._Line.Length)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    this._Line = this._Stream.ReadLine().Split(Separator);
                    this._Pos = 0;
                }

                return this._Line[this._Pos++];
            }

            public int GetNextAsInt()
            {
                return int.Parse(this.GetNext());
            }

            public long GetNextAsLong()
            {
                return long.Parse(this.GetNext());
            }

            public double GetNextAsDouble()
            {
                return double.Parse(this.GetNext());
            }

            #endregion

            #region get array

            public string[] ToArrayFromNextLine()
            {
                if (this._Pos >= this._Line.Length)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    this._Line = this._Stream.ReadLine().Split(Separator);
                }

                this._Pos = this._Line.Length;
                return this._Line;
            }

            public int[] ToIntArrayFromNextLine()
            {
                return ToIntArray(this.ToArrayFromNextLine());
            }

            public long[] ToLongArrayFromNextLine()
            {
                return ToLongArray(this.ToArrayFromNextLine());
            }

            public double[] ToDoubleArrayFromNextLine()
            {
                return ToDoubleArray(this.ToArrayFromNextLine());
            }

            #endregion

            #region convert array

            private static int[] ToIntArray(string[] array)
            {
                var result = new int[array.Length];
                for (var i = 0; i < array.Length; i++)
                {
                    result[i] = int.Parse(array[i]);
                }

                return result;
            }

            private static long[] ToLongArray(string[] array)
            {
                var result = new long[array.Length];
                for (var i = 0; i < array.Length; i++)
                {
                    result[i] = long.Parse(array[i]);
                }

                return result;
            }

            private static double[] ToDoubleArray(string[] array)
            {
                var result = new double[array.Length];
                for (var i = 0; i < array.Length; i++)
                {
                    result[i] = double.Parse(array[i]);
                }

                return result;
            }

            #endregion
        }

        #endregion
    }

        #region BinarySearch

        public static class BinarySearch
        {
            /// <summary>
            /// 与えられた比較関数に従って，<paramref name="list"/> の要素のうち，<paramref name="value"/> 以上の要素であるような最小のインデックスを取得します．
            /// </summary>
            /// <typeparam name="T"><see cref="IList{T}"/> の要素の型を指定します．</typeparam>
            /// <param name="list">対象となるコレクション</param>
            /// <param name="value">対象となる要素</param>
            /// <param name="comparison"></param>
            /// <returns><paramref name="value"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
            /// <remarks> <paramref name="list"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
            public static int LowerBound<T>(this IList<T> list, T value, Comparison<T> comparison) =>
                SearchImpl(list, value, Comparer<T>.Create(comparison), true);

            /// <summary>
            ///　デフォルトの比較関数に従って，<paramref name="list"/> の要素のうち，<paramref name="value"/> 以上の要素であるような最小のインデックスを取得します．
            /// </summary>
            /// <typeparam name="T"><see cref="IList{T}"/> の要素の型を指定します．</typeparam>
            /// <param name="list">対象となるコレクション</param>
            /// <param name="value">対象となる要素</param>
            /// <returns><paramref name="value"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
            /// <remarks> <paramref name="list"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
            public static int LowerBound<T>(this IList<T> list, T value) =>
                SearchImpl(list, value, Comparer<T>.Default, true);

            /// <summary>
            /// 与えられた比較関数に従って，<paramref name="list"/> の要素のうち，<paramref name="value"/> 以上の要素であるような最小のインデックスを取得します．
            /// </summary>
            /// <typeparam name="T"><see cref="IList{T}"/> の要素の型を指定します．</typeparam>
            /// <param name="list">対象となるコレクション</param>
            /// <param name="value">対象となる要素</param>
            /// <param name="comparison"></param>
            /// <returns><paramref name="value"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
            /// <remarks> <paramref name="list"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
            public static int UpperBound<T>(this IList<T> list, T value, Comparison<T> comparison) =>
                SearchImpl(list, value, Comparer<T>.Create(comparison), false);

            /// <summary>
            ///　デフォルトの比較関数に従って，<paramref name="list"/> の要素のうち，<paramref name="value"/> より真に大きい要素が現れる最小のインデックスを取得します．
            /// </summary>
            /// <typeparam name="T"><see cref="IList{T}"/> の要素の型を指定します．</typeparam>
            /// <param name="list">対象となるコレクション</param>
            /// <param name="value">対象となる要素</param>
            /// <returns><paramref name="value"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
            /// <remarks> <paramref name="list"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
            public static int UpperBound<T>(this IList<T> list, T value) =>
                SearchImpl(list, value, Comparer<T>.Default, false);

            private static int SearchImpl<T>(IList<T> list, T value, IComparer<T> comparer, bool isLowerBound)
            {
                var l = 0;
                var r = list.Count - 1;
                while (l <= r)
                {
                    var m = (l + r) / 2;
                    var res = comparer.Compare(list[m], value);
                    if (res < 0 || (res == 0 && !isLowerBound))
                    {
                        l = m + 1;
                    }
                    else
                    {
                        r = m - 1;
                    }
                }

                return l;
            }
        }

        #endregion
}