Problem p03008 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = int64_t;
using uint = uint32_t;
using ulint = uint64_t;

template <class T> using vector2d = vector<vector<T>>;

template <class T> bool UpdateMax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> bool UpdateMin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> void OutVec(const vector<T> &vec) {
  for (int i = 0; i < vec.size() - 1; ++i) {
    cout << vec[i] << " ";
  }
  cout << vec.back() << endl;
}

template <class T> void OutVec2d(const vector2d<T> &vec) {
  for (auto v : vec) {
    OutVec(v);
  }
}

lint solve(lint n, lint ga, lint gb, lint sa, lint sb, lint ba, lint bb) {
  lint tmp_max_nuts = n;
  for (lint gi = 0; gi <= 5000; gi++) {
    for (lint si = 0; si <= 5000; si++) {
      lint nuts = n - gi * ga - si * sa;
      if (nuts < 0)
        continue;
      if (ba < bb) {
        lint bi = nuts / ba;
        nuts -= bi * ba;
        nuts += gi * gb + si * sb + bi * bb;
        UpdateMax(tmp_max_nuts, nuts);
      } else {
        nuts += gi * gb + si * sb;
        UpdateMax(tmp_max_nuts, nuts);
      }
    }
  }

  lint result = tmp_max_nuts;
  for (lint gi = 0; gi <= 5000; gi++) {
    for (lint si = 0; si <= 5000; si++) {
      lint nuts = tmp_max_nuts - gi * gb - si * sb;
      if (nuts < 0)
        continue;
      if (ba > bb) {
        lint bi = nuts / bb;
        nuts -= bi * bb;
        nuts += gi * ga + si * sa + bi * ba;
        UpdateMax(result, nuts);
      } else {
        nuts += gi * ga + si * sa;
        UpdateMax(result, nuts);
      }
    }
  }

  return result;
}

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  lint n, ga, gb, sa, sb, ba, bb;
  cin >> n >> ga >> sa >> ba >> gb >> sb >> bb;

  lint result = n;
  UpdateMax(result, solve(n, ga, gb, sa, sb, ba, bb));
  UpdateMax(result, solve(n, sa, sb, ba, bb, ga, gb));
  UpdateMax(result, solve(n, ba, bb, ga, gb, sa, sb));
  return result;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = int64_t;
using uint = uint32_t;
using ulint = uint64_t;

template <class T> using vector2d = vector<vector<T>>;

template <class T> bool UpdateMax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> bool UpdateMin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

template <class T> void OutVec(const vector<T> &vec) {
  for (int i = 0; i < vec.size() - 1; ++i) {
    cout << vec[i] << " ";
  }
  cout << vec.back() << endl;
}

template <class T> void OutVec2d(const vector2d<T> &vec) {
  for (auto v : vec) {
    OutVec(v);
  }
}

lint solve(lint n, lint ga, lint gb, lint sa, lint sb, lint ba, lint bb) {
  lint tmp_max_nuts = n;
  for (lint gi = 0; gi <= 5000; gi++) {
    for (lint si = 0; si <= 5000; si++) {
      lint nuts = n - gi * ga - si * sa;
      if (nuts < 0)
        continue;
      if (ba < bb) {
        lint bi = nuts / ba;
        nuts -= bi * ba;
        nuts += gi * gb + si * sb + bi * bb;
        UpdateMax(tmp_max_nuts, nuts);
      } else {
        nuts += gi * gb + si * sb;
        UpdateMax(tmp_max_nuts, nuts);
      }
    }
  }

  lint result = tmp_max_nuts;
  for (lint gi = 0; gi <= 5000; gi++) {
    for (lint si = 0; si <= 5000; si++) {
      lint nuts = tmp_max_nuts - gi * gb - si * sb;
      if (nuts < 0)
        continue;
      if (ba > bb) {
        lint bi = nuts / bb;
        nuts -= bi * bb;
        nuts += gi * ga + si * sa + bi * ba;
        UpdateMax(result, nuts);
      } else {
        nuts += gi * ga + si * sa;
        UpdateMax(result, nuts);
      }
    }
  }

  return result;
}

int main() {
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  lint n, ga, gb, sa, sb, ba, bb;
  cin >> n >> ga >> sa >> ba >> gb >> sb >> bb;

  lint result = n;
  UpdateMax(result, solve(n, ga, gb, sa, sb, ba, bb));
  UpdateMax(result, solve(n, sa, sb, ba, bb, ga, gb));
  UpdateMax(result, solve(n, ba, bb, ga, gb, sa, sb));
  cout << result << endl;

  return 0;
}