Problem p03806 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
//    #include <boost/multiprecision/cpp_int.hpp>
#include <functional>
//     #define int long long
#define inf 1000000007
#define pa pair<int, int>
#define ll long long
#define pal pair<double, int>
#define ppa pair<int, pa>
#define ssa pair<string, int>
#define mp make_pair
#define pb push_back
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;
// priority_queue<int, vector<int>, greater<int> > que;
class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(x * a, y * a); }
  Point operator/(double a) { return Point(x / a, y / a); }
  double absv() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

struct Segment {
  Point p1, p2;
};

double hen(Vector a) {
  if (fabs(a.x) < EPS && a.y > 0)
    return acos(0);
  else if (fabs(a.x) < EPS && a.y < 0)
    return 3 * acos(0);
  else if (fabs(a.y) < EPS && a.x < 0)
    return 2 * acos(0);
  else if (fabs(a.y) < EPS && a.x > 0)
    return 0.0;
  else if (a.y > 0)
    return acos(a.x / a.absv());
  else
    return 2 * acos(0) + acos(-a.x / a.absv());
}

string itos(int i) {
  ostringstream s;
  s << i;
  return s.str();
}

int gcd(int v, int b) {
  if (v > b)
    return gcd(b, v);
  if (v == b)
    return b;
  if (b % v == 0)
    return v;
  return gcd(v, b % v);
}
double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }

double distans(double x1, double y1, double x2, double y2) {
  double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
  return sqrt(rr);
}

//----------------kokomade tenpure------------

void no() {
  cout << "NO" << endl;
  return;
}

int a[50], b[50], c[50];

vector<pa> ve1[220];
vector<pa> ve2[220];
signed main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  if (n == 1) {
    int z, zz, zzz;
    cin >> z >> zz >> zzz;
    if (ma == z && mb == zz)
      cout << zzz << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  int r = n / 2;
  int r2 = n - r;
  for (int i = 0; i < (1 << r); i++) {
    int u = 0, uu = 0, uuu = 0;
    for (int j = 0; j < r; j++) {
      if ((i >> j) & 1) {
        u += a[j + 1];
        uu += b[j + 1];
        uuu += c[j + 1];
      }
    }
    ve1[u].pb(mp(uu, uuu));
    //	cout<<i<<endl;
  }

  for (int i = 0; i < (1 << r2); i++) {
    int u = 0, uu = 0, uuu = 0;
    for (int j = 0; j < r2; j++) {
      if ((i >> j) & 1) {
        u += a[r + 1 + j];
        uu += b[r + 1 + j];
        uuu += c[r + 1 + j];
      }
    }
    ve2[u].pb(mp(uu, uuu));
    //		cout<<i<<endl;
  }
  for (int i = 0; i <= 200; i++) {
    //	if(ve1[i].size()==0)continue;
    sort(ve1[i].begin(), ve1[i].end());
    sort(ve2[i].begin(), ve2[i].end());
  }

  int ans = inf;

  for (int i = 0; i <= 200; i++) {
    if (ve1[i].size() == 0)
      continue;
    for (int j = 0; j <= 200; j++) {

      if (ve2[j].size() == 0 || i + j == 0)
        continue;

      if ((mb * (i + j)) % ma != 0)
        continue;
      int d = (mb * (i + j)) / ma;
      int r1 = ve1[i].size(), r2 = ve2[j].size();
      int e = r2;
      //		cout<<i<<" "<<j<<" "<<d<<" "<<r1<<endl;
      for (int k = 0; k < r1; k++) {
        if (ve1[i][k].first > d)
          break;
        int f = d - ve1[i][k].first;
        //		int
        //e=lower_bound(ve2[j].begin(),ve2[j].end(),mp(f,-1))-ve2[j].begin();
        //		if(e==r2) continue;
        if (ve2[j][0].first > f)
          break;
        while (ve2[j][e - 1].first >= f) {
          e--;
          if (e == 0)
            break;
        }
        if (e == r2)
          continue;
        if (ve2[j][e].first != f)
          continue;
        ans = min(ans, ve1[i][k].second + ve2[j][e].second);
      }
    }
  }

  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;

  // else no();
  // 	cout<<ans<<endl;
  //  	printf("%.10f\n",ans[n-1]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
//    #include <boost/multiprecision/cpp_int.hpp>
#include <functional>
//     #define int long long
#define inf 1000000007
#define pa pair<int, int>
#define ll long long
#define pal pair<double, int>
#define ppa pair<int, pa>
#define ssa pair<string, int>
#define mp make_pair
#define pb push_back
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;
// priority_queue<int, vector<int>, greater<int> > que;
class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(x * a, y * a); }
  Point operator/(double a) { return Point(x / a, y / a); }
  double absv() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

