#ifndef _DEBUG #define _DEBUG #include __FILE__ int main() { long A, B, N, M; cin >> A >> B >> N >> M; auto judge = [&](const auto& mid) { auto a = A; auto b = B; auto n = N; auto m = M; if (a < mid) { auto tmp = mid - a; if (b / m < tmp) return false; a += tmp; b -= tmp * m; } if (b < mid) { auto tmp = mid - b; if (a / n < tmp) return false; b += tmp; a -= tmp * n; } return a >= mid && b >= mid; }; long ok = 0, ng = LONG_MAX / 2; while (abs(ok - ng) > 1) { auto mid = (ok + ng) / 2; judge(mid) ? ok = mid : ng = mid; } cout << ok << '\n'; } #else // #undef _DEBUG #include using namespace std; template bool cmin(T& a, const T2& b) { return a > b ? a = b, true : false; } template bool cmax(T& a, const T2& b) { return a < b ? a = b, true : false; } __attribute__((constructor)) void ios_setup() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } template ostream& operator<<(ostream& os, const vector& vec); template ostream& operator<<(ostream& os, const vector >& vec); template ostream& operator<<(ostream& os, const vector > >& vec); template ostream& operator<<(ostream& os, const pair& p) { return os << "(" << p.first << ", " << p.second << ")"; } template ostream& operator<<(ostream& os, const tuple& t) { const auto& [a, b, c] = t; return os << "(" << a << ", " << b << ", " << c << ")"; } template ostream& operator<<(ostream& os, const tuple& t) { const auto& [a, b, c, d] = t; return os << "(" << a << ", " << b << ", " << c << ", " << d << ")"; } template ostream& operator<<(ostream& os, priority_queue pq) { os << "{"; while (!pq.empty()) os << pq.top() << (pq.size() > 1 ? " " : ""), pq.pop(); return os << "}"; } #define FOREACH(it, a) for (auto it = (a).begin(); it != (a).end(); it++) template ostream& operator<<(ostream& os, const set& se) { os << "{"; FOREACH (it, se) { os << (it == se.begin() ? "" : " ") << *it; } return os << "}"; } template ostream& operator<<(ostream& os, const multiset& ms) { os << "{"; FOREACH (it, ms) { os << (it == ms.begin() ? "" : " ") << *it; } return os << "}"; } template ostream& operator<<(ostream& os, const map& ma) { os << "{"; FOREACH (it, ma) { os << (it == ma.begin() ? "" : " ") << *it; } return os << "}"; } template ostream& operator<<(ostream& os, const vector& vec) { os << "{"; FOREACH (it, vec) { os << (it == vec.begin() ? "" : " ") << *it; } return os << "}"; } template ostream& operator<<(ostream& os, const vector >& vec) { os << "{\n"; FOREACH (it, vec) { os << (it == vec.begin() ? " " : "\n ") << *it; } return os << "\n}"; } template ostream& operator<<(ostream& os, const vector > >& vec) { os << "{\n"; FOREACH (it, vec) { os << (it == vec.begin() ? "" : "\n") << "(" << it - vec.begin() << ") " << *it; } return os << "\n}"; } void dump() {} template void dump(const Head& head, const Tail&... tail) { cout << head << (sizeof...(tail) ? ", " : "\n"), dump(tail...); } #ifdef _DEBUG #define DUMP(...) cout << "[" << (#__VA_ARGS__) << "] ", dump(__VA_ARGS__) #else #define DUMP(...) (void)0 #endif #endif