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

#include <bits/stdc++.h>
/* S.size() s.substr(l,r=s.size()-l) -> l文字目からr文字分
__gcd()　lower_bound(a+l,a+r,x)-a でindex
// 配列  -> min/max({})
// vector-> *min_element(a+l,a+r)  *忘れず lとrで[l,r)
//reverse(a+l,a+r)   配列aの[l,r)を逆順に strはreverse(all(s))
//sort(a+l,a+r,greater <int>()) 配列aの[l,r)昇順sort,大きい順はgreater<type>()
//vectorならall(v)
clock()/CLOCKS_PER_SEC で秒数
vector v.push_back(x),v.pop_back()
q.push(x),front()で先頭を返す,pop()で削除, size(),empty()
<deque>(push/pop)_(front/back)/fornt/back/insert/
priority_queue 宣言は priority_queue< Type, vector<Type>, greater<Type>> Q1;
// pq.push(x),top()で参照 pop()で削除   greaterで最小 lessで最大がtopに*/
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repl(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define rrep(i, n) for (int(i) = (n)-1; (i) != -1; (i)--)
#define vep(i, v) for (auto(i) = v.begin(); (i) < (v.end()); (i)++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
inline long long ceildiv(long long a, long long b) { return (a + b - 1) / b; }
typedef pair<long long, long long> P;
typedef pair<int, int> PI;
typedef std::vector<int> vi;
typedef std::vector<long long> vl;
typedef std::vector<bool> vb;
typedef std::vector<std::vector<bool>> vvb;
using Graph = std::vector<std::vector<int>>;
const long long inf = 10e17;
const long long mod = 1000000007;
// int |x|<=2*10**9     long |x|<= 9*10**18
// printf(".(小数点以下の桁数)lf(double時、float ならf)")
#define ll long long
#define print(i) std::cout << (i) << '\\n'
int n;
int main() {
  cin >> n;
  vl x(n), y(n), p(n);
  vi xs, ys, unused;
  ll pow3 = 1ll;
  rep(i, n) {
    cin >> x[i] >> y[i] >> p[i];
    pow3 *= 3;
  }

  vl res(n + 1, inf);
  for (ll t = 0ll; t < pow3; t++) {
    xs.clear();
    ys.clear();
    unused.clear();
    ll i = t;
    rep(j, n) {
      switch (i % 3) {
      case 0:
        unused.emplace_back(j);
        break;
      case 1:
        xs.emplace_back(j);
        break;
      case 2:
        ys.emplace_back(j);
      }
      i /= 3;
    }
    ll dist = 0ll;
    for (int uin : unused) {
      ll ret = min(abs(x[uin]), abs(y[uin]));
      for (int xin : xs) {
        ret = min(ret, abs(x[uin] - x[xin]));
      }
      for (int yin : ys) {
        ret = min(ret, abs(y[uin] - y[yin]));
      }
      dist += ret * p[uin];
    }
    res[xs.size() + ys.size()] = min(res[xs.size() + ys.size()], dist);
  }
  for (auto h : res)
    print(h);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>

/* template start */

#define rep(i, a, b) for (long long i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()

#ifdef LOCAL
#define debug(...)                                                             \\
  std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << std::endl; }

template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {
  std::cerr << " " << h;
  if (sizeof...(t) > 0)
    std::cout << " :";
  debug_out(t...);
}

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

template <typename Num> constexpr Num mypow(Num a, long long b) {
  if (b == 0)
    return 1;
  if (a == 0)
    return 0;
  Num x = 1;
  while (b > 0) {
    if (b & 1)
      x *= a;
    a *= a;
    b >>= 1;
  }
  return x;
}

/* template end */

std::uint_fast64_t next_combination(std::uint_fast64_t num) {
  std::uint_fast64_t x = num & (-num);
  std::uint_fast64_t y = x + num;
  return y | (((num & ~y) / x) >> 1);
}

using ll = long long;

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  ll n;
  std::cin >> n;

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

  constexpr ll INF = 1e18;

  constexpr ll MAX = 20200;

  std::vector<ll> ans(n + 1, INF);

  using u64 = std::uint_fast64_t;

  ans[0] = 0;

  rep(i, 0, n) { ans[0] += p[i] * std::min(std::abs(x[i]), std::abs(y[i])); }

  ll BITMAX = mypow(3ull, n);

  debug(BITMAX);

  rep(bit, 0, BITMAX) {
    ll tmp = bit;
    ll cnt = 0;

    std::vector<ll> x_axis = {-MAX, 0, MAX}, y_axis = {-MAX, 0, MAX};
    rep(i, 0, n) {
      if (tmp % 3 == 1) {
        cnt++;
        x_axis.emplace_back(x[i]);
      } else if (tmp % 3 == 2) {
        cnt++;
        y_axis.emplace_back(y[i]);
      }
      tmp /= 3;
    }

    std::sort(all(x_axis));
    std::sort(all(y_axis));

    ll val = 0;

    rep(i, 0, n) {
      ll min = INF;
      ll itr = std::lower_bound(all(x_axis), x[i]) - x_axis.begin();
      chmin(min, x_axis[itr] - x[i]);
      chmin(min, x[i] - x_axis[itr - 1]);
      itr = std::lower_bound(all(y_axis), y[i]) - y_axis.begin();
      chmin(min, y_axis[itr] - y[i]);
      chmin(min, y[i] - y_axis[itr - 1]);

      val += p[i] * min;
    }

    chmin(ans[cnt], val);
  }

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

  return 0;
}