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

#include <bits/stdc++.h>
using namespace std;

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &a) {
  return is >> a.first >> a.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &a) {
  return os << a.first << " " << a.second;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vc) {
  for (int i = 0; i < vc.size(); i++)
    is >> vc[i];
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vc) {
  for (int i = 0; i < vc.size(); i++)
    os << vc[i] << endl;
  return os;
}

#define ForEach(it, c)                                                         \\
  for (__typeof(c).begin() it = (c).begin(); it != (c).end(); it++)
#define ALL(v) (v).begin(), (v).end()
#define UNQ(s)                                                                 \\
  {                                                                            \\
    sort(ALL(s));                                                              \\
    (s).erase(unique(ALL(s)), (s).end());                                      \\
  }

typedef long long int64;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
const int INF = 1 << 30;

int dp[3001][3001];

int main() {
  int N, T, S;
  cin >> N >> T >> S;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  int ret = 0;
  for (int i = 1; i <= T; ++i) {
    for (int j = 1; j <= N; ++j) {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      int prev = i - B[j - 1];
      if (prev >= 0 && (prev >= S || i <= S))
        dp[i][j] = max(dp[i][j], dp[prev][j - 1] + A[j - 1]);
    }
    ret = max(ret, dp[i][N]);
  }
  cout << ret << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &a) {
  return is >> a.first >> a.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &a) {
  return os << a.first << " " << a.second;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vc) {
  for (int i = 0; i < vc.size(); i++)
    is >> vc[i];
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vc) {
  for (int i = 0; i < vc.size(); i++)
    os << vc[i] << endl;
  return os;
}

#define ForEach(it, c)                                                         \\
  for (__typeof(c).begin() it = (c).begin(); it != (c).end(); it++)
#define ALL(v) (v).begin(), (v).end()
#define UNQ(s)                                                                 \\
  {                                                                            \\
    sort(ALL(s));                                                              \\
    (s).erase(unique(ALL(s)), (s).end());                                      \\
  }

typedef long long int64;
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
const int INF = 1 << 30;

int main() {
  int N, T, S;
  cin >> N >> T >> S;
  vector<int64> A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  // dp[i][j]: 時間iでj-1まで夜店を訪れたときのMの値
  static vector<vector<int64>> dp(T + 2, vector<int64>(N + 1, -1));
  for (int i = 0; i <= T + 1; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i <= T + 1; i++) {
    for (int j = 0; j <= N; j++) {
      if (i > 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (j > 0)
        dp[i][j] = max(dp[i][j], dp[i][j - 1]);
      if (i == S || j == N || i + B[j] > T || (i < S && i + B[j] > S))
        continue;
      dp[i + B[j]][j + 1] = max(dp[i + B[j]][j + 1], dp[i][j] + A[j]);
    }
  }

  int64 ret = 0;
  for (int i = 0; i <= N; i++) {
    ret = max(ret, dp[T + 1][i]);
  }
  cout << ret << endl;
}