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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;

using Atcoder_Library_NetCore3;
using static Atcoder_Library_NetCore3.MyBitUtil;
using static Atcoder_Library_NetCore3.MyInputUtil;
using static System.Console;

namespace Atcoder_Template_NetCore3
{
    class Program
    {
        static void Main()
        {
            var (_, k) = ReadInput2<int>();
            var (r, s, p) = ReadInput3<int>();
            var str = ReadLine();
            var act = new char[str.Length];

            for (int i = 0; i < str.Length; i++)
            {
                act[i] += str[i] switch
                {
                    'r' => 'p',
                    's' => 'r',
                    'p' => 's',
                    _ => throw new NotSupportedException(),
                };
                if (i >= k)
                {
                    if (act[i - k] == act[i])
                    {
                        act[i] = 'n'; // None
                    }
                }
            }

            long ans = 0;
            foreach (var item in act)
            {
                ans += item switch
                {
                    'r' => r,
                    's' => s,
                    'p' => p,
                    'n' => 0,
                    _ => throw new NotSupportedException(),
                };
            }

            WriteLine(ans);
        }
    }
}

namespace Atcoder_Library_NetCore3
{
    /// <summary>
    /// グループへの所属判定とグループの結合を高速に行う機能を提供します。
    /// </summary>
    public class UnionFindTree
    {
        private readonly List<int> parentIndexs;
        private readonly List<int> ranks;

        /// <summary>
        /// 要素が空の状態として <see cref="UnionFindTree"/> の新しいインスタンスを初期化します。
        /// </summary>
        public UnionFindTree()
        {
            parentIndexs = new List<int>();
            ranks = new List<int>();
        }

        /// <summary>
        /// 指定の要素数で <see cref="UnionFindTree"/> の新しいインスタンスを初期化します。
        /// </summary>
        /// <param name="count">初期状態として存在する要素数。</param>
        public UnionFindTree(int count)
        {
            parentIndexs = new List<int>(Enumerable.Range(0, count));
            var arr = new int[count];
            Array.Fill(arr, 1);
            ranks = new List<int>(arr);
        }

        /// <summary>
        /// 2つの要素の間に結合を張ります。
        /// </summary>
        public void Unite(int a, int b)
        {
            if (IsSameGroup(a, b))
            {
                return;
            }
            else
            {
                var ra = ranks[a];
                var rb = ranks[b];

                if (ra == rb)
                {
                    var bParent = FindParent(b);
                    parentIndexs[bParent] = a;
                    ranks[a]++;
                }
                else if (ra > rb)
                {
                    var bParent = FindParent(b);
                    parentIndexs[bParent] = a;
                }
                else
                {
                    var aParent = FindParent(a);
                    parentIndexs[aParent] = b;
                }
            }
        }

        /// <summary>
        /// 2つの要素が結合で結ばれているかどうか調べます。間接的に結ばれている場合を含みます。
        /// </summary>
        public bool IsSameGroup(int a, int b)
        {
            return FindParent(a) == FindParent(b);
        }

        /// <summary>
        /// 現在のグループを <see cref="Dictionary{Tkey,TValue}"/> として取得します。
        /// グループの親を Key とし、親を含むすべてのメンバーを含む <see cref="List{T}"/> を Value とします。
        /// </summary>
        public Dictionary<int, List<int>> GetMemberDictionary()
        {
            var dic = new Dictionary<int, List<int>>();

            for (int i = 0; i < parentIndexs.Count; i++)
            {
                var parent = FindParent(i);
                if (dic.ContainsKey(parent))
                {
                    dic[parent].Add(i);
                }
                else
                {
                    dic.Add(parent, new List<int>(new[] { i }));
                }
            }

            return dic;
        }

        /// <summary>
        /// 新しい要素を親として追加します。
        /// </summary>
        public void Add()
        {
            parentIndexs.Add(1);
            ranks.Add(1);
        }

