Problem p02820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, b, e) for (int i = (b); i < (e); ++i)
#define RREP(i, b, e) for (int i = (b)-1; i >= e; --i)
#define rep(i, e) for (int i = 0; i < (e); ++i)

constexpr int MOD = 1000000007;
constexpr int INF = 1LL << 30;
constexpr long long LLINF = 1LL << 62;
constexpr double EPS = 1e-9;

struct Initializer {
  Initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(15) << boolalpha;
  }
} initializer;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    if (it != vec.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pa) {
  return os << "(" << pa.first << ", " << pa.second << ")";
}

template <typename Tuple, size_t N> struct TuplePrinter {
  static void print(const Tuple &t) {
    TuplePrinter<Tuple, N - 1>::print(t);
    cout << ", " << get<N - 1>(t);
  }
};

template <typename Tuple> struct TuplePrinter<Tuple, 1> {
  static void print(const Tuple &t) { cout << get<0>(t); }
};

template <typename... T>
ostream &operator<<(ostream &os, const tuple<T...> &tup) {
  os << "(";
  TuplePrinter<decltype(tup), sizeof...(T)>::print(tup);
  return os << ")";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &se) {
  os << "{";
  for (auto it = se.begin(); it != se.end(); ++it) {
    if (it != se.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &ma) {
  os << "{";
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    if (it != ma.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  return os << "}";
}

inline void print(void) { cout << '\n'; }
template <class T> inline void print(const T &x) { cout << x << '\n'; }
template <class T, class... U> inline void print(const T &x, const U &...y) {
  cout << x << " ";
  print(y...);
}
#define dump(...)                                                              \
  cout << #__VA_ARGS__ << ": [L_" << __LINE__ << "]" << '\n';                  \
  print(__VA_ARGS__);                                                          \
  cout << '\n';

template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (b < a) {
    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;

  int ans = 0;
  rep(i, k) {
    int cur = i;
    int j = 0;
    vector<vector<int>> dp(n, vector<int>(4, 0));
    if (t[cur] == 'r')
      dp[j][3] = p;
    if (t[cur] == 's')
      dp[j][1] = r;
    if (t[cur] == 'p')
      dp[j][2] = s;
    cur += k;
    ++j;
    while (cur < n) {
      if (t[cur] == 'r') {
        dp[j][1] = max(dp[j - 1][2], dp[j - 1][3]);
        dp[j][2] = max(dp[j - 1][1], dp[j - 1][3]);
        dp[j][3] = max(dp[j - 1][1] + p, dp[j - 1][2] + p);
      }
      if (t[cur] == 's') {
        dp[j][1] = max(dp[j - 1][2] + r, dp[j - 1][3] + r);
        dp[j][2] = max(dp[j - 1][1], dp[j - 1][3]);
        dp[j][3] = max(dp[j - 1][1], dp[j - 1][2]);
      }
      if (t[cur] == 'p') {
        dp[j][1] = max(dp[j - 1][2], dp[j - 1][3]);
        dp[j][2] = max(dp[j - 1][1] + s, dp[j - 1][3] + s);
        dp[j][3] = max(dp[j - 1][1], dp[j - 1][2]);
      }
      cur += k;
      ++j;
    }
    int res = 0;
    rep(l, 4) res = max(res, dp[j - 1][l]);
    ans += res;
  }
  print(ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, b, e) for (int i = (b); i < (e); ++i)
#define RREP(i, b, e) for (int i = (b)-1; i >= e; --i)
#define rep(i, e) for (int i = 0; i < (e); ++i)

constexpr int MOD = 1000000007;
constexpr int INF = 1LL << 30;
constexpr long long LLINF = 1LL << 62;
constexpr double EPS = 1e-9;

struct Initializer {
  Initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(15) << boolalpha;
  }
} initializer;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    if (it != vec.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pa) {
  return os << "(" << pa.first << ", " << pa.second << ")";
}

template <typename Tuple, size_t N> struct TuplePrinter {
  static void print(const Tuple &t) {
    TuplePrinter<Tuple, N - 1>::print(t);
    cout << ", " << get<N - 1>(t);
  }
};

template <typename Tuple> struct TuplePrinter<Tuple, 1> {
  static void print(const Tuple &t) { cout << get<0>(t); }
};

template <typename... T>
ostream &operator<<(ostream &os, const tuple<T...> &tup) {
  os << "(";
  TuplePrinter<decltype(tup), sizeof...(T)>::print(tup);
  return os << ")";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &se) {
  os << "{";
  for (auto it = se.begin(); it != se.end(); ++it) {
    if (it != se.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &ma) {
  os << "{";
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    if (it != ma.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  return os << "}";
}

inline void print(void) { cout << '\n'; }
template <class T> inline void print(const T &x) { cout << x << '\n'; }
template <class T, class... U> inline void print(const T &x, const U &...y) {
  cout << x << " ";
  print(y...);
}
#define dump(...)                                                              \
  cout << #__VA_ARGS__ << ": [L_" << __LINE__ << "]" << '\n';                  \
  print(__VA_ARGS__);                                                          \
  cout << '\n';

template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (b < a) {
    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;

  int ans = 0;
  rep(i, k) {
    int cur = i;
    int j = 0;
    vector<vector<int>> dp(n / k + 10, vector<int>(4, 0));
    if (t[cur] == 'r')
      dp[j][3] = p;
    if (t[cur] == 's')
      dp[j][1] = r;
    if (t[cur] == 'p')
      dp[j][2] = s;
    cur += k;
    ++j;
    while (cur < n) {
      if (t[cur] == 'r') {
        dp[j][1] = max(dp[j - 1][2], dp[j - 1][3]);
        dp[j][2] = max(dp[j - 1][1], dp[j - 1][3]);
        dp[j][3] = max(dp[j - 1][1] + p, dp[j - 1][2] + p);
      }
      if (t[cur] == 's') {
        dp[j][1] = max(dp[j - 1][2] + r, dp[j - 1][3] + r);
        dp[j][2] = max(dp[j - 1][1], dp[j - 1][3]);
        dp[j][3] = max(dp[j - 1][1], dp[j - 1][2]);
      }
      if (t[cur] == 'p') {
        dp[j][1] = max(dp[j - 1][2], dp[j - 1][3]);
        dp[j][2] = max(dp[j - 1][1] + s, dp[j - 1][3] + s);
        dp[j][3] = max(dp[j - 1][1], dp[j - 1][2]);
      }
      cur += k;
      ++j;
    }
    int res = 0;
    rep(l, 4) res = max(res, dp[j - 1][l]);
    ans += res;
  }
  print(ans);

  return 0;
}