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

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
// please, read the question correctly (do you need set or multiset)???
const int N = 22; // check the limits, dummy
ll x[N], y[N], p[N];
ll n, m;
int main() {
  int t;
  scanf("%lld", &n);
  vector<pair<int, int>> v1, v2, v3, v4;
  for (int i = 0; i < n; ++i) {
    scanf("%lld%lld%lld", x + i, y + i, p + i);
    // x[i]=y[i]=p[i]=1;
    v1.push_back(mk(x[i], i));
    v2.push_back(mk(y[i], i));
    v3.push_back(mk(x[i], i));
    v4.push_back(mk(y[i], i));
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  sort(v3.begin(), v3.end());
  sort(v4.begin(), v4.end());
  reverse(v3.begin(), v3.end());
  reverse(v4.begin(), v4.end());
  ll ans[22];
  for (int i = 0; i <= n; ++i) {
    ans[i] = 1e18;
  }
  ll mn[22];
  int fl[22];
  for (int i = 0; i < (1 << n); ++i) {
    vector<int> v;
    for (int j = 0; j < n; ++j) {
      if ((1 << j) & i) {
        v.push_back(j);
      }
    }
    int sz = v.size();
    for (int j = 0; j < (1 << sz); ++j) {
      for (int k = 0; k < n; ++k) {
        mn[k] = min(abs(x[k]), abs(y[k]));
        fl[k] = -1;
      }
      for (int k = 0; k < sz; ++k) {
        if ((1 << k) & j) {
          fl[v[k]] = 0;
        } else {
          fl[v[k]] = 1;
        }
      }
      int lst = -1e9;
      for (auto it : v1) {
        if (fl[it.sc] == 0) {
          lst = it.fs;
        }
        mn[it.sc] = min(mn[it.sc], (ll)it.fs - lst);
      }
      lst = 1e9;
      for (auto it : v3) {
        if (fl[it.sc] == 0) {
          lst = it.fs;
        }
        mn[it.sc] = min(mn[it.sc], (ll)lst - it.fs);
      }
      lst = -1e9;
      for (auto it : v2) {
        if (fl[it.sc] == 1) {
          lst = it.fs;
        }
        mn[it.sc] = min(mn[it.sc], (ll)it.fs - lst);
      }
      lst = 1e9;
      for (auto it : v4) {
        if (fl[it.sc] == 1) {
          lst = it.fs;
        }
        mn[it.sc] = min(mn[it.sc], (ll)lst - it.fs);
      }
      ll tmp = 0;
      for (int l = 0; l < n; ++l) {
        tmp += mn[l] * p[l];
      }
      ans[sz] = min(ans[sz], tmp);
    }
  }
  for (int i = 0; i <= n; ++i) {
    cout << ans[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
// please, read the question correctly (do you need set or multiset)???
const int N = 22; // check the limits, dummy
ll x[N], y[N], p[N];
ll n, m;
int main() {
  int t;
  scanf("%lld", &n);
  vector<pair<ll, ll>> v1, v2, v3, v4;
  for (int i = 0; i < n; ++i) {
    scanf("%lld%lld%lld", x + i, y + i, p + i);
    // x[i]=y[i]=p[i]=1;
    v1.push_back(mk(x[i], i));
    v2.push_back(mk(y[i], i));
    v3.push_back(mk(x[i], i));
    v4.push_back(mk(y[i], i));
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  sort(v3.begin(), v3.end());
  sort(v4.begin(), v4.end());
  reverse(v3.begin(), v3.end());
  reverse(v4.begin(), v4.end());
  ll ans[22];
  for (int i = 0; i <= n; ++i) {
    ans[i] = 1e18;
  }
  ll mn[22];
  int fl[22];
  for (int i = 0; i < (1 << n); ++i) {
    vector<int> v;
    for (int j = 0; j < n; ++j) {
      if ((1 << j) & i) {
        v.push_back(j);
      }
    }
    int sz = v.size();
    for (int j = 0; j < (1 << sz); ++j) {
      for (int k = 0; k < n; ++k) {
        mn[k] = min(abs(x[k]), abs(y[k]));
        fl[k] = -1;
      }
      for (int k = 0; k < sz; ++k) {
        if ((1 << k) & j) {
          fl[v[k]] = 0;
        } else {
          fl[v[k]] = 1;
        }
      }
      ll lst = -1e18;
      for (auto it : v1) {
        if (fl[it.sc] == 0) {
          lst = it.fs;
        }
        mn[it.sc] = min(mn[it.sc], it.fs - lst);
      }
      lst = 1e18;
      for (auto it : v3) {
        if (fl[it.sc] == 0) {
          lst = it.fs;
        }
        mn[it.sc] = min(mn[it.sc], lst - it.fs);
      }
      lst = -1e18;
      for (auto it : v2) {
        if (fl[it.sc] == 1) {
          lst = it.fs;
        }
        mn[it.sc] = min(mn[it.sc], it.fs - lst);
      }
      lst = 1e18;
      for (auto it : v4) {
        if (fl[it.sc] == 1) {
          lst = it.fs;
        }
        mn[it.sc] = min(mn[it.sc], lst - it.fs);
      }
      ll tmp = 0;
      for (int l = 0; l < n; ++l) {
        tmp += mn[l] * p[l];
      }
      ans[sz] = min(ans[sz], tmp);
    }
  }
  for (int i = 0; i <= n; ++i) {
    cout << ans[i] << endl;
  }
}