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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

int n;
int id[17], m[17], w[17];

double dp[1 << 16][16];
int dp_last[1 << 16][16];

double dfs(int S, int v, int we) {
  if (dp[S][v] != 1e300)
    return dp[S][v];
  if (S == (1 << n) - 1)
    return dp[S][v] = 0.0;

  double ret = 1e300;
  int last = -1;
  FOR(i, n) {
    if (!(S & (1 << i))) {
      double val = dfs(S | (1 << i), i, we + w[i]);
      int len = abs(m[v] - m[i]);
      val += len / (2000.0 / (70 + we * 20));
      if (ret > val) {
        ret = val;
        last = i;
      }
    }
  }

  dp_last[S][v] = last;
  return dp[S][v] = ret;
}

int main() {
  cin >> n;
  FOR(i, n) cin >> id[i] >> m[i] >> w[i];

  FOR(i, 1 << 16) FOR(j, 16) { dp[i][j] = 1e300; }

  dfs(0, n, 0);

  int last = dp_last[0][n];
  int S = (1 << last);
  vector<int> ans;
  ans.push_back(last);
  while (S != (1 << n) - 1) {
    last = dp_last[S][last];
    S |= (1 << last);
    ans.push_back(last);
  }

  FOR(i, n) { printf("%d%c", id[ans[i]], i == n - 1 ? '\\n' : ' '); }

  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

#define FOR(i, n) for (int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

int n;
int id[15], m[15], w[15];

int main() {
  cin >> n;
  FOR(i, n) cin >> id[i] >> m[i] >> w[i];

  vector<int> v(n);
  FOR(i, n) v[i] = i;
  double ans = 1e300;
  vector<int> ans_v;
  do {
    int c_w = 0;
    double c_t = 0;
    int c_m = 0;
    FOR(i, n) {
      c_t = abs(m[v[i]] - c_m) / (2000.0 / (70 + c_w));
      c_m = m[v[i]];
      c_w += w[v[i]];
    }
    if (ans > c_t) {
      ans = c_t;
      ans_v.clear();
      copy(v.begin(), v.end(), back_inserter(ans_v));
    }
  } while (next_permutation(v.begin(), v.end()));

  FOR(i, n) { printf("%d%c", id[ans_v[i]], i == n - 1 ? '\\n' : ' '); }

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

#include <iostream>
#include <vector>
using namespace std;
struct state {
  vector<int> v;
  long long cost;
};
int n, id[19], p[19], w[19], sum[32777], popcount[32777];
state dp[19][32777];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> id[i] >> p[i] >> w[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 1 << i; j < 2 << i; j++) {
      sum[j] = sum[j - (1 << i)] + w[i] * 20;
      popcount[j] = popcount[j - (1 << i)] + 1;
    }
  }
  for (int i = 0; i < n; i++)
    dp[i][1 << i] = state({vector<int>({i}), 0});
  for (int i = 1; i < 1 << n; i++) {
    if (popcount[i] == 1)
      continue;
    for (int j = 0; j < n; j++) {
      if (!(i & (1 << j)))
        continue;
      dp[j][i].cost = 1LL << 60;
      int sub = i - (1 << j);
      for (int k = 0; k < n; k++) {
        if (!(sub & (1 << k)))
          continue;
        long long cost =
            dp[k][sub].cost + 1LL * (70 + sum[sub]) * abs(p[j] - p[k]);
        if (dp[j][i].cost > cost) {
          dp[j][i].cost = cost;
          dp[j][i].v = dp[k][sub].v;
          dp[j][i].v.push_back(j);
        }
      }
    }
  }
  int pos = 0;
  for (int i = 1; i < n; i++) {
    if (dp[i][(1 << n) - 1].cost < dp[pos][(1 << n) - 1].cost) {
      pos = i;
    }
  }
  vector<int> ret = dp[pos][(1 << n) - 1].v;
  for (int i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << id[ret[i]];
  }
  cout << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#ifdef _MSC_VER
#define __builtin_popcount(x) __popcnt(x)
#endif
using namespace std;
struct state {
  vector<int> v;
  long double t;
  int w;
  state() : v(vector<int>()), t(1e10L), w(0){};
  state(vector<int> v_, long double t_, int w_) : v(v_), t(t_), w(w_){};
};
bool operator==(const state &s1, const state &s2) {
  return s1.v == s2.v && abs(s1.t - s2.t) < 1e-10L && s1.w == s2.w;
}
bool operator<(const state &s1, const state &s2) { return s1.t < s2.t; }
int n, a[22], b[22], c[22];
state dp[2100000];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  for (int i = 0; i < n; i++)
    dp[1 << i] = state({i}, 0.0L, c[i]);
  for (int i = 1; i < (1 << n); i++) {
    if (__builtin_popcount(i) > 1) {
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          state u = dp[i - (1 << j)];
          u.t += 1.0L * abs(b[u.v.back()] - b[j]) * (u.w + 70.0L) / 2000.0L;
          u.w += c[j] * 20;
          u.v.push_back(j);
          dp[i] = min(dp[i], u);
        }
      }
    }
  }
  vector<int> ret = dp[(1 << n) - 1].v;
  for (int i = 0; i < n; i++)
    printf(i + 1 != n ? "%d " : "%d\\n", a[ret[i]]);
  return 0;
}
Problem p00146 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

