Problem p00885 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

struct Balloon {
  Balloon() {
    location = 0;
    time = 0;
  }
  Balloon(int arg_location, int arg_time) {
    location = arg_location;
    time = arg_time;
  }
  int location, time;
};

int main() {

  int N, ans, last_catch, dist, dp[41][4];
  bool FLG;
  Balloon info[41];

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    ans = BIG_NUM;
    last_catch = 0;

    for (int i = 1; i <= N; i++) {
      scanf("%d %d", &info[i].location, &info[i].time);
    }

    for (int i = 1; i <= N; i++) {
      for (int k = 0; k <= 3; k++)
        dp[i][k] = BIG_NUM;
    }

    if (info[1].location > info[1].time) {
      printf("NG 1\\n");
      continue;
    } else {
      dp[1][1] = info[1].location;
      last_catch = 1;
    }

    for (int i = 2; i <= N; i++) {
      FLG = false;
      for (int k = 0; k <= 3; k++) {
        if (dp[i - 1][k] == BIG_NUM)
          continue;

        dist = abs(info[i].location - info[i - 1].location);

        switch (k) {
        case 0:
          if (dist * (k + 1) <= info[i].time - info[i - 1].time) {
            dp[i][k + 1] = min(dp[i][k + 1], dp[i - 1][k] + dist);
            FLG = true;
          } else {
            // Do nothing
          }
          break;
        case 1:
        case 2:
          if (dist * (k + 1) <= info[i].time - info[i - 1].time) {
            dp[i][k + 1] = min(dp[i][k + 1], dp[i - 1][k] + dist);
            FLG = true;
          } else {
            // Do nothing
          }

          if (info[i - 1].location * (k + 1) + info[i].location <=
              info[i].time - info[i - 1].time) {
            dp[i][1] = min(dp[i][1], dp[i - 1][k] + info[i - 1].location +
                                         info[i].location);
            FLG = true;
          }
          break;
        case 3:
          if (info[i - 1].location * (k + 1) + info[i].location <=
              info[i].time - info[i - 1].time) {
            dp[i][1] = min(dp[i][1], dp[i - 1][k] + info[i - 1].location +
                                         info[i].location);
            FLG = true;
          }
          break;
        }
      }
      if (!FLG) {
        break;
      } else {
        last_catch = i;
      }
    }

    if (last_catch == N) {
      for (int i = 0; i <= 3; i++)
        ans = min(ans, dp[N][i]);
    }

    if (ans == BIG_NUM) {
      printf("NG %d\\n", last_catch + 1);
    } else {
      printf("OK %d\\n", ans + info[N].location);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

struct Balloon {
  Balloon() {
    location = 0;
    time = 0;
  }
  Balloon(int arg_location, int arg_time) {
    location = arg_location;
    time = arg_time;
  }
  int location, time;
};

struct Robot {
  Robot(int arg_location, int arg_time, int arg_num_balloon, int arg_sum,
        int arg_latest_balloon) {
    location = arg_location;
    time = arg_time;
    num_balloon = arg_num_balloon;
    sum = arg_sum;
    latest_balloon = arg_latest_balloon;
  }
  int location, time, num_balloon, sum, latest_balloon;
};

int main() {

  int N, ans, last_catch, next, dist;
  Balloon info[41];
  queue<Robot> Q;

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    ans = BIG_NUM;
    last_catch = -1;

    for (int i = 1; i <= N; i++) {
      scanf("%d %d", &info[i].location, &info[i].time);
    }

    Q.push(Robot(0, 0, 0, 0, 0));

    while (!Q.empty()) {

      last_catch = max(last_catch, Q.front().latest_balloon);

      next = Q.front().latest_balloon + 1;

      if (next == N + 1) {

        ans = min(ans, Q.front().sum + Q.front().location);
        Q.pop();
        continue;
      }

      dist = abs(Q.front().location - info[next].location);

      switch (Q.front().num_balloon) {
      case 0:
        if (dist * (Q.front().num_balloon + 1) <=
            info[next].time - Q.front().time) {
          Q.push(Robot(info[next].location, info[next].time, 1,
                       Q.front().sum + dist, next));
        } else {
          // Do nothing
        }
        break;
      case 1:
      case 2:
        if (dist * (Q.front().num_balloon + 1) <=
            info[next].time - Q.front().time) {
          Q.push(Robot(info[next].location, info[next].time,
                       Q.front().num_balloon + 1, Q.front().sum + dist, next));
        } else {
          // Do nothing
        }

        if (Q.front().location * (Q.front().num_balloon + 1) +
                info[next].location <=
            info[next].time - Q.front().time) {
          Q.push(Robot(info[next].location, info[next].time, 1,
                       Q.front().sum + Q.front().location + info[next].location,
                       next));
        }

        break;
      case 3:
        if (Q.front().location * (Q.front().num_balloon + 1) +
                info[next].location <=
            info[next].time - Q.front().time) {
          Q.push(Robot(info[next].location, info[next].time, 1,
                       Q.front().sum + Q.front().location + info[next].location,
                       next));
        }
        break;
      }
      Q.pop();
    }

    if (ans == BIG_NUM) {
      printf("NG %d\\n", last_catch + 1);
    } else {
      printf("OK %d\\n", ans);
    }
  }

  return 0;
}