Problem p02604 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v, a) (reverse((v), (v) + (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

int N;
int X[16];
int Y[16];
int P[16];

int x_r[16];
int y_r[16];
int x_r_cnt = 0;
int y_r_cnt = 0;

int len() {
  int sum = 0;

  for (int i = 0; i < N; i++) {
    int mi = LLONG_MAX;
    for (int j = 0; j < x_r_cnt; j++) {
      int sub = abs(x_r[j] - X[i]);
      mi = min(mi, sub);
    }
    for (int j = 0; j < y_r_cnt; j++) {
      int sub = abs(y_r[j] - Y[i]);
      mi = min(mi, sub);
    }

    sum += mi * P[i];
  }

  return sum;
}

int ans;
vector<int> kumi;

void check() {
  int max = (int)pow(2, kumi.size());
  for (int i = 0; i < max; i++) {
    x_r[0] = 0;
    y_r[0] = 0;
    x_r_cnt = 1;
    y_r_cnt = 1;

    for (int j = 0; j < kumi.size(); j++) {
      if ((i & (1LL << j)) == 0) {
        x_r[x_r_cnt] = X[kumi[j]];
        x_r_cnt++;
      } else {
        y_r[y_r_cnt] = Y[kumi[j]];
        y_r_cnt++;
      }
    }

    int l = len();
    ans = min(ans, l);
  }
}

void func(int rest, int index) {
  if (rest == 0) {
    check();
    return;
  }

  for (int i = index; i < N; i++) {
    kumi.push_back(i);
    func(rest - 1, i + 1);
    kumi.pop_back();
  }
}

signed main() {
  cin >> N;
  REP(i, N) cin >> X[i] >> Y[i] >> P[i];

  for (int i = 0; i <= N; i++) {
    ans = LLONG_MAX;
    func(i, 0);
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v, a) (reverse((v), (v) + (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

int N;
int X[16];
int Y[16];
int P[16];

int len(vector<int> &x_r, vector<int> &y_r) {
  int sum = 0;

  for (int i = 0; i < N; i++) {
    int mi = INT_MAX;
    for (int x : x_r) {
      int sub = abs(x - X[i]);
      mi = min(mi, sub);
    }
    for (int y : y_r) {
      int sub = abs(y - Y[i]);
      mi = min(mi, sub);
    }

    sum += mi * P[i];
  }

  return sum;
}

int ans;
vector<int> kumi;

void check() {
  int max = pow(2, kumi.size());
  for (int i = 0; i < max; i++) {
    vector<int> x_r;
    vector<int> y_r;
    x_r.push_back(0);
    y_r.push_back(0);

    for (int j = 0; j < kumi.size(); j++) {
      if ((i & (1LL << j)) == 0) {
        x_r.push_back(X[kumi[j]]);
      } else {
        y_r.push_back(Y[kumi[j]]);
      }
    }

    int l = len(x_r, y_r);
    ans = min(ans, l);
  }
}

int func(int rest, int index) {
  if (rest == 0) {
    check();
    return 0;
  }

  for (int i = index; i < N; i++) {
    kumi.push_back(i);
    func(rest - 1, i + 1);
    kumi.pop_back();
  }
}

signed main() {
  cin >> N;
  REP(i, N) cin >> X[i] >> Y[i] >> P[i];

  for (int i = 0; i <= N; i++) {
    ans = INT_MAX;
    func(i, 0);
    cout << ans << endl;
  }

  // vector<int> x_r;
  // vector<int> y_r;
  // x_r.push_back(0);
  // y_r.push_back(0);
  // y_r.push_back(4);
  // cout << len(x_r, y_r) << endl;
}