const int MAX_N = 15, INF = 1 << 24;
const double EPS = 1e-10;
int ns[16], d[16], cost[16][16], w[16], weight[1 << MAX_N];
int n, next[1 << MAX_N][16];
double dp[1 << MAX_N][16];

double rec(int S, int v) {
  if (dp[S][v] > -EPS) {
    return dp[S][v];
  }

  if (S == (1 << n) - 1) {
    return 0;
  }

  double min_res = INF;
  for (int i = 0; i < n; i++) {
    if (!(S >> i & 1)) {
      double res =
          (70.0 + weight[S]) * cost[v][i] / 2000.0 + rec(S | (1 << i), i);
      if (res < min_res + EPS) {
        min_res = res;
        next[S][v] = i;
      }
    }
  }

  return dp[S][v] = min_res;
}

int main() {
  std::cin >> n;

  for (int i = 0; i < n; i++) {
    std::cin >> ns[i] >> d[i] >> w[i];
  }

  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < n; j++) {
      if (i >> j & 1) {
        weight[i] += w[j];
      }
    }
    weight[i] *= 20;
    std::fill(dp[i], dp[i] + 16, -1);
  }

  for (int i = 0; i < n; i++) {
    cost[n][i] = cost[i][n] = 0;
    for (int j = i + 1; j < n; j++) {
      cost[i][j] = cost[j][i] = abs(d[i] - d[j]);
    }
  }

  rec(0, n);

  std::vector<int> route;
  int S = 0, u = n;
  while (S != (1 << n) - 1) {
    int v = next[S][u];
    S |= (1 << v);
    u = v;
    route.push_back(ns[u]);
  }

  std::cout << route[0];
  for (int i = 1; i < route.size(); i++) {
    std::cout << " " << route[i];
  }
  std::cout << std::endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

const int MAX_N = 15, INF = 1 << 24;
const double EPS = 1e-10;
int ns[16], d[16], cost[16][16], w[16], weight[1 << MAX_N];
int n, next[1 << MAX_N];
double dp[1 << MAX_N][16];

double rec(int S, int v) {
  if (dp[S][v] > -EPS) {
    return dp[S][v];
  }

  if (S == (1 << n) - 1) {
    return 0;
  }

  double min_res = INF;
  for (int i = 0; i < n; i++) {
    if (!(S >> i & 1)) {
      double res = (70.0 + 20.0 * (weight[S] + w[i])) * cost[v][i] / 2000.0 +
                   rec(S | (1 << i), i);
      if (res < min_res + EPS) {
        min_res = res;
        next[S] = i;
      }
    }
  }

  return dp[S][v] = min_res;
}

