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

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

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

lli offset = 10100;
lli score[16];
lli tmpScore[16];
lli xs[(1LL << 16)][16];
lli ys[(1LL << 16)][16];

void func(long long N, std::vector<long long> X, std::vector<long long> Y,
          std::vector<long long> P) {

  REP(i, 0, 16) score[i] = INF;

  REP(i, 0, (1LL << N)) {
    REP(j, 0, N) tmpScore[j] = abs(X[j]);

    REP(j, 0, N) {
      if (!(i & (1LL << j)))
        continue;
      REP(k, 0, N) { chmin(tmpScore[k], abs(X[j] - X[k])); }
    }
    REP(k, 0, N) { xs[i][k] = tmpScore[k]; }
  }
  REP(i, 0, (1LL << N)) {
    REP(j, 0, N) tmpScore[j] = abs(Y[j]);

    REP(j, 0, N) {
      if (!(i & (1LL << j)))
        continue;
      REP(k, 0, N) { chmin(tmpScore[k], abs(Y[j] - Y[k])); }
    }
    REP(k, 0, N) { ys[i][k] = tmpScore[k]; }
  }

  lli maxCnt = 1;
  REP(i, 0, N) maxCnt *= 3;

  for (lli i = 0; i < maxCnt; i++) {
    lli xState = 0, yState = 0;
    lli cnt = 0;
    lli nowScore = 0;
    lli bitCount = 0;
    lli tmp = i;
    while (tmp > 0) {
      if (tmp % 3 == 1) {
        xState |= (1LL << cnt);
        bitCount++;
      } else if (tmp % 3 == 2) {
        yState |= (1LL << cnt);
        bitCount++;
      }
      cnt++;
      tmp /= 3;
    }
    lli sumScore = 0;
    REP(j, 0, N) { sumScore += P[j] * min(xs[xState][j], ys[yState][j]); }
    chmin(score[bitCount], sumScore);
  }
  REP(i, 0, N + 1) { cout << score[i] << endl; }
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  std::vector<long long> X(N);
  std::vector<long long> Y(N);
  std::vector<long long> P(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &X[i]);
    scanf("%lld", &Y[i]);
    scanf("%lld", &P[i]);
  }
  func(N, std::move(X), std::move(Y), std::move(P));
  return 0;
}

Error: TLE

Fixed submission:

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

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

lli offset = 10100;
lli score[16];
lli xComp[21000];
lli yComp[21000];

lli popCount(lli num) {
  lli rtn = 0;
  while (num > 0) {
    if (num % 2)
      rtn++;
    num /= 2;
  }
  return rtn;
}

void func(long long N, std::vector<long long> X, std::vector<long long> Y,
          std::vector<long long> P) {

  REP(i, 0, 16) score[i] = INF;

  REP(i, 0, 21000) {
    xComp[i] = -1;
    yComp[i] = -1;
  }

  vector<lli> xs, ys;
  REP(i, 0, N) {
    xs.push_back(X[i]);
    ys.push_back(Y[i]);
  }
  lli xCnt = 0, yCnt = 0;
  SORT(xs);
  SORT(ys);
  for (auto e : xs) {
    xComp[e + offset] = xCnt;
    xCnt++;
  }
  for (auto e : ys) {
    yComp[e + offset] = yCnt;
    yCnt++;
  }

  lli maxCnt = 1;
  REP(i, 0, N) maxCnt *= 3;

  for (lli i = 0; i < maxCnt; i++) {
    lli xState = 0, yState = 0;
    lli cnt = 0;
    lli tmp = i;
    while (tmp > 0) {
      if (tmp % 3 == 1)
        xState |= (1LL << xComp[X[cnt] + offset]);
      else if (tmp % 3 == 2)
        yState |= (1LL << yComp[Y[cnt] + offset]);
      tmp /= 3;
      cnt++;
    }
    lli bitCount = popCount(xState) + popCount(yState);

    // calcScore
    lli scoreSum = 0;
    REP(j, 0, N) {
      lli nowScore = min(abs(X[j]), abs(Y[j]));
      REP(k, 0, xCnt) {
        if (xState & (1LL << k)) {
          chmin(nowScore, abs(X[j] - xs[k]));
        }
      }
      REP(k, 0, yCnt) {
        if (yState & (1LL << k)) {
          chmin(nowScore, abs(Y[j] - ys[k]));
        }
      }
      scoreSum += P[j] * nowScore;
    }
    chmin(score[bitCount], scoreSum);
  }
  REP(i, 0, N + 1) { cout << score[i] << endl; }
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  std::vector<long long> X(N);
  std::vector<long long> Y(N);
  std::vector<long long> P(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &X[i]);
    scanf("%lld", &Y[i]);
    scanf("%lld", &P[i]);
  }
  func(N, std::move(X), std::move(Y), std::move(P));
  return 0;
}