let rec fix ~f x = f (fix ~f) x

let hands = [|'r'; 's'; 'p'|]

let () =
  Scanf.scanf "%d %d %d %d %d %s" @@ fun n k r s p t ->
  let wp = [|r; s; p|] in
  let calc m =
    fix 0 [|0; 0; 0|] ~f:(fun f i mem ->
      if i * k + m >= n then Array.fold_left max 0 mem
      else 
        [|0; 1; 2|]
        |> Array.map (fun j ->
          let c = if hands.((j+1) mod 3) = t.[i * k + m] then wp.(j) else 0
          in c + max mem.((j+1) mod 3) mem.((j+2) mod 3))
        |> f (i+1))
  in
  fix 0 0 ~f:(fun f i ans -> if i >= k then ans else f (i+1) (ans + calc i))
  |> Printf.printf "%d\n"
