module module_sort
    use iso_fortran_env
    implicit none

    interface quicksort
        module procedure :: int32_qsort
        module procedure :: int64_qsort
    end interface quicksort
contains

    recursive function int32_qsort(m) result(res)
        integer(int32), intent(in) :: m(:)
        integer(int32) :: res(size(m)) 

        if (size(m) <= 1) then; res = m
        else; res = [int32_qsort( pack(m(2:), m(2:) >= m(1)) ), m(1), int32_qsort( pack(m(2:), m(2:) < m(1)) )]
        end if

    end function int32_qsort

    recursive function int64_qsort(m) result(res)
        integer(int64), intent(in) :: m(:)
        integer(int64) :: res(size(m)) 

        if (size(m) <= 1) then; res = m
        else; res = [int64_qsort( pack(m(2:), m(2:) >= m(1)) ), m(1), int64_qsort( pack(m(2:), m(2:) < m(1)) )]
        end if

    end function int64_qsort

end module module_sort

program main
    use module_sort
    use iso_fortran_env
    implicit none
    integer(int64) :: n
    integer(int64), allocatable :: ai(:), bi(:)
    integer(int64) :: i, ie
    integer(int64) :: ans

    read *, n
    allocate( ai(n) )
    read *, ai( 1:n )
    ans = 0

    if ( mod((n-1), 2) == 0 ) then
        ie = (n-1) / 2
        allocate( bi(ie+1) )
        bi = ai(1:ie+1)
        bi = quicksort( bi )
        ans = 2 * sum( bi(1:ie) ) - bi(1) + bi(ie + 1)
    else
        ie = n / 2
        allocate( bi(ie) )
        bi = ai(1:ie)
        bi = quicksort( bi )
        ans = 2 * sum( bi(1:ie) ) - bi(1)
    endif

    print *, ans

end program main