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

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

typedef long long ll;

int n, x[15], y[15], p[15], D[15], d[15];
ll ans[16];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  memset(ans, 0x3f, sizeof(ans));
  ans[0] = 0;
  for (int i = 0; i < n; i++) {
    D[i] = min(abs(x[i]), abs(y[i]));
    ans[0] += (ll)p[i] * D[i];
  }
  for (int s = 0; s < 1 << n; s++) {
    for (int t = s; t; t = (t - 1) & s) {
      memcpy(d, D, sizeof(d));
      for (int i = 0; i < n; i++) {
        if (s >> i & 1) {
          if (t >> 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]));
            }
          }
        }
      }
      ll cur = 0;
      for (int i = 0; i < n; i++) {
        cur += (ll)d[i] * p[i];
      }
      ans[__builtin_popcount(s)] = min(ans[__builtin_popcount(s)], cur);
    }
    {
      int t = 0;
      memcpy(d, D, sizeof(d));
      for (int i = 0; i < n; i++) {
        if (s >> i & 1) {
          if (t >> 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]));
            }
          }
        }
      }
      ll cur = 0;
      for (int i = 0; i < n; i++) {
        cur += (ll)d[i] * p[i];
      }
      ans[__builtin_popcount(s)] = min(ans[__builtin_popcount(s)], cur);
    }
  }
  for (int i = 0; i <= n; i++)
    cout << ans[i] << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

int n, x[15], y[15], p[15], D[15], d[15];
ll ans[16];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  memset(ans, 0x3f, sizeof(ans));
  ans[0] = 0;
  for (int i = 0; i < n; i++) {
    D[i] = min(abs(x[i]), abs(y[i]));
    ans[0] += (ll)p[i] * D[i];
  }
  for (int s = 0; s < 1 << n; s++) {
    for (int t = s; t; t = (t - 1) & s) {
      memcpy(d, D, sizeof(d));
      for (int i = 0; i < n; i++)
        if (s >> i & 1) {
          for (int j = 0; j < n; j++) {
            d[j] =
                min(d[j], (t >> i & 1) ? abs(x[i] - x[j]) : abs(y[i] - y[j]));
          }
        }
      ll cur = 0;
      for (int i = 0; i < n; i++) {
        cur += (ll)d[i] * p[i];
      }
      ans[__builtin_popcount(s)] = min(ans[__builtin_popcount(s)], cur);
    }
    {
      const int t = 0;
      memcpy(d, D, sizeof(d));
      for (int i = 0; i < n; i++)
        if (s >> i & 1) {
          for (int j = 0; j < n; j++) {
            d[j] =
                min(d[j], (t >> i & 1) ? abs(x[i] - x[j]) : abs(y[i] - y[j]));
          }
        }
      ll cur = 0;
      for (int i = 0; i < n; i++) {
        cur += (ll)d[i] * p[i];
      }
      ans[__builtin_popcount(s)] = min(ans[__builtin_popcount(s)], cur);
    }
  }
  for (int i = 0; i <= n; i++)
    cout << ans[i] << '\\n';

  return 0;
}