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

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

constexpr int64_t inf = 1e18;

int main() {
  int n;
  cin >> n;
  vector<int64_t> x(n), y(n), p(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  vector dx(1 << n, vector(n, inf)), dy(1 << n, vector(n, inf));
  for (int i = 0; i < 1 << n; i++) {
    for (int j = 0; j < n; j++) {
      dx.at(i).at(j) = p.at(j) * abs(x.at(j));
      dy.at(i).at(j) = p.at(j) * abs(y.at(j));
    }
    for (int j = 0; j < n; j++) {
      if (i >> j & 1) {
        for (int k = 0; k < n; k++) {
          dx.at(i).at(k) =
              min(dx.at(i).at(k), p.at(k) * abs(x.at(j) - x.at(k)));
          dy.at(i).at(k) =
              min(dy.at(i).at(k), p.at(k) * abs(y.at(j) - y.at(k)));
        }
      }
    }
  }
  vector<int64_t> ans(n + 1, inf);
  vector<int> v(n + 1);
  while (v.at(n) == 0) {
    int k = 0, s1 = 0, s2 = 0;
    for (int i = 0; i < n; i++) {
      if (v.at(i) == 1) {
        s1 += 1 << i;
      } else if (v.at(i) == 2) {
        s2 += 1 << i;
      }
    }
    int64_t d_total = 0;
    for (int i = 0; i < n; i++) {
      if (v.at(i))
        k++;
      d_total += min(dx.at(s1).at(i), dy.at(s2).at(i));
    }
    ans.at(k) = min(ans.at(k), d_total);
    v.at(0)++;
    int l = 0;
    while (v.at(l) == 3) {
      v.at(l) = 0;
      v.at(l + 1)++;
      l++;
    }
  }
  for (int i = 0; i <= n; i++) {
    cout << ans.at(i) << endl;
  }
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

constexpr int64_t inf = 1e18;

int main() {
  int n;
  cin >> n;
  vector<int64_t> x(n), y(n), p(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  vector<int64_t> ans(n + 1, inf);
  vector<int> v(n + 1);
  while (v.at(n) == 0) {
    int k = 0;
    vector<int64_t> d(n, inf);
    for (int i = 0; i < n; i++) {
      d.at(i) = p.at(i) * min({d.at(i), abs(x.at(i)), abs(y.at(i))});
      if (v.at(i))
        k++;
    }
    for (int i = 0; i < n; i++) {
      if (v.at(i) == 1) {
        for (int j = 0; j < n; j++) {
          d.at(j) = min(d.at(j), p.at(j) * abs(x.at(j) - x.at(i)));
        }
      } else if (v.at(i) == 2) {
        for (int j = 0; j < n; j++) {
          d.at(j) = min(d.at(j), p.at(j) * abs(y.at(j) - y.at(i)));
        }
      }
    }
    int64_t d_total = 0;
    for (int i = 0; i < n; i++) {
      d_total += d.at(i);
    }
    ans.at(k) = min(ans.at(k), d_total);
    v.at(0)++;
    int l = 0;
    while (v.at(l) == 3) {
      v.at(l) = 0;
      v.at(l + 1)++;
      l++;
    }
  }
  for (int i = 0; i <= n; i++) {
    cout << ans.at(i) << endl;
  }
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize(2)
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 20;

int x[N], y[N], p[N];
int dis[N];
LL ans[N];
int vis[N];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", &x[i], &y[i], &p[i]), ans[i] = 1e18;
  ans[0] = 1e18;

  for (int i = 0; i < (1 << n); i++) {
    int num = 0;
    for (int j = 0; j < n; j++)
      if ((i >> j) & 1)
        vis[++num] = j + 1; // 此次建num个road

    for (int j = 0; j < (1 << num); j++) {

      for (int k = 1; k <= n; k++)
        dis[k] = min(abs(x[k]), abs(y[k]));

      for (int k = 0; k < num; k++) {
        if ((j >> k) & 1) { // 在num个road里选x方向建road
          for (int t = 1; t <= n; t++)
            dis[t] = min(dis[t], abs(x[t] - x[vis[k + 1]]));
        } // 维护每个站到新road的最短距离
        else {
          for (int t = 1; t <= n; t++)
            dis[t] = min(dis[t], abs(y[t] - y[vis[k + 1]]));
        }
      }
      LL s = 0;
      for (int k = 1; k <= n; k++)
        s += (LL)dis[k] * p[k];
      ans[num] = min(ans[num], s);
    }
  }
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 20;

int x[N], y[N], p[N];
int dis[N];
LL ans[N];
int vis[N];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", &x[i], &y[i], &p[i]), ans[i] = 1e18;
  ans[0] = 1e18;

  for (int i = 0; i < (1 << n); i++) {
    int num = 0;
    for (int j = 0; j < n; j++)
      if ((i >> j) & 1)
        vis[++num] = j + 1; // 此次建num个road

    for (int j = 0; j < (1 << num); j++) {

      for (int k = 1; k <= n; k++)
        dis[k] = min(abs(x[k]), abs(y[k]));

      for (int k = 0; k < num; k++) {
        if ((j >> k) & 1) { // 在num个road里选x方向建road
          for (int t = 1; t <= n; t++)
            dis[t] = min(dis[t], abs(x[t] - x[vis[k + 1]]));
        } // 维护每个站到新road的最短距离
        else {
          for (int t = 1; t <= n; t++)
            dis[t] = min(dis[t], abs(y[t] - y[vis[k + 1]]));
        }
      }
      LL s = 0;
      for (int k = 1; k <= n; k++)
        s += (LL)dis[k] * p[k];
      ans[num] = min(ans[num], s);
    }
  }
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 20;

int x[N], y[N], p[N];
int dis[N];
LL ans[N];
int vis[N];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", &x[i], &y[i], &p[i]), ans[i] = 1e18;
  ans[0] = 1e18;

  for (int i = 0; i < (1 << n); i++) {
    int num = 0;
    for (int j = 0; j < n; j++)
      if ((i >> j) & 1)
        vis[++num] = j + 1; // 此次建num个road

    for (int j = 0; j < (1 << num); j++) {

      for (int k = 1; k <= n; k++)
        dis[k] = min(abs(x[k]), abs(y[k]));

      for (int k = 0; k < num; k++) {
        if ((j >> k) & 1) { // 在num个road里选x方向建road
          for (int t = 1; t <= n; t++)
            dis[t] = min(dis[t], abs(x[t] - x[vis[k + 1]]));
        } // 维护每个站到新road的最短距离
        else {
          for (int t = 1; t <= n; t++)
            dis[t] = min(dis[t], abs(y[t] - y[vis[k + 1]]));
        }
      }
      LL s = 0;
      for (int k = 1; k <= n; k++)
        s += (LL)dis[k] * p[k];
      ans[num] = min(ans[num], s);
    }
  }
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>

#include <algorithm>
#include <complex>
#include <queue>
#include <set>

int read(void) {
  int res = 0, sign = 1;
  char ch = std::getchar();
  while (!std::isdigit(ch)) {
    if (ch == '-')
      sign = -1;
    ch = std::getchar();
  }
  while (std::isdigit(ch))
    res = res * 10 + ch - 48, ch = std::getchar();
  return sign * res;
}

template <class Tp> void write(const Tp &x) {
  if (x > 9)
    write(x / 10);
  std::putchar(x % 10 + 48);
}

typedef long long ll;
typedef unsigned long long ull;
typedef double rl;
typedef long double RL;

const rl PI = 3.1415926535898, E = 2.71828182845904;

const int MAXN = 18;

ll ans[MAXN];
int dist[MAXN];

int n;
int x[MAXN], y[MAXN], p[MAXN];
int in[MAXN];

int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    x[i] = read(), y[i] = read(), p[i] = read();
  for (int i = 0; i <= n; ++i)
    ans[i] = 1e18;
  for (int i = 0; i < (1 << n); ++i) {
    int cnt = 0;
    for (int j = 1; j <= n; ++j)
      if (i & (1 << (j - 1)))
        in[++cnt] = j;
    for (int j = 0; j < (1 << cnt); ++j) {
      for (int p = 1; p <= n; ++p)
        dist[p] = std::min(int(std::abs(x[p])), int(std::abs(y[p])));
      for (int k = 1; k <= cnt; ++k)
        if (j & (1 << (k - 1)))
          for (int p = 1; p <= n; ++p)
            dist[p] = std::min(dist[p], int(std::abs(x[p] - x[in[k]])));
        else
          for (int p = 1; p <= n; ++p)
            dist[p] = std::min(dist[p], int(std::abs(y[p] - y[in[k]])));
      ll s = 0ll;
      for (int j = 1; j <= n; ++j)
        s += (ll)dist[j] * p[j];
      ans[cnt] = std::min(ans[cnt], s);
    }
  }
  for (int i = 0; i <= n; ++i)
    std::printf("%lld\\n", ans[i]);
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<ll, ll> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  VL x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  const int pj = 3;
  VL xx, yy, hl;
  VL ans(n + 1, 1LL << 55);
  rep(i, (int)pow(pj, n)) {
    xx.clear();
    yy.clear(), hl.clear();
    for (int j = 0, wki = i; j < n; j++, wki /= pj) {
      int nw = wki % pj;
      if (nw == 0)
        hl.push_back(j);
      else if (nw == 1)
        xx.push_back(x[j]);
      else if (nw == 2)
        yy.push_back(y[j]);
    }
    // for(auto x :vi) cerr<<x<<" "; dbg();
    ll sm = 0;
    for (auto z : hl) {
      ll cnd = min<ll>(abs(x[z]), abs(y[z]));
      for (auto k : xx)
        cnd = min(cnd, abs(k - x[z]));
      for (auto k : yy)
        cnd = min(cnd, abs(k - y[z]));
      sm += cnd * p[z];
    }
    int sc = sz(xx) + sz(yy);
    // dbg(sc, i, sm);
    ans[sc] = min(ans[sc], sm);
  }

  rep(i, n + 1) cout << ans[i] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<ll, ll> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  VL x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  const int pj = 3;
  VL xx, yy, hl;
  VL ans(n + 1, 1LL << 55);
  rep(i, (int)pow(pj, n)) {
    VI vi(n);
    for (int j = 0, wki = i; j < n; j++, wki /= pj) {
      vi[n - 1 - j] = wki % pj;
    }
    // for(auto x :vi) cerr<<x<<" "; dbg();
    xx.clear();
    yy.clear(), hl.clear();
    rep(j, n) {
      if (vi[j] == 0)
        hl.push_back(j);
      else if (vi[j] == 1)
        xx.push_back(x[j]);
      else if (vi[j] == 2)
        yy.push_back(y[j]);
    }
    ll sm = 0;
    for (auto z : hl) {
      ll cnd = min<ll>(abs(x[z]), abs(y[z]));
      for (auto k : xx)
        cnd = min(cnd, abs(k - x[z]));
      for (auto k : yy)
        cnd = min(cnd, abs(k - y[z]));
      sm += cnd * p[z];
    }
    int sc = sz(xx) + sz(yy);
    // dbg(sc, i, sm);
    ans[sc] = min(ans[sc], sm);
  }

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

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

typedef long long ll;
ll n;
ll x[20], y[20], p[20];
ll ret[20];
ll D[20];

int a[20];
int cnt;
void solve(int k) {
  if (k == 0) {
    for (int i = 0; i < n; i++)
      D[i] = min(abs(x[i]), abs(y[i]));
  }
  if (k == n - 2) {
    for (int i = 0; i < n; i++) {
      if (i < k && a[i] != 0) {
        D[i] = 0;
      } else {
        D[i] = min(abs(x[i]), abs(y[i]));
        for (int j = 0; j < k; j++) {
          if (a[j] == 1)
            D[i] = min(D[i], abs(x[i] - x[j]));
          if (a[j] == 2)
            D[i] = min(D[i], abs(y[i] - y[j]));
        }
      }
    }
  }
  if (k == n) {
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] != 0)
        continue;
      ll d = D[i];
      for (int j = max((int)n - 2, 0); j < n; j++) {
        if (a[j] == 1)
          d = min(d, abs(x[i] - x[j]));
        if (a[j] == 2)
          d = min(d, abs(y[i] - y[j]));
      }
      sum += d * p[i];
    }
    ret[cnt] = min(ret[cnt], sum);
    return;
  }
  for (int j = 0; j < 3; j++) {
    a[k] = j;
    if (j > 0)
      cnt++;
    solve(k + 1);
    if (j > 0)
      cnt--;
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  for (int k = 0; k <= n; k++) {
    ret[k] = 1000000000000000000;
  }
  solve(0);
  for (int k = 0; k <= n; k++) {
    cout << ret[k] << endl;
  }
}

Error: TLE

Fixed submission:

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

typedef long long ll;
ll n;
ll x[20], y[20], p[20];
ll ret[20];

int a[20];
int cnt;
void solve(int k) {
  if (k == n) {
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] != 0)
        continue;
      ll d = min(abs(x[i]), abs(y[i]));
      for (int j = 0; j < n; j++) {
        if (a[j] == 1)
          d = min(d, abs(x[i] - x[j]));
        if (a[j] == 2)
          d = min(d, abs(y[i] - y[j]));
      }
      sum += d * p[i];
    }
    ret[cnt] = min(ret[cnt], sum);
    return;
  }
  for (int j = 0; j < 3; j++) {
    a[k] = j;
    if (j > 0)
      cnt++;
    solve(k + 1);
    if (j > 0)
      cnt--;
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  for (int k = 0; k <= n; k++) {
    ret[k] = 1000000000000000000;
  }
  solve(0);
  for (int k = 0; k <= n; k++) {
    cout << ret[k] << endl;
  }
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> y(n);
  vector<long long> p(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];

  // 初期化
  int ax[33000][15];
  int ay[33000][15];
  long long c[20];
  for (int i = 0; i <= n; i++)
    c[i] = 1000000000000000000;

  for (int bit = 0; bit < (1 << n); bit++) {
    for (int i = 0; i < n; i++) {
      ax[bit][i] = abs(x[i]);
      for (int j = 0; j < n; j++) {
        if ((bit >> j) & 1)
          ax[bit][i] = min(ax[bit][i], abs(x[i] - x[j]));
      }
    }
  }

  for (int bit = 0; bit < (1 << n); bit++) {
    for (int i = 0; i < n; i++) {
      ay[bit][i] = abs(y[i]);
      for (int j = 0; j < n; j++) {
        if ((bit >> j) & 1)
          ay[bit][i] = min(ay[bit][i], abs(y[i] - y[j]));
      }
    }
  }

  // 3進数bit全探索
  for (int bit = 0; bit < pow(3, n); bit++) {
    int tmp = bit;
    int bitx = 0;
    int bity = 0;
    int k = 0;
    for (int pos = 0; pos < n; pos++) {
      if (tmp % 3 == 1) {
        bitx += (1 << pos);
        k++;
      }
      if (tmp % 3 == 2) {
        bity += (1 << pos);
        k++;
      }
      tmp /= 3;
    }
    long long t = 0;
    for (int i = 0; i < n; i++) {
      t += min(ax[bitx][i], ay[bity][i]) * p[i];
    }
    c[k] = min(c[k], t);
  }

  for (int k = 0; k <= n; k++)
    cout << c[k] << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> y(n);
  vector<long long> p(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];

  // 初期化
  vector<int> a(n);
  vector<long long> c(n + 1);
  for (int i = 0; i <= n; i++)
    c[i] = 100000000000000000;

  for (int mask = 0; mask < pow(3, n); mask++) {
    int tmp = mask;
    int k = 0;
    long long t = 0;
    // return 0;
    for (int pos = 0; pos < n; pos++) {
      a[pos] = tmp % 3;
      if (a[pos] > 0)
        k++;
      tmp /= 3;
    }
    for (int i = 0; i < n; i++) {
      int z = min(abs(x[i]), abs(y[i]));
      for (int j = 0; j < n; j++) {
        if (a[j] == 1)
          z = min(z, abs(x[i] - x[j]));
        if (a[j] == 2)
          z = min(z, abs(y[i] - y[j]));
      }
      t += z * p[i];
    }
    c[k] = min(c[k], t);
  }
  for (int k = 0; k <= n; k++)
    cout << c[k] << endl;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<ll> xs, ys;
vector<int> no_rails;

int main() {
  int n;
  cin >> n;
  ll powthree = 1;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) {
    powthree *= 3;
    cin >> x[i] >> y[i] >> p[i];
  }
  vector<ll> ans(n + 1, 1e18);
  for (int i = 0; i < powthree; i++) {
    xs.clear();
    ys.clear();
    no_rails.clear();
    int info = i;
    for (int j = 0; j < n; j++) {
      if (info % 3 == 0)
        no_rails.push_back(j);
      if (info % 3 == 1)
        xs.push_back(x[j]);
      if (info % 3 == 2)
        ys.push_back(y[j]);
      info /= 3;
    }
    ll score = 0;
    for (int j : no_rails) {
      ll dist = min(abs(x[j]), abs(y[j]));
      for (ll xi : xs)
        dist = min(dist, abs(xi - x[j]));
      for (ll yi : ys)
        dist = min(dist, abs(yi - y[j]));
      score += p[j] * dist;
    }
    ans[xs.size() + ys.size()] = min(ans[xs.size() + ys.size()], score);
  }

  for (int i = 0; i <= n; i++) {
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  ll powthree = 1;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) {
    powthree *= 3;
    cin >> x[i] >> y[i] >> p[i];
  }
  vector<ll> ans(n + 1, 1e18);
  for (int i = 0; i < powthree; i++) {
    int info = i;
    vector<ll> xs, ys;
    vector<int> no_rails;
    for (int j = 0; j < n; j++) {
      if (info % 3 == 0)
        no_rails.push_back(j);
      if (info % 3 == 1)
        xs.push_back(x[j]);
      if (info % 3 == 2)
        ys.push_back(y[j]);
      info /= 3;
    }
    ll score = 0;
    for (int j : no_rails) {
      ll dist = min(abs(x[j]), abs(y[j]));
      for (ll xi : xs)
        dist = min(dist, abs(xi - x[j]));
      for (ll yi : ys)
        dist = min(dist, abs(yi - y[j]));
      score += p[j] * dist;
    }
    ans[xs.size() + ys.size()] = min(ans[xs.size() + ys.size()], score);
  }

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

#pragma G++ optimize(O2)
#pragma G++ optimize(O3)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 16;
LL res[N];
struct node {
  int x, y, p;
} a[N];
int vis[N];
int dis[N];

void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].p);
  }
  memset(res, 0x3f, sizeof res);

  // i：选择哪几个点上添加横线，1为添加，0为不添加
  for (int i = 0; i < (1 << n); i++) {
    int cnt = 0;
    for (int j = 0; j < n; j++)
      if ((i >> j) & 1)
        vis[++cnt] = j + 1;

    // j:选择的点的所有状态，1为x方向，0为y方向
    for (int j = 0; j < (1 << cnt); j++) {

      for (int k = 1; k <= n; k++)
        dis[k] = min(abs(a[k].x), abs(a[k].y));

      for (int k = 0; k < cnt; k++) {
        if ((j >> k) & 1) {
          for (int t = 1; t <= n; t++) {
            dis[t] = min(dis[t], abs(a[t].x - a[vis[k + 1]].x));
          }
        } else {
          for (int t = 1; t <= n; t++) {
            dis[t] = min(dis[t], abs(a[t].y - a[vis[k + 1]].y));
          }
        }
      }
      LL sum = 0;
      for (int k = 1; k <= n; k++)
        sum += (LL)dis[k] * a[k].p;
      res[cnt] = min(res[cnt], sum);
    }
  }
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", res[i]);
}

