Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N, K;
  cin >> N >> K;
  ll R, S, P;
  cin >> R >> S >> P;
  string s;
  cin >> s;

  // 勝てる数を手ごとに保存
  vector<ll> v(3);

  rep(i, K) {
    ll prev = 3;
    // iはそれぞれの先頭
    for (int j = 0; j < N; j += K) {
      // cout<<i+j<<endl;
      // jはkことばし
      if (s[i + j] == 'r') {
        // paper win
        if (prev != 0) {
          v[2]++;
          prev = 0;
        } else
          prev = 3;
      } else if (s[i + j] == 's') {
        // rock win
        if (prev != 1) {
          v[0]++;
          prev = 1;
        } else
          prev = 3;
      } else {
        // scissors win
        if (prev != 2) {
          v[1]++;
          prev = 2;
        } else
          prev = 3;
      }
    }
  }
  ll ans = 0;
  // rep(i,3)cout<<v[i]<<endl;
  ans += v[0] * R + v[1] * S + v[2] * P;
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main() {
  ll N, K;
  cin >> N >> K;
  ll R, S, P;
  cin >> R >> S >> P;
  string s;
  cin >> s;

  // 勝てる数を手ごとに保存
  vector<ll> v(3);

  rep(i, K) {
    ll prev = 3;
    // iはそれぞれの先頭
    for (int j = 0; i + j < N; j += K) {
      // cout<<i+j<<endl;
      // jはkことばし
      if (s[i + j] == 'r') {
        // paper win
        if (prev != 0) {
          v[2]++;
          prev = 0;
        } else
          prev = 3;
      } else if (s[i + j] == 's') {
        // rock win
        if (prev != 1) {
          v[0]++;
          prev = 1;
        } else
          prev = 3;
      } else {
        // scissors win
        if (prev != 2) {
          v[1]++;
          prev = 2;
        } else
          prev = 3;
      }
    }
  }
  ll ans = 0;
  // rep(i,3)cout<<v[i]<<endl;
  ans += v[0] * R + v[1] * S + v[2] * P;
  cout << ans << endl;
}