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

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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vst;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define all(ct) ct.begin(), ct.end()
#define endl "\\n"
#define fr(i, a) for (auto i : a)
#define f(i, a, b) for (int i = a; i < b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define in(d, v) d.find(v) != d.end()
#define mp make_pair
#define size(a) int(a.size())

const ll mod = 1e9 + 7;
const ll inf = ll(4e18);
const double PI = acos(-1);

ll modexp(ll x, ll n, int md) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return modexp((x * x) % md, n / 2, md);
  return (x * modexp((x * x) % md, n / 2, md)) % md;
}
int n;
pair<ll, pii> a[15];
ll ans[16];
int ax[16] = {0}, ay[16] = {0}, countx = 0, county = 0;
void road(int i) {
  if (i == n) {
    int count = countx + county;
    ll curr = 0;
    f(i, 0, n) {
      ll curr1 = inf;
      f(j, 0, countx + 1) {
        curr1 = min(curr1, a[i].first * abs(ax[j] - a[i].second.first));
      }
      ll curr2 = inf;
      f(j, 0, county + 1) {
        curr2 = min(curr2, a[i].first * abs(ay[j] - a[i].second.second));
      }
      curr += min(curr1, curr2);
      if (curr > ans[count])
        return;
    }
    ans[count] = curr;
    return;
  }
  road(i + 1);
  ++countx;
  ax[countx] = a[i].second.first;
  road(i + 1);
  --countx;
  ++county;
  ay[county] = a[i].second.second;
  road(i + 1);
  --county;
}
int main() {
  fastio;
  // clock_t tm = clock();
  int x, y;
  ll p;
  cin >> n;
  f(i, 0, n) { cin >> a[i].second.first >> a[i].second.second >> a[i].first; }
  sort(a, a + n, greater<pair<ll, pii>>());
  f(i, 0, n + 1) ans[i] = inf;
  road(0);
  f(i, 0, n + 1) cout << ans[i] << endl;
  // cout<<((clock()-tm)*1.0)/CLOCKS_PER_SEC<<endl;
}

Error: TLE

Fixed submission:

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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vst;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define all(ct) ct.begin(), ct.end()
#define endl "\\n"
#define fr(i, a) for (auto i : a)
#define f(i, a, b) for (int i = a; i < b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define in(d, v) d.find(v) != d.end()
#define mp make_pair
#define size(a) int(a.size())

const ll mod = 1e9 + 7;
const ll inf = ll(4e18);
const double PI = acos(-1);

ll modexp(ll x, ll n, int md) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return modexp((x * x) % md, n / 2, md);
  return (x * modexp((x * x) % md, n / 2, md)) % md;
}
int n;
pair<ll, pii> a[15];
int ch[15] = {0};
ll ans[16];
vi ax, ay;
void road(int i) {
  if (i == n) {
    ax.clear();
    ay.clear();
    ax.pb(0);
    ay.pb(0);
    ll count = 0;
    f(i, 0, n) {
      if (ch[i] == 0)
        continue;
      ++count;
      if (ch[i] == 1)
        ax.pb(a[i].second.first);
      if (ch[i] == 2)
        ay.pb(a[i].second.second);
    }
    sort(all(ax));
    sort(all(ay));
    ll curr = 0;
    f(i, 0, n) {
      int z = lower_bound(all(ax), a[i].second.first) - ax.begin();
      ll curr1 = inf;
      if (z < size(ax)) {
        curr1 = (ax[z] - a[i].second.first) * a[i].first;
      }
      if (z > 0) {
        --z;
        curr1 = min(curr1, (a[i].second.first - ax[z]) * a[i].first);
      }
      z = lower_bound(all(ay), a[i].second.second) - ay.begin();
      ll curr2 = inf;
      if (z < size(ay)) {
        curr2 = (ay[z] - a[i].second.second) * a[i].first;
      }
      if (z > 0) {
        --z;
        curr2 = min(curr2, (a[i].second.second - ay[z]) * a[i].first);
      }
      curr += min(curr1, curr2);
      if (curr > ans[count])
        return;
    }
    ans[count] = curr;
    return;
  }
  road(i + 1);
  ch[i] = 1;
  road(i + 1);
  ch[i] = 2;
  road(i + 1);
  ch[i] = 0;
}
int main() {
  fastio;
  // clock_t tm = clock();
  int x, y;
  ll p;
  cin >> n;
  f(i, 0, n) { cin >> a[i].second.first >> a[i].second.second >> a[i].first; }
  f(i, 0, n + 1) ans[i] = inf;
  road(0);
  f(i, 0, n + 1) cout << ans[i] << endl;
  // cout<<((clock()-tm)*1.0)/CLOCKS_PER_SEC<<endl;
}