alexjercan/bugnet huggingface dataset:
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int N;
map<string, int> Idx;
set<string> S[100];
vector<vector<string>> V;

void count(int num) {
  for (int i = 0; i < V[num].size(); i++) {
    if (Idx[V[num][i]] != 0) {
      int id = Idx[V[num][i]] - 1;
      if (S[id].empty())
        count(id);
      for (set<string>::iterator it = S[id].begin(); it != S[id].end(); it++)
        S[num].insert(*it);
    } else
      S[num].insert(V[num][i]);
  }
}

void solve() {
  int size = 1;
  Idx.clear();
  V.clear();
  V.resize(N);
  for (int i = 0; i < 100; i++)
    S[i].clear();
  for (int i = 0; i < N; i++) {
    string now;
    cin >> now;
    for (int j = 0; j < now.size(); j++)
      if (!islower(now[j]))
        now[j] = ' ';
    istringstream is(now);
    string temp;
    is >> temp;
    Idx[temp] = size++;
    while (is >> temp)
      V[i].push_back(temp);
  }
  count(0);
  cout << S[0].size() << endl;
}

int main() {
  while (cin >> N) {
    if (N == 0)
      break;
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int N;

void solve() {
  map<string, int> idx;
  int size = 1;
  vector<vector<string>> group(N);
  for (int i = 0; i < N; i++) {
    string now;
    cin >> now;
    string read = "";
    int t = 0;
    while (now[t] != ':')
      read += now[t++];
    t++;
    idx[read] = size++;
    read = "";
    while (now[t] != '.') {
      if (now[t] == ',') {
        group[i].push_back(read);
        read = "";
        t++;
        continue;
      }
      read += now[t++];
    }
    group[i].push_back(read);
  }
  int i = 0;
  while (true) {
    if (i >= group[0].size())
      break;
    if (idx[group[0][i]] != 0) {
      int num = idx[group[0][i]] - 1;
      group[0].erase(group[0].begin() + i);
      bool f = false;
      for (int j = 0; j < group[num].size(); j++) {
        group[0].push_back(group[num][j]);
        i = 0;
        f = true;
      }
      if (f)
        continue;
    }
    i++;
  }
  sort(group[0].begin(), group[0].end());
  group[0].erase(unique(group[0].begin(), group[0].end()), group[0].end());

  cout << group[0].size() << endl;
}

int main() {
  while (cin >> N) {
    if (N == 0)
      break;
    solve();
  }
  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef unordered_set<string> Set;
typedef unordered_map<string, Set> Map;

Set expand(Map &members, const string &group, Map &memo) {
  if (memo.count(group))
    return memo[group];
  Set r;
  for (auto &name : members[group]) {
    if (members.count(name)) {
      Set s = expand(members, name, memo);
      r.insert(s.begin(), s.end());
    } else {
      r.insert(name);
    }
  }
  memo[group] = r;
  return r;
}

int main() {
  int n;
  while (cin >> n, n) {
    string line;
    getline(cin, line); // skip LR
    string first_group;
    Map members;
    for (int i = 0; i < n; ++i) {
      getline(cin, line);
      size_t colon = line.find(':');
      string group_name = line.substr(0, colon);
      Set member;
      for (size_t idx = colon + 1;;) {
        size_t next = line.find(',', idx);
        if (next == string::npos)
          next = line.find('.', idx);
        member.insert(line.substr(idx, next - idx));
        if (line[next] == '.')
          break;
        idx = next + 1;
      }
      members[group_name] = member;
      if (i == 0)
        first_group = group_name;
    }

    Map memo;
    Set ret = expand(members, first_group, memo);
    cout << ret.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

typedef unordered_set<string> Set;
typedef unordered_map<string, Set> Map;

void expand(Map &members, string group, Set &ret) {
  for (auto &name : members[group]) {
    if (members.count(name)) {
      expand(members, name, ret);
    } else {
      ret.insert(name);
    }
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    string line;
    getline(cin, line); // skip LR
    string first_group;
    Map members;
    for (int i = 0; i < n; ++i) {
      getline(cin, line);
      size_t colon = line.find(':');
      string group_name = line.substr(0, colon);
      Set member;
      for (size_t idx = colon + 1;;) {
        size_t next = line.find(',', idx);
        if (next == string::npos)
          next = line.find('.', idx);
        member.insert(line.substr(idx, next - idx));
        if (line[next] == '.')
          break;
        idx = next + 1;
      }
      members[group_name] = member;
      if (i == 0)
        first_group = group_name;
    }

    Set ret;
    expand(members, first_group, ret);
    cout << ret.size() << endl;
  }
}
Problem p00884 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

void solve(vector<map<string, int>> &mp, map<string, int> &namelist, int i,
           bool (&check)[103]) {
  if (check[i])
    return;
  check[i] = true;
  for (auto itr = mp[i].begin(); itr != mp[i].end(); itr++) {
    if (namelist[itr->first]) {
      mp[i][itr->first] = 0;
      solve(mp, namelist, namelist[itr->first], check);
    } else {
      mp[1][itr->first]++;
    }
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<map<string, int>> mp(103, map<string, int>());
    map<string, int> namelist;
    bool check[103];
    fill(check, check + 103, false);
    for (int i = 1; i <= n; i++) {
      string s;
      cin >> s;
      int si = 0;
      while (s[si] != ':')
        si++;
      namelist[s.substr(0, si)] = i;
      int starts = si + 1;
      for (int sj = si; sj < s.size(); sj++) {
        if (s[sj] == ',' || s[sj] == '.') {
          mp[i][s.substr(starts, sj - starts)] = 1;
          starts = sj + 1;
        }
      }
    }
    solve(mp, namelist, 1, check);
    int ct = 0;
    for (auto itr = mp[1].begin(); itr != mp[1].end(); itr++) {
      if (itr->second > 0)
        ct++;
    }
    cout << ct << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

void solve(vector<map<string, int>> &mp, map<string, int> &namelist, int i,
           bool (&check)[11]) {
  if (check[i])
    return;
  check[i] = true;
  for (auto itr = mp[i].begin(); itr != mp[i].end(); itr++) {
    if (namelist[itr->first]) {
      mp[i][itr->first] = 0;
      solve(mp, namelist, namelist[itr->first], check);
    } else {
      mp[1][itr->first]++;
    }
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<map<string, int>> mp(11, map<string, int>());
    map<string, int> namelist;
    bool check[11];

    fill(check, check + 11, false);
    for (int i = 1; i <= n; i++) {
      string s;
      cin >> s;
      int si = 0;
      while (s[si] != ':')
        si++;
      namelist[s.substr(0, si)] = i;
      int starts = si + 1;
      for (int sj = si; sj < s.size(); sj++) {
        if (s[sj] == ',' || s[sj] == '.') {
          mp[i][s.substr(starts, sj - starts)] = 1;
          starts = sj + 1;
        }
      }
    }
    solve(mp, namelist, 1, check);
    int ct = 0;
    for (auto itr = mp[1].begin(); itr != mp[1].end(); itr++) {
      if (itr->second > 0)
        ct++;
    }
    cout << ct << endl;
  }
  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <vector>

using namespace std;

map<string, vector<string>> M;
map<string, bool> counted;

int N, ans;
string wanted;

void init() {
  M.clear();
  counted.clear();
  ans = 0;
}

void input() {

  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;

    vector<string> V;
    string tmp = "";
    string gname;

    for (int j = 0; j < s.length(); j++) {
      if (s[j] == ':') {
        gname = tmp;
        tmp = "";
        if (i == 0)
          wanted = gname;
      } else if (s[j] == ',' || s[j] == '.') {
        V.push_back(tmp);
        tmp = "";
      } else
        tmp += s[j];
    }

    M[gname] = V;
  }
}

void solve(string s) {

  vector<string> V = M[s];

  counted[s] = true;

  for (int i = 0; i < V.size(); i++) {
    if (M[V[i]].size() > 0 && !counted[V[i]])
      solve(V[i]);
    else if (counted[V[i]] == false) {
      counted[V[i]] = true;
      ans++;
    }
  }
}

int main() {

  while (cin >> N && N) {
    init();
    input();
    solve(wanted);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using namespace std;

map<string, vector<string>> M;
map<string, bool> counted;
int N, ans;
string wanted;

void init() {
  M.clear();
  counted.clear();
  ans = 0;
}

void input() {

  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;

    vector<string> V;
    string tmp = "";
    string gname;

    for (int j = 0; j < s.length(); j++) {
      if (s[j] == ':') {
        gname = tmp;
        tmp = "";
        if (i == 0)
          wanted = gname;
      } else if (s[j] == ',' || s[j] == '.') {
        V.push_back(tmp);
        tmp = "";
      } else
        tmp += s[j];
    }

    M[gname] = V;
  }
}

void solve(string s) {

  vector<string> V = M[s];

  for (int i = 0; i < V.size(); i++) {
    if (M[V[i]].size() > 0)
      solve(V[i]);
    else if (counted[V[i]] == false) {
      counted[V[i]] = true;
      ans++;
    }
  }
}

int main() {

  while (cin >> N && N) {
    init();
    input();
    solve(wanted);
    cout << ans << endl;
  }

  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int N;
vector<vector<int>> groups;
set<int> ans;
map<string, int> nameToId;
vector<bool> flgs;

int toId(string name) {
  auto it = nameToId.find(name);
  if (it == nameToId.end()) {
    int nid = nameToId.size();
    nameToId[name] = nid;
    groups.push_back(vector<int>());
    return nid;
  } else {
    return it->second;
  }
}

void dfs(int id) {
  if (flgs[id])
    return;
  flgs[id] = true;
  // cout << name << endl;
  for (auto &v : groups[id]) {
    // cout << v << endl;
    if (groups[v].size()) {
      dfs(v);
    } else {
      ans.insert(v);
    }
  }
}

int main() {
  while (cin >> N, N) {
    groups.clear();
    nameToId.clear();
    for (int i = 0; i < N; i++) {
      string str;
      cin >> str;
      int idx = str.find(":");
      int g = toId(str.substr(0, idx));
      idx++;
      int idx2;
      while ((idx2 = str.find(",", idx)) != string::npos) {
        groups[g].push_back(toId(str.substr(idx, idx2 - idx)));
        idx = idx2 + 1;
      }
      groups[g].push_back(toId(str.substr(idx, str.size() - idx - 1)));
    }
    ans.clear();
    flgs.assign(groups.size(), false);
    dfs(0);
    cout << ans.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int N;
map<string, vector<string>> groups;
set<string> ans;

void dfs(string &name) {
  // cout << name << endl;
  for (auto &v : groups[name]) {
    // cout << v << endl;
    if (groups.find(v) == groups.end()) {
      ans.insert(v);
    } else {
      dfs(v);
    }
  }
}

int main() {
  while (cin >> N, N) {
    string first;
    groups.clear();
    for (int i = 0; i < N; i++) {
      string str;
      cin >> str;
      int idx = str.find(":");
      string g = str.substr(0, idx);
      if (i == 0)
        first = g;
      idx++;
      int idx2;
      while ((idx2 = str.find(",", idx)) != string::npos) {
        groups[g].push_back(str.substr(idx, idx2 - idx));
        idx = idx2 + 1;
      }
      groups[g].push_back(str.substr(idx, str.size() - idx - 1));
    }
    ans.clear();
    dfs(first);
    cout << ans.size() << endl;
  }
}
Problem p00884 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;
int n;
map<string, vector<string>> tbl;
set<string> T;
map<string, bool> used;

void solve(string s) {
  queue<string> Q;
  Q.push(s);
  while (Q.size()) {
    string u = Q.front();
    Q.pop();
    if (used[u])
      continue;
    used[u] = true;
    if (tbl[u].empty())
      T.insert(u);
    else
      for (string v : tbl[u])
        Q.push(v);
  }
}

int main() {
  while (cin >> n, n) {
    tbl.clear();
    T.clear();
    used.clear();
    string key0 = "";
    for (int i = 0; i < n; ++i) {
      string s;
      cin >> s;
      for (int j = 0; j < s.length(); ++j)
        if (s[j] == ':' || s[j] == ',' || s[j] == '.')
          s[j] = ' ';
      string key;
      stringstream ss;
      ss << s;
      ss >> key;
      if (key0 == "")
        key0 = key;
      string tmp;
      while (ss >> tmp)
        tbl[key].push_back(tmp);
    }
    solve(key0);
    cout << T.size() << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;
int n;
map<string, vector<string>> tbl;
set<string> T;

void solve(string s) {
  queue<string> Q;
  Q.push(s);
  while (Q.size()) {
    string u = Q.front();
    Q.pop();
    if (tbl[u].empty())
      T.insert(u);
    else
      for (string v : tbl[u])
        Q.push(v);
  }
}

int main() {
  while (cin >> n, n) {
    tbl.clear();
    T.clear();
    string key0 = "";
    for (int i = 0; i < n; ++i) {
      string s;
      cin >> s;
      for (int j = 0; j < s.length(); ++j)
        if (s[j] == ':' || s[j] == ',' || s[j] == '.')
          s[j] = ' ';
      string key;
      stringstream ss;
      ss << s;
      ss >> key;
      if (key0 == "")
        key0 = key;
      string tmp;
      while (ss >> tmp)
        tbl[key].push_back(tmp);
    }
    solve(key0);
    cout << T.size() << endl;
  }

  return 0;
}
Problem p00885 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
bool direct(int i, pii b, pii bb) {
  return abs(b.second - bb.second) * (i + 2) <= bb.first - b.first;
}

bool back(int i, pii b, pii bb) {
  return bb.second + b.second * (i + 2) <= bb.first - b.first;
}
int main() {
  int n;
  while (cin >> n, n) {
    vector<pii> b(n);
    for (int i = 0; i < n; i++) {
      cin >> b[i].second >> b[i].first;
    }
    sort(b.begin(), b.end());
    int dp[3][n];
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < n; j++) {
        dp[i][j] = 1e9;
      }
    }
    dp[0][0] = b[0].second;
    if (b[0].second > b[0].first) {
      cout << "NG " << 1 << endl;
      continue;
    }
    for (int j = 0; j < n - 1; j++) {
      for (int i = 0; i < 3; i++) {
        if (dp[i][j] == 1e9)
          continue;
        if (i < 2 && direct(i, b[j], b[j + 1])) {
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1],
                                 dp[i][j] + abs(b[j].second - b[j + 1].second));
        }
        if (back(i, b[j], b[j + 1])) {
          dp[0][j + 1] =
              min(dp[0][j + 1], dp[i][j] + b[j].second + b[j + 1].second);
        }
      }
    }
    int ans = 1e9;
    for (int i = 0; i < 3; i++) {
      ans = min(ans, dp[i][n - 1]);
    }
    if (ans != 1e9) {
      cout << "OK " << ans + b[n - 1].second << endl;
    } else {

      for (int j = n - 1; j >= 0; j--) {
        for (int i = 0; i < 3; i++) {
          if (dp[i][j] != 1e9) {
            cout << "NG " << j + 2 << endl;
            j = 0;
            break;
          }
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
int maxdep = 0;
int dfs(const vector<pii> &b, int depth = 0, int cost = 0, int seq = 0) {
  maxdep = max(depth, maxdep);
  if (depth == b.size() - 1) {
    return cost + b[depth].second;
  }
  int res = 1e9;
  if (abs(b[depth].second - b[depth + 1].second) * (seq + 1) <=
          b[depth + 1].first - b[depth].first &&
      seq < 3) {
    // cout << "depth:" << depth << " seq:" << seq << " cost:" << cost << endl;
    res = min(res,
              dfs(b, depth + 1,
                  cost + abs(b[depth].second - b[depth + 1].second), seq + 1));
  }
  if (b[depth].second * (seq + 1) + b[depth + 1].second <=
      b[depth + 1].first - b[depth].first) {
    res = min(res, dfs(b, depth + 1,
                       cost + b[depth].second + b[depth + 1].second, 1));
  }
  return res;
}

int main() {
  int n;
  while (cin >> n, n) {
    maxdep = 0;
    vector<pii> balloon(n + 1);
    balloon[0] = make_pair(0, 0);
    for (int i = 1; i <= n; i++) {
      cin >> balloon[i].second >> balloon[i].first;
    }
    int ans = 1e9;
    sort(balloon.begin(), balloon.end());
    ans = dfs(balloon);
    if (ans == 1e9)
      cout << "NG " << maxdep + 1 << endl;
    else
      cout << "OK " << ans << endl;
  }
}
Problem p00885 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

int g_dis;
int ng;
int n;
pair<int, int> p[50]; //???????????????

void dfs(int i, int d, int k) {
  if (k > 3)
    return;
  //    show(t)
  //        show(i)
  int c = i == 0 ? 0 : p[i - 1].first;
  int t = i == 0 ? 0 : p[i - 1].second;
  if (g_dis < d)
    return;
  if (i == n) {
    g_dis = min(g_dis, d + c);
    return;
  }

  // show(p[i].second)
  // show(dist * (k + 1))
  // show(c * (k + 1) + p[i].first)

  int dist = abs(p[i].first - c);
  ng = max(ng, i);
  if (p[i].second >= t + dist * (k + 1))
    dfs(i + 1, d + dist, k + 1);
  if (p[i].second >= t + c * (k + 1) + p[i].first)
    dfs(i + 1, d + c + p[i].first, 1);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> n, n) {
    g_dis = 1e8;
    ng = 0;
    rep(i, n) cin >> p[i].first >> p[i].second;
    dfs(0, 0, 0);
    if (g_dis == 1e8)
      cout << "NG " << ng + 1 << endl;
    else
      cout << "OK " << g_dis << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

int g_dis;
int ng;
pair<int, int> p[50]; //???????????????

void dfs(int n, int i, int c, int d, int t, int k) {
  //    show(t)
  //        show(i)
  if (i == n) {
    g_dis = min(g_dis, d + c);
    return;
  }

  int dist = abs(p[i].first - c);
  bool f1 = (p[i].second < t + dist * (k + 1));
  bool f2 = (p[i].second < t + c * (k + 1) + p[i].first);
  // show(p[i].second)
  // show(dist * (k + 1))
  // show(c * (k + 1) + p[i].first)

  if (f1 && f2) {
    ng = min(ng, i);
    return;
  } else {
    if (k != 0 && not f1)
      dfs(n, i + 1, p[i].first, d + dist, p[i].second, k + 1);
    if (not f2)
      dfs(n, i + 1, p[i].first, d + c + p[i].first, p[i].second, 1);
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    g_dis = 1e8;
    ng = 1e8;
    rep(i, n) cin >> p[i].first >> p[i].second;
    dfs(n, 0, 0, 0, 0, 0);
    if (g_dis == 1e8)
      cout << "NG " << ng + 1 << endl;
    else
      cout << "OK " << g_dis << endl;
  }
}
Problem p00885 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
using namespace std;

const int N_MAX = 40;
int ps[N_MAX + 1];
int ts[N_MAX + 1];
int c[N_MAX + 1][4];

int main() {
  int n;
  while (cin >> n, n) {
    for (int i = 1; i <= n; i++) {
      cin >> ps[i] >> ts[i];
    }
    if (ps[1] > ts[1]) {
      cout << "NG " << 1 << endl;
      continue;
    }
    for (int i = 1; i <= n; i++) {
      fill_n(c[i], 4, INT_MAX);
    }
    c[0][0] = 0;
    c[1][1] = ps[1];
    int kth = -1;
    for (int i = 1; i < n; i++) {
      int dp = abs(ps[i + 1] - ps[i]);
      int dt = ts[i + 1] - ts[i];
      bool capture = false;
      for (int j = 1; j < 4; j++) {
        if (c[i][j] == INT_MAX) {
          continue;
        }
        if (j < 3 && dp * (j + 1) <= dt) {
          c[i + 1][j + 1] = min(c[i + 1][j + 1], c[i][j] + dp);
          capture = true;
        }
        if (ps[i] * (j + 1) + ps[i + 1] <= dt) {
          c[i + 1][1] = min(c[i + 1][1], c[i][j] + ps[i] + ps[i + 1]);
          capture = true;
        }
      }
      if (!capture) {
        kth = i + 1;
        break;
      }
    }
    if (kth < 0) {
      int d_min = min(c[n][1], min(c[n][2], c[n][3])) + ps[n];
      cout << "OK " << d_min << endl;
    } else {
      cout << "NG " << kth << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

const int N_MAX = 40;
int n;
int p[N_MAX + 1];
int t[N_MAX + 1];
int d_min;
int kth;

void rec_run(int idx, int carry, int dist) {
  if (idx == n + 1) {
    dist += p[idx - 1];
    if (d_min < 0 || dist < d_min) {
      d_min = dist;
    }
    return;
  }

  bool capture = false;
  int dp = abs(p[idx] - p[idx - 1]);
  int dt = t[idx] - t[idx - 1];
  if (carry < 3 && dp * (carry + 1) <= dt) {
    capture = true;
    rec_run(idx + 1, carry + 1, dist + dp);
  }
  if (0 < carry && p[idx - 1] * (carry + 1) + p[idx] <= dt) {
    capture = true;
    rec_run(idx + 1, 1, dist + p[idx - 1] + p[idx]);
  }
  if (!capture) {
    kth = idx;
  }
}

int main() {
  while (cin >> n, n) {
    for (int i = 1; i <= n; i++) {
      cin >> p[i] >> t[i];
    }
    p[0] = t[0] = 0;
    d_min = -1;
    rec_run(1, 0, 0);
    if (d_min < 0) {
      cout << "NG " << kth << endl;
    } else {
      cout << "OK " << d_min << endl;
    }
  }
}
Problem p00885 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF (1 << 28)

int n, p[100], t[100], dp[100][4];

void solve() {
  rep(i, 100) rep(j, 4) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, n - 1) rep(j, 4) {
    const int dt = t[i + 1] - t[i];
    if ((j + 1) * p[i] + p[i + 1] <= dt) {
      dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + p[i] + p[i + 1]);
    }
    if (j < 3 && (j + 1) * abs(p[i] - p[i + 1]) <= dt) {
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(p[i] - p[i + 1]));
    }
  }
  rep(i, n) {
    bool ok = false;
    rep(j, 4) if (dp[i][j] < INF) ok = true;
    if (!ok) {
      printf("NG %d\\n", i);
      return;
    }
  }
  int ans = INF;
  rep(j, 4) ans = min(ans, dp[n - 1][j] + p[n - 1]);
  printf("OK %d\\n", ans);
}

int main() {
  for (;;) {
    scanf("%d", &n);
    if (n == 0)
      return 0;
    p[0] = t[0] = 0;
    rep(i, n) scanf("%d%d", p + i + 1, t + i + 1);
    n++;
    solve();
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF (1 << 28)

int n, p[100], t[100], dp[100][4];

void solve() {
  rep(i, 100) rep(j, 4) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, n - 1) rep(j, 4) {
    const int dt = t[i + 1] - t[i];
    if ((j + 1) * p[i] + p[i + 1] <= dt) {
      dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + p[i] + p[i + 1]);
    }
    if (j < 3 && (j + 1) * abs(p[i] - p[i + 1]) <= dt) {
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(p[i] - p[i + 1]));
    }
  }
  rep(i, n) {
    bool ok = false;
    rep(j, 4) if (dp[i][j] < INF) ok = true;
    if (!ok) {
      printf("NG %d\\n", i);
      return;
    }
  }
  int ans = INF;
  rep(j, 4) ans = min(ans, dp[n - 1][j] + p[n - 1]);
  printf("OK %d\\n", ans);
}

int main() {
  for (;;) {
    scanf("%d", &n);
    p[0] = t[0] = 0;
    rep(i, n) scanf("%d%d", p + i + 1, t + i + 1);
    n++;
    solve();
  }
}