(* O(1) *)
let get_next_time cook_time current waits =
  let time = cook_time + current in
  let m = time mod 10 in
  if m = 0 || not waits then time else time + 10 - m

(* O(n) *)
let rec fold_btw f a i n = if i >= n then a else fold_btw f (f a i) (i + 1) n
let fold_int f a n = fold_btw f a 0 n

(* O(|dishes|!) *)
let solve dishes =
  let n = Array.length dishes in
  let used = Array.make n false in
  let perm = Array.make n (-1) in
  let rec loop min_time i =
    if i >= n then
      min
        min_time
        (fold_int
          (fun acc i -> get_next_time dishes.(perm.(i)) acc (i <> n - 1))
          0 n)
    else
      fold_int
        (fun acc j ->
          if used.(j) then
            acc
          else
            (used.(j) <- true;
            perm.(i) <- j;
            let mi = loop acc (i + 1) in
            used.(j) <- false;
            mi))
        min_time
        n
    in
      loop max_int 0

let read_int_array n = Array.init n (fun _ -> read_int ())

let _ = read_int_array 5 |> solve |> string_of_int |> print_endline