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

#include <bits/stdc++.h>
#include <chrono>

using namespace std;
using namespace std::chrono;

// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
// #pragma optimization_level 3
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define f0r(a, b) for (long long a = 0; a < (b); ++a)
#define f1r(a, b, c) for (long long a = (b); a < (c); ++a)
#define f0rd(a, b) for (long long a = (b); a >= 0; --a)
#define f1rd(a, b, c) for (long long a = (b); a >= (c); --a)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define pb push_back
#define send                                                                   \\
  { ios_base::sync_with_stdio(false); }
#define help                                                                   \\
  {                                                                            \\
    cin.tie(NULL);                                                             \\
    cout.tie(NULL);                                                            \\
  }
#define fix(prec)                                                              \\
  { cout << setprecision(prec) << fixed; }
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define getunique(v)                                                           \\
  {                                                                            \\
    sort(all(v));                                                              \\
    v.erase(unique(all(v)), v.end());                                          \\
  }
#define readgraph(list, edges)                                                 \\
  for (int i = 0; i < edges; i++) {                                            \\
    int a, b;                                                                  \\
    cin >> a >> b;                                                             \\
    a--;                                                                       \\
    b--;                                                                       \\
    list[a].pb(b);                                                             \\
    list[b].pb(a);                                                             \\
  }
#define ai(a, n)                                                               \\
  for (int ele = 0; ele < n; ele++)                                            \\
    cin >> a[ele];
#define ain(a, lb, rb)                                                         \\
  for (int ele = lb; ele <= rb; ele++)                                         \\
    cin >> a[ele];
#define ao(a, n)                                                               \\
  {                                                                            \\
    for (int ele = 0; ele < (n); ele++) {                                      \\
      if (ele)                                                                 \\
        cout << " ";                                                           \\
      cout << a[ele];                                                          \\
    }                                                                          \\
    cout << '\\n';                                                              \\
  }
#define aout(a, lb, rb)                                                        \\
  {                                                                            \\
    for (int ele = (lb); ele <= (rb); ele++) {                                 \\
      if (ele > (lb))                                                          \\
        cout << " ";                                                           \\
      cout << a[ele];                                                          \\
    }                                                                          \\
    cout << '\\n';                                                              \\
  }
#define vsz(x) ((long long)x.size())
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
  return cout << "(" << p.f << ", " << p.s << ")";
}
template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) {
  cout << "[";
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ", ";
    cout << v[i];
  }
  return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
  cin >> p.first;
  return cin >> p.second;
}

mt19937 rng(steady_clock::now().time_since_epoch().count());
// mt19937 rng(61378913);
/* usage - just do rng() */

void usaco(string filename) {
  // #pragma message("be careful, freopen may be wrong")
  freopen((filename + ".in").c_str(), "r", stdin);
  freopen((filename + ".out").c_str(), "w", stdout);
}

const lld pi = 3.14159265358979323846;
// const ll mod = 1000000007;
// const ll mod = 998244353;
// ll mod;

ll n, m, k, q, l, r, x, y, z;
ll a[1000005];
ll b[1000005];
ll c[1000005];
string s, t;
ll ans = 0;

void solve(int tc) {
  cin >> n;

  pii pts[n];
  int val[n];
  f0r(i, n) cin >> pts[i] >> val[i];

  int n3 = 1;
  f0r(i, n) n3 *= 3;

  ll ans[n + 1];
  f0r(i, n + 1) ans[i] = 1e18;
  for (int mask = 0; mask < n3; mask++) { // 0 - none, 1 - ver, 2 - hor
    ll a[n];

    ll c = 0;

    int v = mask;
    f0rd(i, n - 1) {
      a[i] = v % 3;
      if (a[i] > 0)
        ++c;
      v /= 3;
    }

    int worst[n];
    f0r(i, n) { worst[i] = min(abs(pts[i].f), abs(pts[i].s)); }

    int unq[n], pt = 0;
    f0r(i, n) {
      if (a[i] == 0 && worst[i] > 0) {
        unq[pt++] = i;
      } else {
        worst[i] = 0;
      }
    }

    f0r(i, n) {
      if (a[i] == 1) {
        for (int j = 0; j < pt; j++) {
          worst[unq[j]] = min(worst[unq[j]], abs(pts[unq[j]].s - pts[i].s));
        }
      } else if (a[i] == 2) {
        for (int j = 0; j < pt; j++) {
          worst[unq[j]] = min(worst[unq[j]], abs(pts[unq[j]].f - pts[i].f));
        }
      }
    }

    ll r = 0;
    f0r(i, n) r += worst[i] * 1LL * val[i];
    ans[c] = min(ans[c], r);
  }
  f0r(i, n + 1) cout << ans[i] << '\\n';
}

