defmodule Main do
  def main do
    IO.gets("")
    a = IO.gets("") 
    |> String.trim() 
    |> String.split() 
    |> Enum.map(&String.to_integer/1)
    |> Enum.sort(&(&1 >= &2))
    # |> IO.inspect()
    [head | tail] = a
    solve(tail, :right, [head], 0)
  end
  
  def solve([], _, acc, sum) do
  # IO.inspect(acc)
  IO.inspect(sum)
  end
  def solve([head | tail], :right, acc, sum) do
  # IO.inspect(acc, label: :acc)
  # IO.inspect(min(hd(acc), tl(acc)), label: :min)
  solve(tail, :left, acc ++ [head], sum + min(List.first(acc), List.last(acc)))
  end
  def solve([head | tail], :left, acc, sum) do
  # IO.inspect(acc, label: :acc)
  # IO.inspect(min(hd(acc), tl(acc)), label: :min)
  solve(tail, :right, [head] ++ acc, sum + min(List.first(acc), List.last(acc)))
  end
end