Problem p02604 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define $(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\n'
#define x first
#define y second
#define int long long
#define double long double
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define beg begin
#define dbg(x) cerr << #x << " = " << x << endl;

template <class T> ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}

template <class T> istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}

template <class T> ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}

template <class T> istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}

const int MOD = 998244353, MAXN = 5e5 + 10, INF = 1e18 + 10, BASE = 37;
int n;
vec<vec<int>> p;
vec<int> ans;
vec<int> prefix;
vec<int> v, h;

void check(int i) {
  if (i == n) {
    int cnt = 0;
    h.clear(), v.clear();
    for (int i = 0; i < n; ++i) {
      if (prefix[i] >= 1) {
        cnt++;
      }
      if (prefix[i] == 1) {
        h.pb(p[i][1]);
      }
      if (prefix[i] == 2) {
        v.pb(p[i][0]);
      }
    }
    v.pb(0);
    h.pb(0);
    int cur = 0;
    for (int i = 0; i < n; ++i) {
      int tmp = INF;
      for (int j = 0; j < $(h); ++j) {
        tmp = min(tmp, abs(h[j] - p[i][1]));
      }
      for (int j = 0; j < $(v); ++j) {
        tmp = min(tmp, abs(v[j] - p[i][0]));
      }
      cur += tmp * p[i][2];
    }
    ans[cnt] = min(ans[cnt], cur);
    return;
  } else {
    for (int j = 0; j < 3; ++j) {
      prefix.pb(j);
      check(i + 1);
      prefix.pop_back();
    }
  }
}

void solve() {
  cin >> n;
  p.resize(n, vec<int>(3));
  cin >> p;
  ans.resize(n + 1, INF);

  check(0);

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

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define $(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\n'
#define x first
#define y second
#define int long long
#define double long double
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define beg begin
#define dbg(x) cerr << #x << " = " << x << endl;

template <class T> ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}

template <class T> istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}

template <class T> ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}

template <class T> istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}

const int MOD = 998244353, MAXN = 5e5 + 10, INF = 1e18 + 10, BASE = 37;
int n;
vec<vec<int>> p;
vec<int> ans;
vec<int> prefix;
vec<int> v, h;

void check(int i) {
  if (i == n) {
    int cnt = 0;
    h.clear(), v.clear();
    for (int i = 0; i < n; ++i) {
      if (prefix[i] >= 1) {
        cnt++;
      }
      if (prefix[i] == 1) {
        h.pb(p[i][1]);
      }
      if (prefix[i] == 2) {
        v.pb(p[i][0]);
      }
    }
    v.pb(0);
    h.pb(0);
    int cur = 0;
    for (int i = 0; i < n; ++i) {
      if (prefix[i] == 1 || prefix[i] == 2)
        continue;
      int tmp = INF;
      for (int j = 0; j < $(h); ++j) {
        tmp = min(tmp, abs(h[j] - p[i][1]));
      }
      for (int j = 0; j < $(v); ++j) {
        tmp = min(tmp, abs(v[j] - p[i][0]));
      }
      cur += tmp * p[i][2];
    }
    ans[cnt] = min(ans[cnt], cur);
    return;
  } else {
    for (int j = 0; j < 3; ++j) {
      prefix.pb(j);
      check(i + 1);
      prefix.pop_back();
    }
  }
}

void solve() {
  cin >> n;
  p.resize(n, vec<int>(3));
  cin >> p;
  ans.resize(n + 1, INF);

  check(0);

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