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

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int p[45], t[45], dp[45][45][4];
signed main(void) {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    REP(i, n) cin >> p[i] >> t[i];

    //?????????????????????????????????
    if (p[0] > t[0]) {
      cout << "NG " << 1 << endl;
      continue;
    }
    // INF??§?????????
    REP(i, 45) REP(j, 45) REP(k, 4) dp[i][j][k] = INF;
    dp[0][0][1] = p[0];

    bool ok;
    int ans;
    FOR(i, 1, n) {
      // cout << "i:" << i << endl;
      ok = false;
      for (int k = 4; k >= 1; --k) {
        // cout << "k:" << k << endl;
        if (dp[i - 1][i - 1][k - 1] != INF) {
          // cout << "c" << endl;
          //?????????????¬????????????????????????????
          if (k <= 3 && abs(p[i] - p[i - 1]) * (k) <= abs(t[i] - t[i - 1])) {
            // cout << "a" << endl;
            dp[i][i][k] = min(dp[i][i][k],
                              dp[i - 1][i - 1][k - 1] + abs(p[i] - p[i - 1]));
            ok = true;
          }
          //??????????????????????¬????????????????????????????
          if (p[i - 1] * (k) + p[i] <= abs(t[i] - t[i - 1])) {
            // cout << "b" << dp[i-1][i-1][k-1] << " " << p[i-1] << " " <<  p[i]
            // << endl;
            dp[i][i][1] =
                min(dp[i][i][1], dp[i - 1][i - 1][k - 1] + p[i - 1] + p[i]);
            ok = true;
          }
        }
      }
      if (!ok) {
        ans = i + 1;
        break;
      }
    }

    /*REP(i, n) {
            REP(k, 4) {
                    cout << dp[t[i]][p[i]][k] << " ";
            }
            cout << endl;
    }*/

    if (ok) {
      int ans = INF;
      REP(k, 4) {
        if (k > 0)
          dp[n - 1][n - 1][k] += p[n - 1];
        // cout << dp[t[n-1]][p[n-1]][k] << endl;
        ans = min(ans, dp[n - 1][n - 1][k]);
      }
      cout << "OK " << ans << endl;
    } else
      cout << "NG " << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int p[45], t[45], dp[50010][105][4];
signed main(void) {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;
    REP(i, n) cin >> p[i] >> t[i];

    //?????????????????????????????????
    if (p[0] > t[0]) {
      cout << "NG " << 1 << endl;
      continue;
    }
    // INF??§?????????
    REP(i, 50010) REP(j, 105) REP(k, 4) dp[i][j][k] = INF;
    dp[t[0]][p[0]][1] = p[0];

    bool ok;
    int ans;
    FOR(i, 1, n) {
      // cout << "i:" << i << endl;
      ok = false;
      for (int k = 3; k >= 1; --k) {
        // cout << "k:" << k << endl;
        if (dp[t[i - 1]][p[i - 1]][k - 1] != INF) {
          //?????????????¬????????????????????????????
          if (k <= 3 && abs(p[i] - p[i - 1]) * (k) <= abs(t[i] - t[i - 1])) {
            // cout << "a" << endl;
            dp[t[i]][p[i]][k] =
                min(dp[t[i]][p[i]][k],
                    dp[t[i - 1]][p[i - 1]][k - 1] + abs(p[i] - p[i - 1]));
            ok = true;
          }
          //??????????????????????¬????????????????????????????
          if (p[i - 1] * (k) + p[i] <= abs(t[i] - t[i - 1])) {
            // cout << "b" << dp[t[i-1]][p[i-1]][k-1] << " " << p[i-1] << " " <<
            // p[i] << endl;
            dp[t[i]][p[i]][1] =
                min(dp[t[i]][p[i]][1],
                    dp[t[i - 1]][p[i - 1]][k - 1] + p[i - 1] + p[i]);
            ok = true;
          }
        }
      }
      if (!ok) {
        ans = i + 1;
        break;
      }
    }

    /*REP(i, n) {
            REP(k, 4) {
                    cout << dp[t[i]][p[i]][k] << " ";
            }
            cout << endl;
    }*/

    if (ok) {
      int ans = INF;
      REP(k, 4) {
        if (k > 0)
          dp[t[n - 1]][p[n - 1]][k] += p[n - 1];
        // cout << dp[t[n-1]][p[n-1]][k] << endl;
        ans = min(ans, dp[t[n - 1]][p[n - 1]][k]);
      }
      cout << "OK " << ans << endl;
    } else
      cout << "NG " << ans << endl;
  }

  return 0;
}