Language: C++

#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
#pragma GCC optimize("O3")
#define REP(i,n) for(int i=0;i<n;i++)
#define REPP(i,n) for(int i=1;i<=n;i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
//const double PI = acos(-1);
//const double EPS = 1e-15;
//long long INF=(long long)1E17;
#define i_7 (long long)(1e9+7)
//#define i_7 998'244'353
long mod(long a){
    long long c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
long long po(long a, long b){
    if(b==0){
        return 1;
    }
    
    long long z = po(a,b/2);
    z = mod(z*z);
    if(b%2!=0){
        z = mod(a*z);
    }
    return z;
}
using namespace std;
//using namespace boost::multiprecision;

bool prime_(int n){
  if(n==1){
    return false;
  }else if(n==2){
    return true;
  }else{
    for(int i=2;i<=sqrt(n);i++){
      if(n%i==0){
        return false;
      }
    }
    return true;
  }
}

long long gcd_(long long a, long long b){
  if(a<b){
    swap(a,b);
  }
  if(a%b==0){
    return b;
  }else{
    return gcd_(b,a%b);
  }
}
 
long long lcm_(long long x, long long y){
  return (x/gcd_(x,y))*y;
}



int main(){
  
  int n,k;
  cin>>n>>k;
  long long r,s,p;
  cin>>r>>s>>p;
  string t;
  cin>>t;
  long long dp[n][4][4][4] = {};
  if(t[0] == 's'){
    dp[0][0][0][1] = r;
  }else if(t[0] == 'p'){
    dp[0][0][0][2] = s;
  }else{
    dp[0][0][0][3] = p;
  }
  REP(i,n){
    if(i == 0)continue;
    if(i < k-1){
      if(t[i] == 's'){
        for(int now = 1; now<=3; now++){
          dp[i][0][0][1] = max(dp[i][0][0][1], dp[i-1][0][0][now] + r);
        }
      }else if(t[i] == 'p'){
        for(int now = 1; now<=3; now++){
          dp[i][0][0][2] = max(dp[i][0][0][2], dp[i-1][0][0][now] + s);
        }
      }else{
        for(int now = 1; now<=3; now++){
          dp[i][0][0][3] = max(dp[i][0][0][3], dp[i-1][0][0][now] + p);
        }
      }
    }else if(i < k){
      for(int j = 1; j<= 3; j++){
        if(t[i] == 's'){
          for(int now = 1; now<=3; now++){
            dp[i][j][0][1] = max(dp[i][j][0][1], dp[i-1][0][0][now] + r);
          }
        }else if(t[i] == 'p'){
          for(int now = 1; now<=3; now++){
            dp[i][j][0][2] = max(dp[i][j][0][2], dp[i-1][0][0][now] + s);
          }
        }else{
          for(int now = 1; now<=3; now++){
            dp[i][j][0][3] = max(dp[i][j][0][3], dp[i-1][0][0][now] + p);
          }
        }
      }
    }else{
      for(int j = 1; j<= 3; j++){
        for(int z = 1; z<=3; z++){
          for(int now = 1; now <=3; now++){
            for(int zz = 0; zz<=3; zz++){
              for(int c = 1; c<=3; c++){
                if(z == now || zz == c)continue;
                if(now == 1 && s[i] == 's'){
                  dp[i][j][z][now] = max(dp[i][j][z][now], dp[i-1][z][zz][c] + r);
                }else if(now == 2 && s[i] == 'p'){
                  dp[i][j][z][now] = max(dp[i][j][z][now], dp[i-1][z][zz][c] + s);
                }else if(now == 3 && s[i] == 'r'){
                  dp[i][j][z][now] = max(dp[i][j][z][now], dp[i-1][z][zz][c] + p);
                }else{
                  dp[i][j][z][now] = max(dp[i][j][z][now], dp[i-1][z][zz][c]);
                }
              }
            }
          }
        }
      }
    }
  }            
  long long ans = 0;
  for(int j = 1; j<= 3; j++){
    for(int z = 1; z<=3; z++){
      for(int now = 1; now<=3; now++){
        ans = max(ans, dp[n-1][j][z][now]);
      }
    }
  }
  cout<<ans<<endl;    
  return 0; 
}


Compiler output:
a.cc: In function 'int main()':
a.cc:122:33: error: invalid types 'long long int[int]' for array subscript
  122 |                 if(now == 1 && s[i] == 's'){
      |                                 ^
a.cc:124:39: error: invalid types 'long long int[int]' for array subscript
  124 |                 }else if(now == 2 && s[i] == 'p'){
      |                                       ^
a.cc:126:39: error: invalid types 'long long int[int]' for array subscript
  126 |                 }else if(now == 3 && s[i] == 'r'){
      |                                       ^