Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
/*---------------------DEBUGGING--------------------------------------------*/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif
/*-------------------------------------------------------------------------------------*/
#define mp make_pair
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define pcc pair<char, char>
#define F first
#define S second
#define int long long
#define pi 3.141592653589793238462643383279502
#define M 1000000007
#define rep(i, a, n) for (int i = a; i < n; i++)
#define N 300005
#define vi vector<int>
#define all(v) v.begin(), v.end()

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  int r, s, p;
  cin >> r >> s >> p;
  string st;
  cin >> st;
  string ans = "";
  vector<int> v;
  for (int i = 0; i < st.length(); i++) {
    if (i < k) {
      if (st[i] == 'r')
        ans += 'p';
      if (st[i] == 's')
        ans += 'r';
      if (st[i] == 'p')
        ans += 's';
    } else {
      if (st[i] == 'r') {
        if (ans[i - k] != 'p')
          ans += 'p';
        else {
          if ((i + k) < st.length()) {
            if (st[i + k] == 's')
              ans += 's';
            if (st[i + k] == 'r')
              ans += 'r';
            if (st[i + k] == 'p')
              ans += 'p';
            v.pb(i);
          }
        }
      }

      if (st[i] == 'p') {
        if (ans[i - k] != 's')
          ans += 's';
        else {
          if ((i + k) < st.length()) {

            if (st[i + k] == 's')
              ans += 's';
            if (st[i + k] == 'r')
              ans += 'r';
            if (st[i + k] == 'p')
              ans += 'p';
            v.pb(i);
          }
        }
      }
      if (st[i] == 's') {
        if (ans[i - k] != 'r')
          ans += 'r';
        else {
          if ((i + k) < st.length()) {

            if (st[i + k] == 's')
              ans += 's';
            if (st[i + k] == 'r')
              ans += 'r';
            if (st[i + k] == 'p')
              ans += 'p';
            v.pb(i);
          }
        }
      }
    }
  }
  int sum = 0;
  int j = 0;
  for (int i = 0; i < ans.length(); i++) {
    if (i == v[j] && j < v.size()) {
      j++;
      continue;
    }
    if (ans[i] == 'r')
      sum += r;
    if (ans[i] == 's')
      sum += s;
    if (ans[i] == 'p')
      sum += p;
  }
  cout << sum << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
/*---------------------DEBUGGING--------------------------------------------*/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif
/*-------------------------------------------------------------------------------------*/
#define mp make_pair
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define pcc pair<char, char>
#define F first
#define S second
#define int long long
#define pi 3.141592653589793238462643383279502
#define M 1000000007
#define rep(i, a, n) for (int i = a; i < n; i++)
#define N 300005
#define vi vector<int>
#define all(v) v.begin(), v.end()

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  int r, s, p;
  cin >> r >> s >> p;
  string st;
  cin >> st;
  string ans = "";
  vector<int> v;
  for (int i = 0; i < st.length(); i++) {
    if (i < k) {
      if (st[i] == 'r')
        ans += 'p';
      if (st[i] == 's')
        ans += 'r';
      if (st[i] == 'p')
        ans += 's';
    } else {
      if (st[i] == 'r') {
        if (ans[i - k] != 'p')
          ans += 'p';
        else {
          if ((i + k) < st.length()) {
            if (st[i + k] == 's')
              ans += 's';
            if (st[i + k] == 'r')
              ans += 'r';
            if (st[i + k] == 'p')
              ans += 'p';
            v.pb(i);
          }
        }
      }

      if (st[i] == 'p') {
        if (ans[i - k] != 's')
          ans += 's';
        else {
          if ((i + k) < st.length()) {

            if (st[i + k] == 's')
              ans += 's';
            if (st[i + k] == 'r')
              ans += 'r';
            if (st[i + k] == 'p')
              ans += 'p';
            v.pb(i);
          }
        }
      }
      if (st[i] == 's') {
        if (ans[i - k] != 'r')
          ans += 'r';
        else {
          if ((i + k) < st.length()) {

            if (st[i + k] == 's')
              ans += 's';
            if (st[i + k] == 'r')
              ans += 'r';
            if (st[i + k] == 'p')
              ans += 'p';
            v.pb(i);
          }
        }
      }
    }
  }
  int sum = 0;
  int j = 0;
  for (int i = 0; i < ans.length(); i++) {
    if (j < v.size()) {
      if (i == v[j]) {
        j++;
        continue;
      }
    }
    if (ans[i] == 'r')
      sum += r;
    if (ans[i] == 's')
      sum += s;
    if (ans[i] == 'p')
      sum += p;
  }
  cout << sum << endl;
}