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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

int N;
ll X[15], Y[15], P[15], ans[16];
vector<int> non;
vector<ll> x, y;

int Solve(int turn) {
  int cnt = 0;
  non.clear();
  x.clear();
  y.clear();
  x.push_back(0);
  y.push_back(0);
  for (int i = 0; i < N; i++) {
    int now = turn % 3;
    turn /= 3;
    if (now == 0)
      non.push_back(i);
    else {
      cnt++;
      if (now == 1)
        x.push_back(X[i]);
      else
        y.push_back(Y[i]);
    }
  }
  ll res = 0;
  for (int i = 0; i < non.size(); i++) {
    int Index = non[i];
    ll Dist = 1e16;

    for (int j = 0; j < x.size(); j++) {
      Dist = min(Dist, abs(X[Index] - x[j]));
    }
    for (int j = 0; j < y.size(); j++) {
      Dist = min(Dist, abs(Y[Index] - y[j]));
    }
    res += P[Index] * Dist;
  }
  ans[cnt] = min(ans[cnt], res);
  return 0;
}

int main() {
  cin >> N;
  fill(ans, ans + N + 1, 1e16);
  for (int i = 0; i < N; i++)
    cin >> X[i] >> Y[i] >> P[i];
  for (int i = 0; i < pow(3, N); i++)
    Solve(i);
  for (int i = 0; i <= N; i++)
    cout << ans[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\\n"); }
void no() { printf("no\\n"); }
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

int N;
ll X[15], Y[15], P[15], ans[16];

int Solve(int turn) {
  int cnt = 0;
  vector<int> non;
  vector<ll> x, y;
  x.push_back(0);
  y.push_back(0);
  for (int i = 0; i < N; i++) {
    int now = turn % 3;
    turn /= 3;
    if (now == 0)
      non.push_back(i);
    else {
      cnt++;
      if (now == 1)
        x.push_back(X[i]);
      else
        y.push_back(Y[i]);
    }
  }
  ll res = 0;
  for (int i = 0; i < non.size(); i++) {
    int Index = non[i];
    ll Dist = 1e16;

    for (int j = 0; j < x.size(); j++) {
      Dist = min(Dist, abs(X[Index] - x[j]));
    }
    for (int j = 0; j < y.size(); j++) {
      Dist = min(Dist, abs(Y[Index] - y[j]));
    }
    res += P[Index] * Dist;
  }
  ans[cnt] = min(ans[cnt], res);
  return 0;
}

int main() {
  cin >> N;
  fill(ans, ans + N + 1, 1e16);
  for (int i = 0; i < N; i++)
    cin >> X[i] >> Y[i] >> P[i];
  for (int i = 0; i < pow(3, N); i++)
    Solve(i);
  for (int i = 0; i <= N; i++)
    cout << ans[i] << endl;

  return 0;
}