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

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int abs(int x) { return x < 0 ? -x : x; }

int table[101][4];

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    vector<int> t(n + 1), p(n + 1);
    for (int i = 1; i <= n; ++i) {
      cin >> p[i] >> t[i];
    }
    memset(table, -1, sizeof(table));
    table[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < 4; ++j) {
        if (table[i - 1][j] < 0) {
          continue;
        }
        int a = (j + 1) * p[i - 1] + p[i];
        if (a <= t[i] - t[i - 1]) {
          int s = p[i - 1] + p[i];
          if (table[i][1] < 0 || table[i][1] > table[i - 1][j] + s) {
            table[i][1] = table[i - 1][j] + s;
          }
        }
        int b = (j + 1) * abs(p[i] - p[i - 1]);
        if (j < 3 && b <= t[i] - t[i - 1]) {
          if (table[i][j + 1] < 0 || table[i][j + 1] > table[i - 1][j] + b) {
            table[i][j + 1] = table[i - 1][j] + abs(p[i] - p[i - 1]);
          }
        }
      }
    }
    int answer = INT_MAX;
    for (int i = 0; i < 4; ++i) {
      if (table[n][i] >= 0) {
        answer = min(answer, table[n][i] + p[n]);
      }
    }
    if (answer == INT_MAX) {
      answer = 0;
      for (int i = n; i >= 0; --i) {
        bool flag = false;
        for (int j = 0; j < 4; ++j) {
          if (table[i][j] >= 0) {
            flag = true;
          }
        }
        if (flag) {
          answer = i + 1;
          break;
        }
      }
      cout << "NG " << answer << endl;
    } else {
      cout << "OK " << answer << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int abs(int x) { return x < 0 ? -x : x; }

int table[101][4];

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    vector<int> t(n), p(n);
    for (int i = 1; i <= n; ++i) {
      cin >> p[i] >> t[i];
    }
    memset(table, -1, sizeof(table));
    table[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < 4; ++j) {
        if (table[i - 1][j] < 0) {
          continue;
        }
        int a = (j + 1) * p[i - 1] + p[i];
        if (a <= t[i] - t[i - 1]) {
          int s = p[i - 1] + p[i];
          if (table[i][1] < 0 || table[i][1] > table[i - 1][j] + s) {
            table[i][1] = table[i - 1][j] + s;
          }
        }
        int b = (j + 1) * abs(p[i] - p[i - 1]);
        if (j < 3 && b <= t[i] - t[i - 1]) {
          if (table[i][j + 1] < 0 || table[i][j + 1] > table[i - 1][j] + b) {
            table[i][j + 1] = table[i - 1][j] + abs(p[i] - p[i - 1]);
          }
        }
      }
    }
    int answer = INT_MAX;
    for (int i = 0; i < 4; ++i) {
      if (table[n][i] >= 0) {
        answer = min(answer, table[n][i] + p[n]);
      }
    }
    if (answer == INT_MAX) {
      answer = 0;
      for (int i = n; i >= 0; --i) {
        bool flag = false;
        for (int j = 0; j < 4; ++j) {
          if (table[i][j] >= 0) {
            flag = true;
          }
        }
        if (flag) {
          answer = i + 1;
          break;
        }
      }
      cout << "NG " << answer << endl;
    } else {
      cout << "OK " << answer << endl;
    }
  }
  return 0;
}