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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
vector<P> info; // position,time

bool iscan;
int ans;
int failedPosition;

void rec(int time, int k, int distance, int position) {
  if (distance >= ans)
    return;

  if (position >= info.size() - 1) {
    ans = min(ans, distance + info[position].F);
    // cout << "ans = " << ans << endl;
    return;
  }

  int time1, time2; // time1 -> そのまま次へ, time2 -> 一旦家に帰る
  time1 = time + (k + 1) * abs(info[position + 1].F - info[position].F);
  time2 =
      time + (k + 1) * abs(info[position].F - info[0].F) + info[position + 1].F;

  if (k == 3) {
    if (time2 <= info[position + 1].S) {
      rec(info[position + 1].S, 1,
          distance + abs(info[position].F - info[0].F) + info[position + 1].F,
          position + 1);
      return;
    } else {
      iscan = false;
      failedPosition = position + 1;
      return;
    }
  }

  if (time1 <= info[position + 1].S && time2 <= info[position + 1].S) {
    rec(info[position + 1].S, k + 1,
        distance + abs(info[position + 1].F - info[position].F), position + 1);
    rec(info[position + 1].S, 1,
        distance + abs(info[position].F - info[0].F) + info[position + 1].F,
        position + 1);
  } else if (time1 <= info[position + 1].S) {
    rec(info[position + 1].S, k + 1,
        distance + abs(info[position + 1].F - info[position].F), position + 1);
  } else if (time2 <= info[position + 1].S) {
    rec(info[position + 1].S, 1,
        distance + abs(info[position].F - info[0].F) + info[position + 1].F,
        position + 1);
  } else {
    iscan = false;
    failedPosition = position + 1;
    return;
  }
}

int main() {
  int n, p, t;
  const int INF = 999999999;

  while (cin >> n && n) {
    ans = INF;
    info.clear();
    iscan = true;

    info.push_back(P(0, 0));
    for (int i = 0; i < n; i++) {
      cin >> p >> t;
      info.push_back(P(p, t));
    }

    rec(0, 0, 0, 0);

    if (!iscan && ans == INF) {
      cout << "NG " << failedPosition << endl;
    } else {
      cout << "OK " << ans << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
vector<P> info; // position,time

bool iscan;
int ans;
int failedPosition;

void rec(int time, int k, int distance, int position) {

  if (position >= info.size() - 1) {
    ans = min(ans, distance + info[position].F);
    // cout << "ans = " << ans << endl;
    return;
  }

  int time1, time2; // time1 -> そのまま次へ, time2 -> 一旦家に帰る
  time1 = time + (k + 1) * abs(info[position + 1].F - info[position].F);
  time2 =
      time + (k + 1) * abs(info[position].F - info[0].F) + info[position + 1].F;
  if (time1 <= info[position + 1].S && time2 <= info[position + 1].S) {
    // cout << "position is " << position << ", if-1" << endl;
    rec(info[position + 1].S, k + 1,
        distance + abs(info[position + 1].F - info[position].F), position + 1);
    if (position != 0)
      rec(info[position + 1].S, 1,
          distance + abs(info[position].F - info[0].F) + info[position + 1].F,
          position + 1);
  } else if (time1 <= info[position + 1].S) { // cout << "position is " <<
                                              // position << ", if-2" << endl;
    rec(info[position + 1].S, k + 1,
        distance + abs(info[position + 1].F - info[position].F), position + 1);
  } else if (time2 <= info[position + 1].S) { // cout << "position is " <<
                                              // position << ", if-3" << endl;
    rec(info[position + 1].S, 1,
        distance + abs(info[position].F - info[0].F) + info[position + 1].F,
        position + 1);
  } else {
    iscan = false;
    failedPosition = position + 1;
    return;
  }
}

int main() {
  int n, p, t;
  const int INF = 1 << 30;

  while (cin >> n && n) {
    ans = INF;
    info.clear();
    iscan = true;

    info.push_back(P(0, 0));
    for (int i = 0; i < n; i++) {
      cin >> p >> t;
      info.push_back(P(p, t));
    }

    rec(0, 0, 0, 0);

    if (!iscan && ans == INF) {
      cout << "NG " << failedPosition << endl;
    } else {
      cout << "OK " << ans << endl;
    }
  }
  return 0;
}