int main() {
  send help

      // usaco("cowland");

      int tc = 1;
  // cin >> tc;
  for (int t = 0; t < tc; t++)
    solve(t);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>

using namespace std;
using namespace std::chrono;

// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
// #pragma optimization_level 3
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define f0r(a, b) for (long long a = 0; a < (b); ++a)
#define f1r(a, b, c) for (long long a = (b); a < (c); ++a)
#define f0rd(a, b) for (long long a = (b); a >= 0; --a)
#define f1rd(a, b, c) for (long long a = (b); a >= (c); --a)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define pb push_back
#define send                                                                   \\
  { ios_base::sync_with_stdio(false); }
#define help                                                                   \\
  {                                                                            \\
    cin.tie(NULL);                                                             \\
    cout.tie(NULL);                                                            \\
  }
#define fix(prec)                                                              \\
  { cout << setprecision(prec) << fixed; }
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define getunique(v)                                                           \\
  {                                                                            \\
    sort(all(v));                                                              \\
    v.erase(unique(all(v)), v.end());                                          \\
  }
#define readgraph(list, edges)                                                 \\
  for (int i = 0; i < edges; i++) {                                            \\
    int a, b;                                                                  \\
    cin >> a >> b;                                                             \\
    a--;                                                                       \\
    b--;                                                                       \\
    list[a].pb(b);                                                             \\
    list[b].pb(a);                                                             \\
  }
#define ai(a, n)                                                               \\
  for (int ele = 0; ele < n; ele++)                                            \\
    cin >> a[ele];
#define ain(a, lb, rb)                                                         \\
  for (int ele = lb; ele <= rb; ele++)                                         \\
    cin >> a[ele];
#define ao(a, n)                                                               \\
  {                                                                            \\
    for (int ele = 0; ele < (n); ele++) {                                      \\
      if (ele)                                                                 \\
        cout << " ";                                                           \\
      cout << a[ele];                                                          \\
    }                                                                          \\
    cout << '\\n';                                                              \\
  }
#define aout(a, lb, rb)                                                        \\
  {                                                                            \\
    for (int ele = (lb); ele <= (rb); ele++) {                                 \\
      if (ele > (lb))                                                          \\
        cout << " ";                                                           \\
      cout << a[ele];                                                          \\
    }                                                                          \\
    cout << '\\n';                                                              \\
  }
#define vsz(x) ((long long)x.size())
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
  return cout << "(" << p.f << ", " << p.s << ")";
}
template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) {
  cout << "[";
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ", ";
    cout << v[i];
  }
  return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
  cin >> p.first;
  return cin >> p.second;
}

mt19937 rng(steady_clock::now().time_since_epoch().count());
// mt19937 rng(61378913);
/* usage - just do rng() */

void usaco(string filename) {
  // #pragma message("be careful, freopen may be wrong")
  freopen((filename + ".in").c_str(), "r", stdin);
  freopen((filename + ".out").c_str(), "w", stdout);
}

const lld pi = 3.14159265358979323846;
// const ll mod = 1000000007;
// const ll mod = 998244353;
// ll mod;

ll n, m, k, q, l, r, x, y, z;
ll a[1000005];
ll b[1000005];
ll c[1000005];
string s, t;
ll ans = 0;

void solve(int tc) {
  cin >> n;

  pll pts[n];
  ll val[n];
  f0r(i, n) cin >> pts[i] >> val[i];

  ll n3 = 1;
  f0r(i, n) n3 *= 3;

  ll ans[n + 1];
  f0r(i, n + 1) ans[i] = 1e18;
  f0r(mask, n3) { // 0 - none, 1 - ver, 2 - hor
    ll a[n];

    ll c = 0;

    ll v = mask;
    f0rd(i, n - 1) {
      a[i] = v % 3;
      if (a[i] > 0)
        ++c;
      v /= 3;
    }

    ll worst[n];
    f0r(i, n) { worst[i] = min(abs(pts[i].f), abs(pts[i].s)); }

    vl unq;
    f0r(i, n) {
      if (a[i] == 0 && worst[i] > 0) {
        unq.pb(i);
      } else {
        worst[i] = 0;
      }
    }

    f0r(i, n) {
      if (a[i] == 1) {
        for (ll x : unq) {
          worst[x] = min(worst[x], abs(pts[x].s - pts[i].s));
        }
      } else if (a[i] == 2) {
        for (ll x : unq) {
          worst[x] = min(worst[x], abs(pts[x].f - pts[i].f));
        }
      }
    }

    ll r = 0;
    f0r(i, n) r += worst[i] * val[i];
    ans[c] = min(ans[c], r);
  }
  f0r(i, n + 1) cout << ans[i] << '\\n';
}

int main() {
  send help

      // usaco("cowland");

      int tc = 1;
  // cin >> tc;
  for (int t = 0; t < tc; t++)
    solve(t);
}