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: Accepted)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace ReadWriteTemplate
{
    public static class Solver
    {
        private static void SolveCase()
        {
            int n = ReadInt();
            var pos = new List<int>();
            var neg = new List<int>();
            for (int i = 0; i < n; i++)
            {
                int x = ReadInt();
                if (x >= 0)
                {
                    pos.Add(x);
                }
                else
                {
                    neg.Add(x);
                }
            }
            pos = pos.OrderByWithShuffle(x => x).ToList();
            neg = neg.OrderByWithShuffle(x => -x).ToList();
            var o = new List<Tuple<int, int>>();
            int tpos = 0;
            int tneg = 0;
            int cur;
            if (pos.Count > 1)
            {
                if (neg.Count > 0)
                {
                    if (neg.Count + pos.Count > 2)
                    {
                        cur = neg[0] - pos[0];
                        tpos++;
                        tneg++;
                        o.Add(new Tuple<int, int>(neg[0], pos[0]));
                    }
                    else
                    {
                        cur = pos[0] - neg[0];
                        tpos++;
                        tneg++;
                        o.Add(new Tuple<int, int>(pos[0], neg[0]));
                    }
                }
                else
                {
                    if (pos.Count >= 2)
                    {
                        if (pos.Count > 2 || neg.Count > 0)
                        {
                            cur = pos[0] - pos[1];
                            tpos += 2;
                            o.Add(new Tuple<int, int>(pos[0], pos[1]));
                        }
                        else
                        {
                            cur = pos[1] - pos[0];
                            tpos += 2;
                            o.Add(new Tuple<int, int>(pos[1], pos[0]));
                        }
                    }
                    else
                    {
                        cur = neg[neg.Count - 1];
                        neg.RemoveAt(neg.Count - 1);
                    }
                }
            }
            else
            {
                cur = neg[neg.Count - 1];
                neg.RemoveAt(neg.Count - 1);
            }

            while (tpos < pos.Count - 1)
            {
                o.Add(new Tuple<int, int>(cur, pos[tpos]));
                cur = cur - pos[tpos];
                tpos++;
            }

            if (tpos < pos.Count)
            {
                o.Add(new Tuple<int, int>(pos[tpos], cur));
                cur = pos[tpos] - cur;
                tpos++;
            }
            else
            {
                if (tneg < neg.Count)
                {
                    o.Add(new Tuple<int, int>(neg[tneg], cur));
                    cur = neg[tneg] - cur;
                    tneg++;
                }
            }


            while (tneg < neg.Count)
            {
                o.Add(new Tuple<int, int>(cur, neg[tneg]));
                cur = cur - neg[tneg];
                tneg++;
            }

            Writer.WriteLine(cur);
            for (int i = 0; i < o.Count; i++)
            {
                Writer.WriteLine($"{o[i].Item1} {o[i].Item2}");
            }
        }

        public static void Solve()
        {
#if DEBUG
            var sw = Stopwatch.StartNew();
#endif

            SolveCase();

#if DEBUG
            sw.Stop();
            Console.WriteLine($"{sw.ElapsedMilliseconds} ms");
#endif
        }

        public static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#if DEBUG
            // Reader = Console.In; Writer = Console.Out;
            Reader = File.OpenText("input.txt"); Writer = File.CreateText("output.txt");
#else
            Reader = Console.In; Writer = Console.Out;
#endif

            // Solve();
            Thread thread = new Thread(Solve, 64 * 1024 * 1024);
            thread.CurrentCulture = CultureInfo.InvariantCulture;
            thread.Start();
            thread.Join();

            Reader.Close();
            Writer.Close();
        }

        public static IOrderedEnumerable<TSource> OrderByWithShuffle<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            return source.Shuffle().OrderBy(keySelector);
        }

        public static T[] Shuffle<T>(this IEnumerable<T> source)
        {
            T[] result = source.ToArray();
            Shuffle(result);
            return result;
        }

        private static void Shuffle<T>(IList<T> array)
        {
            Random rnd = new Random();
            for (int i = array.Count - 1; i >= 1; i--)
            {
                int k = rnd.Next(i + 1);
                T tmp = array[k];
                array[k] = array[i];
                array[i] = tmp;
            }
        }

        #region Read/Write

        private static TextReader Reader;

        private static TextWriter Writer;

        private static Queue<string> CurrentLineTokens = new Queue<string>();

        private static string[] ReadAndSplitLine()
        {
            return Reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string ReadToken()
        {
            while (CurrentLineTokens.Count == 0)
                CurrentLineTokens = new Queue<string>(ReadAndSplitLine());
            return CurrentLineTokens.Dequeue();
        }

        public static string ReadLine()
        {
            return Reader.ReadLine();
        }

        public static int ReadInt()
        {
            return int.Parse(ReadToken());
        }

        public static long ReadLong()
        {
            return long.Parse(ReadToken());
        }

        public static double ReadDouble()
        {
            return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
        }

        public static int[] ReadIntArray()
        {
            return ReadAndSplitLine().Select(int.Parse).ToArray();
        }

        public static long[] ReadLongArray()
        {
            return ReadAndSplitLine().Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
        }

        public static int[][] ReadIntMatrix(int numberOfRows)
        {
            int[][] matrix = new int[numberOfRows][];
            for (int i = 0; i < numberOfRows; i++)
                matrix[i] = ReadIntArray();
            return matrix;
        }

        public static string[] ReadLines(int quantity)
        {
            string[] lines = new string[quantity];
            for (int i = 0; i < quantity; i++)
                lines[i] = Reader.ReadLine().Trim();
            return lines;
        }

        public static void WriteArray<T>(IEnumerable<T> array)
        {
            Writer.WriteLine(string.Join(" ", array));
        }

        #endregion
    }
}