let rec solve w h = function
  | [] -> 0
  | (p, `P) :: rest -> (h + 1) * p + solve (w - 1) h rest
  | (q, `Q) :: rest -> (w + 1) * q + solve w (h - 1) rest

let () = Scanf.scanf "%d %d\n" @@ fun w h ->
  begin
    Array.to_list (Array.init w (fun _ ->
      Scanf.scanf "%d\n" (fun p -> (p, `P)))) @
    Array.to_list (Array.init h (fun _ ->
      Scanf.scanf "%d\n" (fun q -> (q, `Q))))
  end
  |> List.sort (fun (p, _) (q, _) -> compare p q)
  |> solve w h
  |> Printf.printf "%d\n"

