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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 50

int f[maxn][4];
int p[maxn], t[maxn];
int maxint = 2000000000;

int turn(int x) { return (x > 0 ? x : -x); }

int min(int a, int b) { return (a < b ? a : b); }

int main() {
  int n;
  int flag;

  while (scanf("%d", &n) && n) {
    int i, j;
    flag = 0;
    for (i = 1; i <= n; i++)
      scanf("%d %d", &p[i], &t[i]);
    for (i = 1; i <= n; i++)
      for (j = 0; j < 4; j++)
        f[i][j] = maxint;
    if (p[1] <= t[1])
      f[1][1] = p[1];
    else {
      printf("NG 1\\n");
      flag = 1;
    }
    for (i = 2; i <= n && !flag; i++) {
      if (t[i] >= t[i - 1] + 2 * p[i - 1] + p[i])
        f[i][1] = min(f[i][1], f[i - 1][1] + p[i - 1] + p[i]);
      if (t[i] >= t[i - 1] + 2 * turn(p[i] - p[i - 1]))
        f[i][2] = min(f[i][2], f[i - 1][1] + turn(p[i] - p[i - 1]));
      if (t[i] >= t[i - 1] + 3 * p[i - 1] + p[i])
        f[i][1] = min(f[i][1], f[i - 1][2] + p[i - 1] + p[i]);
      if (t[i] >= t[i - 1] + 3 * turn(p[i] - p[i - 1]))
        f[i][3] = min(f[i][3], f[i - 1][2] + turn(p[i] - p[i - 1]));
      if (t[i] >= t[i - 1] + 4 * p[i - 1] + p[i])
        f[i][1] = min(f[i][1], f[i - 1][3] + p[i - 1] + p[i]);
      int num = 0;
      for (j = 1; j <= 3; j++)
        if (f[i][j] == maxint)
          num++;
      if (num == 3) {
        printf("NG %d\\n", i);
        flag = 1;
        break;
      }
    }
    if (!flag) {
      int ans = maxint;
      for (i = 1; i <= 3; i++)
        ans = min(ans, f[n][i] + p[n]);
      printf("OK %d\\n", ans);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int inf = 123456789;
int n, pp, tt, p, t, f0, f1, f2, f3, ff1, ff2, ff3, i, k, flag, min1, j;
int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    pp = 0;
    tt = 0;
    flag = 1;
    f0 = 0;
    f1 = -1;
    f2 = -1;
    f3 = -1;
    for (k = 1; k <= n; k++) {
      scanf("%d%d", &p, &t);
      ff1 = -1;
      ff2 = -1;
      ff3 = -1;
      j = p - pp;
      if (j < 0)
        j = -j;
      if (k != 1) {
        min1 = inf;
        if (f1 != -1)
          if ((2 * pp + p) <= (t - tt))
            if (min1 > f1 + pp * 2)
              min1 = f1 + pp * 2;
        if (f2 != -1)
          if ((3 * pp + p) <= (t - tt))
            if (min1 > f2 + pp * 2)
              min1 = f2 + pp * 2;
        if (f3 != -1)
          if ((4 * pp + p) <= (t - tt))
            if (min1 > f3 + pp * 2)
              min1 = f3 + pp * 2;
        if (min1 == inf)
          f0 = -1;
        else
          f0 = min1;
        if (f0 != -1)
          ff1 = f0 + j;
      }
      if (k == 1)
        if (j <= (t - tt))
          ff1 = f0 + j;
      if (f1 != -1)
        if (2 * j <= (t - tt))
          ff2 = f1 + j;
      if (f2 != -1)
        if (3 * j <= (t - tt))
          ff3 = f2 + j;
      if ((ff1 == -1) && (ff2 == -1) && (ff3 == -1)) {
        flag = 0;
        printf("NG %d\\n", k);
        break;
      }
      f1 = ff1;
      f2 = ff2;
      f3 = ff3;
      //   printf("%d %d %d\\n",f1,f2,f3);
      tt = t;
      pp = p;
    }
    if (flag) {
      min1 = inf;
      if ((f1 < min1) && (f1 != -1))
        min1 = f1;
      if ((f2 < min1) && (f2 != -1))
        min1 = f2;
      if ((f3 < min1) && (f3 != -1))
        min1 = f3;
      min1 += p;
      printf("OK %d\\n", min1);
    }
  }
  return 0;
}