Language: C++

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using D = double;
using uint = unsigned int;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second

__int128 abs(__int128 a)
{
	return a > 0 ? a : -a;
}

__int128 gcd(__int128 a, __int128 b)
{
	return a == 0 ? b : gcd(b % a, a);
}

struct frac
{
	__int128 up, down;
	
	frac() {}
	frac(__int128 up_, __int128 down_)
	{
		up = up_;
		down = down_;
		
		__int128 g = gcd(abs(up), abs(down));
		up /= g;
		down /= g;
		if (down < 0)
		{
			up *= -1;
			down *= -1;
		}
	}
};

inline frac operator+(const frac &a, const frac &b)
{
	return frac(a.up * b.down + a.down * b.up, a.down * b.down);
}

inline bool operator<(const frac &a, const frac &b)
{
	return a.up * b.down < b.up * a.down;
}

const int maxn = 100005;
const ll inf = 1e18;

vector<pair2<int>> fast, slow;
ll sumbfast[maxn];
int n;
ll totald;

frac solvefast(ll tA)
{
	int pos = lower_bound(sumbfast, sumbfast + (int)fast.size(), -totald - tA) - sumbfast;
	if (pos != 0)
	{
		ll tB2 = sumbfast[pos - 1];
		ll up = -totald - tA - tB2;
		ll down = fast[pos - 1].se;
		up = min(up, down);
		assert(up >= 0);
		return frac(pos - 1, n) + frac(up, n * down);
	}
	return frac(0, 1);
}

int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		if (x <= y) fast.pb({x, y});
		else slow.pb({x, y});
	}
	sort(all(fast), [&] (pair2<int> &a, pair2<int> &b) {return a.se < b.se; });
	sumbfast[0] = 0;
	for (int i = 0; i < (int)fast.size(); i++) sumbfast[i + 1] = sumbfast[i] + fast[i].se;
	sort(all(slow), [&] (pair2<int> &a, pair2<int> &b) {return a.fi < b.fi; });
	
	totald = 0;
	for (auto t : fast) totald += t.fi - t.se;
	
	ll totaldslow = 0;
	ll minB = inf;
	ll tB = 0;
	ll tA = 0;
	frac answer = solvefast(0);
	for (int i = 0; i < (int)slow.size(); i++)
	{
		totaldslow += slow[i].fi - slow[i].se;
		tB += slow[i].se;
		minB = min(minB, (ll)slow[i].se);
		if (tB > -(totald + totaldslow)) break;
		ll up = -(totald + totaldslow) - tB;
		up = min(up, (ll)minB);
		frac curans = frac(i, n) + frac(up, n * minB);
		//cout << i << ' ' << curans.up << ' ' << curans.down << endl;
		//cout << totald << ' ' << totaldslow << ' ' << tB << ' ' << minB << endl;
		//cout << up << ' ' << frac(up, n * minB).up << endl;
		
		tA += slow[i].fi;
		curans = curans + solvefast(tA);
		//cout << i << ' ' << curans.up << ' ' << curans.down << endl;
		if (answer < curans) answer = curans;
	}
	printf("%lld %lld\n", (ll)answer.up, (ll)answer.down);
    return 0;
}


Compiler output:
a.cc: In constructor 'frac::frac(__int128, __int128)':
a.cc:48:37: error: call of overloaded 'abs(__int128&)' is ambiguous
   48 |                 __int128 g = gcd(abs(up), abs(down));
      |                                  ~~~^~~~
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:42,
                 from a.cc:1:
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:28:10: note: candidate: '__int128 abs(__int128)'
   28 | __int128 abs(__int128 a)
      |          ^~~
In file included from /usr/include/c++/14/cstdlib:81:
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~
a.cc:48:46: error: call of overloaded 'abs(__int128&)' is ambiguous
   48 |                 __int128 g = gcd(abs(up), abs(down));
      |                                           ~~~^~~~~~
/usr/include/stdlib.h:980:12: note: candidate: 'int abs(int)'
  980 | extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
      |            ^~~
a.cc:28:10: note: candidate: '__int128 abs(__int128)'
   28 | __int128 abs(__int128 a)
      |          ^~~
/usr/include/c++/14/bits/std_abs.h:137:3: note: candidate: 'constexpr __float128 std::abs(__float128)'
  137 |   abs(__float128 __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:85:3: note: candidate: 'constexpr __int128 std::abs(__int128)'
   85 |   abs(__GLIBCXX_TYPE_INT_N_0 __x) { return __x >= 0 ? __x : -__x; }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:79:3: note: candidate: 'constexpr long double std::abs(long double)'
   79 |   abs(long double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:75:3: note: candidate: 'constexpr float std::abs(float)'
   75 |   abs(float __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:71:3: note: candidate: 'constexpr double std::abs(double)'
   71 |   abs(double __x)
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:61:3: note: candidate: 'long long int std::abs(long long int)'
   61 |   abs(long long __x) { return __builtin_llabs (__x); }
      |   ^~~
/usr/include/c++/14/bits/std_abs.h:56:3: note: candidate: 'long int std::abs(long int)'
   56 |   abs(long __i) { return __builtin_labs(__i); }
      |   ^~~