stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);++i)

using namespace std;

typedef long long ll;



int main() {

	ios::sync_with_stdio(false);

	cin.tie(0);

	int n, q;

	string S;

	cin >> n >> q >> S;

	int l, r;

	vector<int> rem(100000, 0);

	for (int i = 1; i < n; ++i) {

		rem[i] = rem[i - 1];

		if (S[i] == 'C' && S[i - 1] == 'A') {

			rem[i]++;

		}

	}

	rep(i, q) {

		cin >> l >> r;

		cout << rem[r - 1] - rem[l - 1] << endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3929033634574934x):
#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);++i)

#define rrep(i,n) for(int i=1;i<(n);++i)

#define all(a) (a).begin(),(a).end()

#define rall(a) (a).rbegin(),(a).rend()

#define maxs(a, b) a = max(a, b)

#define mins(a, b) a = min(a, b)

using namespace std;

typedef long long ll;

typedef pair<int, int> P;

const ll linf = (1ll << 61);

const int inf = 1001001001;

const int mod = 1000000007;



int main() {

	ios::sync_with_stdio(false);

	cin.tie(0);

	int n, q;

	cin >> n >> q;

	string s;

	cin >> s;

	vector<int> sum1(n + 1), sum2(n + 1);

	for (int i = 0; i < n - 1; ++i) {

		if (s.substr(i, 2) == "AC") {

			sum1[i + 1]++;

		}

		sum1[i + 1] += sum1[i];

	}

	while (q--) {

		int l, r;

		cin >> l >> r;

		l--; r--;

		int ans = sum1[r] - sum1[l];

		printf("%d\n", ans);

	}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define rep(i,n) for(signed i=0;i<n;++i)

#define repi(n) rep(i,n)

#define int long long

#define pb(a) push_back(a)

#define all(v) v.begin(),v.end()

#define yn(b) cout<<((b)?"Yes":"No")<<endl

#define YN(b) cout<<((b)?"YES":"NO")<<endl

#define call(a) for(auto t:a)cout<<t<<" ";cout<<endl

#define ENDL printf("\n");

#define debg(a) cout<<#a<<":"<<a<<endl;

#define SORT(a) sort(all(a));

#define INF 1LL<<60

#define out(i) cout<<i<<endl;

#define m_p make_pair

typedef long long ll;

typedef string str;

typedef vector<int> V;

typedef pair<int,int> P;

int min(int a,int b){if(a>b)return b;return a;}

int max(int a,int b){if(a>b)return a;return b;}

int gcd(int a,int b){if(b==0)return a; return gcd(b,a%b);}

int lcm(int a,int b){return a/gcd(a,b)*b;}

V sum(114514);

int partsum(int a,int b){

  return sum[b]-sum[a];

}



signed main(){

  int n,m;

  cin>>n>>m;

  str s;

  cin>>s;

  V l(m),r(m);

  rep(i,m)cin>>l[i]>>r[i],l[i]--,r[i]--;

  sum[0]=0;

  rep(i,n-1)if(s[i]=='A'&&s[i+1]=='C')sum[i+1]=1;

  for(int i=1;i<=n;++i)sum[i]+=sum[i-1];

  rep(i,m){

    cout<<partsum(l[i],r[i])<<endl;

  }



  

   return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.9143616676398778x):
#include<bits/stdc++.h>

using namespace std;



signed main(){



  int n,q;

  string s;

  cin>>n>>q>>s;

  vector<int> sum(n+1);

  for(int i=1;i<n;++i){

    if(s[i]=='C'&&s[i-1]=='A')sum[i]++;

    sum[i+1] += sum[i];

  }

  vector<int> ans;

  while(q--){

    int l,r;cin>>l>>r;

    l--;r--;

    ans.emplace_back(sum[r]-sum[l]);

  }

  //for(auto s:sum)cout<<s<<" ";cout<<endl;

  for(auto x:ans){

    cout<<x<<endl;

  }



}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int main(){

    int count[100000]={0};

    int n,q;

    cin>>n>>q;

    string s;

    cin>>s;

    for(int i=1;i<n;i++){

        count[i]=count[i-1];

        if(s[i-1]=='A'&&s[i]=='C')count[i]=count[i-1]+1;

        //cout<<count[i]<<" ";

    }

    //cout<<endl;

    for(int i=0;i<q;i++){

        int l,r;

        cin>>l>>r;

        cout<<count[r-1]-count[l-1]<<endl;

    }

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.3886870844735757x):
#include<bits/stdc++.h>

using namespace std;

int main(){

    int n,q;

    cin>>n>>q;

    string s;

    cin>>s;

    vector<int> count(n,0);

    bool flag1=false,flag2=false;

    for(int i=1;i<n;i++){

        count[i]=count[i-1];

        if(s[i-1]=='A'&&s[i]=='C'){

            count[i]++;

        }

    }

    for(int i=0;i<q;i++){

        int l,r;

        cin>>l>>r;

        cout<<count[r-1]-count[l-1]<<endl;



    }

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int main(){

  int n, q;

  cin >> n >> q;

  string s;

  cin >> s;

  vector<int> a(100005,0);

  int sum = 0;

  for(int i=0; i<n-1; i++){

    if(s.at(i) == 'A' && s.at(i+1) == 'C'){

      sum++;

    }

    a.at(i+1) = sum;

  }

  for(int i=0; i<q; i++){

    int l,r;

    cin >> l >> r;

    l--;

    r--;

    cout << a.at(r) - a.at(l) << endl;

  }



  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.398407204467078x):
// #include<iostream>

// #include<vector>

// #include<algorithm>

// #include<map>

// #include<cstring>

// #include<math.h>

// #include<bitset>

// #include<queue>

// #include<set>

// #include<iomanip>

// #include<math.h>

// #include<assert.h>

// #include<string>

// #include<cstdlib>

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

constexpr ll INF = 1LL << 40;

const double INFD = 1e100;

const ll mod = 1000000007;

const double PI = 3.1415926535897932384626433832795028841971;

const int dx[4] = {1, 0, -1, 0};

const int dy[4] = {0, 1, 0, -1};

template<class T> inline bool chmin(T& a, T b) {

    if (a > b) {

        a = b;

        return true;

    }

    return false;

}

template<class T> inline bool chmax(T& a, T b) {

    if (a < b) {

        a = b;

        return true;

    }

    return false;

}

// ----------------------------------------------------------------------------



int main(){

  int N,Q;

  cin >> N >> Q;

  string s;

  cin >> s;

  vector<int> S(N+1,0);

  for(int i=0; i<N; i++){

    S[i+1] = S[i] + (s[i]=='A' && s[i+1]=='C');

  }

  for(int i=0; i<Q; i++){

    int l,r;

    cin >> l >> r;

    l--;

    r--;

    cout << S[r]-S[l] << endl;

  }

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<ll,ll> P;

const ll mod=1000000007;

int main(void){

    int n,q;

    string s;

    cin>>n>>q>>s;

    int AC[100010]={};

    for(int i=0;i<n-1;i++){

        AC[i+1]=AC[i];

        if(s[i]=='A'&&s[i+1]=='C'){

            AC[i+1]++;

        }

    }

    for(int i=0;i<q;i++){

        int L,R;

        cin>>L>>R;

        L--,R--;

        if(L==0) cout<<AC[R]<<endl;

        else cout<<AC[R]-AC[L]<<endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 1.3957918837386816x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<ll,ll> P;

const ll INF=1LL<<60;

const double EPS = 1e-10;

const ll mod=1000000007;

//const ll mod=998244353;

ll a[100200]={};

int main(void){

  ll n,q;

  string s;

  cin>>n>>q>>s;

  for(int i=1;i<n;i++){

    a[i]=a[i-1];

    if(s[i-1]=='A'&&s[i]=='C') a[i]++;

  }

  for(int i=0;i<q;i++){

    ll L,R;

    cin>>L>>R;

    L--,R--;

    if(L==0) cout<<a[R]<<endl;

    else{

      cout<<a[R]-a[L]<<endl;

    }

  }

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define _overload3(_1,_2,_3,name,...) name

#define _rep(i,n) repi(i,0,n)

#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)

#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()

#define PRINT(V) cout << V << "\n"

#define SORT(V) sort((V).begin(),(V).end())

#define RSORT(V) sort((V).rbegin(), (V).rend())

using namespace std;

using ll = long long int;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }

template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

const ll INF = 1e10;

const ll MOD = 1000000007;



int main() {

    int n,q;

    string s;

    cin >> n >> q >> s;

    int count[100005] = {0};

    rep(i,n-1){

        if (s[i] == 'A' && s[i+1] == 'C') count[i+1]++;

    }

    rep(i,n-1) count[i+1] += count[i];

    int l,r;

    rep(i,q){

        cin >> l >> r;

        PRINT(count[r-1]-count[l-1]);

    }

}

// Faster C++ solution for the same problem (measured speedup: 1.3762872356920337x):
#include <iostream>

#include <iomanip>

#include <algorithm>

#include <complex>

#include <utility>

#include <vector>

#include <string>

#include <stack>

#include <queue>

#include <tuple>

#include <cmath>

#include <bitset>

#include <cctype>

#include <set>

#include <map>

#include <unordered_map>

#include <numeric>

#include <functional>

#define _overload3(_1,_2,_3,name,...) name

#define _rep(i,n) repi(i,0,n)

#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)

#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()

#define PRINT(V) cout << V << "\n"

#define SORT(V) sort((V).begin(),(V).end())

#define RSORT(V) sort((V).rbegin(), (V).rend())

using namespace std;

using ll = long long;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }

template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }

template<class itr> void cins(itr first,itr last){

    for (auto i = first;i != last;i++){

        cin >> (*i);

    }

}

template<class itr> void array_output(itr start,itr goal){

    string ans = "",k = " ";

    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;

    if (!ans.empty()) ans.pop_back();

    PRINT(ans);

}

ll gcd(ll a, ll b) {

    return a ? gcd(b%a,a) : b;

}

const ll INF = 1e18;

const ll MOD = 1000000007;

const ll MOD2 = 998244353;

typedef pair<ll,ll> P;

typedef pair<double,double> point;

const ll MAX = 200000;

constexpr ll nx[8] = {1,0,-1,0,1,-1,1,-1};

constexpr ll ny[8] = {0,1,0,-1,1,1,-1,-1};

int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    ll n,q;

    cin >> n >> q;

    string s;

    cin >> s;

    vector<ll> c(n+1,0);

    rep(i,n-1){

        if (s[i] == 'A' && s[i+1] == 'C'){

            c[i+2]++;

        }

    }

    rep(i,n) c[i+1] += c[i];

    rep(i,q){

        ll l,r;

        cin >> l >> r;

        PRINT(c[r]-c[l]);

    }

}
// Slower C++ solution:
#include <iostream>

#include <string>

using namespace std;

int N, Q, l, r, sum[100000]; string S;

int main() {

  cin >> N >> Q >> S;

  for(int i = 1; i < N; ++i) {

    sum[i] = sum[i-1]+(S.substr(i-1,2)=="AC");

  }

  for(int i = 0; i < Q; ++i) {

    cin >> l >> r;

    cout << sum[r-1]-sum[l-1] << endl;

  }

}

// Faster C++ solution for the same problem (measured speedup: 5.252559358024407x):
#include <cstdio>

using namespace std;

int N, Q, l, r, sum[100000]; char S[100000];

int main() {

  scanf("%d %d %s", &N, &Q, &S);

  for(int i = 1; i < N; ++i) {

    sum[i] = sum[i-1]+(S[i-1]=='A'&&S[i]=='C');

  }

  for(int i = 0; i < Q; ++i) {

    scanf("%d %d", &l, &r);

    printf("%d\n", sum[r-1]-sum[l-1]);

  }

}
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <vector>



using namespace std;



int main() {

    int N, Q;

    vector<int> acs;

    bool a;

    scanf("%d %d", &N, &Q); getchar();

    for (int i=0; i<N; i++) {

        int c = getchar();

        if (a && c == 'C') acs.push_back(i-1);

        if (c == 'A') a = true;

        else a = false;

    }

    for (int i=0; i<Q; i++) {

        int l, r;

        scanf("%d %d", &l, &r);

        auto first = lower_bound(acs.begin(), acs.end(), l-1),

             last = lower_bound(acs.begin(), acs.end(), r-1);

        printf("%ld\n", last - first);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.557700454287937x):
#include <cstdio>

#include <algorithm>



#define MAX_N 100000



//累積和で高速化ver



using namespace std;



int main() {

    int N, Q, csum[MAX_N] = {0};

    bool a;

    scanf("%d %d", &N, &Q); getchar();

    for (int i=0; i<N; i++) {

        int c = getchar();

        if (a && c == 'C') csum[i] = csum[i-1] + 1;

        else if (i > 0) csum[i] = csum[i-1];

        if (c == 'A') a = true;

        else a = false;

    }

    for (int i=0; i<Q; i++) {

        int l, r;

        scanf("%d %d", &l, &r);

        printf("%d\n", csum[r-1] - csum[l-1]);

    }

    return 0;

}
// Slower C++ solution:
#include <algorithm>

#include <cassert>

#include <cmath>

#include <climits>

#include <cstdlib>

#include <iostream>

#include <map>

#include <numeric>

#include <queue>

#include <set>

#include <string>

#include <vector>

#define DEBUG 1

constexpr int kMod = 1000000007;

typedef long long LL;



std::string v[100000];

int main() {

  int N, K; std::cin >> N >> K;

  int R, S, P; std::cin >> R >> S >> P;



  for (int i = 0; i < N; ++i) {

    char c; std::cin >> c;

    switch (c) {

      case 'r': v[i%K] += 'p'; break;

      case 's': v[i%K] += 'r'; break;

      case 'p': v[i%K] += 's'; break;

    }

  }



  LL score = 0;

  for (int i = 0; i < K; ++i) {

    for (int j = 1; j < v[i].size(); ++j) {

      if (v[i][j-1] == v[i][j]) v[i][j] = 'x';

    }

    for (char c : v[i]) {

      switch(c) {

        case 'r': score += R; break;

        case 's': score += S; break;

        case 'p': score += P; break;

      }

    }

//    std::cout << v[i] << std::endl;

  }

  std::cout << score << std::endl;

}


// Faster C++ solution for the same problem (measured speedup: 4.306973911971974x):
#include <algorithm>

#include <cassert>

#include <cmath>

#include <climits>

#include <cstdlib>

#include <iostream>

#include <map>

#include <numeric>

#include <queue>

#include <set>

#include <string>

#include <vector>

#define DEBUG 1

constexpr int kMod = 1000000007;

typedef long long LL;



int main() {

  int N, K; std::cin >> N >> K;

  int R, S, P; std::cin >> R >> S >> P;

  std::map<char, LL> score = {

    {'r', P},  {'s', R}, {'p', S}

  };



  std::vector<std::string> T(K, "");

  for (int i = 0; i < N; ++i) {

    char c; std::cin >> c;

    T[i%K] += c;

  }



  LL total_score = 0;

  for (std::string& s : T) {

    total_score += score[s[0]];

    for (int i = 1; i < s.size(); ++i) {

      if (s[i] == s[i-1]) s[i] = 'x';

      else                total_score += score[s[i]];

    }

  }



  std::cout << total_score << std::endl;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define Sheryang main

typedef long long ll;

#pragma GCC optimize(3)

#define HEAP(...) priority_queue<__VA_ARGS__ >

#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >

template<class T> inline T min(T &x,const T &y){return x>y?y:x;}

template<class T> inline T max(T &x,const T &y){return x<y?y:x;}

struct ios

{

    inline char gc()

    {

        static const int IN_LEN=1<<18|1;

        static char buf[IN_LEN],*s,*t;

        return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++;

    }

    template <typename _Tp> inline ios & operator >> (_Tp&x)

    {

        static char ch,sgn; ch = gc(), sgn = 0;

        for(;!isdigit(ch);ch=gc())

        {   

            if(ch==-1)return *this;sgn|=ch=='-';

        }

        for(x=0;isdigit(ch);ch=gc())x=(((x<<2)+x)<<1)+(ch^'0');

        sgn&&(x=-x); return *this;

    }

}io;

const int maxn = 1e5 + 7;

const int mod = 1e9 + 7;

/** keep hungry and keep calm! **/

 

int n,k,cnt;

int R,S,P;

string t;

ll ans;

int vis[100005];

int main()

{

    cin >> n >> k;

    cin >> R >> S >> P;

    cin >> t;

    memset(vis,0,sizeof(vis));//数组清零 

    for(int i=0;i<n;i++)

    {

    	if(i-k >= 0 && vis[i-k] == 1 && t[i] == t[i-k])	continue;

        if(t[i] == 'r')	ans += P;

        if(t[i] == 's')	ans += R;

        if(t[i] == 'p')	ans += S;

        vis[i] = 1;

    }

    cout << ans << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3967757131578173x):
#include <bits/stdc++.h>



using namespace std;



char f[100010];

string t;



int main()

{

    int n,k,r,p,s,i,ans = 0;

    cin >> n >> k;

    cin >> r >> s >> p;

    cin >> t;

    for(i = 0; i < n; i ++)

    {

        if(i < k)

        {

            if(t[i] == 'r') ans += p;

            else if(t[i] == 's') ans += r;

            else if(t[i] == 'p') ans += s;

            f[i] = t[i];

        }

        else if(f[i-k] == t[i]) f[i] = 0;

        else

        {

            if(t[i] == 'r') ans += p;

            else if(t[i] == 's') ans += r;

            else if(t[i] == 'p') ans += s;

            f[i] = t[i];

        }

    }

    cout << ans << endl;

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<string>



int const MAXN = 1e5;

int N, K, R, S, P;

std::string T, RSP = "rsp";

int iRSP[256];

int sRSP[3];

inline bool win(int c, int d) { return (c-d+3)%3 == 1; }



int dp[MAXN][3];

int calc(int n, int d) {

  if(dp[n][d] >= 0) return dp[n][d];

  int c = iRSP[T[n]];

  int res = win(c, d)? sRSP[d]: 0;

  if(n+K < N) res += std::max(calc(n+K, (d+1)%3), calc(n+K, (d+2)%3));

  return dp[n][d] = res;

}



int main() {

  std::cin >> N >> K >> R >> S >> P >> T;

  sRSP[0] = R; sRSP[1] = S; sRSP[2] = P;

  iRSP['r'] = 0; iRSP['s'] = 1; iRSP['p'] = 2;

  std::fill_n(dp[0], MAXN*3, -1);

  int ans = 0;

  for(int i = 0; i < K; ++i) {

    int m = std::max(calc(i, 0), calc(i, 1));

    ans += std::max(m, calc(i, 2));

  }

  std::cout << ans << std::endl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.2020391460675315x):
#include<iostream>

#include<algorithm>

#include<string>



int const MAXN = 1e5;

int N, K, R, S, P;

std::string T, RSP = "rsp";

int iRSP[256];

int sRSP[3];



// greedy

int calc(int n) {

  int c = (iRSP[T[n]]+2)%3;

  int res = sRSP[c];

  while(n+K < N) {

    n += K;

    int newc = (iRSP[T[n]]+2)%3;

    if(c != newc) res += sRSP[c = newc];

     else c = -1;

  }

  return res;

}



int main() {

  std::cin >> N >> K >> R >> S >> P >> T;

  sRSP[0] = R; sRSP[1] = S; sRSP[2] = P;

  iRSP['r'] = 0; iRSP['s'] = 1; iRSP['p'] = 2;

  int ans = 0;

  for(int i = 0; i < K; ++i) ans += calc(i);

  std::cout << ans << std::endl;

  return 0;

}
// Slower C++ solution:
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef pair<ll,ll> pll;

typedef long double ld;

#define mp make_pair

#define f first

#define s second

#define pb push_back



const int N = 1e5 + 5;

const int M = 1e6 + 5;

const int Q = 2e5 + 5;

const int OO = 1e9;

const int mod = 1e9 + 7;



int n,k,cur[4],r[N],s[N],p[N],ans,dp[N][4];

string t;



int solve(int idx, int col) {



    if(idx>=n) return 0;



    int &ret = dp[idx][col];



    if(~ret) return ret;

    

    ret = 0;



    for(int i=1;i<=3;++i) {



        bool ok = 0;



        if(i==1 && t[idx]=='s') ok = 1;

        if(i==2 && t[idx]=='p') ok = 1;

        if(i==3 && t[idx]=='r') ok = 1;

        if(i==0) ok = 1;



        if(col!=i) ret = max(ret,(ok ? cur[i] : 0) +solve(idx+k,i));



    }



    return ret;



}



int main() {



    ios_base::sync_with_stdio(false);

    cin.tie(0);



    cin >> n >> k >> cur[1] >> cur[2] >> cur[3] >> t;



    memset(dp,-1,sizeof(dp));



    for(int i=0;i<k;++i) ans += solve(i,0);



    cout << ans << '\n';



    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.556750348735702x):
//#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef pair<ll,ll> pll;

typedef long double ld;

#define mp make_pair

#define f first

#define s second

#define pb push_back



const int N = 1e5 + 5;

const int M = 201;

const int Q = 2e5 + 5;

const int OO = 1e9;

const int mod = 1e9 + 7;



int n,k,ans;

char lastChar[N];

string t;

map<char,char> winner = {{'s','r'},{'p','s'},{'r','p'}};

map<char, int> profit;



void init() {



}



int main() {



    ios_base::sync_with_stdio(false);

    cin.tie(0);



    cin >> n >> k >> profit['r'] >> profit['s'] >> profit['p'] >> t;



    for(int i=0;i<k;++i) lastChar[i] = '#';



    for(int i=0;i<t.size();++i) {



        char myChar = winner[t[i]];



        if(myChar!=lastChar[i%k]) {

            ans += profit[myChar];

            lastChar[i%k] = myChar;

        } else

            lastChar[i%k] = '#';



    }



    cout << ans << '\n';



    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

using pint = pair<int,int>;

ll mod = 1000000007,mod2 = 998244353;



void chmin(long long &a, long long b) { if (a > b) a = b; }

void chmax(long long &a, long long b) { if (a < b) a = b; }

const long long INF = 1LL<<60;

ll gcd(ll a,ll b){

    if(b == 0) return a;

    else return gcd(b,a%b);

}



//重みある時

using Edge = pair<int,int>;

using Graph = vector<vector<Edge> >;



vector<string> S(100010);  // mod kごとにわける



int main() {

    int n,k,r,s,p;

    string T;

    ll ans = 0;

    cin >> n >> k >> r >> s >> p;

    cin >> T;

    for (int i = 0; i < n; ++i) {

        if(i >= k){

            if(T[i-k] == T[i]){

                T[i] = 'a';

            }else{

                if(T[i] == 'r') ans += p;

                if(T[i] == 's') ans += r;

                if(T[i] == 'p') ans += s;

            }

        }else{

            if(T[i] == 'r') ans += p;

            if(T[i] == 's') ans += r;

            if(T[i] == 'p') ans += s;

        }

    }

    cout << ans << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.470334700260587x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

using pint = pair<int,int>;

ll mod = 1000000007,mod2 = 998244353;



void chmin(long long &a, long long b) { if (a > b) a = b; }

void chmax(long long &a, long long b) { if (a < b) a = b; }

//void chmin(int &a, int b) { if (a > b) a = b; }

//void chmax(int &a, int b) { if (a < b) a = b; }



const long long INF = 1LL<<60;

ll gcd(ll a,ll b){

    if(b == 0) return a;

    else return gcd(b,a%b);

}



//重みある時

using Edge = pair<int,int>;

using Graph = vector<vector<Edge> >;





int main() {

    int n,k,r,s,p;

    ll ans = 0;

    cin >> n >> k >> r >> s >> p;

    string T;

    cin >> T;

    for (int i = 0; i < n; ++i) {

        if(i >= k){

            if(T[i-k] == T[i]) T[i] = '#';

            else{

                if(T[i] == 'r') ans += p;

                if(T[i] == 's') ans += r;

                if(T[i] == 'p') ans += s;

            }

        }else{

            if(T[i] == 'r') ans += p;

            if(T[i] == 's') ans += r;

            if(T[i] == 'p') ans += s;

        }

    }

    cout << ans << endl;

    return 0;

}