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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;

int main() {
  cin.tie(0);
  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];
  vector<vector<ll>> xmin(1 << n, vector<ll>(n)), ymin(1 << n, vector<ll>(n));
  rep(i, 1 << n) {
    rep(j, n) {
      xmin[i][j] = abs(X[j]);
      ymin[i][j] = abs(Y[j]);
    }
    rep(k, n) {
      if ((i >> k) & 1) {
        rep(j, n) {
          xmin[i][j] = min(abs(X[j] - X[k]), xmin[i][j]);
          ymin[i][j] = min(abs(Y[j] - Y[k]), ymin[i][j]);
        }
      }
    }
  }
  vector<ll> pw(n + 1);
  pw[0] = 1;
  rep(i, n) pw[i + 1] = pw[i] * 3;
  vector<ll> ans(n + 1, longinf);
  auto dfs = [&](auto dfs, int i, ll xb, ll yb) -> void {
    if (i == n) {
      ll cost = 0;
      rep(j, n) cost += P[j] * min(xmin[xb][j], ymin[yb][j]);
      int cnt = __builtin_popcount(xb) + __builtin_popcount(yb);
      if (cnt > n)
        return;
      ans[cnt] = min(cost, ans[cnt]);
      return;
    }
    dfs(dfs, i + 1, xb, yb);
    dfs(dfs, i + 1, xb | (1 << i), yb);
    dfs(dfs, i + 1, xb, yb | (1 << i));
  };
  dfs(dfs, 0, 0, 0);
  rep(i, n + 1) cout << ans[i] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;

