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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 28;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 998244353;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

int n;
vl a, x, y, p;

int main() {
  cin >> n;
  a = vl(n + 1, INF);
  x = y = p = vl(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  int N = 1;
  for (int i = 0; i < n; i++)
    N *= 3;
  for (int i = 0; i < N; i++) {
    vi b(n);
    int t = i;
    for (int j = 0; j < n; j++) {
      b[j] = t % 3;
      t /= 3;
    }
    t = 0;
    for (int j = 0; j < n; j++)
      if (b[j])
        t++;
    ll sum = 0;
    for (int j = 0; j < n; j++)
      if (!b[j]) {
        ll tmp = min(abs(x[j]), abs(y[j]));
        for (int k = 0; k < n; k++) {
          if (b[k] == 1)
            tmp = min(tmp, abs(x[j] - x[k]));
          if (b[k] == 2)
            tmp = min(tmp, abs(y[j] - y[k]));
        }
        sum += tmp * p[j];
        if (sum >= a[t])
          break;
      }
    a[t] = min(a[t], sum);
  }
  for (auto i : a)
    cout << i << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 28;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 998244353;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

int n;
vl a, x, y, p;

int main() {
  cin >> n;
  a = vl(n + 1, INF);
  x = y = p = vl(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  int N = 1;
  for (int i = 0; i < n; i++)
    N *= 3;
  for (int i = 0; i < N; i++) {
    vi b(n);
    int t = i;
    for (int j = 0; j < n; j++) {
      b[j] = t % 3;
      t /= 3;
    }
    t = 0;
    for (int j = 0; j < n; j++)
      if (b[j])
        t++;
    ll sum = 0;
    for (int j = 0; j < n; j++)
      if (!b[j]) {
        ll tmp = min(abs(x[j]), abs(y[j]));
        for (int k = 0; k < n; k++) {
          if (b[k] == 1)
            tmp = min(tmp, abs(x[j] - x[k]));
          if (b[k] == 2)
            tmp = min(tmp, abs(y[j] - y[k]));
        }
        sum += tmp * p[j];
        if (sum > a[t])
          break;
      }
    a[t] = min(a[t], sum);
  }
  for (auto i : a)
    cout << i << endl;
}