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

#ifndef __SETS_H__
#define __SETS_H__

/* updated: 2020-03-28 */

// 集合系の関数などを置きたい

#include <cassert>
#include <cstdint>
#include <vector>

#ifdef _MSC_VER
#include <intrin.h>
#endif

// ビット演算系

/* 立っているビットの数を数える */
inline int popcount(uint64_t value) {
#ifdef _MSC_VER
  return __popcnt64(value);
#elif defined(__GNUC__)
  return __builtin_popcountll(value);
#else
  value = (value & 0x5555555555555555) + ((value >> 1) & 0x5555555555555555);
  value = (value & 0x3333333333333333) + ((value >> 2) & 0x3333333333333333);
  value = (value & 0x0f0f0f0f0f0f0f0f) + ((value >> 4) & 0x0f0f0f0f0f0f0f0f);
  value = (value & 0x00ff00ff00ff00ff) + ((value >> 8) & 0x00ff00ff00ff00ff);
  value = (value & 0x0000ffff0000ffff) + ((value >> 16) & 0x0000ffff0000ffff);
  value = (value & 0x00000000ffffffff) + ((value >> 32) & 0x00000000ffffffff);
  return value;
#endif
}

/* 最上位ビットの位置を返す (0-based, value == 0 のときは -1 を返す) */
/* floor(log2(value)) と一致する */
inline int msbPosition(uint64_t value) {
  value |= value >> 1;
  value |= value >> 2;
  value |= value >> 4;
  value |= value >> 8;
  value |= value >> 16;
  value |= value >> 32;
  return popcount(value) - 1;
}

/* 最下位ビットの位置を返す (0-based, value == 0 のときは 64 を返す) */
inline int lsbPosition(uint64_t value) {
  value |= value << 1;
  value |= value << 2;
  value |= value << 4;
  value |= value << 8;
  value |= value << 16;
  value |= value << 32;
  return 64 - popcount(value);
}

/* 最上位ビットを取得 (value == 0 のときは 0 を返す) */
inline uint64_t msb(uint64_t value) {
  if (value == 0)
    return uint64_t();
  return uint64_t(1) << msbPosition(value);
}

/* 最下位ビットを取得 (value == 0 のときは 0 を返す) */
/* TODO: これに関しては明らかに value & -value
 * のほうが速いと思うけどどうしようか */
inline uint64_t lsb(uint64_t value) {
  if (value == 0)
    return uint64_t();
  return uint64_t(1) << lsbPosition(value);
}

/* ceil(log2(value)) を返す (value == 0 のときは -1 を返す) */
/* 戻り値の範囲は -1 <= ret <= 64 */
inline int ceilLog2(uint64_t value) {
  if (value == 0)
    return -1;
  return msbPosition(value - 1) + 1;
}

/* ビットの並びを逆転させる */
inline uint64_t bitReverse(uint64_t val) {
  val = (val & 0x5555555555555555) << 1 | (val & 0xAAAAAAAAAAAAAAAA) >> 1;
  val = (val & 0x3333333333333333) << 2 | (val & 0xCCCCCCCCCCCCCCCC) >> 2;
  val = (val & 0x0F0F0F0F0F0F0F0F) << 4 | (val & 0xF0F0F0F0F0F0F0F0) >> 4;
  val = (val & 0x00FF00FF00FF00FF) << 8 | (val & 0xFF00FF00FF00FF00) >> 8;
  val = (val & 0x0000FFFF0000FFFF) << 16 | (val & 0xFFFF0000FFFF0000) >> 16;
  val = (val & 0x00000000FFFFFFFF) << 32 | (val & 0xFFFFFFFF00000000) >> 32;
  return val;
}

/* 下位 digits ビットについてビットの並びを逆転させる (上位ビットは無視) */
inline uint64_t bitReverse(uint64_t val, int digits) {
  assert(digits > 0 || digits <= 64);
  return bitReverse(val) >> (64 - digits);
}

/* 1 << quantity を返す */
inline uint64_t shift(int quantity) { return uint64_t(1) << quantity; }

/* 整数 sets を集合としてみたとき、element 番目の要素を持つかどうか bool(s & (1
 * << element)) */
inline bool has(uint64_t sets, int element) { return sets & shift(element); }

template <class T> void bitReverseArray(std::vector<T> &arr);

