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

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

using namespace std;

#define PB push_back
#define f first
#define s second
#define what_is(x) cerr << #x << " is " << x << endl;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

ll ans[16];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i <= n; i++) {
    ans[i] = 1LL * 1000000000000;
  }

  ll orig_mn[n], mn[n];
  vector<pair<pll, ll>> v;
  for (int i = 0; i < n; i++) {
    ll x, y, p;
    cin >> x >> y >> p;
    orig_mn[i] = min(abs(x), abs(y));
    v.PB({{x, y}, p});
  }
  for (int i = 0; i < (1 << n); i++) {
    ll s;
    if (i == 0) {
      s = 0;
    } else
      s = __builtin_popcount(i);
    vector<int> cities;
    for (int j = 0; j < n; j++) {
      if (i & (1 << j)) {
        cities.PB(j);
      }
    }
    vector<ll> pos[2];
    for (int j = 0; j < (1 << s); j++) {
      pos[0].clear();
      pos[1].clear();
      for (int k = 0; k < s; k++) {
        int xy = (1 << k) & j;
        if (xy) {
          pos[1].PB(v[cities[k]].f.s);
        } else {
          pos[0].PB(v[cities[k]].f.f);
        }
      }
      ll tot = 0;
      for (int i = 0; i < n; i++) {
        mn[i] = orig_mn[i];
        for (int j = 0; j < 2; j++) {
          for (ll val : pos[j]) {
            if (j) {
              mn[i] = min(mn[i], abs(v[i].f.s - val));
            } else {
              mn[i] = min(mn[i], abs(v[i].f.f - val));
            }
          }
        }
        tot += (v[i].s * mn[i]);
      }

      ans[s] = min(ans[s], tot);
    }
  }

  for (int i = 0; i <= n; i++) {
    cout << ans[i] << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

#define PB push_back
#define f first
#define s second
#define what_is(x) cerr << #x << " is " << x << endl;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

ll ans[16];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i <= n; i++) {
    ans[i] = 1LL * 1000000000000;
  }

  vector<pair<pll, ll>> v;
  for (int i = 0; i < n; i++) {
    ll x, y, p;
    cin >> x >> y >> p;
    v.PB({{x, y}, p});
  }
  for (int i = 0; i < (1 << n); i++) {
    ll s;
    if (i == 0) {
      s = 0;
    } else
      s = __builtin_popcount(i);
    vector<int> cities;
    for (int j = 0; j < n; j++) {
      if (i & (1 << j)) {
        cities.PB(j);
      }
    }
    int siz = cities.size();
    for (int j = 0; j < (1 << siz); j++) {
      vector<ll> pos[2];

      for (int k = 0; k < siz; k++) {
        int xy = (1 << k) & j;
        if (xy) {
          pos[1].PB(v[cities[k]].f.s);
        } else {
          pos[0].PB(v[cities[k]].f.f);
        }
      }
      ll mn[n];
      ll tot = 0;
      for (int i = 0; i < n; i++) {
        mn[i] = v[i].s * min(abs(v[i].f.f), abs(v[i].f.s));
        for (int j = 0; j < 2; j++) {
          for (ll val : pos[j]) {
            if (j) {
              mn[i] = min(mn[i], v[i].s * abs(v[i].f.s - val));
            } else {
              mn[i] = min(mn[i], v[i].s * abs(v[i].f.f - val));
            }
          }
        }
        //            if(s == 1)
        //            {
        //                cout << mn[i] << "\\n";
        //            }
        tot += mn[i];
      }
      //        if(s == 1)
      //        {
      //            for(int j=0; j<2; j++)
      //            {
      //                for(ll val : pos[j])
      //                {
      //                    cout << j << " " << val << "\\n";
      //                }
      //            }
      //        }
      ans[s] = min(ans[s], tot);
    }
  }

  for (int i = 0; i <= n; i++) {
    cout << ans[i] << "\\n";
  }
  return 0;
}