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

#include <bits/stdc++.h>
#pragma GCC optimize(2) // 手动O2开关
using namespace std;
const int maxn = 17;
typedef long long ll;

int x[maxn], y[maxn], r[maxn], d[maxn], dd[maxn], cun[maxn];
ll ans[maxn];
int cnt;

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &x[i], &y[i], &r[i]);
    d[i] = min(abs(x[i]), abs(y[i]));
  }
  memset(ans, 0x3f, sizeof(ans));
  for (int i = 0; i < (1 << n); i++) {
    int now = i;
    cnt = 0;
    for (int j = 0; j < 15; j++) {
      if ((now >> j) & 1)
        cun[++cnt] = j + 1;
    }
    for (int j = 0; j < (1 << cnt); j++) {
      ll ans_ = 0;
      memcpy(dd, d, sizeof(dd));
      for (int k = 0; k < cnt; k++) {
        if ((j >> k) & 1) {
          for (int e = 1; e <= n; e++) {
            dd[e] = min(dd[e], abs(x[e] - x[cun[k + 1]]));
          }
        } else {
          for (int e = 1; e <= n; e++) {
            dd[e] = min(dd[e], abs(y[e] - y[cun[k + 1]]));
          }
        }
      }
      for (int i = 1; i <= n; i++) {
        ans_ += 1ll * dd[i] * r[i];
      }
      ans[cnt] = min(ans[cnt], ans_);
    }
  }
  for (int i = 0; i <= n; i++) {
    printf("%lld\\n", ans[i]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize(2) // 手动O2开关
using namespace std;
const int maxn = 30;
typedef long long ll;

int x[maxn], y[maxn], r[maxn], d[maxn], dd[maxn], cun[maxn];
ll ans[maxn];
int cnt;

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &x[i], &y[i], &r[i]);
    d[i] = min(abs(x[i]), abs(y[i]));
  }
  memset(ans, 0x3f, sizeof(ans));
  for (int i = 0; i < (1 << n); i++) {
    int now = i;
    cnt = 0;
    for (int j = 0; j < 15; j++) {
      if ((now >> j) & 1)
        cun[++cnt] = j + 1;
    }
    for (int j = 0; j < (1 << cnt); j++) {
      ll ans_ = 0;
      memcpy(dd, d, sizeof(dd));
      for (int k = 0; k < cnt; k++) {
        if ((j >> k) & 1) {
          for (int e = 1; e <= n; e++) {
            dd[e] = min(dd[e], abs(x[e] - x[cun[k + 1]]));
          }
        } else {
          for (int e = 1; e <= n; e++) {
            dd[e] = min(dd[e], abs(y[e] - y[cun[k + 1]]));
          }
        }
      }
      for (int i = 1; i <= n; i++) {
        ans_ += 1ll * dd[i] * r[i];
      }
      ans[cnt] = min(ans[cnt], ans_);
    }
  }
  for (int i = 0; i <= n; i++) {
    printf("%lld\\n", ans[i]);
  }
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
#define LL long long
using namespace std;

