Problem p00484 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define endl "\\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

int f(int i) { return i * 2; }

void solve() {
  int N, K;
  cin >> N >> K;
  vector<pii> B(N);
  for (int i = 0; i < N; i++) {
    ll s, k;
    cin >> s >> k;
    k--;
    B[i] = {k, s};
  }
  sort(all(B), [](pii a, pii b) {
    if (a.first == b.first)
      return a.second > b.second;
    return a.first < b.first;
  });

  vector<int> offset(10, -1);
  for (int i = 0; i < N; i++) {
    if (offset[B[i].first] == -1)
      offset[B[i].first] = i;
  }

  vector<vector<int>> dp(K + 1, vector<int>(2, -1));

  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    vector<vector<int>> ndp(K + 1, vector<int>(2, -1));
    for (int j = 0; j <= K; j++) {
      if (j + 1 <= K && dp[j][0] != -1) {
        chmax(ndp[j + 1][0],
              dp[j][0] + B[i].second + f(i - offset[B[i].first]));
      }
      // 切り替わるタイミングで0, そうでないとき1
      int nextf =
          1 - ((i == N - 1) || ((i < N - 1) && B[i].first != B[i + 1].first));
      if (dp[j][0] != -1)
        chmax(ndp[j][nextf], dp[j][0]);
      if (dp[j][1] != -1)
        chmax(ndp[j][nextf], dp[j][1]);
    }
    dp = ndp;
  }

  cout << max(dp[K][0], dp[K][1]) << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define endl "\\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

int f(int i) { return i * 2; }

void solve() {
  int N, K;
  cin >> N >> K;
  vector<pii> B(N);
  for (int i = 0; i < N; i++) {
    ll s, k;
    cin >> s >> k;
    k--;
    B[i] = {k, s};
  }
  sort(all(B), [](pii a, pii b) {
    if (a.first == b.first)
      return a.second > b.second;
    return a.first < b.first;
  });

  vector<int> offset(10, -1);
  for (int i = 0; i < N; i++) {
    if (offset[B[i].first] == -1)
      offset[B[i].first] = i;
  }

  vector<vector<vector<int>>> dp(
      N + 1, vector<vector<int>>(K + 1, vector<int>(2, -1)));

  dp[0][0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= K; j++) {
      if (j + 1 <= K && dp[i][j][0] != -1) {
        chmax(dp[i + 1][j + 1][0],
              dp[i][j][0] + B[i].second + f(i - offset[B[i].first]));
      }

      // 切り替わるタイミングで0, そうでないとき1
      int nextf =
          1 - ((i == N - 1) || ((i < N - 1) && B[i].first != B[i + 1].first));
      if (dp[i][j][0] != -1)
        chmax(dp[i + 1][j][nextf], dp[i][j][0]);
      if (dp[i][j][1] != -1)
        chmax(dp[i + 1][j][nextf], dp[i][j][1]);
    }
  }

  cout << max(dp[N][K][0], dp[N][K][1]) << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}