Problem p03008 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

// Template Headers {{{
using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<long long>;
template <class T> istream &operator>>(istream &, vector<T> &);
template <class T> ostream &operator<<(ostream &, const vector<T> &);
template <class T, class U> istream &operator>>(istream &, pair<T, U> &);
template <class T, class U> ostream &operator<<(ostream &, const pair<T, U> &);
constexpr int INF = 0x3f3f3f3f;
constexpr ll BINF = 0x3f3f3f3f3f3f3f3fLL;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// }}}

#define int ll
int solve() {
  int n;
  cin >> n;
  vi a(3), b(3);
  cin >> a >> b;
  vpii c(3);
  for (int i = 0; i < 3; i++)
    c[i] = {a[i], b[i]};
  sort(c.begin(), c.end(),
       [&](auto l, auto r) { return l.first - l.second < r.first - r.second; });
  if (c[0].first - c[0].second >= 0) {
    cerr << "case 1" << endl;
    // all buy B, sale A
    int ans = n;
    for (int x = 0; x <= n; x++)
      for (int y = 0; y <= n; y++) {
        int rem = n - c[0].second * x - c[1].second * y;
        if (rem < 0)
          break;
        int z = rem / c[2].second;
        rem -= c[2].second * z;
        ans = max(ans, rem + x * c[0].first + y * c[1].first + z * c[2].first);
      }
    cout << ans << endl;
  } else if (c[2].first - c[2].second <= 0) {
    cerr << "case 2" << endl;
    // all buy A, sale B
    int ans = n;
    for (int x = 0; x <= n; x++)
      for (int y = 0; y <= n; y++) {
        int rem = n - c[0].first * x - c[1].first * y;
        if (rem < 0)
          break;
        int z = rem / c[2].first;
        rem -= c[2].first * z;
        ans =
            max(ans, rem + x * c[0].second + y * c[1].second + z * c[2].second);
      }
    cout << ans << endl;
  } else if (c[1].first - c[1].second >= 0) {
    cerr << "case 3" << endl;
    // -ve, +ve, +ve
    // A<B, A>B, A>B
    // first buy A sale B
    // second, third buy B sale A
    int now = n;
    int canbuy = n / c[0].first;
    now -= canbuy * c[0].first;
    now += canbuy * c[0].second;
    int ans = now;
    for (int x = 0; x <= now; x++) {
      int rem = now - 1LL * x * c[1].second;
      if (rem < 0)
        break;
      int y = rem / c[2].second;
      rem -= y * c[2].second;
      ans = max(ans, rem + x * c[1].first + y * c[2].first);
    }
    cout << ans << endl;
  } else {
    cerr << "case 4" << endl;
    // -ve, -ve, +ve
    // A<B, A<B, B>A
    int ans = n;
    for (int x = 0; x <= n; x++)
      for (int y = 0; y <= n; y++) {
        int rem = n - x * c[0].first - y * c[1].first;
        if (rem < 0)
          break;
        int now = rem + x * c[0].second + y * c[1].second;
        // cerr << x << ' ' << y << ' ' << now;
        int z = now / c[2].second;
        now -= z * c[2].second;
        ans = max(ans, now + z * c[2].first);
        cerr << ' ' << ans << endl;
      }
    cout << ans << endl;
  }
  return 0;
}

// Template Main {{{
signed main() {
  ios::sync_with_stdio(0);
  // precompute();
  // int t; cin >> t; for (int i=1;i<=t;i++)
  solve();
  // cout << (solve() ? "YES" : "NO") << endl;
  return 0;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it)
    is >> *it;
  return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto it = v.begin(); it != v.end();)
    os << *it, os << " \n"[++it == v.end()];
  return os;
}

template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ' ' << p.second;
}
// }}}

// vim: fdm=marker


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// Template Headers {{{
using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<long long>;
template <class T> istream &operator>>(istream &, vector<T> &);
template <class T> ostream &operator<<(ostream &, const vector<T> &);
template <class T, class U> istream &operator>>(istream &, pair<T, U> &);
template <class T, class U> ostream &operator<<(ostream &, const pair<T, U> &);
constexpr int INF = 0x3f3f3f3f;
constexpr ll BINF = 0x3f3f3f3f3f3f3f3fLL;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// }}}

#define int ll
int solve() {
  int n;
  cin >> n;
  vi a(3), b(3);
  cin >> a >> b;
  vpii c(3);
  for (int i = 0; i < 3; i++)
    c[i] = {a[i], b[i]};
  sort(c.begin(), c.end(),
       [&](auto l, auto r) { return l.first - l.second < r.first - r.second; });
  if (c[0].first - c[0].second >= 0) {
    cerr << "case 1" << endl;
    // all buy B, sale A
    int ans = n;
    for (int x = 0; x <= n; x++)
      for (int y = 0; y <= n; y++) {
        int rem = n - c[0].second * x - c[1].second * y;
        if (rem < 0)
          break;
        int z = rem / c[2].second;
        rem -= c[2].second * z;
        ans = max(ans, rem + x * c[0].first + y * c[1].first + z * c[2].first);
      }
    cout << ans << endl;
  } else if (c[2].first - c[2].second <= 0) {
    cerr << "case 2" << endl;
    // all buy A, sale B
    int ans = n;
    for (int x = 0; x <= n; x++)
      for (int y = 0; y <= n; y++) {
        int rem = n - c[0].first * x - c[1].first * y;
        if (rem < 0)
          break;
        int z = rem / c[2].first;
        rem -= c[2].first * z;
        ans =
            max(ans, rem + x * c[0].second + y * c[1].second + z * c[2].second);
      }
    cout << ans << endl;
  } else if (c[1].first - c[1].second >= 0) {
    cerr << "case 3" << endl;
    // -ve, +ve, +ve
    // A<B, A>B, A>B
    // first buy A sale B
    // second, third buy B sale A
    int now = n;
    int canbuy = n / c[0].first;
    now -= canbuy * c[0].first;
    now += canbuy * c[0].second;
    int ans = now;
    for (int x = 0; x <= now; x++) {
      int rem = now - 1LL * x * c[1].second;
      if (rem < 0)
        break;
      int y = rem / c[2].second;
      rem -= y * c[2].second;
      ans = max(ans, rem + x * c[1].first + y * c[2].first);
    }
    cout << ans << endl;
  } else {
    cerr << "case 4" << endl;
    // -ve, -ve, +ve
    // A<B, A<B, B>A
    int ans = n;
    for (int x = 0; x <= n; x++)
      for (int y = 0; y <= n; y++) {
        int rem = n - x * c[0].first - y * c[1].first;
        if (rem < 0)
          break;
        int now = rem + x * c[0].second + y * c[1].second;
        // cerr << x << ' ' << y << ' ' << now;
        int z = now / c[2].second;
        now -= z * c[2].second;
        ans = max(ans, now + z * c[2].first);
        // cerr << ' ' << ans << endl;
      }
    cout << ans << endl;
  }
  return 0;
}

// Template Main {{{
signed main() {
  ios::sync_with_stdio(0);
  // precompute();
  // int t; cin >> t; for (int i=1;i<=t;i++)
  solve();
  // cout << (solve() ? "YES" : "NO") << endl;
  return 0;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it)
    is >> *it;
  return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto it = v.begin(); it != v.end();)
    os << *it, os << " \n"[++it == v.end()];
  return os;
}

template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ' ' << p.second;
}
// }}}

// vim: fdm=marker