HTML & JS
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define f(i, x, y) for(int i = x; i < y; i++)
#define all(X) X.begin(), X.end()
#define int long long
#define key pair<int, int>
#define ff first
#define ss second
const  int mod = 1e9 + 7;
const int inf = 1e18;
using namespace std;

int h1, h2, w1, w2;

int calc(int c)
{
    int ans = 0;
    for(int i = 0; i <= c; i += w1)
    {
        int t = (c - i)/w2;
        //cout<<t<<" ";
        ans = max(ans, (i/w1)*h1 + t*h2);
    }
    return ans;
}

main()
{
    int c; cin>>c>>h1>>h2>>w1>>w2;
    if(w1 < w2)
    {
        swap(w1, w2); swap(h1, h2);
    }
    int lcm = ((w1*w2)/(__gcd(w1, w2)));
    
    int ans = 0; int t1 = lcm/w1 , t2 = lcm/w2;
    if(t1*h1 > t2*h2)
        ans += (c/lcm)*t1*h1;
    else 
        ans += (c/lcm)*t2*h2;
    
    c -= (c/lcm)*lcm;
//    cout<<c<<" ";
    ans += calc(c);
    
    cout<<ans;
}

Generated URL:

Output:

101
Report Bug