This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll n;
ll a, b, c, d;
cin >> n >> a >> b >> c >> d;
ll lcm = (a * c) / __gcd(a, c);
ll take = (n / lcm) * lcm;
ll best = min((take / a) * b, (take / c) * d);
n -= take;
ll ans = 1e18;
if(a >= c) {
assert(n / a <= 1e7);
for(int i = 0; i <= (n + a - 1) / a; i++) {
ll other = max(0ll, n - i * a + c - 1) / c;
ans = min(ans, i * b + other * d);
}
}
else {
assert(n / c <= 1e7);
for(int i = 0; i <= (n + c - 1) / c; i++) {
ll other = max(0ll, n - i * c + a - 1) / a;
ans = min(ans, i * d + other * b);
}
}
cout << ans + best;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |