alexjercan/bugnet huggingface dataset:
Problem p00144 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
const int INF = 1e9;
using namespace std;
int main() {
  int d[100][100], n, p;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      d[i][j] = INF;
  for (int i = 0, r, k, t; i < n && cin >> r >> k; i++)
    for (int j = 0; j < k && cin >> t; j++)
      d[r - 1][t - 1] = 1;
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      d[i][j]++;
  cin >> p;
  for (int i = 0, s, t, v; i < p; i++) {
    cin >> s >> t >> v;
    if (d[s - 1][t - 1] > v)
      cout << "NA" << endl;
    else
      cout << d[s - 1][t - 1] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int main() {
  int a, n, m, x, s, d, v, map[200][200][1] = {};
  queue<int> A;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    cin >> map[i][0][0];
    for (int j = 1; j <= map[i][0][0]; j++) {
      cin >> map[i][j][0];
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> d >> v;
    A.push(s);
    while (1) {
      x = A.front();
      if (x == d)
        break;
      A.pop();
      for (int j = 1; j <= map[x][0][0]; j++) {
        if (map[map[x][j][0]][0][1] == 0) {
          A.push(map[x][j][0]);
          map[map[x][j][0]][0][1] = map[x][0][1] + 1;
        }
      }
    }
    if (v > map[x][0][1])
      printf("%d\\n", map[x][0][1]);
    else
      printf("NA\\n");
  }
  return 0;
}
Problem p00145 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef pair<int, int> pii;
int const INF = 1 << 30;
int main() {
  int n;
  cin >> n;
  vector<int> v1(n), v2(n);
  REP(i, n) { cin >> v1[i] >> v2[i]; }
  long long dp[n][n];
  REP(i, n) REP(j, n) dp[i][j] = INF;
  REP(i, n) dp[i][i] = 0;

  for (int i = 1; i < n; ++i)
    for (int j = 0; j < n - i; ++j)
      for (int k = j; k < j + i; ++k)
        dp[j][j + i] =
            min(dp[j][j + i], dp[j][k] + dp[k + 1][j + i] +
                                  v1[j] * v2[k] * v1[k + 1] * v2[j + i]);
  cout << dp[0][n - 1] << endl;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef pair<int, int> pii;
int const INF = 1 << 30;

int main() {
  int n;
  cin >> n;
  vector<int> v1(n), v2(n);
  REP(i, n) { cin >> v1[i] >> v2[i]; }
  int ans = 0;
  while (v1.size() > 1) {
    pii t1(INF, 0), t2, b1(INF, 0), b2;
    REP(i, n) {
      if (t1 > pii(v1[i], i)) {
        t2 = t1;
        t1 = pii(v1[i], i);
      } else {
        t2 = min(pii(v1[i], i), t2);
      }
      if (b1 > pii(v2[i], i)) {
        b2 = b1;
        b1 = pii(v2[i], i);
      } else {
        b2 = min(pii(v2[i], i), b2);
      }
    }
    int ue, shita;
    if (t1.second == b1.second) {
      if (t2.first < b2.first) {
        ue = t2.second;
        shita = b1.second;
      } else {
        ue = t1.second;
        shita = b2.second;
      }
    } else {
      ue = t1.second;
      shita = b1.second;
    }
    // cout << ue << " " << shita<< endl;
    ans += v1[ue] * v2[ue] * v1[shita] * v2[shita];
    v1.push_back(v1[ue]);
    v2.push_back(v2[shita]);
    v1.erase(v1.begin() + ue);
    v1.erase(v1.begin() + shita);
    v2.erase(v2.begin() + ue);
    v2.erase(v2.begin() + shita);
  }
  cout << ans << endl;
}
Problem p00145 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll n;
ll INF = (1LL << 34);
P t[100];
ll dp[100][100];

P on(P x, P y) { return P(x.first, y.second); }

ll cost(P x, P y) { return x.first * x.second * y.first * y.second; }

int compute(int a, int b) {
  ll A, B;
  if (dp[a][b] != -1)
    return dp[a][b];
  if (a == b)
    return 0;
  if (a + 1 == b)
    return cost(t[a], t[b]);
  if (a + 2 == b) {
    A = cost(t[a], t[a + 1]);
    A += cost(on(t[a], t[a + 1]), t[b]);
    B = cost(t[b - 1], t[b]);
    B += cost(t[a], on(t[b - 1], t[b]));
    return (A < B ? A : B);
  }

  ll res = INF;
  ll sum;
  P X, Y;
  for (int i = a; i < b; i++) {
    X = on(t[a], t[i]);
    Y = on(t[i + 1], t[b]);

    sum = cost(X, Y);
    sum = compute(a, i) + sum + compute(i + 1, b);
    res = (sum < res ? sum : res);
  }
  dp[a][b] = res;
  return res;
}

int main() {

  while (cin >> n && n) {
    for (int i = 0; i < n; i++)
      cin >> t[i].first >> t[i].second;
    for (int i = 0; i < 100; i++)
      for (int j = 0; j < 100; j++)
        dp[i][j] = -1;
    cout << compute(0, n - 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll n;
ll INF = (1LL << 32);
P A, B;
P t[100];
ll dp[100][100];

P on(P x, P y) { return P(x.first, y.second); }
int cost(P x, P y) { return x.first * x.second * y.first * y.second; }

int compute(int a, int b) {
  if (dp[a][b] != -1)
    return dp[a][b];
  if (a == b)
    return 0;

  if (a + 1 == b)
    return cost(t[a], t[b]);
  if (a + 2 == b)
    return min(cost(t[a], t[a + 1]) + cost(on(t[a], t[a + 1]), t[a + 2]),
               cost(t[a + 1], t[a + 2]) + cost(t[a], on(t[a + 1], t[a + 2])));

  ll res = INF;
  ll cost;

  for (int i = a + 1; i + 1 < b; i++) {

    cost = t[a].first * t[i].second * t[i + 1].first * t[b].second;

    res = min(res, compute(a, i) + cost + compute(i + 1, b));
  }
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> t[i].first >> t[i].second;

  if (n == 1) {
    cout << "0" << endl;
    return 0;
  }

  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 100; j++)
      dp[i][j] = -1;

  cout << compute(0, n - 1) << endl;

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int a[100];
int b[100];
int memo[100][100];

int solve(int pos, int num) {
  int ret = INT_MAX;
  if (num == 1)
    return 0;
  if (memo[pos][num])
    return memo[pos][num];
  REP(i, num - 1) {
    int t = pos + i;
    int cost = solve(pos, i + 1) + solve(pos + i + 1, num - i - 1);
    cost += a[pos] * b[t] * a[t + 1] * b[pos + num - 1];
    ret = min(ret, cost);
  }
  return memo[pos][num] = ret;
}

int main() {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d%d", &a[i], &b[i]);
  printf("%d\\n", solve(0, n));
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int a[100];
int b[100];

int solve(int pos, int num) {
  int ret = INT_MAX;
  if (num == 1)
    return 0;
  REP(i, num - 1) {
    int t = pos + i;
    int cost = solve(pos, i + 1) + solve(pos + i + 1, num - i - 1);
    cost += a[pos] * b[t] * a[t + 1] * b[pos + num - 1];
    ret = min(ret, cost);
  }
  return ret;
}

int main() {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d%d", &a[i], &b[i]);
  printf("%d", solve(0, n));
  return 0;
}
Problem p00145 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define INF LLONG_MAX
#define P pair<ll, ll>
#define N 101
#define ll long long int

ll loop(ll left, ll right);
void resetDp();
ll n;
vector<P> cost;
ll dp[N][N];

int main() {
  resetDp();
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll top, bottom;
    cin >> top >> bottom;
    cost.push_back(make_pair(top, bottom));
  }
  cout << loop(0, n - 1) << endl;
  return 0;
}

ll loop(ll left, ll right) {
  if (left == right)
    return 0;
  else if (dp[left][right] != INF)
    return dp[left][right];
  else {
    ll ret = INF;
    for (int i = left; i < right; i++) {
      ret = min(ret, loop(left, i) + loop(i + 1, right) +
                         (cost[left].first * cost[i].second *
                          cost[i + 1].first * cost[right].second));
    }
    return dp[left][right] = ret;
  }
}

void resetDp() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = INF;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define INF 1 << 21
#define P pair<int, int>

int dp(int left, int right);

int n;
vector<P> cost;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int top, bottom;
    cin >> top >> bottom;
    cost.push_back(make_pair(top, bottom));
  }
  cout << dp(0, n - 1) << endl;
  return 0;
}

int dp(int left, int right) {
  if (left == right)
    return 0;
  int ret = INF;
  for (int i = left; i < right; i++) {
    ret = min(ret, dp(left, i) + dp(i + 1, right) +
                       (cost[left].first * cost[i].second * cost[i + 1].first *
                        cost[right].second));
  }
  return ret;
}
Problem p00145 (C++) — buggy submission, verdict: Memory 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;

// O(n^3)
// dp[i][j] := [i,j]????¨???????????????????????°?????????????????????°
// i??????????????????(?????°????????°)??????(p[i - 1], p[i])
int matrixChainMultiplication(int n, pair<int, int> p[105]) {
  int dp[105][105] = {{0}};
  range(seg, 2, n + 1) { //?¨????????????????????????°
    range(i, 0, n - seg + 1) {
      int j = i + seg - 1;
      dp[i][j] = INT_MAX;
      range(k, i, j) {
        // cout << i << ' ' << k << ' ' << j << endl;
        // show(dp[i][k])
        // show(dp[k + 1][j])
        // show(p[i].second * p[k].first * p[k + 1].second * p[j].first);
        int cost = dp[i][k] + dp[k + 1][j] +
                   p[i].second * p[k].first * p[k + 1].second * p[j].first;
        // show(cost)
        dp[i][j] = min(dp[i][j], cost);
      }
    }
  }
  return dp[0][n - 1];
}

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

  pair<int, int> p[105];
  rep(i, n) cin >> p[i].second >> p[i].first;
  cout << matrixChainMultiplication(n, p) << endl;
}

Error: MLE

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;
const long long INF = (1LL << 60);
using namespace std;

long long dp[205][205][205];

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

  pair<int, int> p[205];
  rep(i, n) cin >> p[i].first >> p[i].second;

  rep(i, 205) rep(j, 205) rep(k, 205) dp[i][j][k] = INF;

  rep(l, n) {
    for (int i = n; i > 1; i--) {
      range(j, 1, 201) {
        range(k, 1, 201) {
          long long next = dp[i - 1][j][k] + j * k * p[l].first * p[l].second;
          dp[i][j][p[l].second] = min(dp[i][j][p[l].second], next);
          dp[i][p[l].first][k] = min(dp[i][p[l].first][k], next);
        }
      }
    }
    dp[1][p[l].first][p[l].second] = 0;
  }

  long long mini = INF;
  rep(i, 201) rep(j, 201) mini = min(mini, dp[n][i][j]);
  cout << mini << endl;
}
Problem p00145 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int a[100], b[100];
int dp[100][100];

int main() {
  int n, i, j, k;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      dp[i][j] = INT_MAX;
  for (i = 0; i < n; i++)
    dp[i][i] = 0;
  for (i = 2; i <= n; i++) {
    for (j = 0; j <= n - i; j++) {
      for (k = j + 1; k < j + i; k++) {
        dp[j][j + i - 1] =
            min(dp[j][j + i - 1], dp[j][k - 1] + dp[k][j + i - 1] +
                                      a[j] * b[k - 1] * a[k] * b[j + i - 1]);
      }
    }
  }
  cout << dp[0][n - 1] << endl;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
struct BOOK {
  int a, b, c, d;
  BOOK(int x, int y, int z, int aa) { a = x, b = y, c = z, d = aa; }
};
bool operator<(const BOOK &a, const BOOK &b) { return a.a * a.b > b.a * b.b; }

bool done[100][100][100];

int main() {
  int n;
  ll a, b;
  cin >> n;
  priority_queue<BOOK> Q;
  rep(i, n) cin >> a >> b, Q.push(BOOK(a, b, 1, 0));
  while (Q.top().c != n) {
    BOOK one = Q.top();
    Q.pop();
    BOOK two = Q.top();
    Q.pop();
    Q.push(BOOK(one.a, two.b, one.c + two.c,
                one.d + two.d + one.a * one.b * two.a * two.b));
    Q.push(BOOK(two.a, one.b, one.c + two.c,
                one.d + two.d + one.a * one.b * two.a * two.b));
  }
  cout << Q.top().d << endl;
}
Problem p00145 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#define INF 1000000000

int top[111];
int bot[111];

int memo[111][111];
int saiki(int st, int en) {
  if (st + 1 == en)
    return 0;
  if (memo[st][en] != 0)
    return memo[st][en];

  int cost = INF;

  for (int i = st + 1; i < en; i++) {
    int next = 0;
    next += saiki(st, i);
    next += saiki(i, en);
    next += top[st] * bot[i - 1] * top[i] * bot[en - 1];

    cost = min(next, cost);
  }

  return memo[st][en] = cost;
}

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

  rep(i, n) cin >> top[i] >> bot[i];

  int ans = saiki(0, n);
  printf("%d\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#define INF 1000000000

int top[111];
int bot[111];

int memo[111][111];
int saiki(int st, int en) {
  if (st + 1 == en)
    return 0;

  int cost = INF;

  for (int i = st + 1; i < en; i++) {
    int next = 0;
    next += saiki(st, i);
    next += saiki(i, en);
    next += top[st] * bot[i - 1] * top[i] * bot[en - 1];

    cost = min(next, cost);
  }

  return cost;
}

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

  rep(i, n) cin >> top[i] >> bot[i];

  int ans = saiki(0, n);
  printf("%d\\n", ans);
}
Problem p00145 (C++) — buggy submission, verdict: Runtime Error

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

#define REP(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) REP(i, 0, n)
#define EREP(i, n, m) for (int i = n; i <= m; i++)
#define erep(i, n) EREP(i, 0, n)

typedef pair<int, int> P;
// typedef long long ll;

unsigned int dp[101][101];

int main(void) {
  int n;
  cin >> n;
  vector<P> v(n);
  rep(i, n) cin >> v[i].first >> v[i].second;

  erep(i, n) {
    erep(j, n) dp[i][j] = INT_MAX;
    dp[i][i] = 0;
  }

  REP(i, 1, n) {         // ツ閉敖づ個個按津ィ
    EREP(j, 0, n - i) {  // ツ開ツ始ツ暗環置ツづ個個按津ィ
      REP(k, j, j + i) { // ツ静伉断ツ暗環置ツづ個個按津ィ
        int cost = v[j].first * v[k].second * v[k + 1].first * v[j + i].second;
        dp[j][j + i] = min(dp[j][j + i], cost + dp[j][k] + dp[k + 1][j + i]);
      }
    }
  }

  cout << dp[0][n - 1] << endl;

  return 0;
}

Error: 0

Fixed submission:

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

#define REP(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) REP(i, 0, n)
#define EREP(i, n, m) for (int i = n; i <= m; i++)
#define erep(i, n) EREP(i, 0, n)

typedef pair<int, int> P;

unsigned int dp[101][101];

int main(void) {
  int n;
  cin >> n;
  vector<P> v(n);
  rep(i, n) cin >> v[i].first >> v[i].second;

  erep(i, n) {
    erep(j, n) dp[i][j] = INT_MAX;
    dp[i][i] = 0;
  }

  REP(i, 1, n) {
    REP(j, 0, n - 1) {
      REP(k, j, j + i) {
        int cost = v[j].first * v[k].second * v[k + 1].first * v[j + i].second;
        dp[j][j + i] = min(dp[j][j + i], cost + dp[j][k] + dp[k + 1][j + i]);
      }
    }
  }

  cout << dp[0][n - 1] << endl;

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

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

typedef pair<double, vector<int>> P;
P dp[(1 << 16)][15];
double INF = 9999999.9;
int n, no[15];
double r[15], t[15];

P rec(int S, int pos) {
  // cout<<S<<' '<<pos<<' '<<cost<<' '<<size<<endl;
  if (dp[S][pos].first != INF) {
    return dp[S][pos];
  }

  if (S == (1 << (n)) - 1) {
    vector<int> res;
    res.push_back(pos);
    return P(0.0, res);
  }

  double w = 0.0;
  for (int i = 0; i < n; i++) {
    if ((S >> i) & 1) {
      w += (t[i] * 20.0);
    }
  }

  int s;
  double v = 2000.0 / (70.0 + w), add;

  for (int i = 0; i < n; i++) {
    if ((S >> i) & 1 == 1)
      continue;

    s = S + (1 << i);
    add = abs(r[i] - r[pos]) / v;
    P X = rec(s, i);
    X.first += add;
    // X.second.erase(X.second.end());

    if (X < dp[S][pos]) {
      dp[S][pos].second.clear();
      dp[S][pos].second.push_back(pos);
      dp[S][pos].first = X.first;
      for (int j = 0; j < (int)X.second.size(); j++) {
        dp[S][pos].second.push_back(X.second[j]);
      }
    }
  }

  return dp[S][pos];
}

int main() {
  while (cin >> n) {
    for (int i = 0; i < n; i++) {
      cin >> no[i] >> r[i] >> t[i];
    }
    for (int i = 0; i < (1 << 16); i++)
      for (int j = 0; j < 15; j++) {
        dp[i][j].first = INF;
        dp[i][j].second.clear();
      }

    P x, ans;
    ans.first = INF;
    for (int i = 0; i < n; i++) {
      x = rec((1 << i), i);
      if (x < ans) {
        ans = x;
      }
      // cout<<x.first<<endl;
    }

    for (int i = 0; i < n; i++) {
      if (i)
        cout << ' ';
      cout << no[ans.second[i]];
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
double dp[(1 << 16)][15];
double INF = 9999999.9;
int n, no[15];
double r[15], t[15];

int ans[15], size = 0;
int tmp[15], mini = INF;

double rec(int S, int pos, double cost) {
  // cout<<S<<' '<<pos<<' '<<cost<<' '<<size<<endl;

  if (size == n) {
    if (mini > cost) {
      mini = cost;
      for (int i = 0; i < n; i++)
        tmp[i] = ans[i];
    }
    return 0.0;
  }

  double w = 0.0;
  for (int i = 0; i < size; i++)
    w += (t[ans[i]] * 20.0);

  int s;
  double v = 2000.0 / (70.0 + w), add;

  for (int i = 0; i < n; i++) {
    if ((S >> i) & 1 == 1)
      continue;

    ans[size] = i;
    size++;
    s = S + (1 << i);
    add = abs(r[i] - r[pos]) / v;
    dp[S][pos] = min(dp[S][pos], rec(s, i, cost + add) + add);
    size--;
  }
  // if(dp[S][pos]==INF)cout<<'a'<<endl;
  return dp[S][pos];
}

int main() {
  while (cin >> n) {
    mini = INF;
    for (int i = 0; i < n; i++) {
      cin >> no[i] >> r[i] >> t[i];
    }
    for (int i = 0; i < (1 << 16); i++)
      for (int j = 0; j < 15; j++)
        dp[i][j] = INF;

    double x;
    for (int i = 0; i < n; i++) {
      ans[0] = i;
      size = 1;
      x = rec((1 << i), i, 0.0);
      // cout<<x<<endl;
    }

    for (int i = 0; i < n; i++) {
      if (i)
        cout << ' ';
      cout << no[tmp[i]];
    }
    cout << endl;
  }
  return 0;
}
Problem p00146 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define MAX 15
#define INF 1e9

int N;
int path[1 << MAX][MAX];
double dp[1 << MAX][MAX];
double _time, d[MAX], B[MAX];
int n[MAX];
vector<int> ans;

double V(double w) { return 2000. / (70. + w); }

double dist(int a, int b) { return abs(d[a] - d[b]); }

double solve(int S, int prev, double b) {
  if (N == __builtin_popcount(S))
    return dp[S][prev] = 0.0;
  if (dp[S][prev] >= 0)
    return dp[S][prev];

  double res = INF;
  for (int i = 0; i < N; i++) {
    if (S >> i & 1)
      continue;
    double next = solve(S | (1 << i), i, b + B[i]) + dist(prev, i) / V(b);
    if (next < res) {
      res = next;
      path[S][prev] = i;
    }
  }
  return dp[S][prev] = res;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> n[i] >> d[i] >> B[i];
    B[i] *= 20;
  }
  _time = INF;
  for (int i = 0; i < 1 << N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = -1;
    }
  }
  for (int i = 0; i < N; i++) {
    double now = solve(1 << i, i, B[i]);
    if (now < _time) {
      _time = now;
      path[0][0] = i;
    }
  }
  int S = 0, p = 0;
  while (true) {
    if (N == __builtin_popcount(S))
      break;
    ans.push_back(n[path[S][p]]);
    int T = S;
    S |= 1 << path[S][p];
    p = path[T][p];
  }
  for (int i = 0; i < N; i++) {
    if (i == N - 1) {
      cout << ans[i] << endl;
    } else {
      cout << ans[i] << " ";
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define MAX 15
#define INF 1e9

struct Pair {
  int n;
  double d, b;
  Pair() {}
  Pair(int n, double d, double b) : n(n), d(d), b(b) {}
};

int N;
double dp[MAX][1 << MAX][MAX], mt;
vector<int> path, p;
vector<Pair> vec;

double V(double w) { return 2000. / (70. + w); }

double dist(int a, int b) { return abs(vec[a].d - vec[b].d); }

double solve(int cur, int S, int prev, double t, double b) {
  if (cur == N) {
    if (t < mt) {
      mt = t;
      path = p;
    }
    return t;
  }
  //  if(dp[cur][S][prev] >= 0) return dp[cur][S][prev];

  double res = INF;
  for (int i = 0; i < N; i++) {
    if (S >> i & 1)
      continue;
    double nt = dist(prev, i) / V(b);
    p.push_back(vec[i].n);
    res = min(res, solve(cur + 1, S | (1 << i), i, t + nt, b + vec[i].b));
    p.pop_back();
  }
  return dp[cur][S][prev] = min(dp[cur][S][prev], res);
}

int main() {
  cin >> N;
  vec.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> vec[i].n >> vec[i].d >> vec[i].b;
    vec[i].b *= 20;
  }
  memset(dp, -1, sizeof(dp));
  mt = INF;
  for (int i = 0; i < N; i++) {
    p.push_back(vec[i].n);
    solve(1, 1 << i, i, vec[i].d / V(0), vec[i].b);
    p.pop_back();
  }
  int size = path.size();
  for (int i = 0; i < size; i++) {
    if (i == 0) {
      cout << path[i];
    } else {
      cout << " " << path[i];
    }
  }
  cout << endl;

  return 0;
}