int main() {
  std::cin >> n;

  // どの点にも0で行ける蔵をつくる
  ns[n] = -1;
  w[n] = 0;

  for (int i = 0; i < n; i++) {
    std::cin >> ns[i] >> d[i] >> w[i];
  }

  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < n; j++) {
      if (i >> j & 1) {
        weight[i] += w[j];
      }
    }
    weight[i] *= 20;
    std::fill(dp[i], dp[i] + 16, -1);
  }

  for (int i = 0; i <= n; i++) {
    cost[n][i] = cost[i][n] = 0;
    for (int j = i + 1; j <= n; j++) {
      cost[i][j] = cost[j][i] = abs(d[i] - d[j]);
    }
  }

  rec(0, n);

  std::vector<int> route;
  int S = 0, u = n;
  while (true) {
    if (ns[u] != -1) {
      route.push_back(ns[u]);
    }
    if (S == (1 << n) - 1)
      break;

    int v = next[S];
    S |= (1 << v);
    u = v;
  }

  std::cout << route[0];
  for (int i = 1; i < route.size(); i++) {
    std::cout << " " << route[i];
  }
  std::cout << std::endl;
}
Problem p00146 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
const int N = 1e1 + 10;
const int MAXN = 1 << 16;
const double INF = 1 << 30;
int abs(int a) { return a > 0 ? a : -a; }
int main() {
  int n, s[N], d[N], v[N], from[MAXN][N], val[MAXN], all, now = 0, top;
  vector<int> ans;
  double dp[MAXN][N], temp;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%d", &s[i], &d[i], &v[i]);
  all = 1 << n;
  for (int i = 0; i < all; i++) {
    val[i] = 0;
    for (int j = 0; j < n; j++) {
      dp[i][j] = INF;
      if (i & (1 << j))
        val[i] += v[j];
    }
    val[i] *= 20;
  }
  for (int i = 0; i < n; i++) {
    from[1 << i][i] = -1;
    dp[1 << i][i] = ((double)70 / 2000) * d[i];
  }
  for (int i = 0; i < all; i++)
    for (int j = 0; j < n; j++)
      if (i & (1 << j)) {
        for (int k = 0; k < n; k++)
          if (k != j && i & (1 << k)) {
            temp = dp[i ^ (1 << j)][k] +
                   ((double)(70 + val[i ^ (1 << j)]) / 2000) * abs(d[j] - d[k]);
            if (dp[i][j] > temp) {
              from[i][j] = k;
              dp[i][j] = temp;
            }
          }
      }
  all--;
  top = dp[all][0];
  for (int i = 1; i < n; i++)
    if (dp[all][i] < top) {
      top = dp[all][i];
      now = i;
    }
  while (all > 0) {
    ans.PB(s[now]);
    top = all;
    all -= 1 << now;
    now = from[top][now];
  }
  printf("%d", ans.back());
  for (int i = static_cast<int>(ans.size() - 2); i >= 0; i--)
    printf(" %d", ans[i]);
  printf("\\n");
}


Error: -11

Fixed submission:

#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
const int N = 1e2 + 10;
const int MAXN = 1 << 16;
const double INF = 1 << 30;
int abs(int a) { return a > 0 ? a : -a; }
int main() {
  int n, s[N], d[N], v[N], from[MAXN][N], val[MAXN], all, now = 0, top;
  vector<int> ans;
  double dp[MAXN][N], temp;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%d", &s[i], &d[i], &v[i]);
  all = 1 << n;
  for (int i = 0; i < all; i++) {
    val[i] = 0;
    for (int j = 0; j < n; j++) {
      dp[i][j] = INF;
      if (i & (1 << j))
        val[i] += v[j];
    }
    val[i] *= 20;
  }
  for (int i = 0; i < n; i++) {
    from[1 << i][i] = -1;
    dp[1 << i][i] = ((double)70 / 2000) * d[i];
  }
  for (int i = 0; i < all; i++)
    for (int j = 0; j < n; j++)
      if (i & (1 << j)) {
        for (int k = 0; k < n; k++)
          if (k != j && i & (1 << k)) {
            temp = dp[i ^ (1 << j)][k] +
                   ((double)(70 + val[i ^ (1 << j)]) / 2000) * abs(d[j] - d[k]);
            if (dp[i][j] > temp) {
              from[i][j] = k;
              dp[i][j] = temp;
            }
          }
      }
  all--;
  top = dp[all][0];
  for (int i = 1; i < n; i++)
    if (dp[all][i] < top) {
      top = dp[all][i];
      now = i;
    }
  while (all > 0) {
    ans.PB(s[now]);
    top = all;
    all -= 1 << now;
    now = from[top][now];
  }
  printf("%d", ans.back());
  for (int i = static_cast<int>(ans.size() - 2); i >= 0; i--)
    printf(" %d", ans[i]);
  printf("\\n");
}
Problem p00146 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

const double INF = 10000000000000;
int n;
int num[16];
double to[16];
double s[16];
double dp[1 << 16][16];
double ans = INF;
int r[16];
int ansr[16];

void dfs(int x, double t, double w, int bit, int root[16], int k) {
  if (t > dp[bit][x])
    return;
  if (bit == (1 << n) - 1 && ans > t) {
    ans = t;
    for (int i = 0; i < n; i++) {
      ansr[i] = root[i];
    }
  } else {
    for (int i = 0; i < n; i++) {
      if (!(bit >> i & 1)) {
        double v = 2000.0 / (70.0 + w);
        double ct = t + (fabs(to[x] - to[i]) / v);
        double nw = w + s[i] * 20;
        root[k] = num[i];
        if (dp[bit | 1 << i][i] > ct) {
          dp[bit | 1 << i][i] = ct;
          dfs(i, ct, nw, (bit | 1 << i), root, k + 1);
        }
      }
    }
  }
}

