let ans, n, k, ws, h = Scanf.(scanf " %d %d %d %d %d" @@ fun n k r s p -> ref 0, n, k, Array.init n (fun _ -> scanf " %c" @@ function 'r' -> 'p' | 's' -> 'r' | _ -> 's'), function 'r' -> r | 's' -> s | _ -> p)
let g t u = List.(filter (fun x -> x <> t && x <> u) ['r'; 's'; 'p'] |> hd)
let _ = Array.iteri (fun i w -> if i - k >= 0 && ws.(i - k) = w then ws.(i) <- g w @@ if i + k < n then ws.(i + k) else w else ans := !ans + h w) ws; Printf.printf "%d\n" !ans