prithivMLmods/Coder-Stat huggingface dataset:
Problem p02820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define puts(i) cout << i << endl
#define INF INT_MAX
#define INFL LLONG_MAX
typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  else
    return gcd(a, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

int r, p, s;

int point(char c) {
  int add = 0;
  if (c == 'r')
    add = p;
  if (c == 'p')
    add = s;
  if (c == 's')
    add = r;
  if (c == 'n')
    add = 0;

  return add;
}

int main() {
  int n, k;
  cin >> n >> k >> r >> s >> p;
  string t;
  cin >> t;
  ll ans = 0;
  for (int i = 0; i < n - k; i++) {
    for (int j = 1; k * j + i < n; j++) {
      if (t.at(i + k * (j - 1)) == t.at(i + k * j))
        t.at(i + j * k) = 'n';
    }
  }

  rep(i, n) { ans += point(t.at(i)); }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define puts(i) cout << i << endl
#define INF INT_MAX
#define INFL LLONG_MAX
typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  else
    return gcd(a, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

int r, p, s;

int point(char c) {
  int add = 0;
  if (c == 'r')
    add = p;
  if (c == 'p')
    add = s;
  if (c == 's')
    add = r;
  if (c == 'n')
    add = 0;

  return add;
}

int main() {
  int n, k;
  cin >> n >> k >> r >> s >> p;
  string t;
  cin >> t;
  ll ans = 0;
  for (int i = k; i < n; i++) {
    if (t.at(i - k) == t.at(i))
      t.at(i) = 'n';
  }

  rep(i, n) { ans += point(t.at(i)); }

  cout << ans << endl;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bitset>
#include <iostream>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;

int main() {
  int N, K, R, S, P;
  cin >> N >> K >> R >> S >> P;
  string T;
  cin >> T;
  bitset<11000> win;
  REP(i, N) if (i < K || T[i - K] != T[i] || !win[i - K]) win[i] = true;
  ll ans = 0;
  REP(i, N) if (win[i]) {
    if (T[i] == 's')
      ans += R;
    if (T[i] == 'p')
      ans += S;
    if (T[i] == 'r')
      ans += P;
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bitset>
#include <iostream>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;

int main() {
  int N, K, R, S, P;
  cin >> N >> K >> R >> S >> P;
  string T;
  cin >> T;
  bitset<110000> win;
  REP(i, N) if (i < K || T[i - K] != T[i] || !win[i - K]) win[i] = true;
  ll ans = 0;
  REP(i, N) if (win[i]) {
    if (T[i] == 's')
      ans += R;
    if (T[i] == 'p')
      ans += S;
    if (T[i] == 'r')
      ans += P;
  }

  cout << ans << endl;
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n, k;
  cin >> n >> k;
  int r, s, p;
  cin >> r >> s >> p;
  string t;
  cin >> t;
  vector<char> tt(n);
  rep(i, n) {
    if (t[i] == 'r')
      tt[i] = 'p';
    if (t[i] == 's')
      tt[i] = 'r';
    if (t[i] == 'p')
      tt[i] = 's';
  }
  ll sum = 0;
  rep(i, n) {
    if (tt[i - k] == tt[i] && i - k >= 0) {
      tt[i] = -1;
      continue;
    }
    if (tt[i] == 'r')
      sum += r;
    if (tt[i] == 's')
      sum += s;
    if (tt[i] == 'p')
      sum += p;
  }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n, k;
  cin >> n >> k;
  int r, s, p;
  cin >> r >> s >> p;
  string t;
  cin >> t;
  vector<char> tt(n);
  rep(i, n) {
    if (t[i] == 'r')
      tt[i] = 'p';
    if (t[i] == 's')
      tt[i] = 'r';
    if (t[i] == 'p')
      tt[i] = 's';
  }
  ll sum = 0;
  rep(i, n) {
    if (i - k >= 0) {
      if (tt[i - k] == tt[i]) {
        tt[i] = -1;
        continue;
      }
    }
    if (tt[i] == 'r')
      sum += r;
    if (tt[i] == 's')
      sum += s;
    if (tt[i] == 'p')
      sum += p;
  }
  cout << sum << endl;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define itn int
#define rep(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>

long long dp[100000][4];

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

  // r = 0,s = 1,p = 2
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (j != 0)
        dp[i + k][j] = max(dp[i + k][j], dp[i][0]);
      if (j != 1)
        dp[i + k][j] = max(dp[i + k][j], dp[i][1]);
      if (j != 2)
        dp[i + k][j] = max(dp[i + k][j], dp[i][2]);
    }
    if (t[i] == 'r')
      dp[i + k][2] += p;
    if (t[i] == 's')
      dp[i + k][0] += r;
    if (t[i] == 'p')
      dp[i + k][1] += s;
  }

  long long ans = 0;
  for (int i = n; i < n + k; i++) {
    ans += max(dp[i][0], max(dp[i][1], dp[i][2]));
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define itn int
#define rep(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>

long long dp[200000][4];

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

  // r = 0,s = 1,p = 2
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (j != 0)
        dp[i + k][j] = max(dp[i + k][j], dp[i][0]);
      if (j != 1)
        dp[i + k][j] = max(dp[i + k][j], dp[i][1]);
      if (j != 2)
        dp[i + k][j] = max(dp[i + k][j], dp[i][2]);
    }
    if (t[i] == 'r')
      dp[i + k][2] += p;
    if (t[i] == 's')
      dp[i + k][0] += r;
    if (t[i] == 'p')
      dp[i + k][1] += s;
  }

  long long ans = 0;
  for (int i = n; i < n + k; i++) {
    ans += max(dp[i][0], max(dp[i][1], dp[i][2]));
  }
  cout << ans << endl;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

char win(char s) {
  switch (s) {
  case 'r':
    return 'p';
  case 's':
    return 'r';
  case 'p':
    return 's';
  default:
    return 'q';
  }
}

int main(void) {
  int n, k, r, s, p;
  char t[10000];
  cin >> n >> k >> r >> s >> p;
  for (int i = 0; i < n; ++i) {
    cin >> t[i];
  }
  ll score = 0;
  for (int i = 0; i < n; ++i) {
    switch (win(t[i])) {
    case 'r':
      if (i < k || t[i] != t[i - k]) {
        score += r;
      } else {
        t[i] = 'q';
      }
      break;
    case 's':
      if (i < k || t[i] != t[i - k]) {
        score += s;
      } else {
        t[i] = 'q';
      }
      break;
    case 'p':
      if (i < k || t[i] != t[i - k]) {
        score += p;
      } else {
        t[i] = 'q';
      }
      break;
    }
  }
  cout << score;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

char win(char s) {
  switch (s) {
  case 'r':
    return 'p';
  case 's':
    return 'r';
  case 'p':
    return 's';
  default:
    return 'q';
  }
}

int main(void) {
  int n, k, r, s, p;
  char t[100000];
  cin >> n >> k >> r >> s >> p;
  for (int i = 0; i < n; ++i) {
    cin >> t[i];
  }
  ll score = 0;
  for (int i = 0; i < n; ++i) {
    switch (win(t[i])) {
    case 'r':
      if (i < k || t[i] != t[i - k]) {
        score += r;
      } else {
        t[i] = 'q';
      }
      break;
    case 's':
      if (i < k || t[i] != t[i - k]) {
        score += s;
      } else {
        t[i] = 'q';
      }
      break;
    case 'p':
      if (i < k || t[i] != t[i - k]) {
        score += p;
      } else {
        t[i] = 'q';
      }
      break;
    }
  }
  cout << score;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {

  int n, k;
  int r, s, p;
  cin >> n >> k;
  cin >> r >> s >> p;
  char T[n + 1];
  cin >> T;

  int i, ans;
  ans = 0;
  for (i = 0; i <= n; i++) {
    if (i - k + 1 > 0 and T[i] == T[i - k]) {

      if (T[i] == T[i + k] and T[i + k] == 'r')
        T[i] = 's';
      else if (T[i] == T[i + k] and T[i + k] == 's')
        T[i] = 'p';
      else if (T[i] == T[i + k] and T[i + k] == 'p')
        T[i] = 'r';

    } else {
      if (T[i] == 'r')
        ans = ans + p;
      else if (T[i] == 's')
        ans = ans + r;
      else if (T[i] == 'p')
        ans = ans + s;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {

  int n, k;
  int r, s, p;
  cin >> n >> k;
  cin >> r >> s >> p;
  char T[n + 1 + k];
  cin >> T;

  int i, ans;
  ans = 0;
  for (i = 0; i <= n; i++) {
    if (i - k + 1 > 0 and T[i] == T[i - k]) {

      if (T[i] == T[i + k] and T[i + k] == 'r')
        T[i] = 's';
      else if (T[i] == T[i + k] and T[i + k] == 's')
        T[i] = 'p';
      else if (T[i] == T[i + k] and T[i + k] == 'p')
        T[i] = 'r';

    } else {
      if (T[i] == 'r')
        ans = ans + p;
      else if (T[i] == 's')
        ans = ans + r;
      else if (T[i] == 'p')
        ans = ans + s;
    }
  }

  cout << ans << endl;
}
Problem p02820 (C++)

Buggy submission (status: Time Limit Exceeded):
#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];

  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;
}

Fixed submission:
#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;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define S second
#define F first

using namespace std;

const int N = 3e3 + 15, M = 2e5 + 5;

#define arr(t, n, s, e) t _##n[e - s + 1], *n = _##n + (-s);

ll n, k, arr[3];
char str[N], alpha[] = "rspx", opp[] = "sprx";
ll dp[N][5];

ll solve(int ind, int prv) {
  if (ind >= n)
    return 0;
  ll &ret = dp[ind][prv];
  if (~ret)
    return ret;
  ret = 0;
  for (int i = 0; i < 3; i++) {
    if (prv != i)
      ret = max(ret, solve(ind + k, i) + arr[i] * (str[ind] == opp[i]));
  }
  return ret;
}

int main() {
  memset(dp, -1, sizeof dp);
  scanf("%lld%lld%lld%lld%lld%s", &n, &k, arr, arr + 1, arr + 2, str);
  ll ans = 0;
  for (int i = 0; i < k; i++)
    ans += solve(i, 3);
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define S second
#define F first

using namespace std;

const int N = 2e5 + 15, M = 2e5 + 5;

#define arr(t, n, s, e) t _##n[e - s + 1], *n = _##n + (-s);

ll n, k, arr[3];
char str[N], alpha[] = "rspx", opp[] = "sprx";
ll dp[N][5];

ll solve(int ind, int prv) {
  if (ind >= n)
    return 0;
  ll &ret = dp[ind][prv];
  if (~ret)
    return ret;
  ret = 0;
  for (int i = 0; i < 3; i++) {
    if (prv != i)
      ret = max(ret, solve(ind + k, i) + arr[i] * (str[ind] == opp[i]));
  }
  return ret;
}

int main() {
  memset(dp, -1, sizeof dp);
  scanf("%lld%lld%lld%lld%lld%s", &n, &k, arr, arr + 1, arr + 2, str);
  ll ans = 0;
  for (int i = 0; i < k; i++)
    ans += solve(i, 3);
  printf("%lld\n", ans);
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int r, s, p;
  scanf("%d%d%d", &r, &s, &p);
  std::string t;
  cin >> t;

  // 0: r
  // 1: s
  // 2: p
  auto calc = [&](char c, int x) {
    if (c == 'r')
      return (x == 2 ? p : 0);
    if (c == 's')
      return (x == 0 ? r : 0);
    if (c == 'p')
      return (x == 1 ? s : 0);
  };

  int ans = 0;
  for (int offset = 0; offset < k; offset++) {
    int add = 0;
    auto dp = make_v<int>(n + 1, 3);
    for (int i = 0; offset + i * k < n; i++) {
      int idx = offset + i * k;

      for (int j = 0; j < 3; j++) {
        int from = dp[i][j];

        for (int k = 0; k < 3; k++) {
          if (j == k)
            continue;

          dp[i + 1][k] = std::max(dp[i + 1][k], from + calc(t[idx], k));
          add = std::max(add, dp[i + 1][k]);
        }
      }
    }

    ans += add;
  }

  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int r, s, p;
  scanf("%d%d%d", &r, &s, &p);
  std::string t;
  cin >> t;

  // 0: r
  // 1: s
  // 2: p
  auto calc = [&](char c, int x) {
    if (c == 'r')
      return (x == 2 ? p : 0);
    if (c == 's')
      return (x == 0 ? r : 0);
    if (c == 'p')
      return (x == 1 ? s : 0);
  };

  int ans = 0;
  for (int offset = 0; offset < k; offset++) {
    int add = 0, tmp = n / k + 5;
    auto dp = make_v<int>(tmp, 3);
    for (int i = 0; offset + i * k < n; i++) {
      int idx = offset + i * k;

      for (int j = 0; j < 3; j++) {
        int from = dp[i][j];

        for (int k = 0; k < 3; k++) {
          if (j == k)
            continue;

          dp[i + 1][k] = std::max(dp[i + 1][k], from + calc(t[idx], k));
          add = std::max(add, dp[i + 1][k]);
        }
      }
    }

    ans += add;
  }

  printf("%d\n", ans);
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  int n, k, r, s, p, i;
  string a;
  cin >> n >> k >> r >> s >> p;
  int dp[10000][3] = {0};
  cin >> a;
  ll ans = 0;
  for (i = 0; i < k; i++) {
    if (a[i] == 'r') {
      ans += p;
      dp[i][2] = 1;
    } else if (a[i] == 's') {
      ans += r;
      dp[i][0] = 1;
    } else {
      ans += s;
      dp[i][1] = 1;
    }
  }
  for (i = k; i < a.size(); i++) {
    if (a[i] == 'r' && dp[i - k][2] == 0) {
      ans += p;
      dp[i][2] = 1;
    } else if (a[i] == 's' && dp[i - k][0] == 0) {
      ans += r;
      dp[i][0] = 1;
    } else if (a[i] == 'p' && dp[i - k][1] == 0) {
      ans += s;
      dp[i][1] = 1;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  int n, k, r, s, p, i;
  string a;
  cin >> n >> k >> r >> s >> p;
  int dp[100005][3] = {0};
  cin >> a;
  ll ans = 0;
  for (i = 0; i < k; i++) {
    if (a[i] == 'r') {
      ans += p;
      dp[i][2] = 1;
    } else if (a[i] == 's') {
      ans += r;
      dp[i][0] = 1;
    } else {
      ans += s;
      dp[i][1] = 1;
    }
  }
  for (i = k; i < a.size(); i++) {
    if (a[i] == 'r' && dp[i - k][2] == 0) {
      ans += p;
      dp[i][2] = 1;
    } else if (a[i] == 's' && dp[i - k][0] == 0) {
      ans += r;
      dp[i][0] = 1;
    } else if (a[i] == 'p' && dp[i - k][1] == 0) {
      ans += s;
      dp[i][1] = 1;
    }
  }
  cout << ans << endl;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

using ll = long long;
const int INF = 1e9 + 1;

int main() {

  int N, K, R, S, P;
  cin >> N >> K >> R >> S >> P;
  string T;
  cin >> T;

  vector<bool> flag(N);

  ll ans = 0;

  rep(i, T.size()) {
    if (T[i] != T[i - K]) {
      if (T[i] == 'r')
        ans += P;
      else if (T[i] == 's')
        ans += R;
      else
        ans += S;
      flag[i] = true;
    } else {
      if (!flag[i - K]) {
        if (T[i] == 'r')
          ans += P;
        else if (T[i] == 's')
          ans += R;
        else
          ans += S;
        flag[i] = true;
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

using ll = long long;
const int INF = 1e9 + 1;

int main() {

  int N, K, R, S, P;
  cin >> N >> K >> R >> S >> P;
  string T;
  cin >> T;

  vector<bool> flag(N);

  ll ans = 0;

  rep(i, T.size()) {
    if (i - K < 0 || T[i] != T[i - K]) {
      if (T[i] == 'r')
        ans += P;
      else if (T[i] == 's')
        ans += R;
      else
        ans += S;
      flag[i] = true;
    } else {
      if (!flag[i - K]) {
        if (T[i] == 'r')
          ans += P;
        else if (T[i] == 's')
          ans += R;
        else
          ans += S;
        flag[i] = true;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define repr(i, from, to) for (int(i) = (from); (i) < (to); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
#define vprint(x)                                                              \
  for (auto a : (x))                                                           \
    cout << x << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
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(void) {
  ll n, k, score[3];
  string t;
  cin >> n >> k >> score[0] >> score[1] >> score[2] >> t;
  map<char, int> mp;
  mp['r'] = score[2];
  mp['s'] = score[0];
  mp['p'] = score[1];
  vector<bool> chk(n, false);
  cin.tie(0);
  ios::sync_with_stdio(false);
  int res = 0;
  rep(i, n) {
    if (n <= k) {
      res += mp[t[i]];
      chk[i] = true;
    } else {
      if (t[i] != t[i - k] || !chk[i - k]) {
        res += mp[t[i]];
        chk[i] = true;
      }
    }
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define repr(i, from, to) for (int(i) = (from); (i) < (to); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
#define vprint(x)                                                              \
  for (auto a : (x))                                                           \
    cout << x << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
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(void) {
  ll n, k, score[3];
  string t;
  cin >> n >> k >> score[0] >> score[1] >> score[2] >> t;
  map<char, int> mp;
  mp['r'] = score[2];
  mp['s'] = score[0];
  mp['p'] = score[1];
  vector<bool> chk(n, false);
  cin.tie(0);
  ios::sync_with_stdio(false);
  int res = 0;
  rep(i, n) {
    if (i < k) {
      res += mp[t[i]];
      chk[i] = true;
    } else {
      if (t[i] != t[i - k] || !chk[i - k]) {
        res += mp[t[i]];
        chk[i] = true;
      }
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007    // 10^9+7
#define INF 1000000000000 // 10^12
#define P pair<int, int>
int n, k;
int r, s, p;
string t;
char c[20000];
int ans = 0;
signed main() {
  cin >> n >> k;
  cin >> r >> s >> p;
  cin >> t;
  rep(i, n) {
    if (i < k) {
      if (t[i] == 'r') {
        c[i] = 'p';
        ans += p;
      } else if (t[i] == 's') {
        c[i] = 'r';
        ans += r;
      } else {
        c[i] = 's';
        ans += s;
      }
    } else {
      if (t[i] == 'r') {
        if (c[i - k] != 'p') {
          c[i] = 'p';
          ans += p;
        } else
          c[i] = '?';
      } else if (t[i] == 's') {
        if (c[i - k] != 'r') {
          c[i] = 'r';
          ans += r;
        } else
          c[i] = '?';
      } else {
        if (c[i - k] != 's') {
          c[i] = 's';
          ans += s;
        } else
          c[i] = '?';
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007    // 10^9+7
#define INF 1000000000000 // 10^12
#define P pair<int, int>
int n, k;
int r, s, p;
string t;
char c[200000];
int ans = 0;
signed main() {
  cin >> n >> k;
  cin >> r >> s >> p;
  cin >> t;
  rep(i, n) {
    if (i < k) {
      if (t[i] == 'r') {
        c[i] = 'p';
        ans += p;
      } else if (t[i] == 's') {
        c[i] = 'r';
        ans += r;
      } else {
        c[i] = 's';
        ans += s;
      }
    } else {
      if (t[i] == 'r') {
        if (c[i - k] != 'p') {
          c[i] = 'p';
          ans += p;
        } else
          c[i] = '?';
      } else if (t[i] == 's') {
        if (c[i - k] != 'r') {
          c[i] = 'r';
          ans += r;
        } else
          c[i] = '?';
      } else {
        if (c[i - k] != 's') {
          c[i] = 's';
          ans += s;
        } else
          c[i] = '?';
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, k, r, s, p;
string t;
long long res = 0;
long long dp[3][100001];
long long cal(string tmp) {
  memset(dp, 0, sizeof dp);
  for (int i = 1; i <= tmp.size(); i++) {
    // tmp[i-1]
    //  0,1,2-> R,S,P
    dp[0][i] = max(dp[1][i - 1], dp[2][i - 1]) + (tmp[i - 1] == 's') * r;
    dp[1][i] = max(dp[0][i - 1], dp[2][i - 1]) + (tmp[i - 1] == 'p') * s;
    dp[2][i] = max(dp[0][i - 1], dp[1][i - 1]) + (tmp[i - 1] == 'r') * p;
  }
  return max(dp[0][tmp.size()], max(dp[1][tmp.size()], dp[2][tmp.size()]));
}
int main() {
  cin >> n >> k >> r >> s >> p;
  cin >> t;
  // cout<<t;

  string tmp;
  int j = 0;
  for (int i = 0; i < k; i++) {
    tmp = "";
    j = 0;
    while (i + j * k < n) {
      tmp += t[i + j * k];
      // cout<<i+j*k<<" ";
      j++;
    }
    res += cal(tmp);
    // cout<<tmp<<" "<<res<<" "<<cal(tmp)<<endl;
  }

  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, k, r, s, p;
string t;
long long res = 0;
long long dp[3][100001];
long long cal(string tmp) {
  // memset(dp,0,sizeof dp);
  dp[0][0] = 0;
  dp[1][0] = 0;
  dp[2][0] = 0;
  for (int i = 1; i <= tmp.size(); i++) {
    // tmp[i-1]
    //  0,1,2-> R,S,P
    dp[0][i] = max(dp[1][i - 1], dp[2][i - 1]) + (tmp[i - 1] == 's') * r;
    dp[1][i] = max(dp[0][i - 1], dp[2][i - 1]) + (tmp[i - 1] == 'p') * s;
    dp[2][i] = max(dp[0][i - 1], dp[1][i - 1]) + (tmp[i - 1] == 'r') * p;
  }
  return max(dp[0][tmp.size()], max(dp[1][tmp.size()], dp[2][tmp.size()]));
}
int main() {
  cin >> n >> k >> r >> s >> p;
  cin >> t;
  // cout<<t;

  string tmp;
  int j = 0;
  for (int i = 0; i < k; i++) {
    tmp = "";
    j = 0;
    while (i + j * k < n) {
      tmp += t[i + j * k];
      // cout<<i+j*k<<" ";
      j++;
    }
    res += cal(tmp);
    // cout<<tmp<<" "<<res<<" "<<cal(tmp)<<endl;
  }

  cout << res << endl;
}
Problem p02820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int dp[3][100010];
  int n, k, r, s, p;
  cin >> n >> k >> r >> s >> p;
  string t;
  cin >> t;
  int ans = 0;
  for (int i = 0; i < k; ++i) {
    memset(dp, 0, sizeof(dp));
    int pos = 0;
    for (int j = 0; j * k + i < t.length(); ++j) {
      if (t[j * k + i] == 'r') {
        dp[0][j + 1] = max(dp[1][j], dp[2][j]);
        dp[1][j + 1] = max(dp[0][j] + p, dp[2][j] + p);
        dp[2][j + 1] = max(dp[1][j], dp[0][j]);
      } else if (t[j * k + i] == 'p') {
        dp[0][j + 1] = max(dp[1][j], dp[2][j]);
        dp[1][j + 1] = max(dp[0][j], dp[2][j]);
        dp[2][j + 1] = max(dp[1][j] + s, dp[0][j] + s);
      } else {
        dp[0][j + 1] = max(dp[1][j] + r, dp[2][j] + r);
        dp[1][j + 1] = max(dp[0][j], dp[2][j]);
        dp[2][j + 1] = max(dp[1][j], dp[0][j]);
      }
      pos = j + 1;
    }
    ans += max({dp[0][pos], dp[1][pos], dp[2][pos]});
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int dp[3][100010];
  int n, k, r, s, p;
  cin >> n >> k >> r >> s >> p;
  string t;
  cin >> t;
  int ans = 0;
  for (int i = 0; i < k; ++i) {
    int pos = 0;
    for (int j = 0; j * k + i < t.length(); ++j) {
      if (t[j * k + i] == 'r') {
        dp[0][j + 1] = max(dp[1][j], dp[2][j]);
        dp[1][j + 1] = max(dp[0][j] + p, dp[2][j] + p);
        dp[2][j + 1] = max(dp[1][j], dp[0][j]);
      } else if (t[j * k + i] == 'p') {
        dp[0][j + 1] = max(dp[1][j], dp[2][j]);
        dp[1][j + 1] = max(dp[0][j], dp[2][j]);
        dp[2][j + 1] = max(dp[1][j] + s, dp[0][j] + s);
      } else {
        dp[0][j + 1] = max(dp[1][j] + r, dp[2][j] + r);
        dp[1][j + 1] = max(dp[0][j], dp[2][j]);
        dp[2][j + 1] = max(dp[1][j], dp[0][j]);
      }
      pos = j + 1;
    }
    ans += max({dp[0][pos], dp[1][pos], dp[2][pos]});
  }
  cout << ans << endl;
}
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;
#define INF 1e9

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

  vector<string> a(k);

  rep(i, k) {

    for (int j = i; j < n; j += k) {

      if (t[j] == 'r')
        a[i] += 'p';
      if (t[j] == 's')
        a[i] += 'r';
      if (t[j] == 'p')
        a[i] += 's';
    }
  }
  int ans = 0;
  rep(i, k) {
    for (int j = 1; j < n; j++) {

      if (a[i][j] == a[i][j - 1]) {
        a[i][j] = 'x';
        continue;
      }
      if (a[i][j] == 'r')
        ans += r;
      if (a[i][j] == 's')
        ans += s;
      if (a[i][j] == 'p')
        ans += p;
    }
    if (a[i][0] == 'r')
      ans += r;
    if (a[i][0] == 's')
      ans += s;
    if (a[i][0] == 'p')
      ans += p;
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
#define INF 1e9

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

  vector<string> a(k);

  rep(i, k) {

    for (int j = i; j < n; j += k) {

      if (t[j] == 'r')
        a[i] += 'p';
      if (t[j] == 's')
        a[i] += 'r';
      if (t[j] == 'p')
        a[i] += 's';
    }
  }
  int ans = 0;
  rep(i, k) {
    for (int j = 1; j < a[i].size(); j++) {

      if (a[i][j] == a[i][j - 1]) {
        a[i][j] = 'x';
        continue;
      }
      if (a[i][j] == 'r')
        ans += r;
      if (a[i][j] == 's')
        ans += s;
      if (a[i][j] == 'p')
        ans += p;
    }
    if (a[i][0] == 'r')
      ans += r;
    if (a[i][0] == 's')
      ans += s;
    if (a[i][0] == 'p')
      ans += p;
  }

  cout << ans << endl;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
using namespace std;

int main() {
  int n, k, r, s, p;
  string t;
  vector<string> g(2 * k);
  cin >> n >> k >> r >> s >> p >> t;
  rep(i, n) g[i % k] += t[i];
  int ans = 0;
  rep(i, k) {
    int m = g[i].size();
    vector<vector<int>> dp(m, vector<int>(3, 0));
    if (g[i][0] == 'r')
      dp[0][2] = p;
    if (g[i][0] == 's')
      dp[0][0] = r;
    if (g[i][0] == 'p')
      dp[0][1] = s;

    for (int j = 1; j < m; ++j) {
      dp[j][2] = max(dp[j - 1][0], dp[j - 1][1]);
      dp[j][0] = max(dp[j - 1][1], dp[j - 1][2]);
      dp[j][1] = max(dp[j - 1][2], dp[j - 1][0]);

      if (g[i][j] == 'r')
        dp[j][2] += p;
      if (g[i][j] == 's')
        dp[j][0] += r;
      if (g[i][j] == 'p')
        dp[j][1] += s;
    }
    int h = max(dp[m - 1][0], dp[m - 1][1]);
    h = max(h, dp[m - 1][2]);
    ans += h;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
using namespace std;

int main() {
  int n, k, r, s, p;
  string t;
  vector<string> g(100000);
  cin >> n >> k >> r >> s >> p >> t;
  rep(i, n) g[i % k] += t[i];
  int ans = 0;
  rep(i, k) {
    int m = g[i].size();
    vector<vector<int>> dp(m, vector<int>(3, 0));
    if (g[i][0] == 'r')
      dp[0][2] = p;
    if (g[i][0] == 's')
      dp[0][0] = r;
    if (g[i][0] == 'p')
      dp[0][1] = s;

    for (int j = 1; j < m; ++j) {
      dp[j][2] = max(dp[j - 1][0], dp[j - 1][1]);
      dp[j][0] = max(dp[j - 1][1], dp[j - 1][2]);
      dp[j][1] = max(dp[j - 1][2], dp[j - 1][0]);

      if (g[i][j] == 'r')
        dp[j][2] += p;
      if (g[i][j] == 's')
        dp[j][0] += r;
      if (g[i][j] == 'p')
        dp[j][1] += s;
    }
    int h = max(dp[m - 1][0], dp[m - 1][1]);
    h = max(h, dp[m - 1][2]);
    ans += h;
  }
  cout << ans << endl;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(obj) obj.begin(), obj.end()

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

const int iINF = 1e9;
const long long llINF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

long long dp[20010][3]; // r = 0, s = 1, p = 2

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

  REP(i, N) {
    REP(j, 3) {
      REP(k, 3) {
        if (j == k)
          continue;
        if (T[i] == 's' && k == 0) {
          dp[i + K][k] = max(dp[i + K][k], dp[i][j] + R);
        }

        if (T[i] == 'p' && k == 1) {
          dp[i + K][k] = max(dp[i + K][k], dp[i][j] + S);
        }

        if (T[i] == 'r' && k == 2) {
          dp[i + K][k] = max(dp[i + K][k], dp[i][j] + P);
        }

        dp[i + K][k] = max(dp[i + K][k], dp[i][j]);
      }
    }
  }
  long long ans = 0;
  for (int i = N; i < N + K; i++) {
    ans += max(max(dp[i][0], dp[i][1]), dp[i][2]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(obj) obj.begin(), obj.end()

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

const int iINF = 1e9;
const long long llINF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

long long dp[200010][3]; // r = 0, s = 1, p = 2

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

  REP(i, N) {
    REP(j, 3) {
      REP(k, 3) {
        if (j == k)
          continue;
        if (T[i] == 's' && k == 0) {
          dp[i + K][k] = max(dp[i + K][k], dp[i][j] + R);
        }

        if (T[i] == 'p' && k == 1) {
          dp[i + K][k] = max(dp[i + K][k], dp[i][j] + S);
        }

        if (T[i] == 'r' && k == 2) {
          dp[i + K][k] = max(dp[i + K][k], dp[i][j] + P);
        }

        dp[i + K][k] = max(dp[i + K][k], dp[i][j]);
      }
    }
  }
  long long ans = 0;
  for (int i = N; i < N + K; i++) {
    ans += max(max(dp[i][0], dp[i][1]), dp[i][2]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  int n, k;
  map<char, int> mp;
  string t;
  cin >> n >> k >> mp['s'] >> mp['p'] >> mp['r'] >> t;
  vector<bool> win(n);
  int ans = 0;
  rep(i, n) {
    if (n >= k && t[i] == t[i - k] && win[i - k])
      win[i] = false;
    else {
      win[i] = true;
      ans += mp[t[i]];
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  int n, k;
  map<char, int> mp;
  string t;
  cin >> n >> k >> mp['s'] >> mp['p'] >> mp['r'] >> t;
  vector<bool> win(n);
  int ans = 0;
  rep(i, n) {
    if (i >= k && t[i] == t[i - k] && win[i - k])
      win[i] = false;
    else {
      win[i] = true;
      ans += mp[t[i]];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
// Hovalhagh
// Ya Aziz & Hakim
// AliReza Kaboli

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k, r, s, p, ans = 0;
  string t, sa;
  cin >> n >> k >> r >> s >> p >> t;
  for (int i = 0; i < n; ++i) {
    if (t[i] == 'r') {
      ans += p;
      sa += 'p';
    } else if (t[i] == 's') {
      ans += r;
      sa += 'r';
    } else {
      ans += s;
      sa += 's';
    }
  }
  for (int i = 0; i < n; ++i) {
    if (i + k < n && sa[i] == sa[i + k]) {
      if (sa[i + k] == 'r')
        ans -= r;
      else if (sa[i + k] == 's')
        ans -= s;
      else
        ans -= p;
      if (sa[i] != 'r' && sa[i + 2 * k] != 'r')
        sa[i + k] = 'r';
      if (sa[i] != 's' && sa[i + 2 * k] != 's')
        sa[i + k] = 's';
      if (sa[i] != 'p' && sa[i + 2 * k] != 'p')
        sa[i + k] = 'p';
    }
  }
  cout << ans << '\n';
}


Fixed submission:
// Hovalhagh
// Ya Aziz & Hakim
// AliReza Kaboli

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k, r, s, p, ans = 0;
  string t, sa;
  cin >> n >> k >> r >> s >> p >> t;
  for (int i = 0; i < n; ++i) {
    if (t[i] == 'r') {
      ans += p;
      sa += 'p';
    } else if (t[i] == 's') {
      ans += r;
      sa += 'r';
    } else {
      ans += s;
      sa += 's';
    }
  }
  for (int i = 0; i < n; ++i) {
    if (i + k < n && sa[i] == sa[i + k]) {
      if (sa[i + k] == 'r')
        ans -= r;
      else if (sa[i + k] == 's')
        ans -= s;
      else
        ans -= p;
      if (i + 2 * k < n) {
        if (sa[i] != 'r' && sa[i + 2 * k] != 'r')
          sa[i + k] = 'r';
        if (sa[i] != 's' && sa[i + 2 * k] != 's')
          sa[i + k] = 's';
        if (sa[i] != 'p' && sa[i + 2 * k] != 'p')
          sa[i + k] = 'p';
      }
    }
  }
  cout << ans << '\n';
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 100010;
bool st[N];

int main() {
  int n, k, r, s, p;
  cin >> n >> k >> r >> s >> p;
  char t[N];
  cin >> t;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (st[i] == false) {
      if (t[i] == t[i + k]) {
        st[i + k] = true;
      }
      if (t[i] == 'r')
        sum += p;
      else if (t[i] == 's')
        sum += r;
      else if (t[i] == 'p')
        sum += s;
    }
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 100010;
bool st[N];

int main() {
  int n, k, r, s, p;
  cin >> n >> k >> r >> s >> p;
  char t[N];
  cin >> t;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (st[i] == false) {
      if (i + k < n && t[i] == t[i + k]) {
        st[i + k] = true;
      }
      if (t[i] == 'r')
        sum += p;
      else if (t[i] == 's')
        sum += r;
      else if (t[i] == 'p')
        sum += s;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<pair<ll, ll>>> vvpll;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

int main() {
  ll n, K, R, S, P;
  string T;
  cin >> n >> K >> R >> S >> P >> T;

  ll ans = 0;
  rep(i, K) {
    for (ll j = i; j < n; j += K) {

      if (T[j] == 'r') {
        ans += P;
      } else if (T[j] == 's') {
        ans += R;
      } else if (T[j] == 'p') {
        ans += S;
      }

      if (T[j] == T[j + K]) {
        j += K;
      }
    }
  }

  out(ans);
  re0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<pair<ll, ll>>> vvpll;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

int main() {
  ll n, K, R, S, P;
  string T;
  cin >> n >> K >> R >> S >> P >> T;

  ll ans = 0;
  rep(i, K) {
    for (ll j = i; j < n; j += K) {

      if (T[j] == 'r') {
        ans += P;
      } else if (T[j] == 's') {
        ans += R;
      } else if (T[j] == 'p') {
        ans += S;
      }

      if (j + K <= n - 1 && T[j] == T[j + K]) {
        j += K;
      }
    }
  }

  out(ans);
  re0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

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

int main() {
  long long int n, k, r, s, p, ans = 0;
  cin >> n >> k >> r >> s >> p;
  string q;
  cin >> q;
  for (long long int i = 0; i < n; i++) {

    if (q[i] == 'r') {
      ans += p;
    } else if (q[i] == 's') {
      ans += r;
    } else if (q[i] == 'p') {
      ans += s;
    }
    if (q[i] == q[i + k]) {
      q[i + k] = 'z';
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

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

int main() {
  long long int n, k, r, s, p, ans = 0;
  cin >> n >> k >> r >> s >> p;
  string q;
  cin >> q;
  for (long long int i = 0; i < n; i++) {

    if (q[i] == 'r') {
      ans += p;
    } else if (q[i] == 's') {
      ans += r;
    } else if (q[i] == 'p') {
      ans += s;
    }
    if (i < n - k && q[i] == q[i + k]) {
      q[i + k] = 'z';
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define log(x) cout << x << endl
#define pb push_back
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long lint;
using namespace std;

// <<fixed<<setprecision(6)<<

int gcd(lint x, lint y) { return y == 0 ? x : gcd(y, x % y); }

int Combination(int n, int r) {
  int num = 1;
  for (int i = 1; i <= r; i++) {
    num = num * (n - i + 1) / i;
  }
  return num;
}

int main() {
  int n, k;
  int rr, ss, pp = 0;
  map<char, int> m;
  m['r'] = 0;
  m['s'] = 0;
  m['p'] = 0;
  int r, s, p;
  cin >> n >> k;
  cin >> r >> s >> p;
  vector<char> l, a;
  string st;
  cin >> st;
  for (int i = 0; i < st.size(); i++) {
    char j = st[i];
    if (j == 'r') {
      l.pb(j);
      a.pb('p');
      m['p']++;
    } else if (j == 's') {
      l.pb(j);
      a.pb('r');
      m['r']++;
    } else if (j == 'p') {
      l.pb(j);
      a.pb('s');
      m['s']++;
    }
  }

  for (int i = k; i < n; i++) {
    char j = a[i];
    if (a[i - k] == j) {
      if (a[i + k] == j) {
        a[i] = 'x';
      }
      m[j]--;
    }
  }
  int result = m['r'] * r + m['s'] * s + m['p'] * p;
  cout << result;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define log(x) cout << x << endl
#define pb push_back
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
typedef long long lint;
using namespace std;

// <<fixed<<setprecision(6)<<

int gcd(lint x, lint y) { return y == 0 ? x : gcd(y, x % y); }

int Combination(int n, int r) {
  int num = 1;
  for (int i = 1; i <= r; i++) {
    num = num * (n - i + 1) / i;
  }
  return num;
}

int main() {
  int n, k;
  int rr, ss, pp = 0;
  map<char, int> m;
  m['r'] = 0;
  m['s'] = 0;
  m['p'] = 0;
  int r, s, p;
  cin >> n >> k;
  cin >> r >> s >> p;
  vector<char> l, a;
  string st;
  cin >> st;
  for (int i = 0; i < st.size(); i++) {
    char j = st[i];
    if (j == 'r') {
      l.pb(j);
      a.pb('p');
      m['p']++;
    } else if (j == 's') {
      l.pb(j);
      a.pb('r');
      m['r']++;
    } else if (j == 'p') {
      l.pb(j);
      a.pb('s');
      m['s']++;
    }
  }

  for (int i = k; i < n; i++) {
    char j = a[i];
    if (a[i - k] == j) {
      if (i + k < n) {
        if (a[i + k] == j) {
          a[i] = 'x';
        }
      }
      m[j]--;
    }
  }
  int result = m['r'] * r + m['s'] * s + m['p'] * p;
  cout << result;
}
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 int64;
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, K, R, S, P;
  cin >> N >> K >> R >> S >> P;
  int score[3] = {R, S, P};
  string H = "rsp";
  string T;
  cin >> T;
  vector<string> str(N / K);
  rep(i, N) { str[i % K] += T[i]; }
  auto f = [&](const string &s) -> int {
    int N = s.size();
    vector<vector<int>> dp(N + 1, vector<int>(3));
    rep(i, N) rep(pre, 3) rep(hand, 3) {
      if (pre == hand)
        continue;
      int nscore = s[i] == H[(hand + 1) % 3] ? score[hand] : 0;
      chmax(dp[i + 1][hand], dp[i][pre] + nscore);
    }
    int ans = 0;
    rep(i, 3) ans = max(ans, dp[N][i]);
    return ans;
  };
  int ans = 0;
  for (auto &s : str) {
    ans += f(s);
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int64;
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, K, R, S, P;
  cin >> N >> K >> R >> S >> P;
  int score[3] = {R, S, P};
  string H = "rsp";
  string T;
  cin >> T;
  vector<string> str(K + 10);
  rep(i, N) { str[i % K] += T[i]; }
  auto f = [&](const string &s) -> int {
    int N = s.size();
    vector<vector<int>> dp(N + 1, vector<int>(3));
    rep(i, N) rep(pre, 3) rep(hand, 3) {
      if (pre == hand)
        continue;
      int nscore = s[i] == H[(hand + 1) % 3] ? score[hand] : 0;
      chmax(dp[i + 1][hand], dp[i][pre] + nscore);
    }
    int ans = 0;
    rep(i, 3) ans = max(ans, dp[N][i]);
    return ans;
  };
  int ans = 0;
  for (auto &s : str) {
    ans += f(s);
  }
  cout << ans << '\n';
  return 0;
}