#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; using lint = long long int; using P = pair; using PL = pair; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define ALL(a) (a).begin(),(a).end() constexpr int MOD = 1000000007; constexpr lint B1 = 1532834020; constexpr lint M1 = 2147482409; constexpr lint B2 = 1388622299; constexpr lint M2 = 2147478017; constexpr int INF = 2147483647; void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";} templatevoid chmax(T &a, const T &b) { if (avoid chmin(T &a, const T &b) { if (b> T >> X >> A >> Y >> B; vector dp(40000001, 1e18); dp[20000000] = 0; set st; st.insert(PL(0, 20000000)); lint ans = 1e18; while(!st.empty()) { PL p = *st.begin(); st.erase(p); lint x = p.second; if(x <= 20000000+T) chmin(ans, dp[x] + abs(20000000+T-x)); if(dp[x] > ans) break; if(x+A < 40000001 && dp[x] + X < dp[x+A]) { dp[x+A] = dp[x] + X; st.insert(PL(dp[x+A], x+A)); } if(x-B >= 0 && dp[x] + Y < dp[x-B]) { dp[x-B] = dp[x] + Y; st.insert(PL(dp[x-B], x-B)); } } cout << ans << endl; }