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

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

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

int pow_int(int a, int n) {
  int i;
  int ans = 1;
  for (i = 0; i < n; ++i) {
    ans *= a;
  }
  return ans;
}

int main() {
  int i, j;
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> y(n);
  vector<ll> p(n);
  for (i = 0; i < n; ++i) {
    cin >> x.at(i);
    cin >> y.at(i);
    cin >> p.at(i);
  }
  int bit = 0;

  vector<vector<int>> xmiti((1 << n), vector<int>(15, 0));
  vector<vector<int>> ymiti((1 << n), vector<int>(15, 0));
  for (bit = 0; bit < (1ll << n); ++bit) {
    set<int> yy;
    set<int> tt;
    for (i = 0; i < n; ++i) {
      if (bit & (1 << i)) {
        yy.insert(x.at(i));
        tt.insert(y.at(i));
      }
    }
    yy.insert(0);
    tt.insert(0);
    vector<int> yoko;
    vector<int> tate;
    for (auto xx : yy) {
      yoko.push_back(xx);
    }
    for (auto xx : tt) {
      tate.push_back(xx);
    }

    for (i = 0; i < n; ++i) {
      int xa = lower_bound(all(yoko), x.at(i)) - yoko.begin();
      int ya = lower_bound(all(tate), y.at(i)) - tate.begin();
      int xd, yd;

      if (xa >= yoko.size()) {
        xd = abs(x.at(i) - yoko.back());
      } else if (xa == 0) {
        xd = abs(x.at(i) - yoko.front());
      } else {
        xd = min(abs(x.at(i) - yoko.at(xa)), abs(x.at(i) - yoko.at(xa - 1)));
      }

      if (ya >= tate.size()) {
        yd = abs(y.at(i) - tate.back());
      } else if (ya == 0) {
        yd = abs(y.at(i) - tate.front());
      } else {
        yd = min(abs(y.at(i) - tate.at(ya)), abs(y.at(i) - tate.at(ya - 1)));
      }

      xmiti.at(bit).at(i) = xd;
      ymiti.at(bit).at(i) = yd;
    }
  }

  vector<ll> ans(n + 1, (1ll << 60));

  int INF = pow_int(3, n);
  for (bit = 0; bit < INF; ++bit) {
    int waru = INF / 3;
    int tmpbit = bit;
    int m = 0;
    int bit1 = 0;
    int bit2 = 0;
    for (i = 0; i < n; ++i) {
      int tmp = tmpbit / waru;
      tmpbit -= tmp * waru;
      waru /= 3;

      if (tmp)
        ++m;
      if (tmp == 1)
        bit1 += (1 << i);
      else if (tmp == 2)
        bit2 += (1 << i);
    }

    ll tmpans = 0;
    for (i = 0; i < n; ++i) {
      ll xd = xmiti.at(bit1).at(i);
      ll yd = ymiti.at(bit2).at(i);
      tmpans += p.at(i) * min(xd, yd);
    }
    ans.at(m) = min(ans.at(m), tmpans);
  }

  for (i = 0; i <= n; ++i) {
    cout << ans.at(i) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

ll pow_int(ll a, ll n) {
  int i;
  ll ans = 1;
  for (i = 0; i < n; ++i) {
    ans *= a;
  }
  return ans;
}

int main() {
  ll i, j;
  ll n;
  cin >> n;
  vector<ll> x(n);
  vector<ll> y(n);
  vector<ll> p(n);
  for (i = 0; i < n; ++i) {
    cin >> x.at(i);
    cin >> y.at(i);
    cin >> p.at(i);
  }
  ll bit = 0;

  vector<vector<ll>> xmiti((1ll << n), vector<ll>(15, 0));
  vector<vector<ll>> ymiti((1ll << n), vector<ll>(15, 0));
  for (bit = 0; bit < (1ll << n); ++bit) {
    set<ll> yy;
    set<ll> tt;
    for (i = 0; i < n; ++i) {
      if (bit & (1 << i)) {
        yy.insert(x.at(i));
        tt.insert(y.at(i));
      }
    }
    yy.insert(0);
    tt.insert(0);
    vector<ll> yoko;
    vector<ll> tate;
    for (auto xx : yy) {
      yoko.push_back(xx);
    }
    for (auto xx : tt) {
      tate.push_back(xx);
    }

    for (i = 0; i < n; ++i) {
      ll xa = lower_bound(all(yoko), x.at(i)) - yoko.begin();
      ll ya = lower_bound(all(tate), y.at(i)) - tate.begin();
      ll xd, yd;

      if (xa >= yoko.size()) {
        xd = abs(x.at(i) - yoko.back());
      } else if (xa == 0) {
        xd = abs(x.at(i) - yoko.front());
      } else {
        xd = min(abs(x.at(i) - yoko.at(xa)), abs(x.at(i) - yoko.at(xa - 1)));
      }

      if (ya >= tate.size()) {
        yd = abs(y.at(i) - tate.back());
      } else if (ya == 0) {
        yd = abs(y.at(i) - tate.front());
      } else {
        yd = min(abs(y.at(i) - tate.at(ya)), abs(y.at(i) - tate.at(ya - 1)));
      }

      xmiti.at(bit).at(i) = xd;
      ymiti.at(bit).at(i) = yd;
    }
  }

  vector<ll> ans(n + 1, 1e18);

  ll INF = pow_int(3, n);
  for (bit = 0; bit < INF; ++bit) {
    ll waru = INF / 3;
    ll tmpbit = bit;
    ll m = 0;
    ll bit1 = 0;
    ll bit2 = 0;
    for (i = 0; i < n; ++i) {
      ll tmp = tmpbit / waru;
      tmpbit -= tmp * waru;
      waru /= 3;

      if (tmp)
        ++m;
      if (tmp == 1)
        bit1 += (1ll << i);
      else if (tmp == 2)
        bit2 += (1ll << i);
    }

    ll tmpans = 0;
    for (i = 0; i < n; ++i) {
      ll xd = xmiti.at(bit1).at(i);
      ll yd = ymiti.at(bit2).at(i);
      tmpans += p.at(i) * min(xd, yd);
    }
    ans.at(m) = min(ans.at(m), tmpans);
  }

  for (i = 0; i <= n; ++i) {
    cout << ans.at(i) << endl;
  }

  return 0;
}