Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)

typedef pair<int, int> P;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

int main() {

  int n;
  vector<pair<double, pair<ll, ll>>> p;

  cin >> n;

  VLL v(6);
  vector<double> vv(6);

  REP(i, 6) {
    cin >> v[i];
    vv[i] = v[i];
  }

  if (vv[0] != vv[3])
    p.push_back({vv[0] / vv[3], {vv[0], vv[3]}});
  if (vv[1] != vv[4])
    p.push_back({vv[1] / vv[4], {vv[1], vv[4]}});
  if (vv[2] != vv[5])
    p.push_back({vv[2] / vv[5], {vv[2], vv[5]}});

  if (p.size() == 0) {
    cout << n << endl;
    return 0;
  }
  if (p.size() == 1) {
    ll a = p[0].second.second;
    ll b = p[0].second.first;
    if (a < b)
      swap(a, b);
    ll k = n / b;
    n %= b;
    n += k * a;
    cout << n << endl;
    return 0;
  }

  sort(ALL(p));

  if (p.size() == 2) {
    if (p[0].first < 1 && p[1].first > 1) {
      ll a = p[0].second.second;
      ll b = p[0].second.first;
      ll k = n / b;
      n %= b;
      n += k * a;

      a = p[1].second.first;
      b = p[1].second.second;
      k = n / b;
      n %= b;
      n += k * a;
      cout << n << endl;
      return 0;
    }
    assert(n == 0);
    ll a = p[0].second.second;
    ll b = p[0].second.first;
    if (a < b)
      swap(a, b);
    ll c = p[1].second.second;
    ll d = p[1].second.first;
    if (c < d)
      swap(c, d);

    ll ans = 0;

    REP(i, n + 1) {
      REP(j, n + 1) {
        if (b * i + d * j > n)
          break;
        ll cur = n + (a - b) * i + (c - d) * j;

        ans = max(ans, cur);
      }
    }
    cout << ans << endl;
    return 0;
  }

  if (p[0].first > 1 || p[2].first < 1) {

    ll a = p[0].second.second;
    ll b = p[0].second.first;
    if (a < b)
      swap(a, b);
    ll c = p[1].second.second;
    ll d = p[1].second.first;
    if (c < d)
      swap(c, d);
    ll e = p[2].second.second;
    ll f = p[2].second.first;
    if (e < f)
      swap(e, f);

    ll ans = 0;

    REP(i, n + 1) {
      REP(j, n + 1) {
        if (b * i + d * j > n)
          break;
        ll k = (n - b * i - d * j) / f;
        ll cur = n + (a - b) * i + (c - d) * j + (e - f) * k;
        ans = max(ans, cur);
      }
    }

    cout << ans << endl;
    return 0;
  }
  //=========================================================
  if (p[1].first < 1) {
    ll a = p[0].second.second;
    ll b = p[0].second.first;
    if (a < b)
      swap(a, b);
    ll c = p[1].second.second;
    ll d = p[1].second.first;
    if (c < d)
      swap(c, d);

    ll ans = 0;

    REP(i, n + 50) {
      REP(j, n + 50) {
        if (((b * i) + (d * j)) > n)
          break;
        ll cur = n + (a - b) * i + (c - d) * j;
        ans = max(ans, cur);
      }
    }

    ll ansa = 0;
    ll e = p[2].second.second;
    ll f = p[2].second.first;
    if (e < f)
      swap(e, f);
    ll k = ans / f;
    ansa = ans + (e - f) * k;
    cout << ansa << endl;
    return 0;
  }
  //=====================================================

  if (p[1].first > 1) {
    ll a = p[0].second.second;
    ll b = p[0].second.first;
    if (a < b)
      swap(a, b);
    ll k = n / b;
    n %= b;
    n += k * a;

    ll c = p[1].second.second;
    ll d = p[1].second.first;
    if (c < d)
      swap(c, d);
    ll e = p[2].second.second;
    ll f = p[2].second.first;
    if (e < f)
      swap(e, f);

    ll ans = 0;

    REP(i, n + 1) {
      if (d * i > n)
        break;
      ll j = (n - d * i) / f;
      ll cur = n + (c - d) * i + (e - f) * j;
      ans = max(ans, cur);
    }
    cout << ans << endl;

    return 0;
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)

typedef pair<int, int> P;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1 << 30;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

int main() {

  int n;
  vector<pair<double, pair<ll, ll>>> p;

  cin >> n;

  VLL v(6);
  vector<double> vv(6);

  REP(i, 6) {
    cin >> v[i];
    vv[i] = v[i];
  }

  if (vv[0] != vv[3])
    p.push_back({vv[0] / vv[3], {vv[0], vv[3]}});
  if (vv[1] != vv[4])
    p.push_back({vv[1] / vv[4], {vv[1], vv[4]}});
  if (vv[2] != vv[5])
    p.push_back({vv[2] / vv[5], {vv[2], vv[5]}});

  if (p.size() == 0) {
    cout << n << endl;
    return 0;
  }
  if (p.size() == 1) {
    ll a = p[0].second.second;
    ll b = p[0].second.first;
    if (a < b)
      swap(a, b);
    ll k = n / b;
    n %= b;
    n += k * a;
    cout << n << endl;
    return 0;
  }

  sort(ALL(p));

  if (p.size() == 2) {
    if (p[0].first < 1 && p[1].first > 1) {
      ll a = p[0].second.second;
      ll b = p[0].second.first;
      ll k = n / b;
      n %= b;
      n += k * a;

      a = p[1].second.first;
      b = p[1].second.second;
      k = n / b;
      n %= b;
      n += k * a;
      cout << n << endl;
      return 0;
    }
    ll a = p[0].second.second;
    ll b = p[0].second.first;
    if (a < b)
      swap(a, b);
    ll c = p[1].second.second;
    ll d = p[1].second.first;
    if (c < d)
      swap(c, d);

    ll ans = 0;

    REP(i, n + 1) {
      REP(j, n + 1) {
        if (b * i + d * j > n)
          break;
        ll cur = n + (a - b) * i + (c - d) * j;

        ans = max(ans, cur);
      }
    }
    cout << ans << endl;
    return 0;
  }

  if (p[0].first > 1 || p[2].first < 1) {

    ll a = p[0].second.second;
    ll b = p[0].second.first;
    if (a < b)
      swap(a, b);
    ll c = p[1].second.second;
    ll d = p[1].second.first;
    if (c < d)
      swap(c, d);
    ll e = p[2].second.second;
    ll f = p[2].second.first;
    if (e < f)
      swap(e, f);

    ll ans = 0;

    REP(i, n + 1) {
      REP(j, n + 1) {
        if (b * i + d * j > n)
          break;
        ll k = (n - b * i - d * j) / f;
        ll cur = n + (a - b) * i + (c - d) * j + (e - f) * k;
        ans = max(ans, cur);
      }
    }

    cout << ans << endl;
    return 0;
  }
  //=========================================================
  if (p[1].first < 1) {
    ll a = p[0].second.second;
    ll b = p[0].second.first;
    if (a < b)
      swap(a, b);
    ll c = p[1].second.second;
    ll d = p[1].second.first;
    if (c < d)
      swap(c, d);

    ll ans = 0;

    REP(i, n + 50) {
      REP(j, n + 50) {
        if (((b * i) + (d * j)) > n)
          break;
        ll cur = n + (a - b) * i + (c - d) * j;
        ans = max(ans, cur);
      }
    }

    ll ansa = 0;
    ll e = p[2].second.second;
    ll f = p[2].second.first;
    if (e < f)
      swap(e, f);
    ll k = ans / f;
    ansa = ans + (e - f) * k;
    cout << ansa << endl;
    return 0;
  }
  //=====================================================

  if (p[1].first > 1) {
    ll a = p[0].second.second;
    ll b = p[0].second.first;
    if (a < b)
      swap(a, b);
    ll k = n / b;
    n %= b;
    n += k * a;

    ll c = p[1].second.second;
    ll d = p[1].second.first;
    if (c < d)
      swap(c, d);
    ll e = p[2].second.second;
    ll f = p[2].second.first;
    if (e < f)
      swap(e, f);

    ll ans = 0;

    REP(i, n + 1) {
      if (d * i > n)
        break;
      ll j = (n - d * i) / f;
      ll cur = n + (c - d) * i + (e - f) * j;
      ans = max(ans, cur);
    }
    cout << ans << endl;

    return 0;
  }
}