program successive_subtraction
  implicit none
  integer :: n, i
  integer(8) :: a(100000), m
  a = 0_8
  read(*,*) n
  read(*,*) a(1:n)
  call quick_sort(a(1:n))
  if (mod(n,2).eq.1) then
    m = a(n)
    do i = 1, n/2
      m = m+a(n-i)-a(i)
    end do
    write(*,'(i0)') m
    do i = 1, n/2
      write(*,'(i0,x,i0)') a(i), a(n-i)
    end do
    m = a(n)
    do i = 1, n/2
      write(*,'(i0,x,i0)') m, a(i)-a(n-i)
      m = m+a(n-i)-a(i)
    end do
  else
    m = a(n)-a(1)
    do i = 2, n/2
      m = m+a(n-i+1)-a(i)
    end do
    write(*,'(i0)') m
    write(*,'(i0)') a(n), a(1)
    do i = 2, n/2
      write(*,'(i0,x,i0)') a(i), a(n-i+1)
    end do
    m = a(n)-a(1)
    do i = 2, n/2
      write(*,'(i0,x,i0)') m, a(i)-a(n-i+1)
      m = m+a(n-i+1)-a(i)
    end do
  end if
  stop
contains
  recursive subroutine quick_sort(a)
    implicit none
    integer(8), intent(inout) :: a(:)
    integer(8) :: p, c
    integer :: n, l, r
    n = size(a)
    l = 1
    r = n
    p = a((l+r)/2)+a(l)+a(r)-max(a((l+r)/2),a(l),a(r))-min(a((l+r)/2),a(l),a(r))
    do
      do while (a(l).lt.p)
        l = l+1
      end do
      do while (a(r).gt.p)
        r = r-1
      end do
      if (l.ge.r) exit
      c = a(l)
      a(l) = a(r)
      a(r) = c
      l = l+1
      r = r-1
    end do
    if (l.gt.2) call quick_sort(a(1:l-1))
    if (n.gt.r+1) call quick_sort(a(r+1:n))
  end subroutine quick_sort
end program successive_subtraction