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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

int n, p[50], t[50], dp[50][4], minnum, ans;

int main() {
  while (scanf("%d", &n) && n != 0) {
    for (int i = 0; i < n; i++)
      scanf("%d%d", &p[i], &t[i]);
    memset(dp, -1, sizeof(dp));
    if (p[0] <= t[0])
      dp[0][1] = p[0];
    for (int i = 1; i < n; i++) {
      minnum = 99999999;
      for (int j = 1; j < 4; j++)
        if (dp[i - 1][j] != -1 && p[i] + p[i - 1] * (j + 1) <= t[i] - t[i - 1])
          minnum = min(minnum, dp[i - 1][j] + p[i] + p[i - 1]);
      if (minnum != 99999999)
        dp[i][1] = minnum;
      if (dp[i - 1][1] != -1 && abs(p[i] - p[i - 1]) * 2 <= t[i] - t[i - 1])
        dp[i][2] = dp[i - 1][1] + abs(p[i] - p[i - 1]);
      if (dp[i - 1][2] != -1 && abs(p[i] - p[i - 1]) * 3 <= t[i] - t[i - 1])
        dp[i][3] = dp[i - 1][2] + abs(p[i] - p[i - 1]);
    }
    ans = -1;
    for (int i = 1; i < 4; i++)
      if (dp[n - 1][i] != -1 && (ans == -1 || ans > dp[n - 1][i]))
        ans = dp[n - 1][i];
    if (ans == -1) {
      for (int j = 0; j < n; j++) {
        ans = -1;
        for (int i = 1; i < 4; i++)
          if (dp[j][i] != -1 && (ans == -1 || ans > dp[j][i]))
            ans = dp[j][i];
        if (ans == -1) {
          printf("NG %d\\n", j + 1);
          break;
        }
      }
    } else
      printf("OK %d\\n", ans + p[n - 1]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000007
#define MP make_pair
const int maxn = 100010;
const double eps = 1e-6, pi = acos(-1.0);
using namespace std;
typedef pair<int, int> P;
typedef long long LL;
int n, dp[50][4], p[50], t[50];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
  int i, j;
  int ans, flag, tmp;
  int tt;
  while (~scanf("%d", &n) && n) {
    for (i = 1; i <= n; i++)
      scanf("%d%d", &p[i], &t[i]);
    for (i = 0; i <= n; i++)
      for (j = 0; j < 4; j++)
        dp[i][j] = INF;
    dp[0][0] = 0;
    p[0] = t[0] = 0;
    flag = 1;
    for (i = 1; flag && i <= n; i++) {
      flag = 0;
      for (j = 0; j < 4; j++)
        if (dp[i - 1][j] != INF) {
          tt = t[i - 1] + p[i - 1] * (j + 1) + p[i];
          if (tt <= t[i]) {
            flag = 1;
            dp[i][1] = min(dp[i][1], dp[i - 1][j] + p[i - 1] + p[i]);
          }
          if (j != 3) {
            tt = t[i - 1] + abs(p[i] - p[i - 1]) * (j + 1);
            if (tt <= t[i]) {
              flag = 1;
              dp[i][j + 1] =
                  min(dp[i][j + 1], dp[i - 1][j] + abs(p[i] - p[i - 1]));
            }
          }
        }
      if (!flag)
        ans = i;
    }
    if (flag) {
      ans = INF;
      for (i = 0; i < 4; i++)
        ans = min(ans, dp[n][i] + p[n]);
    }
    if (!flag)
      printf("NG %d\\n", ans);
    else
      printf("OK %d\\n", ans);
  }
  return 0;
}