Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MAX = 5100000;
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// 以下から
const int MAX_N = 100000;
struct Edge {
  int to;
  int id;
};

vector<int> to[100005];
int n;
vector<int> dist;
void dfs(int v, int d = 0, int p = -1) {
  dist[v] = d;
  for (int u : to[v]) {
    if (u == p)
      continue;
    dfs(u, d + 1, v);
  }
}

vector<int> calcDist(int s) {
  dist = vector<int>(n);
  dfs(s);
  return dist;
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll r, s, p;
  cin >> r >> s >> p;
  string t;
  cin >> t;

  ll sum = 0;
  rep(i, t.size()) {
    if (t[i] == t[i + k]) {
      t[i + k] = 'x';
    }
    switch (t[i]) {
    case 'r':
      sum += p;
      break;
    case 's':
      sum += r;
      break;
    case 'p':
      sum += s;
    default:
      break;
    }
  }
  cout << sum << endl;
}

// int main() {
//     ll n, m;
//     vector<ll> v;
//     rep(i,n) {
//         ll a; cin >> a;
//         v.push_back(a);
//     }
// }


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MAX = 5100000;
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// 以下から
const int MAX_N = 100000;
struct Edge {
  int to;
  int id;
};

vector<int> to[100005];
int n;
vector<int> dist;
void dfs(int v, int d = 0, int p = -1) {
  dist[v] = d;
  for (int u : to[v]) {
    if (u == p)
      continue;
    dfs(u, d + 1, v);
  }
}

vector<int> calcDist(int s) {
  dist = vector<int>(n);
  dfs(s);
  return dist;
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll r, s, p;
  cin >> r >> s >> p;
  string t;
  cin >> t;

  ll sum = 0;
  rep(i, t.size()) {
    if ((i + k) < n) {
      if (t[i] == t[i + k]) {
        t[i + k] = 'x';
      }
    }
    switch (t[i]) {
    case 'r':
      sum += p;
      break;
    case 's':
      sum += r;
      break;
    case 'p':
      sum += s;
    default:
      break;
    }
  }
  cout << sum << endl;
}

// int main() {
//     ll n, m;
//     vector<ll> v;
//     rep(i,n) {
//         ll a; cin >> a;
//         v.push_back(a);
//     }
// }