struct Segment {
  Point p1, p2;
};

double hen(Vector a) {
  if (fabs(a.x) < EPS && a.y > 0)
    return acos(0);
  else if (fabs(a.x) < EPS && a.y < 0)
    return 3 * acos(0);
  else if (fabs(a.y) < EPS && a.x < 0)
    return 2 * acos(0);
  else if (fabs(a.y) < EPS && a.x > 0)
    return 0.0;
  else if (a.y > 0)
    return acos(a.x / a.absv());
  else
    return 2 * acos(0) + acos(-a.x / a.absv());
}

string itos(int i) {
  ostringstream s;
  s << i;
  return s.str();
}

int gcd(int v, int b) {
  if (v > b)
    return gcd(b, v);
  if (v == b)
    return b;
  if (b % v == 0)
    return v;
  return gcd(v, b % v);
}
double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }

double distans(double x1, double y1, double x2, double y2) {
  double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
  return sqrt(rr);
}

//----------------kokomade tenpure------------

void no() {
  cout << "NO" << endl;
  return;
}

int a[50], b[50], c[50];

vector<pa> ve1[220];
vector<pa> ve2[220];
signed main() {
  int n, ma, mb;
  cin >> n >> ma >> mb;
  if (n == 1) {
    int z, zz, zzz;
    cin >> z >> zz >> zzz;
    if (ma == z && mb == zz)
      cout << zzz << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  int r = n / 2;
  int r2 = n - r;
  for (int i = 0; i < (1 << r); i++) {
    int u = 0, uu = 0, uuu = 0;
    for (int j = 0; j < r; j++) {
      if ((i >> j) & 1) {
        u += a[j + 1];
        uu += b[j + 1];
        uuu += c[j + 1];
      }
    }
    ve1[u].pb(mp(uu, uuu));
    //	cout<<i<<endl;
  }

  for (int i = 0; i < (1 << r2); i++) {
    int u = 0, uu = 0, uuu = 0;
    for (int j = 0; j < r2; j++) {
      if ((i >> j) & 1) {
        u += a[r + 1 + j];
        uu += b[r + 1 + j];
        uuu += c[r + 1 + j];
      }
    }
    ve2[u].pb(mp(uu, uuu));
    //		cout<<i<<endl;
  }
  for (int i = 0; i <= 200; i++) {
    //	if(ve1[i].size()==0)continue;
    sort(ve1[i].begin(), ve1[i].end());
    sort(ve2[i].begin(), ve2[i].end());
  }

  int ans = inf;

  for (int i = 0; i <= 200; i++) {
    if (ve1[i].size() == 0)
      continue;
    for (int j = 0; j <= 200; j++) {

      if (ve2[j].size() == 0 || i + j == 0)
        continue;

      if ((mb * (i + j)) % ma != 0)
        continue;
      int d = (mb * (i + j)) / ma;
      int r1 = ve1[i].size(), r2 = ve2[j].size();
      int e = r2;
      //		cout<<i<<" "<<j<<" "<<d<<" "<<r1<<endl;
      for (int k = 0; k < r1; k++) {
        if (ve1[i][k].first > d)
          break;
        if (k > 0)
          if (ve1[i][k - 1].first == ve1[i][k].first)
            continue;
        int f = d - ve1[i][k].first;
        //		int
        //e=lower_bound(ve2[j].begin(),ve2[j].end(),mp(f,-1))-ve2[j].begin();
        //		if(e==r2) continue;
        if (ve2[j][0].first > f)
          break;
        while (ve2[j][e - 1].first >= f) {
          e--;
          if (e == 0)
            break;
        }
        if (e == r2)
          continue;
        if (ve2[j][e].first != f)
          continue;
        ans = min(ans, ve1[i][k].second + ve2[j][e].second);
      }
    }
  }

  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;

  // else no();
  // 	cout<<ans<<endl;
  //  	printf("%.10f\n",ans[n-1]);
  return 0;
}