int main() {
  //    freopen("in.txt", "r", stdin);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#pragma G++ optimize(O2)
#pragma G++ optimize(O3)
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 16;
LL res[N];
struct node {
  int x, y, p;
} a[N];
int vis[N];
int dis[N];

void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].p);
  }
  memset(res, 0x3f, sizeof res);

  // i：选择哪几个点上添加横线，1为添加，0为不添加
  for (int i = 0; i < (1 << n); i++) {
    int cnt = 0;
    for (int j = 0; j < n; j++)
      if ((i >> j) & 1)
        vis[++cnt] = j + 1;

    // j:选择的点的所有状态，1为x方向，0为y方向
    for (int j = 0; j < (1 << cnt); j++) {

      for (int k = 1; k <= n; k++)
        dis[k] = min(abs(a[k].x), abs(a[k].y));

      for (int k = 0; k < cnt; k++) {
        if ((j >> k) & 1) {
          for (int t = 1; t <= n; t++) {
            dis[t] = min(dis[t], abs(a[t].x - a[vis[k + 1]].x));
          }
        } else {
          for (int t = 1; t <= n; t++) {
            dis[t] = min(dis[t], abs(a[t].y - a[vis[k + 1]].y));
          }
        }
      }
      LL sum = 0;
      for (int k = 1; k <= n; k++)
        sum += (LL)dis[k] * a[k].p;
      res[cnt] = min(res[cnt], sum);
    }
  }
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", res[i]);
}