int n;
int xx[20], yy[20], dis[20];
LL ans[20], p[20];
void f(int t, int x) {
  int i, j;
  LL res = 0;
  int dis2[20];
  for (i = 1; i <= n; i++) {
    dis2[i] = dis[i];
    res += dis2[i] * p[i];
  }
  ans[t] = min(ans[t], res);
  if (x > n)
    return;
  for (i = 1; i <= n; i++)
    dis[i] = min(dis[i], abs(xx[i] - xx[x]));
  f(t + 1, x + 1);
  for (i = 1; i <= n; i++)
    dis[i] = min(dis2[i], abs(yy[i] - yy[x]));
  f(t + 1, x + 1);
  for (i = 1; i <= n; i++)
    dis[i] = dis2[i];
  f(t, x + 1);
}
int main() {
  int i;
  cin >> n;
  for (i = 0; i <= n; i++)
    ans[i] = 1ll << 60;
  for (i = 1; i <= n; i++) {
    cin >> xx[i] >> yy[i] >> p[i];
    dis[i] = min(abs(xx[i]), abs(yy[i]));
  }
  f(0, 1);
  for (i = 0; i <= n; i++) {
    if (ans[i] < (1ll << 60))
      cout << ans[i] << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#define LL long long
using namespace std;

int n, nx, ny;
int x[20], y[20], dis[20];
long long ans[20], p[20];
void f(int t) {
  int i, j;
  LL res = 0;
  int dis2[20];
  for (i = 1; i <= n; i++) {
    dis2[i] = dis[i];
    res += dis2[i] * p[i];
  }
  ans[t] = min(ans[t], res);
  for (i = 1; i <= n; i++) {
    if (dis[i]) {
      for (j = 1; j <= n; j++)
        dis[j] = min(dis[j], abs(x[j] - x[i]));
      f(t + 1);
      for (j = 1; j <= n; j++)
        dis[j] = min(dis2[j], abs(y[j] - y[i]));
      f(t + 1);
      for (j = 1; j <= n; j++)
        dis[j] = dis2[j];
    }
  }
}
int main() {
  int i;
  cin >> n;
  for (i = 0; i <= n; i++)
    ans[i] = 1ll << 60;
  for (i = 1; i <= n; i++) {
    cin >> x[i] >> y[i] >> p[i];
    dis[i] = min(abs(x[i]), abs(y[i]));
  }
  f(0);
  for (i = 0; i <= n; i++) {
    if (ans[i] < (1ll << 60))
      cout << ans[i] << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); };
const long long MOD = 1000000007;

ll N, X[20], Y[20], P[20];
ll cost[1 << 20];
ll dp[1 << 20][20];

void init() {
  REP(st, 1 << N) {
    ll z = INT64_MAX / 2;
    REP(i, N) {
      ll x = 0, y = 0;
      if ((st & (1 << i)) == 0)
        continue;
      REP(j, N) {
        if ((st & (1 << j)) == 0)
          continue;
        x += P[j] * abs(X[i] - X[j]);
        y += P[j] * abs(Y[i] - Y[j]);
      }
      z = min(z, min(x, y));
    }
    cost[st] = z;
  }
}

void solve() {
  init();
  REP(st, 1 << N) {
    REP(i, N) {
      if (st & (1 << i))
        dp[st][0] += P[i] * min(abs(X[i]), abs(Y[i]));
    }
  }
  REP(i, N) {
    REP(s, 1 << N) {
      ll m = INT64_MAX / 2;
      for (int t = s; t >= 0; t--) {
        t &= s;
        m = min(m, dp[s - t][i] + cost[t]);
      }
      dp[s][i + 1] = m;
    }
  }
}

signed main() {
  cin >> N;
  REP(i, N) cin >> X[i] >> Y[i] >> P[i];
  solve();
  REP(i, N + 1) PRINT(dp[(1 << N) - 1][i]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); };
const long long MOD = 1000000007;

ll N, K, X[16], Y[16], P[16];
ll cost[1 << 16];
ll dp[1 << 16][16];

ll init() {
  REP(st, 1 << N) {
    ll z = INT64_MAX / 2;
    REP(i, N) {
      ll x = 0, y = 0;
      if ((st & (1 << i)) == 0)
        continue;
      REP(j, N) {
        if ((st & (1 << j)) == 0)
          continue;
        x += P[j] * abs(X[i] - X[j]);
        y += P[j] * abs(Y[i] - Y[j]);
      }
      z = min(z, min(x, y));
    }
    cost[st] = z;
  }
}

ll solve() {
  init();
  REP(st, 1 << N) {
    REP(i, N) {
      if (st & (1 << i))
        dp[st][0] += P[i] * min(abs(X[i]), abs(Y[i]));
    }
  }
  REP(i, N) {
    REP(s, 1 << N) {
      ll m = INT64_MAX / 2;
      for (int t = s; t >= 0; t--) {
        t &= s;
        m = min(m, dp[s - t][i] + cost[t]);
      }
      dp[s][i + 1] = m;
    }
  }
}

signed main() {
  cin >> N;
  REP(i, N) cin >> X[i] >> Y[i] >> P[i];
  solve();
  REP(i, N + 1) PRINT(dp[(1 << N) - 1][i]);
  return 0;
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(V) V.begin(), V.end()
const ll MOD = 1000000007;

ll X[18], Y[18], P[18], N;
ll R[18];

void dfs(vector<int> &XX, vector<int> &YY, int c, int k) {
  if (c == N) {
    ll xd = MOD, yd = MOD, S = 0;
    for (int i = 0; i < N; i++) {
      xd = MOD, yd = MOD;
      for (ll x : XX) {
        xd = min(xd, abs(x - X[i]));
      }
      for (ll y : YY) {
        yd = min(yd, abs(y - Y[i]));
      }
      S += min(xd, yd) * P[i];
    }
    R[k] = min(R[k], S);
    return;
  }

  XX.push_back(X[c]);
  dfs(XX, YY, c + 1, k + 1);
  XX.pop_back();

  YY.push_back(Y[c]);
  dfs(XX, YY, c + 1, k + 1);
  YY.pop_back();

  dfs(XX, YY, c + 1, k);
}

int main() {
  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
    R[i] = INT64_MAX;
  }
  R[N] = INT64_MAX;
  vector<int> BX = {0};
  vector<int> BY = {0};
  dfs(BX, BY, 0, 0);

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

Error: TLE

Fixed submission:

#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 thr[16] = {0};
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;
  thr[0] = ma / 3;
  for (char i = 1; i < n; i++)
    thr[i] = thr[i - 1] / 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;
    for (char i = 0; i < n; i++)
      mi[i] = m1[i];
    for (char i = 0; i < n; i++) {
      now = ppk / thr[i];
      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 -= thr[i] * 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>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmin(x, y) x = min(x, y)

const ll INF = 1e18;

ll dp[20][20];

vector<ll> calc(vector<P> &a) {
  int n = a.size();
  sort(a.begin(), a.end());
  rep(i, n + 1) rep(j, n + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(l, n) {
    for (int r = l + 1; r <= n; ++r) {
      {
        ll now = INF;
        for (int k = l; k < r; ++k) {
          ll s = 0;
          for (int i = l; i < r; ++i) {
            s += abs(a[i].first - a[k].first) * ll(a[i].second);
          }
          chmin(now, s);
        }
        rep(j, n) chmin(dp[r][j + 1], dp[l][j] + now);
      }
      {
        ll now = 0;
        for (int i = l; i < r; ++i) {
          now += abs(a[i].first) * ll(a[i].second);
        }
        rep(j, n + 1) chmin(dp[r][j], dp[l][j] + now);
      }
    }
  }
  vector<ll> res(n + 1, INF);
  rep(j, n + 1) res[j] = dp[n][j];
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<int> 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()) {
      ans[i + j] = min(ans[i + j], dx[i] + dy[j]);
    }
  }

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

Error: TLE

Fixed submission:

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

const ll INF = 1e18;

vector<ll> calc(vector<P> &a) {
  int n = a.size();
  sort(a.begin(), a.end());
  vector<ll> res(n + 1, INF);
  rep(s, 1 << n) {
    vector<int> p;
    p.reserve(__builtin_popcount(s));
    rep(i, n) if (s >> i & 1) p.push_back(a[i].first);
    ll now = 0;
    int pi = 0;
    rep(i, n) {
      int x = a[i].first;
      while (pi + 1 < p.size() && p[pi + 1] < x)
        pi++;
      int m = abs(x);
      if (pi < p.size())
        m = min(m, abs(x - p[pi]));
      if (pi + 1 < p.size())
        m = min(m, abs(x - p[pi + 1]));
      now += ll(m) * a[i].second;
    }
    int b = p.size();
    res[b] = min(res[b], now);
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<int> 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()) {
      ans[i + j] = min(ans[i + j], dx[i] + dy[j]);
    }
  }

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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll inf = 1e9;
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<int>> v(1 << n, vector<int>(n)), h(1 << n, vector<int>(n));
  rep(b, 1 << n) {
    vector<int> vi = {-inf, 0, inf}, hi = {-inf, 0, inf};
    rep(i, n) if (b >> i & 1) vi.push_back(x[i]), hi.push_back(y[i]);
    sort(vi.begin(), vi.end());
    sort(hi.begin(), hi.end());
    rep(i, n) {
      if (b >> i & 1)
        continue;
      int xi = lower_bound(vi.begin(), vi.end(), x[i]) - vi.begin();
      int yi = lower_bound(hi.begin(), hi.end(), y[i]) - hi.begin();
      v[b][i] = min(vi[xi] - x[i], x[i] - vi[xi - 1]);
      h[b][i] = min(hi[yi] - y[i], y[i] - hi[yi - 1]);
    }
  }

  int t = 1;
  rep(_, n) t *= 3;
  vector<ll> ans(n + 1, 1e18L);
  rep(b, t) {
    int k = 0;
    int x = 0, y = 0, bi = b;
    rep(i, n) {
      int t = bi % 3;
      if (t != 0)
        ++k;
      if (t == 1)
        x |= 1 << i;
      if (t == 2)
        y |= 1 << i;
      bi /= 3;
    }
    ll res = 0;
    rep(i, n) res += p[i] * min(v[x][i], h[y][i]);
    ans[k] = min(ans[k], res);
  }
  rep(k, n + 1) cout << ans[k] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll inf = 1e9;
int main() {
  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  int t = 1;
  rep(_, n) t *= 3;
  vector<ll> ans(n + 1, 1e18L);
  rep(b, t) {
    int k = 0;
    vector<int> v = {-inf, 0, inf}, h = {-inf, 0, inf};
    int bi = b;
    rep(i, n) {
      int t = bi % 3;
      if (t != 0)
        ++k;
      if (t == 1)
        v.push_back(x[i]);
      if (t == 2)
        h.push_back(y[i]);
      bi /= 3;
    }
    sort(v.begin(), v.end());
    sort(h.begin(), h.end());

    ll res = 0;
    rep(i, n) {
      int vi = lower_bound(v.begin(), v.end(), x[i]) - v.begin();
      int hi = lower_bound(h.begin(), h.end(), y[i]) - h.begin();
      if (v[vi] == x[i] || h[hi] == y[i])
        continue;
      res += p[i] * min({v[vi] - x[i], x[i] - v[vi - 1], h[hi] - y[i],
                         y[i] - h[hi - 1]});
    }
    ans[k] = min(ans[k], res);
  }
  rep(k, n + 1) cout << ans[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<tuple<int, int, int>> V(N);
  for (int i = 0, x, y, p; cin >> x >> y >> p; i++)
    V.at(i) = {x, y, p};

  vector X(1 << N, vector(N, 0));
  auto Y = X;
  for (int bit = 0; bit < 1 << N; bit++) {
    for (int i = 0; i < N; i++) {
      auto [x, y, p] = V.at(i);
      X.at(bit).at(i) = abs(x);
      Y.at(bit).at(i) = abs(y);
      for (int j = 0; j < N; j++) {
        if (bit >> j & 1) {
          auto [x2, y2, p2] = V.at(j);
          X.at(bit).at(i) = min(X.at(bit).at(i), abs(x - x2));
          Y.at(bit).at(i) = min(Y.at(bit).at(i), abs(y - y2));
        }
      }
    }
  }

  vector ans(N + 1, LONG_MAX);
  for (int bit = 0; bit < 1 << N; bit++) {
    int cnt = __builtin_popcount(bit);
    for (int i = bit; i >= 0; i--) {
      i &= bit;
      long tmp = 0;
      for (int j = 0; j < N; j++) {
        if (!(bit >> j & 1)) {
          auto [x, y, p] = V.at(j);
          tmp += (long)min(X.at(i).at(j), Y.at(bit - i).at(j)) * p;
        }
      }
      ans.at(cnt) = min(ans.at(cnt), tmp);
    }
  }
  for (auto a : ans)
    cout << a << "\\n";
}

Error: TLE

Fixed submission:

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  long N;
  cin >> N;
  vector<long> X(N), Y(N), P(N);
  for (int i = 0; i < N; i++) {
    cin >> X.at(i) >> Y.at(i) >> P.at(i);
  }

  auto T = X;
  sort(T.begin(), T.end());
  T.erase(unique(T.begin(), T.end()), T.end());
  int xxx = T.size();
  T = Y;
  sort(T.begin(), T.end());
  T.erase(unique(T.begin(), T.end()), T.end());
  xxx = min(xxx, (int)T.size());

  vector<long> ans(N + 1, LONG_MAX);
  for (int bit_x = 0; bit_x < 1 << N; bit_x++) {
    int b = __builtin_popcount(bit_x);
    if (b >= xxx) {
      ans.at(b) = 0;
      continue;
    }
    vector<long> NX, NY, NP;
    for (int i = 0; i < N; i++) {
      if (!(bit_x >> i & 1)) {
        NX.push_back(X.at(i));
        NY.push_back(Y.at(i));
        NP.push_back(P.at(i));
      }
    }
    int M = NX.size();
    for (int bit_y = 0; bit_y < 1 << M; bit_y++) {
      int bp = __builtin_popcount(bit_x) + __builtin_popcount(bit_y);
      if (bp >= xxx) {
        ans.at(bp) = 0;
        continue;
      }
      long tmp = 0;
      for (int i = 0; i < M; i++) {
        if (!(bit_y >> i & 1)) {
          int x = NX.at(i);
          int y = NY.at(i);
          long d = min(abs(x), abs(y));
          for (int j = 0; j < N; j++) {
            if (bit_x >> j & 1)
              d = min(d, abs(X.at(j) - x));
          }
          for (int j = 0; j < M; j++) {
            if (bit_y >> j & 1)
              d = min(d, abs(NY.at(j) - y));
          }
          tmp += d * NP.at(i);
        }
      }
      ans.at(bp) = min(ans.at(bp), tmp);
    }
  }
  for (auto a : ans)
    cout << a << "\\n";
}
Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long int;
using pll = pair<ll, ll>;

const ll INF = 1LL << 62;
const ll MAXN = 16;

ll N;

ll X[MAXN];
ll Y[MAXN];
ll P[MAXN];

ll CX[MAXN][1 << MAXN];
ll CY[MAXN][1 << MAXN];

ll calcCost(int xmask, int ymask) {
  ll cost = 0;
  rep(i, N) { cost += min(CX[i][xmask], CY[i][ymask]) * P[i]; }

  return cost;
}

ll dfs(int index, int last, int xmask, int ymask) {
  if (index + last > N) {
    return INF;
  }
  if (index == N || last == 0) {
    return calcCost(xmask, ymask);
  }

  const auto a = dfs(index + 1, last, xmask, ymask);
  const auto b = dfs(index + 1, last - 1, xmask | (1 << index), ymask);
  const auto c = dfs(index + 1, last - 1, xmask, ymask | (1 << index));

  return min(a, min(b, c));
}

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

  rep(i, N) rep(j, 1 << N) {
    CX[i][j] = abs(X[i]);
    CY[i][j] = abs(Y[i]);

    rep(k, N) {
      if ((j >> k) & 1) {
        CX[i][j] = min(CX[i][j], abs(X[i] - X[k]));
      }
      if ((j >> k) & 1) {
        CY[i][j] = min(CY[i][j], abs(Y[i] - Y[k]));
      }
    }
  }

  rep(i, N + 1) { cout << max(0LL, dfs(0, i, 0, 0)) << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define REP(i, p, n) for (ll i = p; i < ll(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long int;
using pll = pair<ll, ll>;

#define P first
#define X second.first
#define Y second.second

const ll MAXN = 16;

ll N;
pair<ll, pll> S[MAXN];

ll DX[MAXN][1 << MAXN];
ll DY[MAXN][1 << MAXN];

ll calcCost(int xx, int yy) {
  ll cost = 0;

  rep(i, N) { cost += min(DX[i][xx], DY[i][yy]) * S[i].P; }

  return cost;
}

ll dfs(int index, int last, int xx, int yy) {
  if (index == N || last == 0) {
    return calcCost(xx, yy);
  }

  return min(dfs(index + 1, last, xx, yy),
             min(dfs(index + 1, last - 1, xx | (1 << index), yy),
                 dfs(index + 1, last - 1, xx, yy | (1 << index))));
}

int main() {
  cin >> N;
  rep(i, N) { cin >> S[i].X >> S[i].Y >> S[i].P; }

  rep(i, N) rep(j, 1 << N) {
    DX[i][j] = abs(S[i].X);
    DY[i][j] = abs(S[i].Y);

    rep(k, N) {
      if ((j >> k) & 1) {
        DX[i][j] = min(DX[i][j], abs(S[i].X - S[k].X));
      }
      if ((j >> k) & 1) {
        DY[i][j] = min(DY[i][j], abs(S[i].Y - S[k].Y));
      }
    }
  }

  rep(i, N + 1) { cout << max(0LL, dfs(0, i, 0, 0)) << endl; }

  return 0;
}