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

#include <bits/stdc++.h>

#define double long double
#define ff first
#define ss second
#define endl '\\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 1000000007
#define curtime chrono::steady_clock::now().time_since_epoch().count
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
const double PI = acos(-1);

using namespace std;

int x[20], y[20], p[20];

long long best[20];

int32_t main() {
  DESYNC;
  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i], best[i] = INF;

  // brute people
  int d[n];
  for (int msk = 0; msk < (1 << n); msk++) {
    int cnt = __builtin_popcount(msk);
    for (int dir = msk;; dir = (dir - 1) & msk) {
      for (int i = 0; i < n; i++)
        d[i] = min(abs(x[i]), abs(y[i]));
      for (int i = 0; i < n; i++) {
        if (msk >> i & 1) {
          if (dir >> i & 1)
            for (int j = 0; j < n; j++) {
              d[j] = min(d[j], abs(x[i] - x[j]));
            }
          else
            for (int j = 0; j < n; j++) {
              d[j] = min(d[j], abs(y[i] - y[j]));
            }
        }
      }

      long long ans = 0;
      for (int i = 0; i < n; i++) {
        ans += (1LL * d[i] * p[i]);
      }
      best[cnt] = min(best[cnt], ans);
      if (dir == 0)
        break;
    }
  }

  for (int i = 0; i <= n; i++)
    cout << best[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define double long double
#define ff first
#define ss second
#define endl '\\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 1000000007
#define curtime chrono::steady_clock::now().time_since_epoch().count
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
const double PI = acos(-1);

using namespace std;

inline int mod(int n, int m = M) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}

int exp(int n, int k) {
  if (k == 0)
    return 1;
  if (k == 1)
    return n;
  int ax = exp(n, k / 2);
  ax = mod(ax * ax);
  if (k % 2)
    ax = mod(ax * n);
  return ax;
}

int gcd(int a, int b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}

int x[20], y[20], p[20];

long long best[20];

// #define MULTIPLE_TEST_CASE
void solution() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];

  for (int i = 0; i <= n + 1; i++)
    best[i] = INF;

  // brute people
  int d[n];
  for (int msk = 0; msk < (1 << n); msk++) {
    for (int dir = msk;; dir = (dir - 1) & msk) {
      for (int i = 0; i < n; i++)
        d[i] = min(abs(x[i]), abs(y[i]));
      for (int i = 0; i < n; i++) {
        if (msk >> i & 1) {
          if (dir >> i & 1)
            for (int j = 0; j < n; j++) {
              d[j] = min(d[j], abs(x[i] - x[j]));
            }
          else
            for (int j = 0; j < n; j++) {
              d[j] = min(d[j], abs(y[i] - y[j]));
            }
        }
      }

      long long ans = 0;
      for (int i = 0; i < n; i++) {
        ans += (1LL * d[i] * p[i]);
      }
      best[__builtin_popcount(msk)] = min(best[__builtin_popcount(msk)], ans);
      if (dir == 0)
        break;
    }
  }

  for (int i = n; i >= 0; i--) {
    if (best[i] == 0)
      break;
    best[i] = 0;
  }
  for (int i = 0; i <= n; i++)
    cout << best[i] << endl;
}

int32_t main() {
  DESYNC;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int t = 1;
#ifdef MULTIPLE_TEST_CASE
  cin >> t;
#endif
  while (t--)
    solution();
}