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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define pb push_back
#define endl "\\n" // TODO: remove
// #define int long long

const int N = 322;
const long long inf = (long long)(2e18) + 322;
const int mod = (int)(1e9) + 7; // check modulo

int x[N], y[N];
long long ans[N];
long long p[N];
int vx[N], szx, vy[N], szy;

void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> p[i];
    ans[i] = inf;
  }
  int cnt = 0;
  for (int mask = 0; mask < (1 << n); ++mask) {
    int count = __builtin_popcount(mask);
    if (count >= n - 1)
      continue;
    for (int mask1 = 0; mask1 < (1 << count); ++mask1) {
      int ones = 0;
      szx = 0;
      szy = 0;
      long long cost = 0;
      vx[0] = 0;
      vy[0] = 0;
      for (int i = 0; i < n; ++i) {
        if ((mask >> i) & 1) {
          if ((mask1 >> ones) & 1) {
            vx[++szx] = x[i];
          } else {
            vy[++szy] = y[i];
          }
          ones++;
        }
      }
      //            cout << bitset<3>(mask) << ' ' << bitset<3>(mask1) << endl;
      //            cout << szx << ' ' << szy << endl;
      sort(vx, vx + szx + 1);
      sort(vy, vy + szy + 1);
      //            for(int i = 0; i <= szx; ++i) cout << vx[i] << ' ';
      //            cout << endl;
      //            for(int i = 0; i <= szy; ++i) cout << vy[i] << ' ';
      //            cout << endl;
      for (int i = 0; i < n; ++i) {
        if (!((mask >> i) & 1)) {
          int X = lower_bound(vx, vx + szx + 1, x[i]) - vx;
          int Y = lower_bound(vy, vy + szy + 1, y[i]) - vy;
          long long mn = inf;
          if (X <= szx)
            mn = min(mn, abs(vx[X] - x[i]) * p[i]);
          if (Y <= szy)
            mn = min(mn, abs(vy[Y] - y[i]) * p[i]);
          if (X)
            X--;
          if (Y)
            Y--;
          if (X <= szx)
            mn = min(mn, abs(vx[X] - x[i]) * p[i]);
          if (Y <= szy)
            mn = min(mn, abs(vy[Y] - y[i]) * p[i]);
          cost += mn;
        }
      }
      ans[count] = min(ans[count], cost);
    }
  }
  for (int i = 0; i < n; ++i) {
    long long mn = inf;
    for (int j = 0; j <= n; ++j) {
      if (i == j)
        continue;
      mn = min({mn, abs(x[j] - x[i]) * p[i], abs(y[i] - y[j]) * p[i]});
    }
    ans[n - 1] = min(ans[n - 1], mn);
  }
  for (int i = 0; i <= n; ++i) {
    cout << ans[i] << endl;
  }
}

signed main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  //    freopen("travel_restrictions_input.txt", "r", stdin);
  //    freopen("A.out", "w", stdout);
  //    int test; cin >> test;
  //    for(int t = 1; t <= test; ++t) {
  //        cout << "Case #" << t << ":" << endl;
  solve();
  //    }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define pb push_back
#define endl "\\n" // TODO: remove
#define int long long

const int N = (int)(1e6) + 322;
const int inf = (int)(2e18);
const int mod = (int)(1e9) + 7; // check modulo

int x[N], y[N], ans[N], p[N], vx[N], szx, vy[N], szy;

void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> p[i];
    ans[i] = inf;
  }
  int cnt = 0;
  for (int mask = 0; mask < (1 << n); ++mask) {
    int count = __builtin_popcount(mask);
    if (count >= n - 1)
      continue;
    for (int mask1 = 0; mask1 < (1 << count); ++mask1) {
      int ones = 0;
      szx = 0;
      szy = 0;
      int cost = 0;
      vx[0] = 0;
      vy[0] = 0;
      for (int i = 0; i < n; ++i) {
        if ((mask >> i) & 1) {
          if ((mask1 >> ones) & 1) {
            vx[++szx] = x[i];
          } else {
            vy[++szy] = y[i];
          }
          ones++;
        }
      }
      //            cout << bitset<3>(mask) << ' ' << bitset<3>(mask1) << endl;
      //            cout << szx << ' ' << szy << endl;
      sort(vx, vx + szx + 1);
      sort(vy, vy + szy + 1);
      //            for(int i = 0; i <= szx; ++i) cout << vx[i] << ' ';
      //            cout << endl;
      //            for(int i = 0; i <= szy; ++i) cout << vy[i] << ' ';
      //            cout << endl;
      for (int i = 0; i < n; ++i) {
        if (!((mask >> i) & 1)) {
          int X = lower_bound(vx, vx + szx + 1, x[i]) - vx;
          int Y = lower_bound(vy, vy + szy + 1, y[i]) - vy;
          int mn = inf;
          if (X <= szx)
            mn = min(mn, abs(vx[X] - x[i]) * p[i]);
          if (Y <= szy)
            mn = min(mn, abs(vy[Y] - y[i]) * p[i]);
          if (X)
            X--;
          if (Y)
            Y--;
          if (X <= szx)
            mn = min(mn, abs(vx[X] - x[i]) * p[i]);
          if (Y <= szy)
            mn = min(mn, abs(vy[Y] - y[i]) * p[i]);
          cost += mn;
        }
      }
      ans[count] = min(ans[count], cost);
    }
  }
  for (int i = 0; i < n; ++i) {
    int mn = inf;
    for (int j = 0; j <= n; ++j) {
      if (i == j)
        continue;
      mn = min({mn, abs(x[j] - x[i]) * p[i], abs(y[i] - y[j]) * p[i]});
    }
    ans[n - 1] = min(ans[n - 1], mn);
  }
  for (int i = 0; i <= n; ++i) {
    cout << ans[i] << endl;
  }
}

signed main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  //    freopen("travel_restrictions_input.txt", "r", stdin);
  //    freopen("A.out", "w", stdout);
  //    int test; cin >> test;
  //    for(int t = 1; t <= test; ++t) {
  //        cout << "Case #" << t << ":" << endl;
  solve();
  //    }
  return 0;
}