/* 配列の要素を添字のビット逆順に並べ替える (配列長は2冪) */
template <class T> void bitReverseArray(std::vector<T> &arr) {
  const int rank = msbPosition(arr.size());
  const int length = 1 << rank;
  assert(arr.size() == length);

  for (int i = 0; i < length; ++i) {
    int j = bitReverse(i, rank);
    if (j > i) {
      std::swap(arr[i], arr[j]);
    }
  }
}

// 畳み込み演算系

template <class T> void transformSubsetZeta(std::vector<T> &arr);
template <class T> void transformInverseSubsetZeta(std::vector<T> &arr);
template <class T> void transformSupersetZeta(std::vector<T> &arr);
template <class T> void transformInverseSupersetZeta(std::vector<T> &arr);
template <class T> void transformHadamard(std::vector<T> &vec);
template <class T>
std::vector<T> convolveOnXor(const std::vector<T> &a, const std::vector<T> &b);

/* 逆変換とまとめられないか？ */
/* 高速ゼータ変換 O(n log n), n は2冪 */
/* b[s] = sum[t is a subset of s] a[t] */
template <class T> void transformSubsetZeta(std::vector<T> &arr) {
  const uint64_t length = arr.size();
  const int level = msbPosition(length);
  assert(length == shift(level));

  for (int k = 0; k < level; ++k) {
    for (uint64_t s = 0; s < length; ++s) {
      if (has(s, k))
        continue;
      arr[s] += arr[s | shift(k)];
    }
  }
}

/* 高速ゼータ変換の逆変換 O(n log n), n は2冪 */
template <class T> void transformInverseSubsetZeta(std::vector<T> &arr) {
  const uint64_t length = arr.size();
  const int level = msbPosition(length);
  assert(length == (uint64_t(1) << level));

  for (int k = 0; k < level; ++k) {
    for (uint64_t s = 0; s < length; ++s) {
      if (has(s, k))
        continue;
      arr[s | shift(k)] -= arr[s];
    }
  }
}

/* 高速(上位集合)ゼータ変換 O(n log n), n は2冪 */
/* b[s] = sum[t is a superset of s] a[t] */
template <class T> void transformSupersetZeta(std::vector<T> &arr) {
  const uint64_t length = arr.size();
  const int level = msbPosition(length);
  assert(length == (uint64_t(1) << level));

  for (int k = 0; k < level; ++k) {
    for (uint64_t s = 0; s < length; ++s) {
      if (has(s, k))
        continue;
      arr[s | shift(k)] += arr[s];
    }
  }
}

/* 高速(上位集合)ゼータ変換の逆変換 O(n log n), n は2冪 */
template <class T> void transformInverseSupersetZeta(std::vector<T> &arr) {
  const uint64_t length = arr.size();
  const int level = msbPosition(length);
  assert(length == (uint64_t(1) << level));

  for (int k = 0; k < level; ++k) {
    for (uint64_t s = 0; s < length; ++s) {
      if (has(s, k))
        continue;
      arr[s] -= arr[s | shift(k)];
    }
  }
}

/* 高速アダマール変換 O(n log n), n は2冪 (オーバーフロー注意) */
/* オーバーフロー注意 */
template <class T> void transformHadamard(std::vector<T> &vec) {
  const int rank = msbPosition(vec.size());
  const int length = 1 << rank;
  assert(vec.size() == length);

  /* バタフライ演算 */
  auto butterfly = [](T &a, T &b) -> void {
    T tmp(a);
    a += b;
    b = tmp - b;
  };

  /* FFT 同様に各ビットについてバタフライ演算を回す */
  for (int cnt = 0; cnt < rank; ++cnt) {
    const int blk_max = length >> (cnt + 1);
    const int blk_size = 1 << (cnt + 1);

    for (int blk = 0; blk < blk_max; ++blk) {
      const int idx_max = 1 << cnt;

      for (int idx = 0; idx < idx_max; ++idx) {
        const int left = (blk * blk_size + idx);
        const int right = left + blk_size / 2;

        butterfly(vec[left], vec[right]);
      }
    }
  }
}

