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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

ll dp[2][3030] = {};
int main(void) {
  int N, T, S;
  int A[3030], B[3030];
  cin >> N >> T >> S;
  REP(i, N) { cin >> A[i] >> B[i]; }
  int cur = 0;
  REP(i, N + 1) {
    REP(j, T + 1) {
      if (i - 1 >= 0)
        dp[cur][j] = max(dp[cur][j], dp[1 - cur][j]);
      if (i - 1 >= 0 && j - B[i - 1] >= 0 && !(j - B[i - 1] < S && S < j)) {
        dp[cur][j] = max(dp[cur][j], dp[1 - cur][j - B[i - 1]] + A[i - 1]);
      }
    }
    cur = 1 - cur;
  }
  ll res = 0;
  REP(i, 2) {
    REP(j, T + 1) { res = max(res, dp[i][j]); }
  }
  cout << res << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

ll dp[3030][3030] = {};
int main(void) {
  int N, T, S;
  int A[3030], B[3030];
  cin >> N >> T >> S;
  REP(i, N) { cin >> A[i] >> B[i]; }
  REP(i, N + 1) {
    REP(j, T + 1) {
      if (i - 1 >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (i - 1 >= 0 && j - B[i - 1] >= 0 && !(j - B[i - 1] < S && S < j)) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - B[i - 1]] + A[i - 1]);
      }
    }
  }
  ll res = 0;
  REP(i, N + 1) {
    REP(j, T + 1) { res = max(res, dp[i][j]); }
  }
  cout << res << endl;
}