int main() {
  cin.tie(0);
  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];
  vector<vector<ll>> xmin(1 << n, vector<ll>(n)), ymin(1 << n, vector<ll>(n));
  rep(i, 1 << n) {
    rep(j, n) {
      xmin[i][j] = abs(X[j]);
      ymin[i][j] = abs(Y[j]);
    }
    rep(k, n) {
      if ((i >> k) & 1) {
        rep(j, n) {
          xmin[i][j] = min(abs(X[j] - X[k]), xmin[i][j]);
          ymin[i][j] = min(abs(Y[j] - Y[k]), ymin[i][j]);
        }
      }
    }
  }
  vector<ll> pw(n + 1);
  pw[0] = 1;
  rep(i, n) pw[i + 1] = pw[i] * 3;
  vector<ll> ans(n + 1, longinf);
  for (ll i = 0; i < pw[n]; i++) {
    ll x = 0, y = 0;
    rep(j, n) {
      if (i / pw[j] % 3 == 1) {
        x |= 1 << j;
      } else if (i / pw[j] % 3 == 2) {
        y |= 1 << j;
      }
    }
    ll cur = 0;
    rep(j, n) cur += P[j] * min(xmin[x][j], ymin[y][j]);
    int cnt = __builtin_popcount(x) + __builtin_popcount(y);
    if (cnt > n)
      continue;
    ans[cnt] = min(cur, ans[cnt]);
  }
  rep(i, n + 1) cout << ans[i] << "\\n";
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  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<vector<ll>> ver(1 << N, vector<ll>(N)), hor(1 << N, vector<ll>(N));
  for (int S = 0; S < 1 << N; S++) {
    for (int i = 0; i < N; i++) {
      ll v = abs(X[i]), h = abs(Y[i]);
      for (int j = 0; j < N; j++) {
        if (S >> j & 1) {
          v = min(v, abs(X[i] - X[j]));
          h = min(h, abs(Y[i] - Y[j]));
        }
      }
      ver[S][i] = v * P[i];
      hor[S][i] = h * P[i];
    }
  }
  vector<ll> ans(N + 1, 1e18);
  for (int S = 0; S < 1 << N; S++) {
    int cnt = 0;
    int v = 0, h = 0;
    vector<int> vv, hh;
    for (int i = 0; i < N; i++) {
      if (S >> i & 1) {
        cnt++;
        vv.push_back(1 << i);
        hh.push_back(1 << i);
      }
    }
    for (int b = 0; b < 1 << cnt; b++) {
      int v = 0, h = 0;
      for (int i = 0; i < cnt; i++) {
        if (b >> i & 1)
          v += vv[i];
        else
          h += hh[i];
      }

      ll cost = 0;
      for (int i = 0; i < N; i++) {
        cost += min(ver[v][i], hor[h][i]);
      }
      ans[cnt] = min(ans[cnt], cost);
    }
  }
  for (int i = 0; i <= N; i++)
    cout << ans[i] << '\\n';
}

Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  ll X[N], Y[N], P[N];
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
  }
  ll ans[N + 1];
  fill(ans, ans + N + 1, 1e18);
  int ver[N], hor[N];
  for (int S = 0; S < 1 << N; S++) {
    int cnt = 0;
    for (int i = 0; i < N; i++) {
      if (S >> i & 1) {
        ver[cnt] = X[i];
        hor[cnt] = Y[i];
        cnt++;
      }
    }
    for (int b = 0; b < 1 << cnt; b++) {
      ll total = 0;
      for (int i = 0; i < N; i++) {
        ll dist = min(abs(X[i]), abs(Y[i]));
        for (int j = 0; j < cnt; j++) {
          if (b >> j & 1)
            dist = min(dist, abs(X[i] - ver[j]));
          else
            dist = min(dist, abs(Y[i] - hor[j]));
        }
        total += dist * P[i];
      }
      ans[cnt] = min(ans[cnt], total);
    }
  }
  for (int i = 0; i <= N; i++)
    cout << ans[i] << '\\n';
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int x[20], y[20], p[20];
int xd[20][40010], yd[20][40010], u[40010], v[40010];
int X[20], Y[20], tot;
long long ans[30];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%d", &x[i], &y[i], &p[i]);
  int k = 1;
  for (int i = 0; i < n; i++)
    k *= 3;
  for (int i = 0; i <= n; i++)
    ans[i] = 1e18;
  int K = 1 << n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < K; j++)
      xd[i][j] = abs(x[i]), yd[i][j] = abs(y[i]);
    for (int j = 0; j < K; j++) {
      for (int l = 0; l < n; l++) {
        if (j & (1 << l)) {
          xd[i][j] = min(xd[i][j], abs(x[i] - x[l]));
          yd[i][j] = min(yd[i][j], abs(y[i] - y[l]));
        }
      }
    }
  }
  for (int i = 0; i < K; i++) {
    tot = 0;
    for (int j = 0; j < n; j++) {
      if (i & (1 << j)) {
        X[tot] = x[j];
        Y[tot++] = y[j];
      }
    }
    if (tot) {
      sort(X, X + tot);
      u[i] = unique(X, X + tot) - X;
      sort(Y, Y + tot);
      v[i] = unique(Y, Y + tot) - Y;
    }
  }
  for (int i = 0; i < k; i++) {
    int a = 0, b = 0;
    for (int j = 0, m = 1; j < n; j++, m *= 3) {
      int c = i / m % 3;
      if (c == 1)
        a |= (1 << j);
      if (c == 2)
        b |= (1 << j);
    }
    long long tmp = 0;
    for (int j = 0; j < n; j++)
      tmp += (long long)p[j] * min(xd[j][a], yd[j][b]);
    if (ans[u[a] + v[b]] > tmp)
      ans[u[a] + v[b]] = tmp;
  }
  for (int i = 0; i <= n; i++) {
    if (i)
      ans[i] = min(ans[i], ans[i - 1]);
    printf("%lld\\n", ans[i]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int x[20], y[20], p[20];
int c[20];
int X[20], Y[20], xtot, ytot;
long long ans[30];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%d", &x[i], &y[i], &p[i]);
  int k = 1;
  for (int i = 0; i < n; i++)
    k *= 3;
  for (int i = 0; i <= n; i++)
    ans[i] = -1;
  for (int i = 0; i < k; i++) {
    xtot = ytot = 0;
    for (int j = 0, m = 1; j < n; j++, m *= 3) {
      c[j] = i / m % 3;
      if (c[j] == 1)
        X[xtot++] = x[j];
      if (c[j] == 2)
        Y[ytot++] = y[j];
    }
    sort(X, X + xtot), sort(Y, Y + ytot);
    int u = unique(X, X + xtot) - X, v = unique(Y, Y + ytot) - Y;
    long long tmp = 0;
    for (int j = 0; j < n; j++) {
      if (c[j])
        continue;
      int dis = min(abs(x[j]), abs(y[j]));
      for (int k = 0; k < u; k++)
        dis = min(dis, abs(x[j] - X[k]));
      for (int k = 0; k < v; k++)
        dis = min(dis, abs(y[j] - Y[k]));
      tmp += (long long)dis * p[j];
    }
    if (ans[u + v] == -1 || ans[u + v] > tmp)
      ans[u + v] = tmp;
  }
  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>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  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];
  }
  long long x_pre[1 << N][N], y_pre[1 << N][N];
  for (int s = 0; s < (1 << N); s++) {
    for (int i = 0; i < N; i++) {
      x_pre[s][i] = abs(X[i]);
      y_pre[s][i] = abs(Y[i]);
      for (int j = 0; j < N; j++)
        if ((s >> j) & 1) {
          x_pre[s][i] = min(x_pre[s][i], abs(X[i] - X[j]));
          y_pre[s][i] = min(y_pre[s][i], abs(Y[i] - Y[j]));
        }
    }
  }
  long long ans[N + 1];
  fill(ans, ans + N + 1, LLONG_MAX / 100);
  for (int s = 0; s < (1 << N); s++) {
    int n = __builtin_popcount(s);
    for (int t = s;; t = (t - 1) & s) {
      long long sum = 0;
      for (int i = 0; i < N; i++)
        if (!((s >> i) & 1)) {
          sum += min(x_pre[t][i], y_pre[s - t][i]) * P[i];
        }
      ans[n] = min(ans[n], sum);
      if (!t)
        break;
    }
  }
  for (int K = 0; K <= N; K++) {
    cout << ans[K] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  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];
  }
  long long ans[N + 1];
  fill(ans, ans + N + 1, LLONG_MAX / 100);
  for (int s = 0; s < (1 << N); s++) {
    int n = __builtin_popcount(s);
    for (int t = s;; t = (t - 1) & s) {
      long long sum = 0;
      long long tmp[N];
      for (int i = 0; i < N; i++)
        tmp[i] = min(abs(X[i]), abs(Y[i])) * P[i];
      for (int j = 0; j < N; j++)
        if ((s >> j) & 1) {
          for (int i = 0; i < N; i++) {
            if ((t >> j) & 1) {
              tmp[i] = min(tmp[i], abs(X[i] - X[j]) * P[i]);
            } else {
              tmp[i] = min(tmp[i], abs(Y[i] - Y[j]) * P[i]);
            }
          }
        }
      for (int i = 0; i < N; i++)
        sum += tmp[i];
      ans[n] = min(ans[n], sum);
      if (!t)
        break;
    }
  }
  for (int K = 0; K <= N; K++) {
    cout << ans[K] << endl;
  }
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;