/* xor 畳み込み O(n log n), n は2冪 (オーバーフロー注意) */
/* ret[n] = sum [i xor j == n] (a[i] * b[j]) */
template <class T>
std::vector<T> convolveOnXor(const std::vector<T> &a, const std::vector<T> &b) {
  const int rank = msbPosition(a.size());
  const int size = 1 << rank;
  assert(a.size() == b.size());
  assert(a.size() == size);

  std::vector<T> a_copied = a;
  std::vector<T> b_copied = b;
  transformHadamard(a_copied);
  transformHadamard(b_copied);

  std::vector<T> ret(size);
  for (int i = 0; i < size; ++i) {
    ret[i] = a_copied[i] * b_copied[i];
  }
  transformHadamard(ret);

  /* 関数 transformHadamard はユニタリ変換ではないので係数を調整 */
  /* IntMod など -> 逆元を前計算することも可能 */
  for (int i = 0; i < size; ++i) {
    ret[i] /= size;
  }
  return ret;
}

#endif

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
#define REP(i, a, n) for (LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP                                                                \\
  {                                                                            \\
    cin.tie(NULL);                                                             \\
    ios::sync_with_stdio(false);                                               \\
  }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
template <class T> T Next() {
  T buf;
  cin >> buf;
  return buf;
}
istream &Ignore(istream &ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}

#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BIT.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "FFT.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Geometric2D.h"
#include "Geometric2DFloat.h"
#include "Geometric3D.h"
#include "Geometric3DFloat.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "IntMod.h"
#include "Interpolation.h"
#include "KDTree.h"
#include "LIS.h"
#include "LazySegmentTree.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

int N;
int X[15];
int Y[15];
int Z[15];
LL ans[16];
int C[15];
int CM[15];
int GX[15][15];
int GY[15][15];
void pre() {
  REP(i, 0, N) {
    REP(j, 0, N) {
      GX[i][j] = abs(X[j] - X[i]);
      GY[i][j] = abs(Y[j] - Y[i]);
    }
  }
  REP(i, 0, N) CM[i] = min(abs(X[i]), abs(Y[i]));
  REP(s, 0, 1 << N) {
    for (int t = s;; t = (t - 1) & s) {
      REP(i, 0, N) C[i] = CM[i];
      int cnt = 0;
      REP(i, 0, N) {
        if (has(s, i)) {
          ++cnt;
          if (has(t, i)) {
            REP(j, 0, N) { C[j] = min(C[j], GX[i][j]); }
          } else {
            REP(j, 0, N) { C[j] = min(C[j], GY[i][j]); }
          }
        }
      }

      LL sum = 0;
      REP(i, 0, N) sum += (LL)Z[i] * C[i];
      ans[cnt] = min(ans[cnt], sum);
      if (t == 0)
        break;
    }
  }
}

int main() {
  cin >> N;
  REP(i, 0, N) { cin >> X[i] >> Y[i] >> Z[i]; }
  Fill(ans, INFLL);
  pre();
  REP(i, 0, N + 1) { cout << ans[i] << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
#define REP(i, a, n) for (LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP                                                                \\
  {                                                                            \\
    cin.tie(NULL);                                                             \\
    ios::sync_with_stdio(false);                                               \\
  }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
template <class T> T Next() {
  T buf;
  cin >> buf;
  return buf;
}
istream &Ignore(istream &ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}

#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BIT.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "FFT.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Geometric2D.h"
#include "Geometric2DFloat.h"
#include "Geometric3D.h"
#include "Geometric3DFloat.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "IntMod.h"
#include "Interpolation.h"
#include "KDTree.h"
#include "LIS.h"
#include "LazySegmentTree.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

int N;
int X[15];
int Y[15];
int Z[15];
LL ans[16];

void rec(int n, vector<int> &v) {
  if (n == 0) {
    vector<LL> xs = {0};
    vector<LL> ys = {0};
    int cnt = 0;
    REP(i, 0, N) {
      if (v[i] == 1) {
        xs.push_back(X[i]);
        ++cnt;
      }
      if (v[i] == 2) {
        ys.push_back(Y[i]);
        ++cnt;
      }
    }

    LL sum = 0;
    REP(i, 0, N) {
      LL mn = INFLL;
      for (int x : xs) {
        mn = min<LL>(mn, abs(X[i] - x));
      }
      for (int y : ys) {
        mn = min<LL>(mn, abs(Y[i] - y));
      }
      sum += mn * Z[i];
    }

    ans[cnt] = min(ans[cnt], sum);
    return;
  }

  REP(k, 0, 3) {
    v.push_back(k);
    rec(n - 1, v);
    v.pop_back();
  }
}

int main() {
  cin >> N;
  REP(i, 0, N) { cin >> X[i] >> Y[i] >> Z[i]; }
  Fill(ans, INFLL);

  vector<int> v;
  rec(N, v);

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