program main
  implicit none
  integer(8) i, ans, frag, r, N
  integer(8),allocatable :: A(:)
  
  read(*, *) N
  allocate(A(N))
  read(*, *) (A(i), i = 1,N)
  call qsort(A)
  ans = 0
  r = N
  frag = 1
  do i = 1,N-1
    ans = ans + A(r)
    if (frag == 1) then
      frag = 0
      r = r-1
    else
      frag = 1
    end if
  end do

  write(*, *) ans
  
contains

recursive subroutine qsort(a)
implicit none
!integer(8),intent(in) :: n
integer(8),intent(inout) :: a(:)
integer(8)::i,j,toobig,toosmall,pv,n
n = size(a)
i = 1
j = n
pv = a((i+j)/2)
do
   do
     if (a(i) < pv) then
       i = i+1
     else
       exit
     end if
   end do
   do
     if(a(j) > pv) then
       j = j-1
     else
       exit
     end if
   end do
   if (i>=j) then
     exit
   else
     toobig = a(j)
     toosmall = a(i)
     a(i) = toobig
     a(j) = toosmall
     i = i+1
     j = j-1
   end if
 end do
 if (i > 2) then
   call qsort(a(1:i-1))
 end if
 if (j < n-1) then
   call qsort(a(j+1:n))
 end if
end subroutine qsort
end program main