int main(void) {
  cin >> n;
  for (int i = 0; i < (1 << 16) - 1; i++) {
    for (int j = 0; j < 16; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> num[i] >> to[i] >> s[i];
  }
  for (int i = 0; i < n; i++) {
    r[0] = num[i];
    dfs(i, 0, s[i] * 20, 1 << i, r, 1);
  }
  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << " ";
    cout << ansr[i];
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

const double INF = 1000000;
int n;
int num[16], to[16], s[16];
double dp[1 << 16];
double ans = INF;
int r[16];
int ansr[16];

void dfs(int x, double t, double w, int bit, int root[16], int k) {
  if (bit == (1 << n) - 1 && ans > t) {
    ans = t;
    for (int i = 0; i < n; i++) {
      ansr[i] = root[i];
    }
  } else {
    for (int i = 0; i < n; i++) {
      if (!(bit >> i & 1)) {
        w += s[i] * 20;
        t += (double)abs(to[x] - to[i]) / (double)(2000 / (70 + w));
        root[k] = num[i];
        dfs(i, t, w, bit | 1 << i, root, k + 1);
        t -= (double)abs(to[x] - to[i]) / (double)(2000 / (70 + w));
        w -= s[i] * 20;
        root[k] = num[x];
      }
    }
  }
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num[i] >> to[i] >> s[i];
  }
  for (int i = 0; i < n; i++) {
    r[0] = num[i];
    dfs(i, 0, s[i] * 20, 1 << i, r, 1);
  }
  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << " ";
    cout << ansr[i];
  }
  cout << endl;
  return 0;
}
Problem p00146 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
pair<double, vector<int>> dp[15][1 << 15];
int iton[15];
int x[15];
int w[15];
int n;

#define INF 1e+20

pair<double, vector<int>> solve(int state, int pos, int weight) {
  if (dp[pos][state].first != INF)
    return dp[pos][state];
  pair<double, vector<int>> ret(0, vector<int>()), tmp;
  ret.second.push_back(iton[pos]);

  if (state == (1 << n) - 1)
    return ret;

  ret.first = INF;
  for (int i = 0; i < n; i++) {
    if ((state & (1 << i)) == 0) {
      tmp = solve(state | (1 << i), i, weight + w[i]);
      tmp.first += abs(x[pos] - x[i]) / (2000.0 / (70 + weight));
      if (ret.first > tmp.first) {
        ret = tmp;
      }
    }
  }

  ret.second.push_back(iton[pos]);
  return dp[pos][state] = ret;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> iton[i] >> x[i] >> w[i];
    w[i] *= 20;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      dp[i][j] = pair<double, vector<int>>(INF, vector<int>());
    }
  }

  pair<double, vector<int>> ret(INF, vector<int>()), tmp;
  for (int i = 0; i < n; i++) {
    tmp = solve(1 << i, i, w[i]);
    if (tmp.first < ret.first) {
      ret = tmp;
    }
  }
  reverse(ret.second.begin(), ret.second.end());

  for (int i = 0; i < ret.second.size() - 1; i++) {
    cout << ret.second[i] << " ";
  }
  cout << ret.second.back() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
pair<double, vector<int>> dp[15][1 << 15];
int iton[15];
int x[15];
int w[15];
int n;

