Scanf.scanf "%d" (fun n ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    Array.sort compare a;

    let rec loop rest c acc =
        if rest = 0 then acc else
            if rest > 1 then loop (rest - 2) (c - 1) (acc + a.(c) * 2)
                        else loop (rest - 1) (c - 1) (acc + a.(c))
    in
    loop (n - 2) (n - 2) a.(n - 1) |> Printf.printf "%d\n"
)