        /// <summary>
        /// 対象の要素の親を検索します。
        /// </summary>
        public int FindParent(int index)
        {
            if (parentIndexs[index] == index)
            {
                return index;
            }
            else
            {
                var parent = FindParent(parentIndexs[index]);
                parentIndexs[index] = parent;
                return parent;
            }
        }
    }

    /// <summary>
    /// Bit取得、およびbit全探索を提供します。
    /// </summary>
    internal static class MyBitUtil
    {
        /// <summary>
        /// 指定の桁のビットを取得します。
        /// </summary>
        /// <param name="count">取得する桁。</param>
        /// <returns></returns>
        public static int GetBit(this int num, int count)
        {
            var x = num >> (count - 1);
            const int mask = 1;
            return x & mask;
        }

        /// <summary>
        /// 各ビットの値を <see cref="int"/> 配列として取得します。
        /// 下位の桁が配列の前に来ます。
        /// </summary>
        public static int[] ToBitArray(this int num)
        {
            const int size = sizeof(int) * 8;
            var array = new int[size];
            for (var i = 0; i < size; i++)
            {
                array[i] = num.GetBit(i + 1);
            }
            return array;
        }

        public static int BitToInt32(int[] bitArray)
        {
            var result = 0;
            for (int i = 0; i < bitArray.Length; i++)
            {
                result += bitArray[i] << i;
            }
            return result;
        }

        /// <summary>
        /// 各ビットを真偽値として解釈した結果を <see cref="bool"/> 配列として取得します。
        /// 下位の桁が配列の前に来ます。
        /// </summary>
        public static bool[] ToBoolArray(this int num)
        {
            const int size = sizeof(int) * 8;
            var array = new bool[size];
            for (var i = 0; i < size; i++)
            {
                array[i] = num.GetBit(i + 1) == 1;
            }
            return array;
        }

        /// <summary>
        /// Bit全探索を行います。
        /// </summary>
        /// <param name="count">対象とする要素の個数。30以下である必要があります。</param>
        /// <param name="action">各パターンに対して行う処理</param>
        public static void BitAllSearch(int count, Action<bool[]> action)
        {
            var max = 1 << count;
            for (var i = 0; i < max; i++)
            {
                action.Invoke(i.ToBoolArray());
            }
        }

        /// <summary>
        /// 指定したコレクションに対してBit全探索を行います。
        /// </summary>
        /// <param name="list">対象とする集合。要素数30以下である必要があります。</param>
        /// <param name="action">各パターンに対して行う処理</param>
        public static IEnumerable<IEnumerable<T>> GetAllPatterns<T>(this IReadOnlyList<T> list)
        {
            var max = 1 << list.Count;
            for (var i = 0; i < max; i++)
            {
                var usePattern = i.ToBoolArray();
                IEnumerable<T> query = list.Where((item, j) => usePattern[j]);
                yield return query;
            }
        }
    }

    /// <summary>
    /// 標準入力の高機能な読み取りを提供します。
    /// </summary>
    internal static class MyInputUtil
    {
        private const char separator = ' ';

        /// <summary>
        /// 標準入力から入力を1つ指定の型に変換して読み取ります。
        /// </summary>
        /// <typeparam name="T">変換先として指定する型。</typeparam>
        /// <param name="converter">変換に使用する関数。<c>null</c>の場合は自動決定を試みます。</param>
        /// <returns></returns>
        public static T ReadInput<T>(Func<string, T> converter = null)
        {
            if (converter is null)
            {
                converter = GetConverter<T>();
            }
            return converter.Invoke(Console.ReadLine());
        }

