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

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

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define all(x) (x).begin(), (x).end()
#define S(x) (int)(x).size()
#define L(x) (int)(x).length()

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int mod = 1e9 + 7;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;

int x[20], y[20], p[20];
ll ans[20];
vi dd;
int main() {
  IOS memset(ans, infi, sizeof ans);
  int n, i;
  cin >> n;
  ans[n] = ans[0] = 0;
  for (i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
    ans[0] += 1LL * p[i] * min(abs(x[i]), abs(y[i]));
    dd.pb(min(abs(x[i]), abs(y[i])));
  }

  for (i = 1; i < (1 << n); i++) {
    int sb = __builtin_popcount(i);
    if (sb == n)
      continue;
    for (int s = i;; s = (s - 1) & i) {
      vi d = dd;
      for (int k = 0; k < n; k++) {
        if (i & (1 << k)) {
          if (s & (1 << k))
            for (int j = 0; j < n; j++)
              d[j] = min(d[j], abs(y[j] - y[k]));
          else
            for (int j = 0; j < n; j++)
              d[j] = min(d[j], abs(x[j] - x[k]));
        }
      }
      ll temp = 0;
      for (int j = 0; j < n; j++)
        temp += p[j] * 1LL * d[j];
      ans[sb] = min(ans[sb], temp);
      if (s == 0)
        break;
    }
  }
  for (i = 0; i <= n; i++)
    cout << ans[i] << '\\n';
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

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

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define all(x) (x).begin(), (x).end()
#define S(x) (int)(x).size()
#define L(x) (int)(x).length()

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int mod = 1e9 + 7;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;

int x[20], y[20], p[20];
ll ans[20];
vi dd;
void solve() {
  memset(ans, infi, sizeof ans);
  int n, i;
  cin >> n;
  ans[n] = ans[0] = 0;
  for (i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
    ans[0] += 1LL * p[i] * min(abs(x[i]), abs(y[i]));
    dd.pb(min(abs(x[i]), abs(y[i])));
  }

  for (i = 1; i < (1 << n); i++) {
    int sb = __builtin_popcount(i);
    if (sb == n)
      continue;
    for (int s = i;; s = (s - 1) & i) {
      vi d = dd;
      for (int k = 0; k < n; k++) {
        if (i & (1 << k)) {
          if (s & (1 << k))
            for (int j = 0; j < n; j++)
              d[j] = min(d[j], abs(y[j] - y[k]));
          else
            for (int j = 0; j < n; j++)
              d[j] = min(d[j], abs(x[j] - x[k]));
        }
      }
      ll temp = 0;
      for (int j = 0; j < n; j++)
        temp += p[j] * 1LL * d[j];
      ans[sb] = min(ans[sb], temp);
      if (s == 0)
        break;
    }
  }
  for (i = 0; i <= n; i++)
    cout << ans[i] << '\\n';
}

int main() {
  IOS int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}