Problem p03806 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = INT_MAX / 3;
const ll LINF = LLONG_MAX / 3LL;
#define CONST 1000000007
#define EPS (1e-8)
#define PB push_back
#define MP make_pair
#define sz(a) ((int)(a).size())
#define reps(i, n, m) for (int i = (n); i < int(m); i++)
#define rep(i, n) reps(i, 0, n)
#define SORT(a) sort((a).begin(), (a).end())
ll mod(ll a, ll m) { return (a % m + m) % m; }
int dx[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
    dy[9] = {1, 0, -1, 0, 1, -1, 1, -1, 0};
ll n, ma, mb;

int main() {
  vector<ll> as, bs, cs;
  cin >> n >> ma >> mb;
  ll suma = 0, sumb = 0;
  rep(i, n) {
    ll a, b, c;
    cin >> a >> b >> c;
    as.PB(a);
    bs.PB(b);
    cs.PB(c);
    suma += a;
    sumb += b;
  }

  map<P, ll> mp;
  for (ll x = 0; x < (1 << (n / 2)); ++x) {
    ll a = 0, b = 0, c = 0;
    rep(i, n / 2) {
      if ((x >> i) & 1LL) {
        a += as[i];
        b += bs[i];
        c += cs[i];
      }
    }
    if (mp[P(a, b)] == 0 || mp[P(a, b)] > c) {
      mp[P(a, b)] = c;
    }
  }

  ll ans = LINF;

  for (ll x = 0; x < (1 << (n - n / 2)); ++x) {
    ll a = 0, b = 0, c = 0;
    rep(i, n - n / 2) {
      if ((x >> i) & 1LL) {
        a += as[i + n / 2];
        b += bs[i + n / 2];
        c += cs[i + n / 2];
      }
    }

    for (int i = 1;; i++) {
      if (suma < i * ma || sumb < i * mb)
        break;
      ll xa = i * ma;
      ll xb = i * mb;
      ll da = xa - a;
      ll db = xb - b;

      if (da == 0 && db == 0) {
        ans = min(ans, c);
        continue;
      }
      if (da <= 0 || db <= 0) {
        continue;
      }
      if (mp[P(da, db)] != 0) {
        ans = min(ans, mp[P(da, db)] + c);
      }
    }
  }

  cout << ((ans == LINF) ? -1 : ans) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = INT_MAX / 3;
const ll LINF = LLONG_MAX / 3LL;
#define CONST 1000000007
#define EPS (1e-8)
#define PB push_back
#define MP make_pair
#define sz(a) ((int)(a).size())
#define reps(i, n, m) for (int i = (n); i < int(m); i++)
#define rep(i, n) reps(i, 0, n)
#define SORT(a) sort((a).begin(), (a).end())
ll mod(ll a, ll m) { return (a % m + m) % m; }
int dx[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
    dy[9] = {1, 0, -1, 0, 1, -1, 1, -1, 0};
ll n, ma, mb;

int main() {
  vector<ll> as, bs, cs;
  cin >> n >> ma >> mb;
  ll suma = 0, sumb = 0;
  rep(i, n) {
    ll a, b, c;
    cin >> a >> b >> c;
    as.PB(a);
    bs.PB(b);
    cs.PB(c);
    suma += a;
    sumb += b;
  }

  map<P, ll> mp;
  for (ll x = 0; x < (1 << (n / 2)); ++x) {
    ll a = 0, b = 0, c = 0;
    rep(i, n / 2) {
      if ((x >> i) & 1LL) {
        a += as[i];
        b += bs[i];
        c += cs[i];
      }
    }
    if (mp[P(a, b)] == 0 || mp[P(a, b)] > c) {
      mp[P(a, b)] = c;
    }
  }

  ll ans = LINF;

  for (ll x = 0; x < (1 << (n - n / 2)); ++x) {
    ll a = 0, b = 0, c = 0;
    rep(i, n - n / 2) {
      if ((x >> i) & 1LL) {
        a += as[i + n / 2];
        b += bs[i + n / 2];
        c += cs[i + n / 2];
      }
    }
    if (ans <= c)
      continue;

    for (int i = 1;; i++) {
      if (suma < i * ma || sumb < i * mb)
        break;
      ll xa = i * ma;
      ll xb = i * mb;
      ll da = xa - a;
      ll db = xb - b;

      if (da == 0 && db == 0) {
        ans = min(ans, c);
        continue;
      }
      if (da <= 0 || db <= 0) {
        continue;
      }
      if (mp[P(da, db)] != 0) {
        ans = min(ans, mp[P(da, db)] + c);
      }
    }
  }

  cout << ((ans == LINF) ? -1 : ans) << endl;

  return 0;
}