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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
ld eps = (ld)1 / 1e6;
ll inf = LLONG_MAX, mod1 = 1e9 + 7, mod2 = 998244353;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll binpow(ll a, ll b, ll mod) {
  return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod
                    : sqr(binpow(a, b / 2, mod)) % mod)
           : 1;
}
ll binmult(ll a, ll b, ll mod) {
  return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod
                    : (2 * binmult(a, b / 2, mod)) % mod)
           : 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ll p[16], ans[16], dp[14400000], mn[16], nb[16], st[16], dist[16][16][2];
pll c[16];

int main() {
  fast_io;
  ll n, i, j, t, s, m, nw;
  vector<ll> v;
  cin >> n;
  for (i = 1, st[0] = 1; i <= n; i++)
    cin >> c[i].first >> c[i].second >> p[i], st[i] = st[i - 1] * 3,
                                              ans[i] = inf;
  for (i = 1; i <= n; i++)
    mn[i] = p[i] * min(abs(c[i].first), abs(c[i].second)), dp[0] += mn[i];
  ans[0] = dp[0];
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      for (t = 0; t <= 1; t++)
        dist[i][j][t] =
            p[i] * abs(t ? c[i].second - c[j].second : c[i].first - c[j].first);
  for (i = 1; i < st[n]; i++) {
    dp[i] = inf;
    t = i, nw = 0;
    v.clear();
    for (j = 1; t > 0 && j <= n; j++, t /= 3) {
      nb[j] = t % 3;
      if (nb[j])
        nw++, v.pb(j), dp[i] = min(dp[i], dp[i - nb[j] * st[j - 1]]);
    }
    for (j = 1, s = 0; j <= n; j++) {
      m = mn[j];
      for (auto f : v)
        m = min(m, dist[j][f][nb[f] - 1]);
      s += m;
    }
    dp[i] = min(dp[i], s);
    ans[nw] = min(ans[nw], dp[i]);
  }
  for (i = 0; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
ld eps = (ld)1 / 1e6;
ll inf = LLONG_MAX, mod1 = 1e9 + 7, mod2 = 998244353;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll binpow(ll a, ll b, ll mod) {
  return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod
                    : sqr(binpow(a, b / 2, mod)) % mod)
           : 1;
}
ll binmult(ll a, ll b, ll mod) {
  return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod
                    : (2 * binmult(a, b / 2, mod)) % mod)
           : 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ll p[16], ans[16], dp[14400000], mn[16], nb[16], st[16], dist[16][16][2];
pll c[16];

int main() {
  fast_io;
  ll n, i, j, k, t, s, m, nw;
  cin >> n;
  for (i = 1, st[0] = 1; i <= n; i++)
    cin >> c[i].first >> c[i].second >> p[i], st[i] = st[i - 1] * 3,
                                              ans[i] = inf;
  for (i = 1; i <= n; i++)
    mn[i] = p[i] * min(abs(c[i].first), abs(c[i].second)), dp[0] += mn[i];
  ans[0] = dp[0];
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      for (k = 0; k <= 1; k++)
        dist[i][j][k] =
            p[i] * abs(k ? c[i].second - c[j].second : c[i].first - c[j].first);
  for (i = 1; i < st[n]; i++) {
    dp[i] = inf;
    t = i, nw = 0;
    for (j = 1; t > 0 && j <= n; j++, t /= 3) {
      nb[j] = t % 3;
      if (nb[j])
        nw++, dp[i] = min(dp[i], dp[i - nb[j] * st[j - 1]]);
    }
    for (j = 1, s = 0; j <= n; j++) {
      m = mn[j];
      for (k = 1; k <= n; k++)
        if (nb[k])
          m = min(m, dist[j][k][nb[k] - 1]);
      s += m;
    }
    dp[i] = min(dp[i], s);
    ans[nw] = min(ans[nw], dp[i]);
  }
  for (i = 0; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}