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

#include <bits/stdc++.h>
using namespace std;
#pragma optimize("-O3")

#define f first
#define s second
#define pb push_back

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int x[n];
  int y[n];
  long long int p[n];
  int mini[n];
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
    mini[i] = min(abs(x[i]), abs(y[i]));
  }

  long long int ans[n + 1];
  for (int i = 0; i <= n; i++) {
    ans[i] = 1e18;
  }
  for (int i = 0; i < (1 << n); i++) {
    int num = __builtin_popcount(i);
    int mask = i;
    vector<int> vec;
    vector<int> vecl;
    for (int j = 0; j < n; j++) {
      if ((i & (1 << j))) {
        vec.pb(j);
      } else {
        vecl.pb(j);
      }
    }

    while (mask > 0) {
      long long int tot = 0;
      for (auto k : vecl) {
        int xk = x[k];
        int yk = y[k];

        int val = mini[k];

        for (auto l : vec) {
          if ((mask & (1 << l))) {
            val = min(val, abs(xk - x[l]));
          } else {
            val = min(val, abs(yk - y[l]));
          }
        }
        tot += (val * (p[k]));
      }
      ans[num] = min(ans[num], tot);
      mask = (mask - 1) & i;
    }
    long long int tot = 0;

    for (auto k : vecl) {
      int xk = x[k];
      int yk = y[k];

      int val = mini[k];

      for (auto l : vec) {

        if ((mask & (1 << l))) {
          val = min(val, abs(xk - x[l]));
        } else {
          val = min(val, abs(yk - y[l]));
        }
      }
      tot += (val * (p[k]));
    }
    ans[num] = min(ans[num], tot);
  }
  for (int i = 0; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma optimize("-O3")

#define f first
#define s second
#define pb push_back

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int x[n];
  int y[n];
  long long int p[n];
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  long long int ans[n + 1];
  for (int i = 0; i <= n; i++) {
    ans[i] = 1e18;
  }
  for (int i = 0; i < (1 << n); i++) {
    int num = __builtin_popcount(i);
    int mask = i;
    vector<int> vec;
    vector<int> vecl;
    for (int j = 0; j < n; j++) {
      if ((i & (1 << j))) {
        vec.pb(j);
      } else {
        vecl.pb(j);
      }
    }

    while (mask > 0) {
      long long int tot = 0;
      for (auto k : vecl) {
        int xk = x[k];
        int yk = y[k];

        int val = 1e9;
        val = min(abs(xk), abs(yk));

        for (auto l : vec) {

          if ((mask & (1 << l))) {
            val = min(val, abs(xk - x[l]));
          } else {
            val = min(val, abs(yk - y[l]));
          }
        }
        tot += (val * (p[k]));
      }
      ans[num] = min(ans[num], tot);
      mask = (mask - 1) & i;
    }
    long long int tot = 0;
    for (int k = 0; k < n; k++) {
      if ((i & (1 << k))) {

        continue;
      }
      int xk = x[k];
      int yk = y[k];
      int val = 1e9;
      val = min(abs(xk), abs(yk));

      for (int l = 0; l < n; l++) {
        if ((i & (1 << l))) {
          if ((mask & (1 << l))) {
            val = min(val, abs(xk - x[l]));
          } else {
            val = min(val, abs(yk - y[l]));
          }
        }
      }
      tot += (val * (p[k]));
    }
    ans[num] = min(ans[num], tot);
  }
  for (int i = 0; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}