        #region 一定個数の入力の読み取り
        // 用法は ReadInput<T> と共通です。
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T> ReadInput2<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T> ReadInput3<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T> ReadInput4<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T, T> ReadInput5<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]), converter.Invoke(input[4]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T, T, T> ReadInput6<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]), converter.Invoke(input[4]), converter.Invoke(input[5]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T, T, T, T> ReadInput7<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]), converter.Invoke(input[4]), converter.Invoke(input[5]), converter.Invoke(input[6]));
        }
        #endregion

        /// <summary>
        /// 標準入力から配列を読み取ります。
        /// </summary>
        /// <returns></returns>
        public static T[] ReadArray<T>(Func<string, T> converter = null)
        {
            var str = ReadLine().Split(separator);
            if (converter is null)
            {
                converter = GetConverter<T>();
            }
            return str.Select(s => converter.Invoke(s)).ToArray();
        }

        #region 変換用メソッド
        /// <summary>
        /// 文字列を指定の型に変換します。
        /// </summary>
        /// <typeparam name="T">変換先の型。すべての組み込み型がサポートされます。</typeparam>
        /// <param name="str">変換元の文字列</param>
        /// <exception cref="NotSupportedException">指定の型引数がサポートされない場合にスローされる例外。</exception>
        /// <remarks>大量の値を処理する場合、このメソッドは低速です。<see cref="ConvertTo{T}(string[])"/> の使用を検討してください。</remarks>
        public static T ConvertTo<T>(string str)
        {
            return GetConverter<T>().Invoke(str);
        }

        /// <summary>
        /// 文字列配列を指定の型の配列に変換します。
        /// </summary>
        /// <typeparam name="T">変換先の型。すべての組み込み型がサポートされます。</typeparam>
        /// <param name="str">変換元の文字列配列</param>
        /// <exception cref="NotSupportedException">指定の型引数がサポートされない場合にスローされる例外。</exception>
        public static T[] ConvertTo<T>(string[] str)
        {
            var conveter = GetConverter<T>();
            var array = new T[str.Length];
            for (var i = 0; i < str.Length; i++)
            {
                array[i] = conveter.Invoke(str[i]);
            }
            return array;
        }

        private static Func<string, T> GetConverter<T>() => GetConverterOrDefault<T>() ?? throw new NotSupportedException("この型に対する変換関数の取得はサポートされていません");
        private static bool TryGetConverter<T>(out Func<string, T> func)
        {
            func = GetConverterOrDefault<T>();
            return !(func is null);
        }

        private static Func<string, T> GetConverterOrDefault<T>()
        {
            Type type = typeof(T);
            Func<string, T> conveter;

            // 出現頻度が高そうなものを上
            if (type == typeof(int))
            {
                conveter = str => (T)(object)int.Parse(str);
            }
            else if (type == typeof(long))
            {
                conveter = str => (T)(object)long.Parse(str);
            }
            else if (type == typeof(double))
            {
                conveter = str => (T)(object)double.Parse(str);
            }
            else if (type == typeof(decimal))
            {
                conveter = str => (T)(object)decimal.Parse(str);
            }
            else if (type == typeof(float))
            {
                conveter = str => (T)(object)float.Parse(str);
            }
            else if (type == typeof(char))
            {
                conveter = str => (T)(object)str[0];
            }
            else if (type == typeof(short))
            {
                conveter = str => (T)(object)short.Parse(str);
            }
            else if (type == typeof(byte))
            {
                conveter = str => (T)(object)byte.Parse(str);
            }
            else if (type == typeof(bool))
            {
                conveter = str => (T)(object)bool.Parse(str);
            }
            else if (type == typeof(ulong))
            {
                conveter = str => (T)(object)ulong.Parse(str);
            }
            else if (type == typeof(uint))
            {
                conveter = str => (T)(object)uint.Parse(str);
            }
            else if (type == typeof(ushort))
            {
                conveter = str => (T)(object)ushort.Parse(str);
            }
            else if (type == typeof(sbyte))
            {
                conveter = str => (T)(object)sbyte.Parse(str);
            }
            else if (type == typeof(string))
            {
                conveter = str => (T)(object)str;
            }
            else if (type == typeof(object))
            {
                conveter = str => (T)(object)str;
            }
            else
            {
                conveter = null;
            }

            return conveter;
        }
        #endregion
    }
}