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

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repp(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;
    vector<int> p(n + 1), t(n + 1);
    p[0] = 0, t[0] = 0;
    rep(i, n) cin >> p[i + 1] >> t[i + 1];
    vector<vector<int>> dp(n + 1, vector<int>(3, IINF));
    dp[0][0] = 0;
    int ans = IINF;
    repp(i, 1, n + 1) {
      rep(j, 3) {
        if (p[i - 1] * (j + 2) + p[i] <= t[i] - t[i - 1]) {
          dp[i][0] = min(dp[i][0], dp[i - 1][j] + p[i - 1] + p[i]);
        }
      }
      repp(j, 1, 3) {
        if ((j + 1) * abs(p[i - 1] - p[i]) <= t[i] - t[i - 1]) {
          dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + abs(p[i - 1] - p[i]));
        }
      }
      bool ok = false;
      rep(j, 3) {
        if (dp[i][j] < IINF)
          ok = true;
      }
      if (!ok) {
        cout << "NG" SP i << endl;
        goto next;
      }
    }
    rep(i, 3) ans = min(ans, dp[n][i] + p[n]);
    cout << "OK" SP ans << endl;
  next:;
  }
  return 0;
}


Error: MLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repp(i, l, r) for (int i = (l); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for (int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP << " " <<
#define CYES cout << "Yes" << endl
#define CNO cout << "No" << endl
#define CFS                                                                    \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;
    vector<int> p(n), t(n);
    rep(i, n) cin >> p[i] >> t[i];
    vector<vector<vector<int>>> dp(
        t[n - 1] + 1000, vector<vector<int>>(102, vector<int>(4, IINF)));
    dp[0][0][0] = 0;
    int nb = 0;
    repp(i, 1, t[n - 1] + 1000) {
      if (i == t[nb]) {
        bool ok = false;
        repp(k, 1, 4) {
          dp[i][p[nb]][k] = dp[i - 1][p[nb]][k - 1];
          if (i > k) {
            dp[i][p[nb]][k] =
                min(dp[i][p[nb]][k], dp[i - k][p[nb] + 1][k - 1] + 1);
            dp[i][p[nb]][k] =
                min(dp[i][p[nb]][k], dp[i - k][p[nb] - 1][k - 1] + 1);
          }
          if (dp[i][p[nb]][k] != IINF)
            ok = true;
          // cout << dp[i][p[nb]][k] << " ";
        }
        // cout << endl;
        if (!ok) {
          cout << "NG" SP nb + 1 << endl;
          goto next;
        }
        nb++;
      } else {
        dp[i][0][0] = dp[i - 1][0][0];
        repp(k, 1, 4) {
          if (i > k && (nb == 0 || t[nb - 1] <= i - (k + 1))) {
            dp[i][0][0] = min(dp[i][0][0], dp[i - (k + 1)][1][k] + 1);
          }
        }
        repp(j, 1, 101) {
          rep(k, 4) {
            dp[i][j][k] = dp[i - 1][j][k];
            if (i > k && (nb == 0 || t[nb - 1] <= i - (k + 1))) {
              dp[i][j][k] = min(dp[i][j][k], dp[i - (k + 1)][j + 1][k] + 1);
              dp[i][j][k] = min(dp[i][j][k], dp[i - (k + 1)][j - 1][k] + 1);
            }
          }
        }
      }
    }
    cout << "OK" SP dp[t[n - 1] + 999][0][0] << endl;
  next:;
  }
  return 0;
}