pair<double, vector<int>> solve(int state, int pos, int weight) {
  if (dp[pos][state].first != 1 << 20)
    return dp[pos][state];
  pair<double, vector<int>> ret(0, vector<int>()), tmp;
  ret.second.push_back(iton[pos]);

  if (state == (1 << n) - 1)
    return ret;

  ret.first = 1 << 20;
  for (int i = 0; i < n; i++) {
    if ((state & (1 << i)) == 0) {
      tmp = solve(state | (1 << i), i, weight + w[i]);
      tmp.first += abs(x[pos] - x[i]) / (2000.0 / (70 + weight));
      if (ret.first > tmp.first) {
        ret = tmp;
      }
    }
  }

  ret.second.push_back(iton[pos]);
  return dp[pos][state] = ret;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> iton[i] >> x[i] >> w[i];
    w[i] *= 20;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      dp[i][j] = pair<double, vector<int>>(1 << 20, vector<int>());
    }
  }

  pair<double, vector<int>> ret(1 << 20, vector<int>()), tmp;
  for (int i = 0; i < n; i++) {
    tmp = solve(1 << i, i, w[i]);
    if (tmp.first < ret.first) {
      ret = tmp;
    }
  }
  reverse(ret.second.begin(), ret.second.end());

  for (int i = 0; i < ret.second.size() - 1; i++) {
    cout << ret.second[i] << " ";
  }
  cout << ret.second.back() << endl;
  return 0;
}
Problem p00146 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int a[15], b[15], c[15], d[15], e[15], f[15][1 << 15], n;
double mn = 1 << 30;
void fn(int p, int q, int r, int s, double t) {
  int i;
  if (p == n) {
    if (mn > t) {
      mn = t;
      memcpy(e, d, sizeof(d));
    }
  } else {
    if (f[r][q] > t) {
      f[r][q] = t;
      for (i = 0; i < n; i++) {
        if ((q & (1 << i)) == 0) {
          d[p] = a[i];
          fn(p + 1, q | (1 << i), i, s + c[i],
             t + fabs(b[r] - b[i]) * (70 + s) / 2000);
        }
      }
    }
  }
  return;
}
int main() {
  int i, j;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    c[i] *= 20;
  }
  for (i = 0; i < n; i++)
    for (j = 0; j < (1 << n); j++)
      f[i][j] = 1 << 30;
  for (i = 0; i < n; i++) {
    d[0] = a[i];
    fn(1, 1 << i, i, c[i], 0);
  }
  for (i = 0; i < n - 1; i++)
    cout << e[i] << " ";
  cout << e[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int a[15], b[15], c[15], d[15], e[15], n;
double mn = 1 << 30;
void fn(int p, int q, int r, int s, double t) {
  int i;
  if (p == n) {
    if (mn > t) {
      mn = t;
      memcpy(e, d, sizeof(d));
    }
  } else {
    for (i = 0; i < n; i++) {
      if ((q & (1 << i)) == 0) {
        d[p] = a[i];
        fn(p + 1, q | (1 << i), b[i], s + c[i],
           t + fabs(r - b[i]) * (70 + s) / 2000);
      }
    }
  }
  return;
}
int main() {
  int i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    c[i] *= 20;
  }
  for (i = 0; i < n; i++) {
    d[0] = a[i];
    fn(1, 1 << i, b[i], c[i], 0);
  }
  for (i = 0; i < n - 1; i++)
    cout << e[i] << " ";
  cout << e[i] << endl;
  return 0;
}
Problem p00147 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))

int main() {
  int d[17];
  int n[100] = {0};
  int cnt = 0, t = 0;
  queue<int> q;
  rep(i, 17) d[i] = 0;
  for (; cnt < 100; t++) {
    if (t % 5 == 0 && t < 500)
      q.push(t / 5);
    while (!q.empty()) {
      int v = q.front();
      int num = v % 5 == 1 ? 5 : 2;
      int g = 0;
      rep(j, 18 - num) {
        if (d[j] < 1 && !g) {
          g = 1;
          reps(k, 1, num - 1) {
            if (d[j + k] > 0)
              g = 0;
          }
          if (g) {
            rep(k, num) d[j + k] = 17 * (v % 2) + 3 * (v % 3) + 19;
          }
        }
      }
      if (!g)
        break;
      q.pop();
      n[v] = t - v * 5;
      cnt++;
    }
    rep(i, 17) if (d[i] > 0) d[i]--;
  }
  while (~scanf("%d", &t))
    printf("%d\\n", n[t]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))

int main() {
  int d[17];
  int n[100] = {0};
  int cnt = 0, t = 0;
  queue<int> q;
  rep(i, 17) d[i] = 0;
  for (; cnt < 100; t++) {
    rep(i, 17) if (d[i] > 0) d[i]--;
    if (t % 5 == 0)
      q.push(t / 5);
    while (!q.empty()) {
      int v = q.front();
      int num = v % 5 == 1 ? 5 : 2;
      int g = 0;
      rep(j, 17 - num) {
        if (d[j] < 1) {
          g = 1;
          reps(k, 1, num - 1) {
            if (d[j + k] > 0)
              g = 0;
          }
          if (g) {
            rep(k, num) d[j + k] = 17 * (v % 2) + 3 * (v % 3) + 19;
          }
        }
        if (g)
          break;
      }
      if (!g)
        break;
      q.pop();
      n[v] = t - v * 5;
      cnt++;
    }
  }
  while (~scanf("%d", &t))
    printf("%d\\n", n[t]);
  return 0;
}