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

#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

class Prints {
private:
  class __Prints {
  public:
    __Prints(const char *sep, const char *term) : sep(sep), term(term) {}
    template <class... Args> void operator()(Args &&...args) const {
      print(std::forward<Args>(args)...);
    }

  private:
    const char *sep, *term;
    void print() const { std::cout << term; }
    void print_rest() const { std::cout << term; }
    template <class T, class... Tail>
    void print(T &&head, Tail &&...tail) const {
      std::cout << head, print_rest(std::forward<Tail>(tail)...);
    }
    template <class T, class... Tail>
    void print_rest(T &&head, Tail &&...tail) const {
      std::cout << sep << head, print_rest(std::forward<Tail>(tail)...);
    }
  };

public:
  Prints() {}
  __Prints operator()(const char *sep = " ", const char *term = "\\n") const {
    return __Prints(sep, term);
  }
  template <class T>
  void print_vec(const std::vector<T> &vec, const char *sep = " ",
                 const char *term = "\\n") const {
    const size_t sz = vec.size();
    for (size_t i = 0; i < sz; i++)
      std::cout << vec[i] << (i == sz - 1 ? term : sep);
  }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ll memo_x[16][1 << 15], memo_y[16][1 << 15];

void solve() {
  ll N;
  cin >> N;
  Vl xs(N), ys(N), ps(N);
  for (ll i = 0; i < N; i++) {
    cin >> xs[i] >> ys[i] >> ps[i];
  }

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < (1ll << N); j++) {
      memo_x[i][j] = abs(xs[i]);
      memo_y[i][j] = abs(ys[i]);
      for (ll k = 0; k < N; k++) {
        if (j >> k & 1) {
          memo_x[i][j] = min(memo_x[i][j], abs(xs[i] - xs[k]));
          memo_y[i][j] = min(memo_y[i][j], abs(ys[i] - ys[k]));
        }
      }
    }
  }

  Vl ans(N + 1, L_INF);

  ll ub = 1;
  for (ll i = 0; i < N; i++) {
    ub *= 3;
  }

  for (ll mask = 0; mask < ub; mask++) {
    ll tmp = mask;
    ll cnt = 0;
    ll fgx = 0, fgy = 0;
    for (ll i = 0; i < N; i++) {
      ll r = tmp % 3;
      if (r == 1) {
        fgx |= 1ll << i;
        ++cnt;
      } else if (r == 2) {
        fgy |= 1ll << i;
        ++cnt;
      }
      tmp /= 3;
    }
    ll score = 0;
    for (ll i = 0; i < N; i++) {
      score += min(memo_x[i][fgx], memo_y[i][fgy]) * ps[i];
    }
    ans[cnt] = min(ans[cnt], score);
  }

  for (ll i = 0; i <= N; i++) {
    prints()(ans[i]);
  }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  cout << setprecision(18) << fixed;
  solve();
  cout << flush;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

class Prints {
private:
  class __Prints {
  public:
    __Prints(const char *sep, const char *term) : sep(sep), term(term) {}
    template <class... Args> void operator()(Args &&...args) const {
      print(std::forward<Args>(args)...);
    }

  private:
    const char *sep, *term;
    void print() const { std::cout << term; }
    void print_rest() const { std::cout << term; }
    template <class T, class... Tail>
    void print(T &&head, Tail &&...tail) const {
      std::cout << head, print_rest(std::forward<Tail>(tail)...);
    }
    template <class T, class... Tail>
    void print_rest(T &&head, Tail &&...tail) const {
      std::cout << sep << head, print_rest(std::forward<Tail>(tail)...);
    }
  };

public:
  Prints() {}
  __Prints operator()(const char *sep = " ", const char *term = "\\n") const {
    return __Prints(sep, term);
  }
  template <class T>
  void print_vec(const std::vector<T> &vec, const char *sep = " ",
                 const char *term = "\\n") const {
    const size_t sz = vec.size();
    for (size_t i = 0; i < sz; i++)
      std::cout << vec[i] << (i == sz - 1 ? term : sep);
  }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void solve() {
  ll N;
  cin >> N;
  Vl xs(N), ys(N), ps(N);
  for (ll i = 0; i < N; i++) {
    cin >> xs[i] >> ys[i] >> ps[i];
  }

  Vl ans(N + 1, L_INF);

  ll ub = 1;
  for (ll i = 0; i < N; i++) {
    ub *= 3;
  }

  vector<bool> lxs(N), lys(N);
  Vi candi;
  for (ll mask = 0; mask < ub; mask++) {
    ll tmp = mask;
    ll cnt = 0;
    candi.clear();
    bool ok = true;
    for (ll i = 0; i < N; i++) {
      ll r = tmp % 3;
      if (r == 1) {
        lxs[i] = true;
        lys[i] = false;
        ++cnt;
      } else if (r == 2) {
        lxs[i] = false;
        lys[i] = true;
        ++cnt;
      } else {
        lxs[i] = false;
        lys[i] = false;
        candi.emplace_back(i);
      }
      tmp /= 3;
    }
    ll score = 0;
    for (auto i : candi) {
      ll d = min(abs(xs[i]), abs(ys[i]));
      for (ll j = 0; j < N; j++) {
        if (lxs[j]) {
          d = min(d, abs(xs[i] - xs[j]));
        }
        if (lys[j]) {
          d = min(d, abs(ys[i] - ys[j]));
        }
      }
      score += ps[i] * d;
      if (score >= ans[cnt]) {
        ok = false;
        break;
      }
    }
    if (ok) {
      ans[cnt] = min(ans[cnt], score);
    }
  }

  for (ll i = 0; i <= N; i++) {
    prints()(ans[i]);
  }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  cout << setprecision(18) << fixed;
  solve();
  cout << flush;
  return 0;
}