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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\\n"
#define inf 2000000000
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
ll mod = 1e9 + 7;

int main() {
  fast

      ll n;
  cin >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];

  ll money = 1000;
  ll stock = 0;

  for (ll i = 0; i < n - 1; i++) {
    if (arr[i + 1] > arr[i]) {

      stock += (money / arr[i]);
      money = money % arr[i];

    } else if (arr[i + 1] < arr[i]) {
      money += (stock * arr[i]);
      stock = 0;
    }
  }
  cout << money + stock * arr[n - 1] << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define endl "\\n"
#define inf 2000000000
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
ll mod = 1e9 + 7;
ll arr[100];
ll n;

ll dp[82][150][3999];

ll solve(ll id, ll stock, ll money) {
  if (id >= n)
    return money;

  if (dp[id][stock][money] != -1)
    return dp[id][stock][money];

  ll op1 = solve(id + 1, stock + money / arr[id],
                 money - ((money / arr[id]) * arr[id]));

  ll op2 = solve(id + 1, stock, money);

  ll op3 = solve(id + 1, 0ll, stock * arr[id] + money);

  return dp[id][stock][money] = max(op1, max(op2, op3));
}

int main() {
  fast

          cin >>
      n;
  for (ll i = 0; i < n; i++)
    cin >> arr[i];

  memset(dp, -1, sizeof(dp));

  cout << solve(0, 0, 1000ll);

  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int

int k, n;
int ci[15], cf[15];
int x[15], y[15], p[15], d[15];
bool used[15] = {0};
ll mini;

void tan(int d, int s) {
  if (d == k) {
    // count
    ll sm = 0;
    rep(i, n) if (!used[i]) {
      int mn = min(abs(x[i]), abs(y[i]));
      rep(j, k) {
        if (cf[j])
          mn = min(mn, abs(x[i] - x[ci[j]]));
        else
          mn = min(mn, abs(y[i] - y[ci[j]]));
      }
      sm += (ll)p[i] * mn;
    }
    mini = min(mini, sm);
    return;
  }
  for (int i = s; i < n - (k - 1 - d); i++) {
    ci[d] = i;
    used[i] = true;
    rep(j, 2) {
      cf[d] = j;
      tan(d + 1, i + 1);
    }
    used[i] = false;
  }
}

int main() {
  cin >> n;
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  for (k = 0; k < n + 1; k++) {
    mini = LLONG_MAX;
    tan(0, 0);
    cout << mini << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int

int k, n;
int ci[15], cf[15];
int x[15], y[15], p[15];
bool used[15] = {0};
ll mini;

int tan(int d, int s) {
  if (d == k) {
    // count
    ll sm = 0;
    rep(i, n) if (!used[i]) {
      int mn = min(abs(x[i]), abs(y[i]));
      rep(j, k) {
        if (cf[j])
          mn = min(mn, abs(x[i] - x[ci[j]]));
        else
          mn = min(mn, abs(y[i] - y[ci[j]]));
      }
      sm += (ll)p[i] * mn;
    }
    mini = min(mini, sm);
    return 0;
  }
  for (int i = s; i < n - (k - 1 - d); i++) {
    ci[d] = i;
    used[i] = true;
    rep(j, 2) {
      cf[d] = j;
      tan(d + 1, i + 1);
    }
    used[i] = false;
  }
}

int main() {
  cin >> n;
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  for (k = 0; k < n + 1; k++) {
    mini = LLONG_MAX;
    tan(0, 0);
    cout << mini << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
int n, x[20], y[20], p[20];
bool used[20];
ll ans[20];
set<int> xs, ys;
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
void rec(int i) {
  if (i == n) {
    int cnt = xs.size() + ys.size() - 2;
    ll sm = 0;
    rep(i, n) if (!used[i]) {
      int d = 1e9;
      auto it = xs.lower_bound(x[i]);
      if (it != xs.end())
        chmin(d, (*it) - x[i]);
      if (it != xs.begin())
        chmin(d, x[i] - *--it);
      it = ys.lower_bound(y[i]);
      if (it != ys.end())
        chmin(d, (*it) - y[i]);
      if (it != ys.begin())
        chmin(d, y[i] - *--it);
      sm += (ll)d * p[i];
    }
    if (ans[cnt] > sm)
      ans[cnt] = sm;
  } else {
    if (xs.count(x[i]) || ys.count(y[i])) {
      used[i] = true;
      rec(i + 1);
      used[i] = false;
    } else {
      rec(i + 1);
      used[i] = true;
      xs.insert(x[i]);
      rec(i + 1);
      xs.erase(x[i]);
      ys.insert(y[i]);
      rec(i + 1);
      ys.erase(y[i]);
      used[i] = false;
    }
  }
}
int main() {
  scanf("%d", &n);
  fill(ans, ans + n + 1, (ll)1e15);
  rep(i, n) scanf("%d%d%d", x + i, y + i, p + i);
  xs.insert(0);
  ys.insert(0);
  rec(0);
  rep(i, n) chmin(ans[i + 1], ans[i]);
  rep(i, n + 1) printf("%lld\\n", ans[i]);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
int n, x[20], y[20], p[20];
ll ans[20];
set<int> xs, ys;
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
void rec(int i) {
  if (i == n) {
    int cnt = xs.size() + ys.size() - 2;
    ll sm = 0;
    rep(i, n) {
      int d = 1e9;
      auto it = xs.lower_bound(x[i]);
      if (it != xs.end())
        chmin(d, (*it) - x[i]);
      if (it != xs.begin())
        chmin(d, x[i] - *--it);
      it = ys.lower_bound(y[i]);
      if (it != ys.end())
        chmin(d, (*it) - y[i]);
      if (it != ys.begin())
        chmin(d, y[i] - *--it);
      sm += (ll)d * p[i];
    }
    if (ans[cnt] > sm)
      ans[cnt] = sm;
  } else {
    if (xs.count(x[i]) || ys.count(y[i]))
      rec(i + 1);
    else {
      rec(i + 1);
      xs.insert(x[i]);
      rec(i + 1);
      xs.erase(x[i]);
      ys.insert(y[i]);
      rec(i + 1);
      ys.erase(y[i]);
    }
  }
}
int main() {
  scanf("%d", &n);
  fill(ans, ans + n + 1, (ll)1e15);
  rep(i, n) scanf("%d%d%d", x + i, y + i, p + i);
  xs.insert(0);
  ys.insert(0);
  rec(0);
  rep(i, n) chmin(ans[i + 1], ans[i]);
  rep(i, n + 1) printf("%lld\\n", ans[i]);
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
using namespace std;
struct T {
  int X, Y, P;
};
T A[20];
set<pair<int, int>> S;
int N, ans[100], h[1 << 20][20], w[1 << 20][20];
void a(int n, int x, int y, int k) {
  if (n == N) {
    int cnt = 0;
    for (int i = 0; i < N; i++)
      cnt += min(h[x][i], w[y][i]) * A[i].P;
    ans[S.size() - 2] = min(ans[S.size() - 2], cnt);
    return;
  }
  int s = S.size();
  S.insert({0, A[n].X});
  a(n + 1, x + k, y, k * 2);
  if (s != S.size())
    S.erase({0, A[n].X});
  s = S.size();
  S.insert({1, A[n].Y});
  a(n + 1, x, y + k, k * 2);
  if (s != S.size())
    S.erase({1, A[n].Y});
  a(n + 1, x, y, k * 2);
}
signed main() {
  S.insert({0, 0});
  S.insert({1, 0});
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i].X >> A[i].Y >> A[i].P;
  for (int bit = 0; bit < (1 << N); bit++) {
    for (int i = 0; i < N; i++)
      h[bit][i] = abs(A[i].X), w[bit][i] = abs(A[i].Y);
    bitset<20> B(bit);
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        if (B.test(j)) {
          h[bit][i] = min(h[bit][i], abs(A[i].X - A[j].X));
          w[bit][i] = min(w[bit][i], abs(A[i].Y - A[j].Y));
        }
  }
  for (int i = 0; i <= N; i++)
    ans[i] = 1000000000000000000;
  a(0, 0, 0, 1);
  int n = 1000000000000000000;
  for (int i = 0; i <= N; i++) {
    n = min(n, ans[i]);
    cout << n << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
struct T {
  int X, Y, P;
};
T A[20];
stack<pair<int, int>> S;
stack<int> V;
int N, ans[100];
void a(int n) {
  if (n == N) {
    set<pair<int, int>> T;
    stack<pair<int, int>> U = S;
    stack<int> W = V;
    while (!U.empty()) {
      T.insert(U.top());
      U.pop();
    }
    int cnt = 0;
    while (!W.empty()) {
      int i = W.top();
      W.pop();
      int mn = 1000000000000;
      for (pair<int, int> p : T) {
        if (p.first == 0)
          mn = min(mn, abs(p.second - A[i].X));
        else
          mn = min(mn, abs(p.second - A[i].Y));
      }
      cnt += mn * A[i].P;
    }
    ans[T.size() - 2] = min(ans[T.size() - 2], cnt);
    return;
  }
  S.push({0, A[n].X});
  a(n + 1);
  S.pop();
  S.push({1, A[n].Y});
  a(n + 1);
  S.pop();
  V.push(n);
  a(n + 1);
  V.pop();
}
signed main() {
  cin >> N;
  S.push(make_pair(0, 0));
  S.push(make_pair(1, 0));
  for (int i = 0; i < N; i++)
    cin >> A[i].X >> A[i].Y >> A[i].P;
  for (int i = 0; i < N; i++)
    ans[i] = 1000000000000;
  a(0);
  for (int i = 0; i <= N; i++)
    cout << ans[i] << endl;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;
typedef pair<Int, Int> P;

vector<int> pow3 = {1};

int main() {
  int N;
  cin >> N;
  vector<Int> X(N), Y(N), P(N);
  rep(i, N) cin >> X[i] >> Y[i] >> P[i];

  rep(i, N) pow3.push_back(pow3.back() * 3);

  // 前処理：縦の路線・横の路線しか建てないときの各集落からの歩行距離
  vector<vector<Int>> V_only(1 << N, vector<Int>(N));
  vector<vector<Int>> H_only(1 << N, vector<Int>(N));
  rep(bit, 1 << N) {
    rep(i, N) { // 集落 i からの歩行距離を計算
      Int dist_V = P[i] * abs(X[i]);
      Int dist_H = P[i] * abs(Y[i]);

      rep(j, N) {
        if (bit >> j & 1) {
          dist_V = min(dist_V, P[i] * abs(X[i] - X[j]));
          dist_H = min(dist_H, P[i] * abs(Y[i] - Y[j]));
        }
      }

      V_only[bit][i] = dist_V;
      H_only[bit][i] = dist_H;
    }
  }

  // 全ての 3^N 通りの道路の建て方を試す．
  vector<Int> ans(N + 1, 1e18);
  rep(trit, pow3[N]) {
    int K = 0;

    int bitV = 0, bitH = 0;
    rep(i, N) {
      if (trit / pow3[i] % 3 == 1) {
        bitV += 1 << i; // X[i] 上に道を建てる
        K++;
      } else if (trit / pow3[i] % 3 == 2) {
        bitH += 1 << i; // Y[i] 上に道を建てる
        K++;
      }
    }

    Int dist = 0;
    rep(i, N) dist += min(V_only[bitV][i], H_only[bitH][i]);

    ans[K] = min(ans[K], dist);
  }

  for (Int x : ans)
    cout << x << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;
typedef pair<Int, Int> P;

vector<Int> pow3 = {1};

int main() {
  int N;
  cin >> N;
  vector<Int> X(N), Y(N), P(N);
  rep(i, N) cin >> X[i] >> Y[i] >> P[i];

  rep(i, N) pow3.push_back(pow3.back() * 3);

  // 前処理：縦の路線・横の路線しか建てないときの各集落からの歩行距離
  vector<vector<Int>> V_only(1 << N, vector<Int>(N, 0));
  vector<vector<Int>> H_only(1 << N, vector<Int>(N, 0));
  rep(bit, 1 << N) {
    rep(i, N) { // 集落 i からの歩行距離を計算
      Int dist_V = P[i] * abs(X[i]);
      Int dist_H = P[i] * abs(Y[i]);

      rep(j, N) {
        if (bit >> j & 1) {
          dist_V = min(dist_V, P[i] * abs(X[i] - X[j]));
          dist_H = min(dist_H, P[i] * abs(Y[i] - Y[j]));
        }
      }

      V_only[bit][i] = dist_V;
      H_only[bit][i] = dist_H;
    }
  }

  // 全ての 3^N 通りの道路の建て方を試す．
  vector<Int> ans(N + 1, 1e18);
  rep(trit, pow3[N]) {
    int K = 0;

    Int bitV = 0, bitH = 0;
    rep(i, N) {
      if (trit / pow3[i] % 3 == 1) {
        bitV += 1 << i; // X[i] 上に道を建てる
        K++;
      } else if (trit / pow3[i] % 3 == 2) {
        bitH += 1 << i; // Y[i] 上に道を建てる
        K++;
      }
    }

    Int dist = 0;
    rep(i, N) dist += min(V_only[bitV][i], H_only[bitH][i]);

    ans[K] = min(ans[K], dist);
  }

  for (Int x : ans)
    cout << x << endl;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long;
using vec = vector<int>;
using P = pair<int, int>;
const ll INF = 1e18;

int main() {
  int n;
  cin >> n;
  vec x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  vector<ll> ans(n + 1, INF);
  vector<vector<ll>> dx(1 << n, vector<ll>(n + 1, INF));
  vector<vector<ll>> dy(1 << n, vector<ll>(n + 1, INF));

  rep(i, 1 << n) {
    rep(j, n) {
      dx[i][j] = abs(x[j]);
      dy[i][j] = abs(y[j]);
      rep(k, n) {
        if (i >> k & 1) {
          dx[i][j] = min(dx[i][j], (ll)abs(x[j] - x[k]));
          dy[i][j] = min(dy[i][j], (ll)abs(y[j] - y[k]));
        }
      }
      dx[i][j] *= p[j];
      dy[i][j] *= p[j];
    }
  }

  rep(i, 1 << n) {
    for (int j = i; j >= 0; j--) {
      j &= i;
      ll cost = 0;
      rep(k, n) {
        if (!(i >> k & 1)) {
          cost += min(dx[j][k], dy[i - j][k]);
        }
      }

      int cnt = __builtin_popcount(i);
      ans[cnt] = min(ans[cnt], cost);
    }
  }

  rep(i, n + 1) { cout << ans[i] << endl; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
using P = pair<int, int>;
const ll INF = 1e18;

vector<ll> calc(vector<P> a) {
  int n = a.size();
  vector<ll> cost(n + 1, INF);
  rep(s, 1 << n) {
    vec p(1, 0);
    rep(i, n) {
      if (s >> i & 1)
        p.push_back(a[i].first);
    }
    ll now = 0;
    rep(i, n) {
      int m = 1e9;
      rep(j, p.size()) { m = min(m, abs(p[j] - a[i].first)); }
      now += ll(m) * a[i].second;
    }
    int b = p.size() - 1;
    cost[b] = min(cost[b], now);
  }

  return cost;
}

int main() {
  int n;
  cin >> n;
  vec x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  vector<ll> ans(n + 1, INF);

  rep(s, 1 << n) {
    vector<P> ax, ay;
    rep(i, n) {
      if (s >> i & 1) {
        ax.emplace_back(x[i], p[i]);
      } else {
        ay.emplace_back(y[i], p[i]);
      }
    }
    vector<ll> dx = calc(ax);
    vector<ll> dy = calc(ay);

    rep(i, dx.size()) rep(j, dy.size()) {
      if (i + j <= n) {
        ans[i + j] = min(ans[i + j], dx[i] + dy[j]);
      }
    }
  }

  rep(i, n + 1) { cout << ans[i] << endl; }
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "algorithm"
#include "cstring"
#include "iostream"

using namespace std;
using ll = long long;
int n, x[16], y[16], d[16], D[16], p[16];
ll ans[16];
int main() {
#ifdef LOCAL
  freopen("in1.txt", "r", stdin);
  freopen("out1.txt", "w", stdout);
#endif

  memset(ans, 0x3f, sizeof(ans));
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &x[i], &y[i], &p[i]);
  }

  for (int i = 0; i < n; i++) {
    D[i] = min(abs(x[i]), abs(y[i]));
  }
  ans[0] = 0;
  for (int i = 0; i < n; i++) {
    ans[0] += (ll)D[i] * p[i];
  }
  for (register int i = 1; i < (1 << n); i++) {
    int j = i;
    do {
      memcpy(d, D, sizeof(D));
      for (register int k = 0; k < n; k++) {
        if (i >> k & 1) {
          if (j >> k & 1) {
            for (register int q = 0; q < n; q++) {
              d[q] = min(d[q], abs(x[q] - x[k]));
            }
          } else {
            for (register int q = 0; q < n; q++) {
              d[q] = min(d[q], abs(y[q] - y[k]));
            }
          }
        }
      }
      ll tmp = 0;
      for (register int i = 0; i < n; i++)
        tmp += (ll)d[i] * p[i];
      ans[__builtin_popcount(i)] = min(ans[__builtin_popcount(i)], tmp);
      j = (j - 1) & i;
    } while (j != i);
  }
  for (register int i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
  return 0;
}


Error: TLE

Fixed submission:

#include "algorithm"
#include "cstring"
#include "iostream"

using namespace std;
using ll = long long;
int n, x[16], y[16], d[16], D[16], p[16];
ll ans[16];
int main() {
#ifdef LOCAL
  freopen("in1.txt", "r", stdin);
  freopen("out1.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  memset(ans, 0x3f, sizeof(ans));
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }

  for (int i = 0; i < n; i++) {
    D[i] = min(abs(x[i]), abs(y[i]));
  }
  ans[0] = 0;
  for (int i = 0; i < n; i++) {
    ans[0] += (ll)D[i] * p[i];
  }
  for (register int i = 1; i < (1 << n); i++) {
    int j = i;
    do {
      memcpy(d, D, sizeof(D));
      for (register int k = 0; k < n; k++) {
        if (i >> k & 1) {
          if (j >> k & 1) {
            for (register int q = 0; q < n; q++) {
              d[q] = min(d[q], abs(x[q] - x[k]));
            }
          } else {
            for (register int q = 0; q < n; q++) {
              d[q] = min(d[q], abs(y[q] - y[k]));
            }
          }
        }
      }
      ll tmp = 0;
      for (register int i = 0; i < n; i++)
        tmp += (ll)d[i] * p[i];
      ans[__builtin_popcount(i)] = min(ans[__builtin_popcount(i)], tmp);
      j = (j - 1) & i;
    } while (j != i);
  }
  for (register int i = 0; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 20;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n;
ll ans[M];
pair<pair<int, int>, ll> a[M];
vector<int> dis(M);
bool vis[M][2];
ll count() {
  ll re = 0;
  for (int i = 0; i < n; i++)
    re += a[i].second * dis[i];
  return re;
}
void dfs(int tot = 0, int floor = 0) {
  ans[tot] = min(ans[tot], count());
  if (tot == n || floor == n)
    return;

  vector<int> tmp = dis;
  for (int i = 0; i < n; i++)
    dis[i] = min(tmp[i], abs(a[i].first.first - a[floor].first.first));
  vis[floor][0] = 1;
  dfs(tot + 1, floor + 1);
  vis[floor][0] = 0;

  for (int i = 0; i < n; i++)
    dis[i] = min(tmp[i], abs(a[i].first.second - a[floor].first.second));
  vis[floor][1] = 1;
  dfs(tot + 1, floor + 1);
  vis[floor][1] = 0;

  dis = tmp;
  dfs(tot, floor + 1);
}
bool cmp(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
  return x.second > y.second;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    ans[i] = INF,
    scanf("%d%d%lld", &a[i].first.first, &a[i].first.second, &a[i].second);
  for (int i = 0; i < n; i++)
    dis[i] = min(abs(a[i].first.first), abs(a[i].first.second));
  dfs();
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
  // printf("%f\\n",1.*clock()/CLOCKS_PER_SEC);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int M = 20;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n;
ll ans[M];
pair<pair<int, int>, ll> a[M];
int dis[M];
bool vis[M][2];
inline ll count() {
  for (int i = 0; i < n; i++)
    dis[i] = min(abs(a[i].first.first), abs(a[i].first.second));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (vis[j][0])
        dis[i] = min(dis[i], abs(a[i].first.first - a[j].first.first));
      if (vis[j][1])
        dis[i] = min(dis[i], abs(a[i].first.second - a[j].first.second));
    }
  ll re = 0;
  for (int i = 0; i < n; i++)
    re += a[i].second * dis[i];
  // printf("%lld\\n",re);
  return re;
}
void dfs(int tot = 0, int floor = 0) {
  ans[tot] = min(ans[tot], count());
  if (tot == n + 1 || floor == n)
    return;
  vis[floor][0] = 1;
  dfs(tot + 1, floor + 1);
  vis[floor][0] = 0;

  vis[floor][1] = 1;
  dfs(tot + 1, floor + 1);
  vis[floor][1] = 0;

  dfs(tot, floor + 1);
}
bool cmp(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
  return x.second > y.second;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    ans[i] = INF,
    scanf("%d%d%lld", &a[i].first.first, &a[i].first.second, &a[i].second);
  // sort(a,a+n,cmp);
  dfs();
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
  // printf("%f\\n",1.*clock()/CLOCKS_PER_SEC);
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define per1(i, n) for (int i = n; i > 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll inf = 1e18;
vec ans(16, inf);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vec X(n), Y(n), P(n);
  rep(i, n) cin >> X[i] >> Y[i] >> P[i];
  vec x[15], y[15];
  rep(i, pow(2, n)) {
    vec A;
    ll p = i;
    rep(j, n) A.push_back(p % 2), p /= 2;
    rep(j, n) {
      ll d = min(abs(X[j]) * P[j], abs(Y[j]) * P[j]);
      rep(k, n) {
        if (A[k])
          d = min(d, abs(X[j] - X[k]) * P[j]);
      }
      x[j].push_back(d);
    }
  }
  rep(i, pow(2, n)) {
    vec A;
    ll p = i;
    rep(j, n) A.push_back(p % 2), p /= 2;
    rep(j, n) {
      ll d = min(abs(X[j]) * P[j], abs(Y[j]) * P[j]);
      rep(k, n) {
        if (A[k])
          d = min(d, abs(Y[j] - Y[k]) * P[j]);
      }
      y[j].push_back(d);
    }
  }
  ll e2[16];
  e2[0] = 1;
  rep(i, 15) e2[i + 1] = e2[i] * 2;
  rep(i, pow(3, n)) {
    vec A, B;
    ll p = i, a = 0, b = 0, c = 0, d = 0;
    rep(j, n) {
      if (p % 3 == 1)
        a += e2[j], d++;
      if (p % 3 == 2)
        b += e2[j], d++;
      p /= 3;
    }
    rep(j, n) { c += min(x[j][a], y[j][b]); }
    ans[d] = min(ans[d], c);
  }
  rep(i, n + 1) cout << ans[i] << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define per1(i, n) for (int i = n; i > 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll inf = 1e18;
vec ans(16, inf);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vec X(n), Y(n), P(n);
  rep(i, n) cin >> X[i] >> Y[i] >> P[i];
  rep(i, pow(2, n)) {
    vec A;
    ll p = i, a = 0;
    rep(j, n) A.push_back(p % 2), a += p % 2, p /= 2;
    rep(j, pow(2, a)) {
      vec B, C(n), x, y;
      ll p = j;
      rep(k, a) B.push_back(p % 2), p /= 2;
      int b = 0;
      rep(k, n) if (A[k]) C[k] = A[k] + B[b], b++;
      rep(k, n) {
        if (C[k] == 1)
          x.push_back(X[k]);
        if (C[k] == 2)
          y.push_back(Y[k]);
      }
      ll e = 0;
      rep(k, n) {
        ll d = min(abs(X[k]) * P[k], abs(Y[k]) * P[k]);
        rep(l, x.size()) d = min(d, abs(X[k] - x[l]) * P[k]);
        rep(l, y.size()) d = min(d, abs(Y[k] - y[l]) * P[k]);
        e += d;
      }
      ans[a] = min(ans[a], e);
    }
  }
  rep(i, n + 1) cout << ans[i] << "\\n";
}