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 Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"sort"
	"strconv"
)

var sc *bufio.Scanner

const BufferSize = 1024

func nextInt() int {
	if !sc.Scan() {
		panic(nil)
	}
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

var production bool

func debugf(format string, v ...interface{}) {
	if !production {
		fmt.Printf(format, v...)
	}
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func answer(reader io.Reader, writer io.Writer) {
	sc = bufio.NewScanner(reader)
	buf := make([]byte, BufferSize)
	sc.Buffer(buf, 1e+6)
	sc.Split(bufio.ScanWords)
	n := nextInt()
	s := make([]int, n)
	x := make([]int, n)
	y := make([]int, n)

	for i := range s {
		s[i] = nextInt()
	}
	sort.Ints(s)
	p := 1
	for p < n && s[p] < 0 {
		p++
	}
	if p == n {
		p--
	}
	debugf("s:%v p:%d\n", s, p)
	idx := 0
	x[0] = s[0]
	for i := p; i < n-1; i++ {
		y[idx] = s[i]
		idx++
		x[idx] = x[idx-1] - y[idx-1]
		debugf("a: x[%d]:%d = x[%d]:%d - y[%d]:%d\n", idx, x[idx], idx-1, x[idx-1], idx-1, y[idx-1])
	}
	y[idx] = x[idx]
	x[idx] = s[len(s)-1]
	idx++
	x[idx] = x[idx-1] - y[idx-1]
	debugf("b: x[%d]:%d = x[%d]:%d - y[%d]:%d\n", idx, x[idx], idx-1, x[idx-1], idx-1, y[idx-1])

	for i := 0; i < p-1; i++ {
		y[idx] = s[i+1]
		idx++
		x[idx] = x[idx-1] - y[idx-1]
		debugf("c: x[%d]:%d = x[%d]:%d - y[%d]:%d\n", idx, x[idx], idx-1, x[idx-1], idx-1, y[idx-1])
	}
	debugf("x:%v\n", x)
	debugf("y:%v\n", y)
	_, _ = fmt.Fprintf(writer, "%d\n", x[idx])
	for i := 0; i < n-1; i++ {
		_, _ = fmt.Fprintf(writer, "%d %d\n", x[i], y[i])
	}
}

func answer2(reader io.Reader, writer io.Writer) {
	sc = bufio.NewScanner(reader)
	buf := make([]byte, BufferSize)
	sc.Buffer(buf, 1e+6)
	sc.Split(bufio.ScanWords)
	n := nextInt()
	s := make([]int, n)
	p := make([]int, 0, n)
	m := make([]int, 0, n)

	for i := range s {
		s[i] = nextInt()
		if s[i] >= 0 {
			p = append(p, s[i])
		}
		if s[i] < 0 {
			m = append(m, s[i])
		}
	}
	sort.Ints(p)
	sort.Sort(sort.Reverse(sort.IntSlice(m)))
	//sort.Ints(m)
	debugf("s:%v\n", s)
	debugf("p:%v (%d)\n", p, len(p))
	debugf("m:%v (%d)\n", m, len(m))

	if len(p) > 0 && len(m) > 0 {
		ans := 0
		ip := 0
		im := 0
		for i := 0; i < n; i++ {
			ans += abs(s[i])
		}
		_, _ = fmt.Fprintf(writer, "%d\n", ans)
		for i := 0; i < n-1; i++ {
			var x, y int
			if ip < len(p)-1 {
				x = m[im]
				y = p[ip]
				ip++
				m[im] = x - y
			} else {
				x = p[ip]
				y = m[im]
				im++
				p[ip] = x - y
			}
			_, _ = fmt.Fprintf(writer, "%d %d\n", x, y)
		}
	}
	if len(p) > 0 && len(m) == 0 {
		ans := 0
		for i := 2; i < n; i++ {
			ans += s[i]
		}
		ans += s[1] - s[0]
		_, _ = fmt.Fprintf(writer, "%d\n", ans)
		if len(s) == 2 {
			s[0], s[1] = s[1], s[0]
		}
		_, _ = fmt.Fprintf(writer, "%d %d\n", s[0], s[1])
		s[1] = s[0] - s[1]
		for i := 2; i < n; i++ {
			var x, y int
			y = s[i]
			x = s[i-1]
			s[i] = s[i-1] - s[i]
			if i == n-1 {
				x, y = y, x
			}
			_, _ = fmt.Fprintf(writer, "%d %d\n", x, y)
		}

	}
	if len(p) == 0 && len(m) > 0 {
		ans := 0
		for i := 2; i < n; i++ {
			ans += -m[i]
		}
		ans += m[0] - m[1]
		_, _ = fmt.Fprintf(writer, "%d\n", ans)
		_, _ = fmt.Fprintf(writer, "%d %d\n", m[0], m[1])
		m[1] = m[0] - m[1]
		for i := 2; i < n; i++ {
			var x, y int
			y = m[i]
			x = m[i-1]
			m[i] = m[i-1] - m[i]
			_, _ = fmt.Fprintf(writer, "%d %d\n", x, y)
		}
	}

}

func main() {
	production = true
	answer(os.Stdin, os.Stdout)
}