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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.math.BigInteger;
import java.util.*;


public class Main {

    private static List<List<Integer>> subsets;
    private static final double LOAD_FACTOR = 0.75;


    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        InputReader.OutputWriter out = new InputReader.OutputWriter(outputStream);
        Scanner scanner = new Scanner(System.in);


        int n = in.nextInt();
        int k = in.nextInt();
        int r = in.nextInt();
        int s = in.nextInt();
        int p = in.nextInt();
        long res = 0;
        char [] c = in.next().toCharArray();
        char [] b = new char[n];

        for (int i = 0; i < c.length; i++) {
            if(i<=k - 1) {
                b[i] = win(c[i]);
                if(b[i] == 'r') res+=r;
                else if(b[i] == 's') res+=s;
                else res+=p;
            }
            else {
                char w = win(c[i]);
                if (w == b[i-k]) {
                    if(i+k<n) {
                        b[i] = c[i+k];
                    }
                    else {
                        b[i] = c[i];
                    }
                }
                else {
                    b[i] = w;
                    if(b[i] == 'r') res+=r;
                    else if(b[i] == 's') res+=s;
                    else res+=p;
                }
            }
        }

        out.println(res);
        //out.println(Arrays.toString(b));

        out.flush();
    }
    private static char win(char c) {
        if(c == 'r') return 'p';
        if(c == 'p') return 's';
        return 'r';
    }
    private static boolean isPrime(int n) {
        if(n < 2) {
            return false;
        }
        for (int i = 2; i * i <= n; i++) {
            if(n%i==0) {
                return false;
            }
        }
        return true;
    }



    private boolean check(int n) {
        return n>=1 && n<=26;
    }
    private static void generateStars(String current, int star, int shouldBe, int n) {
        if(current.length() ==  n) {
            if(star == shouldBe) {
                System.out.println(current);
            }
            return;
        }
        if(star < shouldBe) {
            if(!current.endsWith("*")) {
                generateStars(current + "*", star + 1, shouldBe, n);
            }
        }
        generateStars(current + ".", star, shouldBe, n);
    }
    private static void generateBrackets(String current, int open, int close, int n) {
        if(current.length() == 2*n) {
            System.out.println(current);
            return;
        }
        if(open < n) {
            generateBrackets(current + "(", open + 1, close,n);
        }
        if(close < open) {
            generateBrackets(current + ")", open, close + 1,n);
        }
    }
    private static boolean isPrime(long n) {
        if(n < 2) return false;
        for (long i = 2; i*i <= n;i++) {
            if(n%i==0) return false;
        }
        return true;
    }
    private static Set<Integer> allSubsetSum(int [] a) {
        int n = a.length;
        int total = 1 << n;
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < total; i++) {
            int sum = 0;
            for (int j = 0; j < n; j++) {
                if((i&(1<<j)) == 0) {
                    sum+=a[j];
                }
            }
            set.add(sum);
        }
        return set;
    }
    private static long sum (int k, long s, long m) {
        return s - (k - 1) * m;
    }


    public static String candyCrush(String s) {
        int n = s.length();
        Deque<Letter> stack = new ArrayDeque<>(n);
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            int count = 1;
            for (; i + 1 < n && s.charAt(i + 1) == c; i++) {
                count++;
            }

            if (!stack.isEmpty() && stack.peek().getLetter() == c) {
                count += stack.pop().getCount();
            }

            if (count < 3) {
                stack.push(new Letter(c, count));
            }

        }

        StringBuilder sb = new StringBuilder(n);
        while (!stack.isEmpty()) {
            Letter letter = stack.pollLast();
            for (int count = letter.getCount(); count > 0; count--) {
                sb.append(letter.getLetter());
            }
        }
        return sb.toString();
    }

    private static int ceil(int [] a, int start, int end, int key) {
        while (end-start>1) {
            int mid = start+(end-start)/2;
            if(a[mid]>=key) {
                end = mid;
            }
            else {
                start = mid;
            }
        }
        return end;
    }
    static boolean getBit(int n, int i) {
        System.out.println((n & (1 << i)));
        return ((n & (1 << i))!=0);
    }
    private static void moveZeroes(int [] a) {
        int index = 0;
        int count = 0;
        for (int i = 0; i < a.length; i++) {
            if(a[i] != 0) {
                int temp = a[i];
                a[index++] = a[i];
                a[i] = temp;
                count++;
            }
        }
        for (int i = index; i < a.length; i++) {
            a[i] = 0;
        }
    }

    private static int EulerFunction(int n) {
        Map<Integer,Integer> factors = factors(n);
        int num = 0;
        for(Map.Entry<Integer,Integer> m : factors.entrySet()) {
            int key = m.getKey();
            int value = m.getValue();
            num+=(key - 1)*binPow(key,value - 1);
        }
        return num;
    }
    private static boolean [] sieveOfEratosthenes(int n) {
        boolean [] primes = new boolean[n+1];
        for (int i = 0; i < n; i++) {
            primes[i] = true;
        }
        for (int i = 2; i*i <=n; i++) {
            if(primes[i]) {
                for (int j = i*i; j <=n; j+=i) {
                    primes[j] = false;
                }
            }
        }
        return primes;
    }
    private static Map<Integer,Integer> factors(int n) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i=2;i*i<=n;i++) {
            while (n%i==0) {
                map.put(i,map.getOrDefault(i,0) + 1);
                n/=i;
            }
        }
        if(n > 1) map.put(n,map.getOrDefault(n,0) + 1);
        return map;
    }
    private static int binPow(int a,int b) {
        if(b == 0) return 1;
        if(b%2!=0) {
            return a*binPow(a,b-1);
        }
        else {
            int res = binPow(a,b/2);
            return res*res;
        }
    }
    private void lis() {
           /*int [] a = {2,3,4,1,1,3,4,6,8,9,4,1};
        int n = a.length;
        int length = 0;
        List<Integer> list = new ArrayList<>();
        List<Integer> longest = new ArrayList<>();
        int maxLength = 1;
        for (int i = 0; i < a.length; i++) {
            int current = Integer.MIN_VALUE;
            for (int j = 0; j < i; j++) {
                if(a[j] > current) {
                    list.add(a[j]);
                    current = a[j];
                }
            }
            if(list.size() > length) {
                length = list.size();
                longest = new ArrayList<>(list);
            }
            list.clear();
        }

        out.println(longest.size());
        out.println(longest);*/

    }
    private static int check(int [] count, int k) {
        for (int i = 0; i < count.length; i++) {
            if(count[i] == k) return i;
        }
        return -1;
    }
    private static void reserve(long [] a, int i, int j) {
        while (i<=j) {
            long temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            i++;
            j--;
        }
    }
    private static String compressTheString(String s) {
        char [] c =  s.toCharArray();
        int count = 1;
        char current = c[0];
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < c.length; i++) {
            if(c[i] == current) {
                count++;
            }
            else {
                sb.append(current);
                sb.append(count);
                count = 1;
                current = c[i];
            }
        }
        sb.append(current);
        sb.append(count);
        if(sb.length() >= c.length) {
            return s;
        }
        else {
            return sb.toString();
        }
    }

}

class Letter {
    private char letter;
    private int  count;

    public int getCount() {
        return count;
    }

    public char getLetter() {
        return letter;
    }

    public Letter (char letter, int count) {
        this.letter = letter;
        this.count = count;
    }
}
class InputReader extends BufferedReader {
    StringTokenizer tokenizer;

    public InputReader(InputStream inputStream) {
        super(new InputStreamReader(inputStream), 32768);
    }

    public InputReader(String filename) {
        super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }
    public Integer nextInt() {return Integer.valueOf(next());}
    public Long nextLong() {return  Long.valueOf(next());}
    public Double nextDouble() {return Double.valueOf(next());}

    static class OutputWriter extends PrintWriter {
        public OutputWriter(OutputStream outputStream) {
            super(outputStream);
        }

        public OutputWriter(Writer writer) {
            super(writer);
        }

        public OutputWriter(String filename) throws FileNotFoundException {
            super(filename);
        }

        public void close() {
            super.close();
        }
    }
}