Language: C++

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i,n) for(int i=0;i<n;++i)
#define ALL(name) name.begin(),name.end()
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 100010
#define SUBMIT

ll N;
ll Ga, Sa, Ba, Gb, Sb, Bb;

// どんぐり m 個もっていて、まず x 個のどんぐりを貴金属 1 個に換えられて、
// そのご貴金属 1 個を y 個のどんぐりに換えられるときの最終的な最大のどんぐりの数
static inline GetDonguri(ll m, ll x, ll y) { return max(m, (m / x) * y + (m % x)); }

// B, S, B のうち bool が true のものだけチェック
// どんぐりを最初 n こもっているときの、最大化可能数
// isAtoB が true なら A -> B をチェック、そうでないなら B -> A をチェック
ll check(bool isG, bool isS, bool isB, ll n, bool isAtoB) {
    int checkCnt = static_cast<ll>(isG) + static_cast<ll>(isS) + static_cast<ll>(isB);
    ll Gx = Ga, Gy = Gb;
    ll Sx = Sa, Sy = Sb;
    ll Bx = Ba, By = Bb;
    if(!isAtoB) {
        swap(Gx, Gy);
        swap(Sx, Sy);
        swap(Bx, By);
    }

    ll ret = n;
    if(checkCnt == 1) {
        for(ll i = 0; i <= n; ++i) {
            ll cand = n - i;
            if(isG) { cand += GetDonguri(i, Gx, Gy); }
            else if(isS) { cand += GetDonguri(i, Sx, Sy); }
            else if(isB) { cand += GetDonguri(i, Bx, By); }
            ret = max(ret, cand);
        }
    }
    else if(checkCnt == 2) {
        for(ll i = 0; i <= n; ++i) {
            ll j = n - i;
            ll k = n - i - j;
            if(isG && isS) {
                ret = max(ret, GetDonguri(i, Gx, Gy), GetDonguri(j, Sx, Sy), GetDonguri(k, Bx, By));
            }
            else if(isS && isB) {
                ret = max(ret, GetDonguri(k, Gx, Gy), GetDonguri(j, Sx, Sy), GetDonguri(i, Bx, By));
            }
            else if(isG && isB){
                ret = max(ret, GetDonguri(j, Gx, Gy), GetDonguri(k, Sx, Sy), GetDonguri(i, Bx, By));
            }
        }
    }
    else if(checkCnt == 3) {
        for(ll i = 0; i <= n; ++i) {
            for(ll j = 0; j <= (n - i); ++j) {
                ll k = n - i - j;
                if(isG && isS) {
                    ret = max(ret, GetDonguri(i, Gx, Gy), GetDonguri(j, Sx, Sy), GetDonguri(k, Bx, By));
                }
                else if(isS && isB) {
                    ret = max(ret, GetDonguri(k, Gx, Gy), GetDonguri(j, Sx, Sy), GetDonguri(i, Bx, By));
                }
                else if(isG && isB){
                    ret = max(ret, GetDonguri(j, Gx, Gy), GetDonguri(k, Sx, Sy), GetDonguri(i, Bx, By));
                }
                ret = max(ret, cand);
            }
        }
    }
    return ret;
}

ll solve() {
    // A -> B でチェックするべきかどうか
    bool isG = (Ga <= Gb), isS = (Sa <= Sb), isB = (Ba <= Bb);

    ll ans = check(isG, isS, isB, N, true);

    // A -> B をチェックしなかったものだけ B -> A をチェック
    ans = check(!isG, !isS, !isB, ans, false);

    return ans;
}

signed main() {
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(false);

    cin >> N >> Ga >> Sa >> Ba >> Gb >> Sb >> Bb;
    printf("%lld\n", solve());

    return 0;
}


Compiler output:
a.cc:30:15: error: ISO C++ forbids declaration of 'GetDonguri' with no type [-fpermissive]
   30 | static inline GetDonguri(ll m, ll x, ll y) { return max(m, (m / x) * y + (m % x)); }
      |               ^~~~~~~~~~
a.cc: In function 'll check(bool, bool, bool, ll, bool)':
a.cc:61:26: error: no matching function for call to 'max(ll&, int, int, int)'
   61 |                 ret = max(ret, GetDonguri(i, Gx, Gy), GetDonguri(j, Sx, Sy), GetDonguri(k, Bx, By));
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 4 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 4 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   candidate expects 2 arguments, 4 provided
a.cc:64:26: error: no matching function for call to 'max(ll&, int, int, int)'
   64 |                 ret = max(ret, GetDonguri(k, Gx, Gy), GetDonguri(j, Sx, Sy), GetDonguri(i, Bx, By));
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 4 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   candidate expects 2 arguments, 4 provided
a.cc:67:26: error: no matching function for call to 'max(ll&, int, int, int)'
   67 |                 ret = max(ret, GetDonguri(j, Gx, Gy), GetDonguri(k, Sx, Sy), GetDonguri(i, Bx, By));
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 4 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   candidate expects 2 arguments, 4 provided
a.cc:76:30: error: no matching function for call to 'max(ll&, int, int, int)'
   76 |                     ret = max(ret, GetDonguri(i, Gx, Gy), GetDonguri(j, Sx, Sy), GetDonguri(k, Bx, By));
      |                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 4 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   candidate expects 2 arguments, 4 provided
a.cc:79:30: error: no matching function for call to 'max(ll&, int, int, int)'
   79 |                     ret = max(ret, GetDonguri(k, Gx, Gy), GetDonguri(j, Sx, Sy), GetDonguri(i, Bx, By));
      |                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 4 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   candidate expects 2 arguments, 4 provided
a.cc:82:30: error: no matching function for call to 'max(ll&, int, int, int)'
   82 |                     ret = max(ret, GetDonguri(j, Gx, Gy), GetDonguri(k, Sx, Sy), GetDonguri(i, Bx, By));
      |                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 4 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 4 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   candidate expects 2 arguments, 4 provided
a.cc:84:32: error: 'cand' was not declared in this scope; did you mean 'rand'?
   84 |                 ret = max(ret, cand);
      |                                ^~~~
      |                                rand