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

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 07/25/20 20:57:10
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int INF = 1e9;

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

  int n;
  cin >> n;
  struct City {
    int x, y, p;
  };
  vector<City> C(n);
  for (int i = 0; i < n; i++) {
    int x, y, p;
    cin >> x >> y >> p;
    C[i] = {x, y, p};
  }
  //  n = 15;
  ll distx[1 << n][n], disty[1 << n][n];
  for (int mask = 0; mask < (1 << n); mask++) {
    for (int i = 0; i < n; i++) {
      int mindistx = abs(C[i].x), mindisty = abs(C[i].y);
      for (int j = 0; j < n; j++) {
        if ((mask & (1 << j)) == 0)
          continue;
        mindistx = min(mindistx, abs(C[i].x - C[j].x));
        mindisty = min(mindisty, abs(C[i].y - C[j].y));
      }
      distx[mask][i] = (ll)mindistx * C[i].p;
      disty[mask][i] = (ll)mindisty * C[i].p;
    }
  }
  vector<ll> ans(n + 1, 1e18);
  for (int mask = 0; mask < (1 << n); mask++) {
    int popcnt = __builtin_popcount(mask);
    for (int xmask = mask; xmask >= 0; xmask = (xmask - 1) & mask) {
      int ymask = mask ^ xmask;
      ll sum = 0;
      for (int i = 0; i < n; i++) {
        sum += min(distx[xmask][i], disty[ymask][i]);
      }
      ans[popcnt] = min(ans[popcnt], sum);
      if (xmask == 0)
        break;
    }
  }
  for (auto a : ans)
    cout << a << endl;

  return 0;
}


Error: TLE

Fixed submission:

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 07/25/20 20:57:10
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int INF = 1e9;

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

  int n;
  cin >> n;
  struct City {
    int x, y, p;
  };
  vector<City> C(n);
  for (int i = 0; i < n; i++) {
    int x, y, p;
    cin >> x >> y >> p;
    C[i] = {x, y, p};
  }
  //  n = 15;
  ll distx[1 << n][n], disty[1 << n][n];
  for (int mask = 0; mask < (1 << n); mask++) {
    for (int i = 0; i < n; i++) {
      int mindistx = abs(C[i].x), mindisty = abs(C[i].y);
      for (int j = 0; j < n; j++) {
        if ((mask & (1 << j)) == 0)
          continue;
        mindistx = min(mindistx, abs(C[i].x - C[j].x));
        mindisty = min(mindisty, abs(C[i].y - C[j].y));
      }
      distx[mask][i] = (ll)mindistx * C[i].p;
      disty[mask][i] = (ll)mindisty * C[i].p;
    }
  }
  vector<ll> ans(n + 1, 1e18);
  int p3n = 1;
  for (int i = 0; i < n; i++)
    p3n *= 3;
  for (int mask = 0; mask < p3n; mask++) {
    int xmask = 0;
    int ymask = 0;
    int cnt = 0;
    ll d = 1;
    for (int i = 0; i < n; i++, d *= 3) {
      int m = mask / d % 3;
      if (m == 0)
        continue;
      cnt++;
      if (m == 1)
        xmask |= 1 << i;
      else if (m == 2)
        ymask |= 1 << i;
    }
    ll sum = 0;
    for (int i = 0; i < n; i++) {
      sum += min(distx[xmask][i], disty[ymask][i]);
    }
    ans[cnt] = min(ans[cnt], sum);
  }
  for (auto a : ans)
    cout << a << endl;

  return 0;
}