int main() {
  ll n;
  std::cin >> n;
  vector<ll> x(n), y(n), p(n);
  for (int i = 0; i < n; i++) {
    std::cin >> x[i] >> y[i] >> p[i];
  }
  vector<ll> pow3(n + 1, 1);
  for (int i = 0; i < n; i++) {
    pow3[i + 1] = pow3[i] * 3;
  }

  // iだけ利用するときに、jのコストの最小値
  vector<vector<ll>> tate((1 << n), vector<ll>(n));
  vector<vector<ll>> yoko((1 << n), vector<ll>(n));
  // map<pair<ll,ll>,ll> tate,yoko;

  for (ll i = 0; i < (1 << n); i++) {
    for (int j = 0; j < n; j++) {
      tate[i][j] = abs(x[j]);
      yoko[i][j] = abs(y[j]);
      for (int k = 0; k < n; k++) {
        if (i & (1 << k)) {
          tate[i][j] = min(tate[i][j], abs(x[j] - x[k]));
          yoko[i][j] = min(yoko[i][j], abs(y[j] - y[k]));
        }
      }
    }
  }

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

  for (ll i = 0; i < pow3[n]; i++) {
    ll cnt = n;
    ll tatei = 0;
    ll yokoi = 0;
    ll nowj = i;
    for (int j = 0; j < n; j++) {
      if (nowj % 3 == 0) {
        cnt--;
      } else if (nowj % 3 == 1) {
        tatei += (1 << j);
      } else {
        yokoi += (1 << j);
      }
      nowj /= 3;
    }
    ll tmp = 0;
    for (int j = 0; j < n; j++) {
      tmp += min(tate[tatei][j], yoko[yokoi][j]) * p[j];
    }
    ans[cnt] = min(ans[cnt], tmp);
  }

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


Error: TLE

Fixed submission:

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;

int main() {
  ll n;
  std::cin >> n;
  vector<ll> x(n), y(n), p(n);
  for (int i = 0; i < n; i++) {
    std::cin >> x[i] >> y[i] >> p[i];
  }
  vector<ll> pow3(n + 1, 1);
  for (int i = 0; i < n; i++) {
    pow3[i + 1] = pow3[i] * 3;
  }

  // iだけ利用するときに、jのコストの最小値
  vector<vector<ll>> tate((1 << n), vector<ll>(n));
  vector<vector<ll>> yoko((1 << n), vector<ll>(n));
  // map<pair<ll,ll>,ll> tate,yoko;

  for (ll i = 0; i < (1 << n); i++) {
    for (int j = 0; j < n; j++) {
      tate[i][j] = abs(x[j]);
      yoko[i][j] = abs(y[j]);
      for (int k = 0; k < n; k++) {
        if (i & (1 << k)) {
          tate[i][j] = min(tate[i][j], abs(x[j] - x[k]));
          yoko[i][j] = min(yoko[i][j], abs(y[j] - y[k]));
        }
      }
    }
  }

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

  for (ll i = 0; i < pow3[n]; i++) {
    ll cnt = n;
    ll tatei = 0;
    ll yokoi = 0;

    for (int j = 0; j < n; j++) {
      if (i / pow3[j] % 3 == 0) {
        cnt--;
      } else if (i / pow3[j] % 3 == 1) {
        tatei += (1 << j);
      } else {
        yokoi += (1 << j);
      }
    }
    ll tmp = 0;
    for (int j = 0; j < n; j++) {
      tmp += min(tate[tatei][j], yoko[yokoi][j]) * p[j];
    }
    ans[cnt] = min(ans[cnt], tmp);
  }

  for (int i = 0; i <= n; i++) {
    std::cout << ans[i] << std::endl;
  }
}
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##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll n;
  cin >> n;
  vl x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  vector<vl> dx(1 << n, vl(n)), dy(1 << n, vl(n));
  vl ans(n + 1, 1e18);
  rep(i, 1 << n) {
    rep(j, n) dx[i][j] = abs(x[j]);
    rep(j, n) dy[i][j] = abs(y[j]);
    vl v;
    rep(j, n) if (i >> j & 1) v.push_back(j);
    for (auto u : v) {
      rep(j, n) {
        dx[i][j] = min(dx[i][j], abs(x[u] - x[j]));
        dy[i][j] = min(dy[i][j], abs(y[u] - y[j]));
      }
    }
  }
  ll m = pow(3, n);
  rep(i, m) {
    ll ii = i, ax = 0, ay = 0, cnt = 0;
    rep(j, n) {
      ll r = ii % 3;
      ii /= 3;
      if (r == 1)
        ax += 1 << j;
      else if (r == 2)
        ay += 1 << j;
      if (r)
        cnt++;
    }
    ll now = 0;
    rep(j, n) now += min(dx[ax][j], dy[ay][j]) * p[j];
    ans[cnt] = min(ans[cnt], now);
  }
  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##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll n;
  cin >> n;
  vl x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  vl d(n), ans(n + 1, 1e18);
  rep(i, n) d[i] = min(abs(x[i]), abs(y[i]));
  ll m = pow(3, n);
  rep(i, m) {
    vl ax, ay;
    ll ii = i;
    rep(j, n) {
      ll r = ii % 3;
      ii /= 3;
      if (r == 1)
        ax.push_back(j);
      else if (r == 2)
        ay.push_back(j);
    }
    vl dd = d;
    for (auto v : ax)
      rep(u, n) dd[u] = min(dd[u], abs(x[v] - x[u]));
    for (auto v : ay)
      rep(u, n) dd[u] = min(dd[u], abs(y[v] - y[u]));
    ll now = 0;
    rep(u, n) now += dd[u] * p[u];
    ll s = ax.size() + ay.size();
    ans[s] = min(ans[s], now);
  }
  rep(i, n + 1) cout << ans[i] << '\\n';
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

const ll INF = 1e18;

int main() {

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

  vector<vector<ll>> ver(1 << n, vector<ll>(n)), hor(1 << n, vector<ll>(n));
  rep(i, 1 << n) {
    vector<int> c; // 路線通す都市
    rep(j, n) {
      if ((i >> j) & 1)
        c.push_back(j);
    }
    rep(j, n) {
      {
        ll cost = abs(x[j]) * p[j];
        for (int q : c) {
          cost = min(cost, abs((x[j] - x[q]) * p[j]));
        }
        ver[i][j] = cost;
      }
      {
        ll cost = abs(y[j]) * p[j];
        for (int q : c) {
          cost = min(cost, abs((y[j] - y[q]) * p[j]));
        }
        hor[i][j] = cost;
      }
    }
  }

  vector<ll> ans(n + 1, INF);
  for (int i = 0; i < (1 << n); i++) {
    int k = _popcnt32(i); // 路線通す数
    for (int j = i; j >= 0; j--) {
      j &= i; // jが立ってるところに横を通す

      ll cost = 0;
      rep(l, n) {
        if (!((i >> l) & 1)) { // lに路線がなかったら
          cost += min(ver[j][l], hor[i - j][l]);
        }
      }
      ans[k] = min(ans[k], cost);
    }
  }
  for (int k = 0; k <= n; k++)
    cout << ans[k] << endl;
  return 0;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
vector<int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};

const ll INF = 1e18;

int main() {

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

  vector<vector<ll>> ver(1 << n, vector<ll>(n)), hor(1 << n, vector<ll>(n));
  rep(i, 1 << n) {
    vector<int> c; // 路線通す都市
    rep(j, n) {
      if ((i >> j) & 1)
        c.push_back(j);
    }
    rep(j, n) {
      {
        ll cost = abs(x[j]) * p[j];
        for (int q : c) {
          cost = min(cost, abs((x[j] - x[q]) * p[j]));
        }
        ver[i][j] = cost;
      }
      {
        ll cost = abs(y[j]) * p[j];
        for (int q : c) {
          cost = min(cost, abs((y[j] - y[q]) * p[j]));
        }
        hor[i][j] = cost;
      }
    }
  }

  vector<ll> ans(n + 1, INF);
  for (int i = 0; i < (1 << n); i++) {
    int k = _popcnt32(i); // 路線通す数
    for (int j = 0; j <= i; j++) {
      j &= i;
      int _ver = j;        //_verが立ってるところに横の路線通す
      int _hor = i - _ver; //_horが立ってるところに縦の路線通す

      ll cost = 0;
      rep(l, n) {
        if (!((i >> l) & 1)) { // lに路線がなかったら
          cost += min(ver[_ver][l], hor[_hor][l]);
        }
      }
      ans[k] = min(ans[k], cost);
    }
  }
  for (int k = 0; k <= n; k++)
    cout << ans[k] << endl;
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("O3")
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long int ll;
constexpr ll kInf = ll(1E16 + 10);

int x[20], y[20], dis[20], rx[20], ry[20], v[20];
ll p[20], ans[20];

int ABS(int n) { return n >= 0 ? n : -n; }

int main() {
  int n, sz, tmp, idx, idy;
  ll tans;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%lld", &x[i], &y[i], &p[i]);
  for (int i = 0; i <= n; i++)
    ans[i] = kInf;
  for (int i = 0; i < (1 << n); i++) {
    sz = 0;
    tmp = i;
    while (tmp) {
      v[sz++] = __lg(tmp & -tmp);
      tmp ^= tmp & -tmp;
    }
    for (int j = 0; j < (1 << sz); j++) {
      idx = idy = 0;
      for (int k = 0; k < sz; k++)
        if (j & (1 << k))
          rx[idx++] = x[v[k]];
        else
          ry[idy++] = y[v[k]];
      for (int k = 0; k < n; k++)
        if (!(i & (1 << k))) {
          dis[k] = min(ABS(x[k]), ABS(y[k]));
          for (int i = 0; i < idx; i++)
            dis[k] = min(dis[k], ABS(x[k] - rx[i]));
          for (int i = 0; i < idy; i++)
            dis[k] = min(dis[k], ABS(y[k] - ry[i]));
        } else
          dis[k] = 0;
      tans = 0;
      for (int k = 0; k < n; k++)
        tans += dis[k] * p[k];
      ans[sz] = min(ans[sz], tans);
    }
  }

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


Error: TLE

Fixed submission:

#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
typedef long long int ll;
constexpr ll kInf = ll(1E16 + 10);

int x[20], y[20], dis[20];
ll p[20], ans[20];

int ABS(int n) { return n >= 0 ? n : -n; }

int main() {
  int n, sz, tmp;
  ll tans;
  vector<int> v;
  vector<int> rx, ry;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%lld", &x[i], &y[i], &p[i]);
  for (int i = 0; i <= n; i++)
    ans[i] = kInf;
  for (int i = 0; i < (1 << n); i++) {
    v.clear();
    tmp = i;
    while (tmp) {
      v.PB(__lg(tmp & -tmp));
      tmp ^= tmp & -tmp;
    }
    sz = int(v.size());
    for (int j = 0; j < (1 << sz); j++) {
      rx.clear(), ry.clear();
      for (int k = 1; k < (1 << sz); k <<= 1)
        if (j & k)
          rx.PB(x[v[__lg(k)]]);
        else
          ry.PB(y[v[__lg(k)]]);
      for (int k = 0; k < n; k++)
        dis[k] = min(ABS(x[k]), ABS(y[k]));
      for (int k = 0; k < n; k++) {
        for (int i : rx)
          dis[k] = min(dis[k], ABS(x[k] - i));
        for (int i : ry)
          dis[k] = min(dis[k], ABS(y[k] - i));
      }
      tans = 0;
      for (int k = 0; k < n; k++)
        tans += dis[k] * p[k];
      ans[sz] = min(ans[sz], tans);
    }
  }

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