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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

ll to_k(ll a, ll b) {
  ll res = 0;
  ll ten = 1;
  while (a > 0) {
    res += ten * (a % b);
    ten *= 10;
    a /= b;
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  vector<pll> t(n);
  vll p(n);
  for (int i = 0; i < n; i++) {
    cin >> t.at(i).first >> t.at(i).second >> p.at(i);
  }
  vvll yoko((1 << n), vll(n, INF)), tate((1 << n), vll(n, INF));
  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < n; j++) {
      ll dx = INF, dy = INF;
      for (int k = 0; k < n; k++) {
        if (i >> k & 1)
          chmin(dx, abs(t.at(j).first - t.at(k).first));
        if (i >> k & 1)
          chmin(dy, abs(t.at(j).second - t.at(k).second));
      }
      chmin(dx, abs(t.at(j).first));
      chmin(dy, abs(t.at(j).second));
      yoko.at(i).at(j) = dx * p.at(j);
      tate.at(i).at(j) = dy * p.at(j);
    }
  }
  ll mx = 1;
  vll ans(n + 1, INF);
  for (int i = 0; i < n; i++)
    mx *= 3;
  for (int i = 0; i < mx; i++) {
    string num = to_string(to_k(i, 3));
    reverse(all(num));
    ll yo = 0, ta = 0;
    for (int j = 0; j < num.size(); j++) {
      if (num.at(j) == '1')
        yo |= (1 << j);
      if (num.at(j) == '2')
        ta |= (1 << j);
    }
    ll cnt = 0;
    for (int j = 0; j < num.size(); j++)
      cnt += (num.at(j) != '0');
    ll sum = 0;
    for (int j = 0; j < n; j++)
      sum += min(yoko.at(yo).at(j), tate.at(ta).at(j));
    chmin(ans.at(cnt), sum);
  }
  for (int i = 0; i <= n; i++)
    cout << ans.at(i) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

ll to_k(ll a, ll b) {
  ll res = 0;
  ll ten = 1;
  while (a > 0) {
    res += ten * (a % b);
    ten *= 10;
    a /= b;
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  vector<pll> t(n);
  vll p(n);
  for (int i = 0; i < n; i++) {
    cin >> t.at(i).first >> t.at(i).second >> p.at(i);
  }
  ll mx = 1;
  vll ans(n + 1, INF);
  for (int i = 0; i < n; i++)
    mx *= 3;
  for (int i = 0; i < mx; i++) {
    string num = to_string(to_k(i, 3));
    reverse(all(num));
    ll cnt = 0;
    for (int j = 0; j < num.size(); j++)
      cnt += (num.at(j) != '0');
    ll sum = 0;
    for (int j = 0; j < n; j++) {
      ll d = INF;
      for (int k = 0; k < num.size(); k++) {
        if (num.at(k) == '1')
          chmin(d, abs(t.at(j).first - t.at(k).first));
        if (num.at(k) == '2')
          chmin(d, abs(t.at(j).second - t.at(k).second));
      }
      chmin(d, abs(t.at(j).first));
      chmin(d, abs(t.at(j).second));
      sum += d * p.at(j);
    }
    chmin(ans.at(cnt), sum);
  }
  for (int i = 0; i <= n; i++)
    cout << ans.at(i) << endl;
}