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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1.6e7 + 23;
const int N = 16;

ll ans[N];
int x[N], y[N], p[N], _min[maxn][N], pw[N][3];
bool vis[maxn];
int n;

vector<int> mask(int j) {
  vector<int> t(n);
  int now = 0;
  while (now < n) {
    t[now++] = j % 3;
    j /= 3;
  }
  return t;
}

int main() {
  memset(ans, 0x3f, sizeof(ans));
  cin >> n;
  int BIT = 1;
  int nn = n;
  for (int i = 0, j = 1; i < n; i++, j *= 3) {
    pw[i][1] = j;
    pw[i][2] = j * 2;
  }
  while (nn--)
    BIT *= 3;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &x[i], &y[i], &p[i]);
  }
  ans[0] = 0;
  for (int i = 0; i < n; i++) {
    _min[0][i] = min(abs(x[i]), abs(y[i]));
    ans[0] += 1ll * _min[0][i] * p[i];
  }
  for (int i = 1; i < BIT; i++) {
    vector<int> bit = mask(i);
    int pop_count = 0;
    for (int j : bit)
      pop_count += j > 0;
    int last = n - 1;
    while (!bit[last])
      last--;
    int pre = i - pw[last][bit[last]];
    ll cost = 0;
    for (int j = 0; j < n; j++) {
      if (bit[last] == 1)
        _min[i][j] = min(_min[pre][j], abs(x[last] - x[j]));
      else if (bit[last] == 2)
        _min[i][j] = min(_min[pre][j], abs(y[last] - y[j]));
      cost += 1ll * _min[i][j] * p[j];
    }
    ans[pop_count] = min(ans[pop_count], cost);
  }
  for (int i = 0; i <= n; i++) {
    printf("%lld\\n", ans[i]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1.6e7 + 23;
const int N = 16;

ll ans[N];
int x[N], y[N], p[N];
bool vis[maxn];
int n;

vector<int> mask(int j) {
  vector<int> t(n);
  int now = 0;
  while (now < n) {
    t[now++] = j % 3;
    j /= 3;
  }
  return t;
}

void dfs(vector<int> _min, vector<int> bit, int dep, int add) {
  if (dep == n)
    return;
  int state = 0;
  for (int i = 0, base = 1; i < n; i++, base *= 3)
    state += bit[i] * base;
  if (vis[state])
    return;
  vis[state] = 1;
  ll cost = 0;
  for (int i = 0; i < n; i++) {
    if (bit[add] == 1) {
      _min[i] = min(_min[i], abs(x[i] - x[add]));
    } else if (bit[add] == 2) {
      _min[i] = min(_min[i], abs(y[i] - y[add]));
    }
    cost += 1ll * _min[i] * p[i];
  }
  ans[dep] = min(ans[dep], cost);
  for (int i = 0; i < n; i++) {
    if (!bit[i]) {
      bit[i] = 1;
      dfs(_min, bit, dep + 1, i);
      bit[i] = 2;
      dfs(_min, bit, dep + 1, i);
      bit[i] = 0;
    }
  }
}

int main() {
  memset(ans, 0x3f, sizeof(ans));
  cin >> n;
  int BIT = 1;
  int nn = n;
  while (nn--)
    BIT *= 3;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &x[i], &y[i], &p[i]);
  }
  vector<int> _min(n);
  for (int i = 0; i < n; i++) {
    _min[i] = min(abs(x[i]), abs(y[i]));
  }
  dfs(_min, vector<int>(n), 0, 0);
  ans[n] = 0;
  for (int i = 0; i <= n; i++) {
    printf("%lld\\n", ans[i]);
  }
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long x[n], y[n], p[n];
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  vector<long long> ans(n + 1, 1LL << 60);
  long long memox[1 << n][n], memoy[1 << n][n];
  for (int s = 0; s < 1 << n; s++) {
    for (int i = 0; i < n; i++) {
      long long xx = abs(x[i]), yy = abs(y[i]);
      for (int j = 0; j < n; j++) {
        if ((s >> j) & 1) {
          yy = min(yy, abs(y[i] - y[j]));
          xx = min(xx, abs(x[i] - x[j]));
        }
      }
      memox[s][i] = xx * p[i];
      memoy[s][i] = yy * p[i];
    }
  }
  for (int s = 0; s < 1 << n; s++) {
    const int k = __builtin_popcount(s);
    for (int t = s;; t = (t - 1) & s) {
      long long cost = 0;
      int h = s ^ t;
      for (int i = 0; i < n; i++) {
        cost += min(memox[h][i], memoy[t][i]);
      }
      ans[k] = min(ans[k], cost);
      if (t == 0)
        break;
    }
  }
  for (int i = 0; i <= n; i++)
    cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long x[n], y[n], p[n];
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  vector<long long> ans(n + 1, 1LL << 60);
  for (int s = 0; s < 1 << n; s++) {
    const int k = __builtin_popcount(s);
    for (int t = s;; t = (t - 1) & s) {
      long long cost = 0;
      int h = s ^ t;
      for (int i = 0; i < n; i++) {
        long long xx = abs(x[i]), yy = abs(y[i]);
        if (((h >> i) & 1) || ((t >> i) & 1))
          continue;
        for (int j = 0; j < n; j++) {
          if ((h >> j) & 1)
            yy = min(yy, abs(y[i] - y[j]));
          else if ((t >> j) & 1)
            xx = min(xx, abs(x[i] - x[j]));
        }
        cost += min(xx, yy) * p[i];
      }
      ans[k] = min(ans[k], cost);
      if (t == 0)
        break;
    }
  }
  for (int i = 0; i <= n; i++)
    cout << ans[i] << endl;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  const ll INF = 1ll << 60;
  vector<ll> res(n + 1, INF);
  vector<int> xs, ys, no;
  for (ll i = 0; i < pow(3, n); ++i) {
    xs.clear();
    ys.clear();
    no.clear();
    ll dig = i;
    rep(j, n) {
      if (dig % 3 == 0)
        no.push_back(j);
      if (dig % 3 == 1)
        xs.push_back(j);
      if (dig % 3 == 2)
        ys.push_back(j);

      dig /= 3;
    }

    ll cost = 0;
    for (int j : no) {
      ll dist = min(abs(x[j]), abs(y[j]));
      for (int k : xs)
        dist = min(dist, abs(x[k] - x[j]));
      for (int k : ys)
        dist = min(dist, abs(y[k] - y[j]));

      cost += dist * p[j];
    }

    int cnt = xs.size() + ys.size();
    res[cnt] = min(res[cnt], cost);
  }

  rep(i, n + 1) cout << res[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  const ll INF = 1ll << 60;
  vector<ll> res(n + 1, INF);
  for (ll i = 0; i < pow(3, n); ++i) {
    vector<int> xs, ys, no;
    ll dig = i;
    rep(j, n) {
      if (dig % 3 == 0)
        no.push_back(j);
      if (dig % 3 == 1)
        xs.push_back(j);
      if (dig % 3 == 2)
        ys.push_back(j);

      dig /= 3;
    }

    ll cost = 0;
    for (int j : no) {
      ll dist = min(abs(x[j]), abs(y[j]));
      for (int k : xs)
        dist = min(dist, abs(x[k] - x[j]));
      for (int k : ys)
        dist = min(dist, abs(y[k] - y[j]));

      cost += dist * p[j];
    }

    int cnt = xs.size() + ys.size();
    res[cnt] = min(res[cnt], cost);
  }

  rep(i, n + 1) cout << res[i] << endl;

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;
const int N = 1e6 + 34;

ll mincx[15][1 << 15];
ll mincy[15][1 << 15];

int main() {
  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  vector<ll> ans(n + 1, 1ll * MOD * MOD);
  for (int mask = 0; mask < (1 << n); mask++) {
    for (int j = 0; j < n; j++) {
      ll res = 1e18;
      for (int z = 0; z < n; z++) {
        if (mask & (1 << z))
          res = min(res, p[j] * abs(x[j] - x[z]));
      }
      res = min(res, p[j] * abs(x[j]));
      mincx[j][mask] = res;
    }
  }
  for (int mask = 0; mask < (1 << n); mask++) {
    for (int j = 0; j < n; j++) {
      ll res = 1e18;
      for (int z = 0; z < n; z++) {
        if (mask & (1 << z))
          res = min(res, p[j] * abs(y[j] - y[z]));
      }
      res = min(res, p[j] * abs(y[j]));
      mincy[j][mask] = res;
    }
  }
  for (int mask = 0; mask < (1 << n); mask++) {
    int cnt = __builtin_popcount(mask);
    for (int submask = mask;; submask = (submask - 1) & mask) {
      int oth = mask - submask;
      ll res = 0;
      for (int j = 0; j < n; j++) {
        res += min(mincx[j][submask], mincy[j][oth]);
      }
      ans[cnt] = min(ans[cnt], res);
      if (submask == 0)
        break;
    }
  }
  for (int i = 0; i < n + 1; i++) {
    cout << ans[i] << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;
const int N = 1e6 + 34;

int main() {
  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  vector<ll> ans(n + 1, 1ll * MOD * MOD);
  for (int mask = 0; mask < (1 << n); mask++) {
    int cnt = __builtin_popcount(mask);
    for (int submask = mask;; submask = (submask - 1) & mask) {
      int oth = mask - submask;
      ll res = 0;
      for (int j = 0; j < n; j++) {
        ll kek = 1e18;
        for (int z = 0; z < n; z++) {
          if (submask & (1 << z))
            kek = min(kek, p[j] * abs(x[j] - x[z]));
          if (oth & (1 << z))
            kek = min(kek, p[j] * abs(y[j] - y[z]));
        }
        kek = min(kek, p[j] * abs(x[j]));
        kek = min(kek, p[j] * abs(y[j]));
        res += kek;
      }
      ans[cnt] = min(ans[cnt], res);
      if (submask == 0)
        break;
    }
  }
  for (int i = 0; i < n + 1; i++) {
    cout << ans[i] << "\\n";
  }
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 17;

typedef pair<int, int> pii;
#define x first
#define y second

ll ans[sz];
int xi[sz], yi[sz], pi[sz];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> xi[i] >> yi[i] >> pi[i];

  vector<pii> dx, dy;
  for (int i = 0; i < n; i++)
    dx.emplace_back(xi[i], i);
  for (int i = 0; i < n; i++)
    dy.emplace_back(yi[i], i);
  sort(dx.begin(), dx.end());
  sort(dy.begin(), dy.end());

  int lim = 1;
  for (int i = 0; i < n; i++)
    lim *= 3;
  for (int i = 0; i <= n; i++)
    ans[i] = LLONG_MAX;
  for (int r = 0; r < lim; r++) {
    int v = r;
    vector<int> a(n);
    int who = 0;
    for (int i = 0; i < n; i++) {
      a[i] = v % 3, v /= 3;
      if (a[i])
        who++;
    }

    int ara[17];
    for (int i = 0; i < n; i++)
      ara[i] = min(abs(xi[i]), abs(yi[i]));

    for (int i = 0; i < n; i++) {
      int k = dx[i].y;
      if (a[k] == 1) {
        ara[k] = 0;
        for (int j = i - 1; j >= 0; j--) {
          int kk = dx[j].y;
          ara[kk] = min(ara[kk], abs(xi[k] - xi[kk]));
          if (a[kk] == 1)
            break;
        }
        for (int j = i + 1; j < n; j++) {
          int kk = dx[j].y;
          ara[kk] = min(ara[kk], abs(xi[k] - xi[kk]));
          if (a[kk] == 1)
            break;
        }
      }
    }

    for (int i = 0; i < n; i++) {
      int k = dy[i].y;
      if (a[k] == 2) {
        ara[k] = 0;
        for (int j = i - 1; j >= 0; j--) {
          int kk = dy[j].y;
          ara[kk] = min(ara[kk], abs(yi[k] - yi[kk]));
          if (a[kk] == 2)
            break;
        }
        for (int j = i + 1; j < n; j++) {
          int kk = dy[j].y;
          ara[kk] = min(ara[kk], abs(yi[k] - yi[kk]));
          if (a[kk] == 2)
            break;
        }
      }
    }

    ll now = 0;
    for (int i = 0; i < n; i++)
      now += 1LL * pi[i] * ara[i];

    ans[who] = min(ans[who], now);
  }
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 17;

ll ans[sz];
int xi[sz], yi[sz], pi[sz];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> xi[i] >> yi[i] >> pi[i];

  int lim = pow(3, n);
  for (int i = 0; i <= n; i++)
    ans[i] = LLONG_MAX;
  for (int r = 0; r < lim; r++) {
    int v = r;
    vector<int> a(n), xx(1, 0), yy(1, 0);
    int who = 0;
    for (int i = 0; i < n; i++) {
      a[i] = v % 3, v /= 3;
      if (a[i])
        who++;
      if (a[i] == 1)
        xx.push_back(xi[i]);
      if (a[i] == 2)
        yy.push_back(yi[i]);
    }

    ll now = 0;
    for (int i = 0; i < n; i++) {
      int here = INT_MAX;
      for (int x : xx)
        here = min(here, abs(xi[i] - x));
      for (int y : yy)
        here = min(here, abs(yi[i] - y));
      now += 1LL * here * pi[i];
    }
    ans[who] = min(ans[who], now);
  }
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long N;
long long X[20];
long long Y[20];
long long P[20];

#define NO 1
#define YOKO 2
#define TATE 3
long long pat[20];

//   num     , cost
map<long long, long long> memo;

long long xx[20];
long long yy[20];
long long noidx[20];

void cal() {
  long long xxidx = 0;
  long long yyidx = 0;
  long long noidxidx = 0;
  long long sum = 0;

  for (long long i = 0; i < N; i++) {
    if (pat[i] == TATE) {
      xx[xxidx] = X[i];
      xxidx++;
    } else if (pat[i] == YOKO) {
      yy[yyidx] = Y[i];
      yyidx++;
    } else {
      noidx[noidxidx] = i;
      noidxidx++;
    }
  }

  for (long long tmptmp = 0; tmptmp < noidxidx; tmptmp++) {
    long long i = noidx[tmptmp];
    long long tmp = min(abs(X[i]), abs(Y[i]));
    for (long long j = 0; j < xxidx; j++) {
      long long x = xx[j];
      tmp = min(tmp, abs(x - X[i]));
    }
    for (long long j = 0; j < yyidx; j++) {
      long long y = yy[j];
      tmp = min(tmp, abs(y - Y[i]));
    }
    sum += tmp * P[i];
  }

  long long num = xxidx + yyidx;
  memo[num] = min(memo[num], sum);
}

void dfs(long long dep) {
  if (dep == N) {
    cal();
    return;
  }
  pat[dep] = NO;
  dfs(dep + 1);
  pat[dep] = YOKO;
  dfs(dep + 1);
  pat[dep] = TATE;
  dfs(dep + 1);
}

int main() {
  cin >> N;
  for (long long i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
    memo[i] = LONG_MAX;
  }

  dfs(0);

  for (long long i = 0; i < N + 1; i++) {
    cout << memo[i] << endl;
  }
}

Error: TLE

Fixed submission:

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

long long N;
long long X[20];
long long Y[20];
long long P[20];

#define NO 1
#define YOKO 2
#define TATE 3
long long pat[20];

//   num     , cost
map<long long, long long> memo;

void cal() {
  set<long long> xx;
  set<long long> yy;
  set<long long> noidx;
  long long sum = 0;

  for (long long i = 0; i < N; i++) {
    if (pat[i] == TATE) {
      xx.insert(X[i]);
    } else if (pat[i] == YOKO) {
      yy.insert(Y[i]);
    } else {
      noidx.insert(i);
    }
  }

  for (auto i : noidx) {
    long long tmp = min(abs(X[i]), abs(Y[i]));
    for (auto x : xx) {
      tmp = min(tmp, abs(x - X[i]));
    }
    for (auto y : yy) {
      tmp = min(tmp, abs(y - Y[i]));
    }
    sum += tmp * P[i];
  }

  long long num = xx.size() + yy.size();
  memo[num] = min(memo[num], sum);
}

void dfs(long long dep) {
  if (dep == N) {
    cal();
    return;
  }
  pat[dep] = NO;
  dfs(dep + 1);
  pat[dep] = YOKO;
  dfs(dep + 1);
  pat[dep] = TATE;
  dfs(dep + 1);
}

int main() {
  cin >> N;
  for (long long i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
    memo[i] = LONG_MAX;
  }

  dfs(0);

  for (long long i = 0; i < N + 1; i++) {
    cout << memo[i] << endl;
  }
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#pragma GCC optimize(2)
short x[16], y[16];
int p[16];
long long ans[16];
short mi[16], m1[16];
short px[16][16], py[16][16];
int ppk, st;
short cnt, now;
int main(void) {

  char n;
  scanf("%hd", &n);
  for (char i = 0; i < n; i++)
    scanf("%hd%hd%d", &x[i], &y[i], &p[i]);
  int ma = 1;
  for (char i = 0; i <= n; ++i)
    ans[i] = 1000000000000000;
  for (char i = 0; i < n; ++i)
    ma *= 3;
  for (char i = 0; i < n; ++i) {
    short x1 = x[i] > 0 ? x[i] : -x[i], y1 = y[i] > 0 ? y[i] : -y[i];
    m1[i] = x1 > y1 ? y1 : x1;
  }
  for (char i = 0; i < n; ++i) {
    for (char j = 0; j < n; ++j) {
      px[i][j] = x[i] - x[j];
      py[i][j] = y[i] - y[j];
      if (px[i][j] < 0)
        px[i][j] = -px[i][j];
      if (py[i][j] < 0)
        py[i][j] = -py[i][j];
    }
  }
  for (int zhu = 0; zhu < ma; ++zhu) {
    ppk = zhu, cnt = n, st = ma / 3;
    for (char i = 0; i < n; i++)
      mi[i] = m1[i];
    for (char i = 0; i < n; i++, st /= 3) {
      now = ppk / st;
      switch (now) {
      case 2:
        for (char j = 0; j < n; ++j)
          if (mi[j] > px[i][j])
            mi[j] = px[i][j];
        break;
      case 1:
        for (char j = 0; j < n; ++j)
          if (mi[j] > py[i][j])
            mi[j] = py[i][j];
        break;
      case 0:
        cnt--;
      }
      ppk -= st * now;
    }
    long long sum = 0;
    for (char i = 0; i < n; ++i)
      sum += (long long)mi[i] * p[i];
    if (ans[cnt] > sum)
      ans[cnt] = sum;
  }
  for (char i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
}


Error: TLE

Fixed submission:

#include <stdio.h>
short x[16], y[16];
int p[16];
long long ans[16];
short mi[16], m1[16];
short px[16][16], py[16][16];
int ppk, st;
short cnt, now;
int main(void) {

  char n;
  scanf("%hd", &n);
  for (char i = 0; i < n; i++)
    scanf("%hd%hd%d", &x[i], &y[i], &p[i]);
  int ma = 1;
  for (char i = 0; i <= n; ++i)
    ans[i] = 1000000000000000;
  for (char i = 0; i < n; ++i)
    ma *= 3;
  for (char i = 0; i < n; ++i) {
    short x1 = x[i] > 0 ? x[i] : -x[i], y1 = y[i] > 0 ? y[i] : -y[i];
    m1[i] = x1 > y1 ? y1 : x1;
  }
  for (char i = 0; i < n; ++i) {
    for (char j = 0; j < n; ++j) {
      px[i][j] = x[i] - x[j];
      py[i][j] = y[i] - y[j];
      if (px[i][j] < 0)
        px[i][j] = -px[i][j];
      if (py[i][j] < 0)
        py[i][j] = -py[i][j];
    }
  }
  for (int zhu = 0; zhu < ma; ++zhu) {
    ppk = zhu, cnt = n, st = ma / 3;
    for (char i = 0; i < n; i++)
      mi[i] = m1[i];
    for (char i = 0; i < n; i++, st /= 3) {
      now = ppk / st;
      switch (now) {
      case 2:
        for (char j = 0; j < n; ++j)
          if (mi[j] > px[i][j])
            mi[j] = px[i][j];
        break;
      case 1:
        for (char j = 0; j < n; ++j)
          if (mi[j] > py[i][j])
            mi[j] = py[i][j];
        break;
      case 0:
        cnt--;
      }
      ppk -= st * now;
    }
    long long sum = 0;
    for (char i = 0; i < n; ++i)
      sum += (long long)mi[i] * p[i];
    if (ans[cnt] > sum)
      ans[cnt] = sum;
  }
  for (char i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
}
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++)
using ll = long long;
const ll INF = 1e17;

int n;
ll x[15], y[15], p[15];

set<ll> sx, sy;
vector<ll> ans(20, INF);

ll dfs(int id) {
  ll dis = INF;
  if (id == n) {
    int l = sx.size() + sy.size() - 2;
    dis = 0;
    rep(i, n) {
      ll res = INF;
      auto itr = sx.lower_bound(x[i]);
      if (itr != sx.end())
        res = min(res, abs(x[i] - (*itr)));
      if (itr != sx.begin()) {
        itr--;
        res = min(res, abs(x[i] - (*itr)));
      }
      itr = sy.lower_bound(y[i]);
      if (itr != sy.end())
        res = min(res, abs(y[i] - (*itr)));
      if (itr != sy.begin()) {
        itr--;
        res = min(res, abs(y[i] - (*itr)));
      }
      dis += p[i] * res;
      if (dis >= ans[l])
        break;
    }
    ans[l] = min(ans[l], dis);
  } else {
    dis = min(dis, dfs(id + 1));
    if (sx.find(x[id]) == sx.end()) {
      sx.insert(x[id]);
      dis = min(dis, dfs(id + 1));
      sx.erase(x[id]);
    }
    if (sy.find(y[id]) == sy.end()) {
      sy.insert(y[id]);
      dis = min(dis, dfs(id + 1));
      sy.erase(y[id]);
    }
  }
  return dis;
}

int main() {
  cin >> n;
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  sx.insert(0);
  sy.insert(0);
  dfs(0);
  rep(i, n + 1) {
    if (i > 0)
      ans[i] = min(ans[i], ans[i - 1]);
    cout << ans[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using ll = long long;
const ll INF = 1e17;

int n;
ll x[15], y[15], p[15];

set<ll> sx, sy;
vector<ll> ans(20, INF);

ll dfs(int id) {
  ll dis = INF;
  if (id == n) {
    dis = 0;
    rep(i, n) {
      ll res = INF;
      auto itr = sx.lower_bound(x[i]);
      if (itr != sx.end())
        res = min(res, abs(x[i] - (*itr)));
      if (itr != sx.begin()) {
        itr--;
        res = min(res, abs(x[i] - (*itr)));
      }
      itr = sy.lower_bound(y[i]);
      if (itr != sy.end())
        res = min(res, abs(y[i] - (*itr)));
      if (itr != sy.begin()) {
        itr--;
        res = min(res, abs(y[i] - (*itr)));
      }
      dis += p[i] * res;
    }
    int l = sx.size() + sy.size() - 2;
    ans[l] = min(ans[l], dis);
  } else {
    dis = min(dis, dfs(id + 1));
    if (sx.find(x[id]) == sx.end()) {
      sx.insert(x[id]);
      dis = min(dis, dfs(id + 1));
      sx.erase(x[id]);
    }
    if (sy.find(y[id]) == sy.end()) {
      sy.insert(y[id]);
      dis = min(dis, dfs(id + 1));
      sy.erase(y[id]);
    }
  }
  return dis;
}

int main() {
  cin >> n;
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  sx.insert(0);
  sy.insert(0);
  dfs(0);
  rep(i, n + 1) {
    if (i > 0)
      ans[i] = min(ans[i], ans[i - 1]);
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

vector<int> a, b, x, y, p;
vector<ll> ans;
int n;

void dfs(int ind) {
  if (ind == n) {
    int pop = a.size() + b.size();
    ll tmp = 0;
    for (int i = 0; i < n; i++) {
      int diff = min(abs(x[i]), abs(y[i]));
      for (int j : a)
        diff = min(diff, abs(j - x[i]));
      for (int j : b)
        diff = min(diff, abs(j - y[i]));
      tmp += (ll)diff * p[i];
    }
    ans[pop] = min(ans[pop], tmp);
    return;
  }
  a.push_back(x[ind]);
  dfs(ind + 1);
  a.pop_back();
  b.push_back(y[ind]);
  dfs(ind + 1);
  b.pop_back();
  dfs(ind + 1);
}

int main() {
  cin >> n;
  x.resize(n);
  y.resize(n);
  p.resize(n);
  ans.resize(n + 1, 1ll << 60);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  dfs(0);
  for (int i = 0; i < n + 1; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n), p(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  vector<ll> ans(n + 1, 1ll << 60);
  for (int i = 0; i < 1 << n; i++) {
    vector<int> ind;
    for (int j = 0; j < n; j++)
      if (i >> j & 1)
        ind.push_back(j);
    int pop = ind.size();
    for (int k = 0; k < 1 << pop; k++) {
      vector<int> xx(1, 0);
      vector<int> yy(1, 0);
      for (int l = 0; l < pop; l++) {
        if (k >> l & 1)
          xx.push_back(x[ind[l]]);
        else
          yy.push_back(y[ind[l]]);
      }
      ll tmp = 0;
      for (int l = 0; l < n; l++) {
        int diff = 30000;
        for (int m : xx)
          diff = min(diff, abs(x[l] - m));
        for (int m : yy)
          diff = min(diff, abs(y[l] - m));
        tmp += (ll)diff * p[l];
      }
      ans[pop] = min(ans[pop], tmp);
    }
  }
  for (int i = 0; i < n + 1; i++)
    cout << ans[i] << endl;
  return 0;
}