Scanf.scanf "%d %d" (fun w h ->
    let p = Array.init w (fun _ -> Scanf.scanf " %d" (fun p -> p)) in
    let q = Array.init h (fun _ -> Scanf.scanf " %d" (fun q -> q)) in
    let r = Array.make (w + h) (0, false) in
    for i = 0 to w - 1 do
        r.(i) <- p.(i), false
    done;
    for i = 0 to h - 1 do
        r.(i + w) <- q.(i), true
    done;
    Array.sort compare r;

    let rec loop i a b acc =
        if i = w + h then acc else
            let (c, flag) = r.(i) in
            if flag then loop (i + 1) a (b - 1) (acc + c * a)
                    else loop (i + 1) (a - 1) b (acc + c * b)

    in
    loop 0 (w + 1) (h + 1) 0 |> Printf.printf "%d\n"
)