Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

void solve();
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 223450
#endif

int N, Q, K;
int A[SIZE];

const int MAX = 5e5;
const int MOD = 1e9 + 7;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  if (!fac[n])
    COMinit();
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int P(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  if (!fac[n])
    COMinit();
  return (fac[n] * finv[n - k]) % MOD;
}

struct Item {
  int x, y, p;
};
vector<Item> V;

int MI[2][15][1LL << 15];

int calc(int i, int j) {
  // コスト
  int ret = 0;
  REP(k, N) { ret += min(MI[0][k][i], MI[1][k][j]); }
  return ret;
}

void solve() {
  cin >> N;
  V.resize(N);
  REP(i, N) { cin >> V[i].x >> V[i].y >> V[i].p; }
  REP(b, 1LL << N) {
    REP(i, N) {
      {
        // x
        int mi = abs(V[i].x);
        REP(j, N) if (b >> j & 1) { chmin(mi, abs(V[i].x - V[j].x)); }
        MI[0][i][b] = mi * V[i].p;
      }
      {
        // y
        int mi = abs(V[i].y);
        REP(j, N) if (b >> j & 1) { chmin(mi, abs(V[i].y - V[j].y)); }
        MI[1][i][b] = mi * V[i].p;
      }
    }
  }
  vector<int> ans(N + 1, LONG_LONG_MAX / 2);
  REP(i, 1LL << N) {
    int b = __builtin_popcount(i);
    int ret = calc(i, 0);
    for (int j = i; j > 0; j = (j - 1) & i) {
      chmin(ret, calc(i ^ j, j));
    }
    chmin(ans[b], ret);
  }

  REP(i, N + 1) { cout << ans[i] << endl; }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

void solve();
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 223450
#endif

int N, Q, K;
int A[SIZE];

const int MAX = 5e5;
const int MOD = 1e9 + 7;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  if (!fac[n])
    COMinit();
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int P(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  if (!fac[n])
    COMinit();
  return (fac[n] * finv[n - k]) % MOD;
}

struct Item {
  int x, y, p;
};
vector<Item> V;

int calc(int i, int j, int maxr) {
  // iに含まれる駅を含む線
  // jのbitが1なら縦,0なら横
  set<int> xs = {0}, ys = {0};
  int jcnt = 0;
  REP(k, N) if (i >> k & 1) {
    if (j >> jcnt & 1) {
      // 縦
      xs.insert(V[k].x);
    } else {
      // 横
      ys.insert(V[k].y);
    }
    jcnt++;
  }
  // コスト
  int ret = 0;
  REP(k, N) {
    if (i >> k & 1)
      continue;
    int mi = LONG_LONG_MAX;
    int x = V[k].x, y = V[k].y, p = V[k].p;
    auto itx = xs.lower_bound(x);
    if (itx != xs.end())
      chmin(mi, *itx - x);
    if (itx != xs.begin())
      chmin(mi, x - *(--itx));
    auto ity = ys.lower_bound(y);
    if (ity != ys.end())
      chmin(mi, *ity - y);
    if (ity != ys.begin())
      chmin(mi, y - *(--ity));
    ret += mi * p;
    if (maxr <= ret)
      return LONG_LONG_MAX / 2;
  }
  return ret;
}

void solve() {
  cin >> N;
  V.resize(N);
  REP(i, N) { cin >> V[i].x >> V[i].y >> V[i].p; }
  vector<int> ans(N + 1, LONG_LONG_MAX / 2);
  REP(i, 1LL << N) {
    int b = (int)__builtin_popcount(i);
    if (ans[b] == 0)
      continue;
    int ret = LONG_LONG_MAX / 2;
    REP(j, 1LL << b) { chmin(ret, calc(i, j, ans[b])); }
    chmin(ans[b], ret);
  }

  REP(i, N + 1) { cout << ans[i] << endl; }
}