int main() {
  //    freopen("in.txt", "r", stdin);
  solve();
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

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

constexpr int64_t INF = 1e18;
constexpr int MAX = 15;
int n;
int64_t x[MAX], y[MAX], p[MAX];
int64_t dpx[MAX][1 << MAX];
int64_t dpy[MAX][1 << MAX];
int64_t ans[MAX + 1];

void init() {
  for (int i = 0; i < MAX; ++i) {
    ans[i] = INF;
    for (int j = 0; j < (1 << MAX); ++j) {
      dpx[i][j] = abs(x[i]);
      dpy[i][j] = abs(y[i]);
    }
  }
  for (int j = 0; j < (1 << MAX); ++j) {
    for (int k = 0; k < MAX; ++k) {
      if (j >> k & 1) {
        for (int i = 0; i < MAX; ++i) {
          dpx[i][j] = min(dpx[i][j], abs(x[i] - x[k]));
          dpy[i][j] = min(dpy[i][j], abs(y[i] - y[k]));
        }
      }
    }
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> x[i] >> y[i] >> p[i];
  init();
  for (int mask = 0; mask < pow(3, n); ++mask) {
    int msk = mask;
    int mskx = 0, msky = 0;
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if (msk % 3 == 1) {
        mskx |= (1 << i);
        ++cnt;
      }
      if (msk % 3 == 2) {
        msky |= (1 << i);
        ++cnt;
      }
      msk /= 3;
    }
    int64_t sum = 0;
    for (int i = 0; i < n; ++i) {
      sum += min(dpx[i][mskx], dpy[i][msky]) * p[i];
    }
    ans[cnt] = min(ans[cnt], sum);
  }
  for (int i = 0; i <= n; ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const int64_t LINF = 8931145141919364364, LMOD = 998244353;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};

int main() {
  int n;
  cin >> n;
  int x[n], y[n], p[n];
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  int64_t ans[n + 1];
  rep(i, n + 1) ans[i] = LINF;
  for (int mask = 0; mask < pow(3, n); ++mask) {
    int msk = mask;
    vector<int64_t> X;
    X.push_back(0);
    vector<int64_t> Y;
    Y.push_back(0);
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if (msk % 3 == 0) {
        X.push_back(x[i]);
        ++cnt;
      } else if (msk % 3 == 1) {
        Y.push_back(y[i]);
        ++cnt;
      }
      msk /= 3;
    }
    int64_t sum = 0;
    for (int i = 0; i < n; ++i) {
      int64_t mini = LINF;
      for (int64_t j : X) {
        mini = min(mini, abs(j - x[i]));
      }
      for (int64_t j : Y) {
        mini = min(mini, abs(j - y[i]));
      }
      sum += mini * p[i];
    }
    ans[cnt] = min(sum, ans[cnt]);
  }
  for (int i = 0; i <= n; ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}