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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <string.h>
int main()
{
	int		N;		/* じゃんけんを行う回数			*/
	int		K;		/* 手の制限回数				*/
	int		R;		/* グー(Rock)で勝った場合の点数		*/
	int		S;		/* チョキ(Scissors)で勝った場合の点数	*/
	int		P;		/* パー(Paper)で勝った場合の点数	*/
	char		T[100005];	/* 筐体側の入力				*/
	char		U[100005];	/* ユーザー側の入力			*/
	int		index;		/* 配列のインデックス			*/
	int		result;		/* 合計点数を保存する			*/
	int	zero_r;			/* Rockでの0点手			*/
	int	zero_s;			/* Scissorsでの0点手			*/
	int	zero_p;			/* Paperでの0点手			*/
	
	/* 入力 */
	scanf("%d %d", &N, &K);
	scanf("%d %d %d", &R, &S, &P);
	scanf("%s", &T);

	result = 0;
	zero_r = 0;
	zero_s = 0;
	zero_p = 0;

	/* ユーザー側のKの値を考えない場合の最善手を入力する */

	for(index = 0; index < N; ++index){
		if(T[index] == 'r')
			U[index] = 'p';
		else if(T[index] == 's')
			U[index] = 'r';
		else if(T[index] == 'p')
			U[index] = 's';
	}
	/* これをTにコピーする */
	
	strcpy(T, U);

	/*
	 * 配列の処理を添え字Kからスタートする
	 * K個前の手と全く同じ手があれば現在の手に変更を加える
	 * K個先の手と比べて違う手を選ぶように処理をする	
	 *------------------------------------------------------------------
	 * if文内で条件として扱っている値を操作するとスタックオーバーフロー
	 * が発生するのでUser入力に値をコピーした
	 */

	for(index = K; index < N; ++index){
		if((T[index] == T[index - K]) && (T[index] == 'r')){

			if(T[index + K] == 's'){
				U[index] = 'p';
				++zero_p;
			}
			else if(T[index + K] == 'p'){
				U[index] = 's';
				++zero_s;
			}
			else{
				U[index] = 'p';
				++zero_p;	
			}
		}
		else if((T[index] == T[index - K]) && (T[index] == 's')){

			if(T[index + K] == 'r'){
				U[index] = 'p';
				++zero_p;
			}
			else if(T[index + K] == 'p'){
				U[index] = 'r';
				++zero_r;
			}
			else{
				U[index] = 'r';
				++zero_r;
			}
		}
		else if((T[index] == T[index - K]) && (T[index] == 'p')){

			if(T[index + K] == 'r'){
				U[index] = 's';
				++zero_s;
			}
			else if(T[index + K] == 's'){
				U[index] = 'r';
				++zero_r;
			}
			else{
				U[index] = 's';
				++zero_s;
			}
		}
      	/* Update */
      	strcpy(T, U);
	}
	/*
	 * ユーザーの手に対する点数をresultに加算していく
	 */
	for(index = 0; index < N; ++index){
		if(U[index] == 'r')
			result += R;
		else if(U[index] == 's')
			result += S;
		else if(U[index] == 'p')
			result += P;
	}
	result -= (R * zero_r);
	result -= (S * zero_s);
	result -= (P * zero_p);
	printf("%d\n", result);
	return (0);
}
/*
 * zero_rをshort intにしていたためテストケース41で範囲が足りなくなりWA
 */