/* -*- coding: utf-8 -*- * * 2099.cc: No.2099 [Cherry Alpha B] Time Machine - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_N = 100; const long long LINF = 1LL << 62; /* typedef */ typedef long long ll; /* global variables */ /* subroutines */ template T gcd(T m, T n) { // m >= 0, n >= 0 if (m < n) swap(m, n); while (n > 0) { T r = m % n; m = n; n = r; } return m; } template T lcm(T m, T n) { return m * n / gcd(m, n); } inline ll forward(ll t, int x, int a) { return t / a * x + t % a; } /* main */ int main() { int t, x, a, y, b; scanf("%d%d%d%d%d", &t, &x, &a, &y, &b); ll dd = 0; if (t < 0) { int s = (-t + b - 1) / b * b; dd = (ll)s / b * y; t += s; } int c = lcm(a, b) / b; ll mind = LINF; for (int i = 0; i < c; i++) { ll d = (ll)y * i + forward(t + (ll)b * i, x, a); mind = min(mind, d); } printf("%lld\n", mind + dd); return 0; }