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

#include <algorithm>
#include <array>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

struct Point {
  int x, y;
};
struct City {
  Point pos;
  int population;
};
int pow(int base, int e) { return e == 0 ? 1 : base * pow(base, e - 1); }
std::vector<long long int> cal_min_length(const vector<City> &cities,
                                          const vector<int> &row_pos,
                                          const vector<int> &col_pos) {
  vector<long long int> result;
  for (const auto &ci : cities) {
    int min_sum = min(abs(ci.pos.x), abs(ci.pos.y));
    for (const auto r : row_pos) {
      min_sum = min<int>(min_sum, abs(cities[r].pos.y - ci.pos.y));
    }
    for (const auto c : col_pos) {
      min_sum = min<int>(min_sum, abs(cities[c].pos.x - ci.pos.x));
    }
    result.push_back((long long int)min_sum * ci.population);
  }
  return result;
}
int main() {
  int n;
  cin >> n;
  vector<City> cities(n);
  for (auto &c : cities) {
    cin >> c.pos.x >> c.pos.y >> c.population;
  }
  vector<vector<long long int>> min_for_row, min_for_col;
  min_for_row.reserve(1 << n);
  min_for_col.reserve(1 << n);
  const auto max = pow(3, n);
  vector<int> row, col;
  for (auto pat = 0; pat < (1 << n); ++pat) {
    row.clear();
    auto copy = pat;
    int count{0};
    for (auto i = 0; i < n; ++i) {
      if (copy & 1) {
        row.push_back(i);
        ++count;
      }
      copy >>= 1;
    }
    min_for_row.push_back(cal_min_length(cities, row, col));
    min_for_col.push_back(cal_min_length(cities, col, row));
  }
  vector<long long int> result(n + 1, LLONG_MAX);
  for (auto pat = 0; pat < max; ++pat) {
    int r{0}, c{0};
    auto copy = pat;
    int count{0};
    for (auto i = 0; i < n; ++i) {
      c <<= 1;
      r <<= 1;
      if (copy % 3 == 2) {
        ++c;
        ++count;
      } else if (copy % 3 == 1) {
        ++r;
        ++count;
      }
      copy /= 3;
    }
    long long int sum = 0;
    for (auto i = 0; i < n; ++i) {
      sum += min(min_for_row[r][i], min_for_col[c][i]);
    }
    result[count] = min(result[count], sum);
  }
  for (const auto r : result) {
    cout << r << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

struct Point {
  int x, y;
};
struct City {
  Point pos;
  int population;
};
int pow(int base, int e) { return e == 0 ? 1 : base * pow(base, e - 1); }
long long int cal_min_length(const vector<City> &cities,
                             const vector<int> &row_pos,
                             const vector<int> &col_pos) {
  long long int result{0};
  for (const auto &ci : cities) {
    int min_sum = min(abs(ci.pos.x), abs(ci.pos.y));
    for (const auto r : row_pos) {
      min_sum = min<int>(min_sum, abs(cities[r].pos.y - ci.pos.y));
    }
    for (const auto c : col_pos) {
      min_sum = min<int>(min_sum, abs(cities[c].pos.x - ci.pos.x));
    }
    result += (long long int)min_sum * ci.population;
  }
  return result;
}
int main() {
  int n;
  cin >> n;
  vector<City> cities(n);
  for (auto &c : cities) {
    cin >> c.pos.x >> c.pos.y >> c.population;
  }
  vector<long long int> min_length(n + 1, LLONG_MAX);
  const auto max = pow(3, n);
  vector<int> row, col;
  for (auto pat = 0; pat < max; ++pat) {
    row.clear();
    col.clear();
    auto copy = pat;
    int count{0};
    for (auto i = 0; i < n; ++i) {
      switch (copy % 3) {
      case 1:
        row.push_back(i);
        ++count;
        break;
      case 2:
        col.push_back(i);
        ++count;
        break;
      default:
        break;
      }
      copy /= 3;
    }
    min_length[count] =
        min(min_length[count], cal_min_length(cities, row, col));
  }
  for (const auto min : min_length) {
    cout << min << '\\n';
  }
}