Problem:
Score : 400 points

Problem Statement

At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows:

The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)

Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):

R points for winning with Rock;

S points for winning with Scissors;

P points for winning with Paper.

However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.)

Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.

The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is r, the machine will play Rock in the i-th round. Similarly, p and s stand for Paper and Scissors, respectively.

What is the maximum total score earned in the game by adequately choosing the hand to play in each round?

Notes

In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.

If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;

if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;

if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;

if both players play the same hand, it is a draw.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq N-1

1 \leq R,S,P \leq 10^4

N,K,R,S, and P are all integers.

|T| = N

T consists of r, p, and s.

Input

Input is given from Standard Input in the following format:

N K
R S P
T

Output

Print the maximum total score earned in the game.

Sample Input 1

5 2
8 7 6
rsrpr

Sample Output 1

27

The machine will play {Rock, Scissors, Rock, Paper, Rock}.

We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27.

Sample Input 2

7 1
100 10 1
ssssppr

Sample Output 2

211

Sample Input 3

30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr

Sample Output 3

4996

Solution in C++14 (Clang 3.8.0) (judge verdict: Time Limit Exceeded)

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>

std::string T;
std::vector<int > T_num;
int N, K, R, S, P;
char convert[3]={'r','s','p'};
int score[3];

long long optimum(int n, int prev=-1){
  //printf("optimum(%d, %d) called\n",n,prev);
  if (n>=T.length()){
    return 0;
  }
  long long val1, val2, val3;
  if (prev==-1){ // 前回値の制限がないとき
    //printf("no prev contraint\n");
    val1 = optimum(n+K, 0) + ((T[n]=='s')?R:0);
    val2 = optimum(n+K, 1) + ((T[n]=='p')?S:0);
    val3 = optimum(n+K, 2) + ((T[n]=='r')?P:0);
    //printf("val1 of optmum(%d, %d)=%lld\n",n,prev,val1);
    //printf("val2 of optimum(%d, %d)=%lld\n",n,prev,val2);
    //printf("val3 of optmum(%d, %d)=%lld\n",n,prev,val3);
    //printf("val3 = %lld + %d\n",optimum(n+K, 2), T[n]=='r'?P:0, optimum(n+K, 2) + (T[n]=='r')?P:0);
    //    printf("val3 = %lld + %d\n",optimum(n+K, 2), T[n]=='r'?P:0, optimum(n+K, 2) + T[n]=='r'?P:0);
    //printf("retval of optum(%d, %d) = %lld\n", n, prev, (long long)fmax((long long)fmax(val1, val2), val3));
    return (long long)fmax((long long)fmax(val1, val2), val3);
    //printf("val1 = %lld + %d\n", optimum(n+K, 0), T[n]=='s'?R:0);
    //printf("val2 = %lld + %d\n", optimum(n+K, 1), T[n]=='p'?S:0);
    //return (long long )fmax((long long )fmax(optimum(n+K, 0) + T[n]=='s'?R:0,
    //                                         optimum(n+K, 1) + T[n]=='p'?S:0),
    //                        optimum(n+K, 2) + T[n]=='r'?P:0);
  } else { // 前回地の制限があるとき
    //printf("prev contstraint\n");
    val1 = optimum(n+K, (prev+1)%3) + ((T[n]==convert[(prev+2)%3])?score[(prev+1)%3]:0);
    val2 = optimum(n+K, (prev+2)%3) + ((T[n]==convert[prev%3])?score[(prev+2)%3]:0);
    //printf("val1 = %lld + %d\n", optimum(n+K, (prev+1)%3), T[n]==convert[(prev+2)%3]?score[(prev+1)%3]:0);
    //printf("val2 = %lld + %d\n", optimum(n+K, (prev+2)%3), T[n]==convert[prev%3]?score[(prev+2)%3]:0);
    //printf("val1 of optmum(%d, %d)=%lld\n",n,prev,val1);
    //printf("val2 of optimum(%d, %d)=%lld\n",n,prev,val2);
    //printf("retval of optum(%d, %d) = %lld\n", n, prev, (long long) fmax(val1, val2));
    return (long long)fmax(val1, val2);
    //return (long long )fmax(optimum(n+K, (prev+1)%3) + T[n]==convert[(prev+2)%3]?score[(prev+1)%3]:0,
    //optimum(n+K, (prev+2)%3) + T[n]==convert[prev%3]?score[(prev+2)%3]:0);
  }
  return 0;
}

int main(){
  scanf("%d", &N);
  scanf("%d", &K);
  scanf("%d", &R);
  scanf("%d", &S);
  scanf("%d", &P);
  std::cin >> T;
  score[0]=R;
  score[1]=S;
  score[2]=P;
  /*  for (int i=0; i<T.length(); i++){
    if (T[i]=='r'){
      T_num.push_back(0);
    } else if (T[i]=='s'){
      T_num.push_back(1);
    } else if (T[i]=='p'){
      T_num.push_back(2);
    }

    }*/

  long long ans = 0;
  for (int i=0; i<K; i++){
    ans += optimum(i, -1);
  }
  printf("%lld\n", ans);
  return 0;
}