module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: merge_sort
contains
    recursive function merge_sort(ar) result(ret)
        integer(int64),intent(in):: ar(:)
        integer(int64):: ret(size(ar))
        integer(int64):: m

        if (size(ar) <= 1) then
            ret(:) = ar(:)
        else
            m = size(ar)/2
            ret = merge_([merge_sort(ar(:m)),merge_sort(ar(m+1:))])
        end if
    end function

    function merge_(ar) result(ret)
        integer(int64),intent(in):: ar(:)
        integer(int64):: ret(size(ar))
        integer(int64):: m,l,r,i

        m = size(ar)/2; l=1;r=m+1
        do i=1,size(ar)
            if (m < l) then
                ret(i)=ar(r); r=r+1
            else if (size(ar) < r) then
                ret(i)=ar(l); l=l+1
            else
                if (ar(l) <= ar(r)) then
                    ret(i) = ar(l); l=l+1
                else
                    ret(i) = ar(r); r=r+1
                end if
            end if
        end do
    end function
end module

program main
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int64):: n,i,now
    integer(int64), allocatable:: a(:)

    read*, n
    allocate(a(n))
    read*, a(:)
    a = merge_sort(a)

    print'(i0)', sum(a(n/2+1:n))-sum(a(1:n/2))
    if (btest(n,0)) then
        now = a(n/2+1)
    else
        print'(*(i0,1x))', a(n/2+1), a(n/2)
        now = a(n/2+1)-a(n/2)
    end if

    do i=1,n-n/2-1
        print'(*(i0,1x))', a(i),now
        now = a(i)-now
        print'(*(i0,1x))', a(i+n/2+1),now
        now = a(i+n/2+1)-now
    end do
end program main