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,amin,amax
    integer(int64), allocatable:: a(:),pa(:),ma(:)

    read*, n
    allocate(a(n))
    read*, a(:)
    a = merge_sort(a)
    pa = pack(a(2:n-1),a(2:n-1)>=0)
    ma = pack(a(2:n-1),a(2:n-1)<0)
    
    print'(i0)', -a(1) + sum(abs(a(2:n-1))) + a(n)

    amin = a(1)
    amax = a(n)


    do i=1,size(pa)
        print'(*(i0,1x))', amin,pa(i)
        amin=amin-pa(i)
    end do

    do i=1,size(ma)
        print'(*(i0,1x))', amax,ma(i)
        amax=amax-ma(i)
    end do
    print'(*(i0,1x))', amax, amin

    
end program main