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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 26;

typedef pair<int, int> pii;

int main() {
  int n;
  while (cin >> n, n) {
    pii b[n + 1];
    b[0] = pii(0, 0);
    REP(i, n) {
      int p, t;
      cin >> p >> t;
      b[i + 1] = pii(t, p);
    }
    sort(b, b + n);
    int dp[n + 1][4];
    REP(i, n + 1) REP(j, 4) dp[i][j] = INF;
    dp[0][0] = 0;

    for (int i = 0; i < n; ++i) {
      REP(k, 4) { // houseツづ可姪淞づゥ
        if (k > 0)
          if (b[i].first + b[i].second * (k + 1) + b[i + 1].second <=
              b[i + 1].first)
            dp[i + 1][1] =
                min(dp[i + 1][1], dp[i][k] + b[i].second + b[i + 1].second);
        if (k < 3) {
          int dis = abs(b[i].second - b[i + 1].second);
          if (b[i].first + dis * (k + 1) <= b[i + 1].first) {
            dp[i + 1][k + 1] = min(dp[i + 1][k + 1], dp[i][k] + dis);
          }
        }
      }
    }
    // for (int i=0; i<=n; ++i) {
    //   for (int j=0; j<4; ++j) {
    //     cout << dp[i][j] << " ";
    //   }
    //   cout << endl;
    // }
    int ans = INF;
    REP(k, 4) { ans = min(ans, dp[n][k] + b[n].second); }
    if (ans == INF) {
      int ma = 0;
      REP(i, n + 1) {
        REP(k, 4) {
          if (dp[i][k] != INF)
            ma = max(ma, i);
        }
      }
      cout << "NG " << ma + 1 << endl;
    } else {
      cout << "OK " << ans << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef pair<int, int> pii;
pii b[40];
int n;
int ma;
int memo[40][2];

int solve(int pos, int t, int k, int now, int dis) {
  // cout << pos << " " << t << " " << k << " "  << now << endl;
  if (pos == 0) {
    if (memo[now][0] && memo[now][0] < t)
      return INF;
  } else {
    if (memo[now][1] && memo[now][1] < t)
      return INF;
  }
  memo[now][pos != 0] = min(memo[now][pos != 0], t);

  ma = max(ma, now);
  if (now == n)
    return dis + pos;
  if (k == 3) {
    return solve(0, t + pos * (k + 1), 0, now, dis + pos);
  }
  int res = INF;
  int d = abs(b[now].second - pos);

  // cout << t+dis*(k+1) << " " << b[now].first << endl;
  if (t + d * (k + 1) <= b[now].first)
    res = solve(b[now].second, b[now].first, k + 1, now + 1, dis + d);
  if (k)
    res = min(res, solve(0, t + pos * (k + 1), 0, now, dis + pos));

  return res;
}

int main() {
  while (cin >> n, n) {
    REP(i, n) {
      int p, t;
      cin >> p >> t;
      b[i] = pii(t, p);
    }
    sort(b, b + n);
    ma = 0;
    memset(memo, 0, sizeof(memo));
    int ans = solve(0, 0, 0, 0, 0);
    if (ans == INF) {
      cout << "NG " << ma + 1 << endl;
    } else {
      cout << "OK " << ans << endl;
    }
  }
}