Problem:
 Score : 1100 points    Problem Statement We have N balance beams numbered 1 to N .
The length of each beam is 1 meters.
Snuke walks on Beam i at a speed of 1/A_i meters per second, and Ringo walks on Beam i at a speed of 1/B_i meters per second.  Snuke and Ringo will play the following game:   First, Snuke connects the N beams in any order of his choice and makes a long beam of length N meters.  Then, Snuke starts at the left end of the long beam. At the same time, Ringo starts at a point chosen uniformly at random on the long beam. Both of them walk to the right end of the long beam.  Snuke wins if and only if he catches up to Ringo before Ringo reaches the right end of the long beam. That is, Snuke wins if there is a moment when Snuke and Ringo stand at the same position, and Ringo wins otherwise.   Find the probability that Snuke wins when Snuke arranges the N beams so that the probability of his winning is maximized.  This probability is a rational number, so we ask you to represent it as an irreducible fraction P/Q (to represent 0 , use P=0, Q=1 ).      Constraints  1 \leq N \leq 10^5  1 \leq A_i,B_i \leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A_1  B_1  A_2  B_2  \vdots  A_N  B_N       Output Print the numerator and denominator of the irreducible fraction that represents the maximum probability of Snuke's winning.        Sample Input 1 2
3 2
1 2      Sample Output 1 1 4  When the beams are connected in the order 2,1 from left to right, the probability of Snuke's winning is 1/4 , which is the maximum possible value.       Sample Input 2 4
1 5
4 7
2 1
8 4      Sample Output 2 1 2       Sample Input 3 3
4 1
5 2
6 3      Sample Output 3 0 1       Sample Input 4 10
866111664 178537096
705445072 318106937
472381277 579910117
353498483 865935868
383133839 231371336
378371075 681212831
304570952 16537461
955719384 267238505
844917655 218662351
550309930 62731178      Sample Output 4 697461712 2899550585

Solution (Python):
import heapq

class Heapq():
    def __init__(self,arr):
        self.que=[]
        for a in arr:
            self.que.append(-a)
        heapq.heapify(self.que)

    def pop(self):
        return -heapq.heappop(self.que)

    def push(self,a):
        heapq.heappush(self.que,-a)

    def top(self):
        return -self.que[0]


def segfunc(x, y):
    return min(x,y)



ide_ele = 10**15


class SegTree:
    def __init__(self, init_val, segfunc, ide_ele):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num

        for i in range(n):
            self.tree[self.num + i] = init_val[i]

        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

import sys,random

input=sys.stdin.readline

N=int(input())
beam=[tuple(map(int,input().split())) for i in range(N)]
def solve2():
    beam.sort(key=lambda x:x[1])
    data=[min(0,beam[i][0]-beam[i][1]) for i in range(N)]
    for i in range(N-2,-1,-1):
        data[i]+=data[i+1]

    cummin=[beam[i][1] for i in range(N)]
    for i in range(N):
        if beam[i][0]-beam[i][1]>0:
            cummin[i]=10**15

    for i in range(N-2,-1,-1):
        cummin[i]=min(cummin[i],cummin[i+1])

    def cond(m):
        if m==0:
            if data[0]<=0:
                return True
            else:
                return False

        que=Heapq([])
        cnt=0
        S=0
        for i in range(N-1):
            if cnt==m:
                test=que.top()
                if beam[i][0]<test:
                    que.pop()
                    que.push(beam[i][0])
                    S+=beam[i][0]-test
            else:
                que.push(beam[i][0])
                S+=beam[i][0]
                cnt+=1
            if cnt==m:
                test=S+data[i+1]
                if test<=0:
                    return True
        return False

    start=0
    end=N
    while end-start>1:
        test=(end+start)//2
        if cond(test):
            start=test
        else:
            end=test

    if not cond(0):
        exit(print(0,1))

    m=start
    ans=[0,1]
    que=Heapq([])
    cnt=0
    S=0
    if m!=0:
        for i in range(N-1):
            if cnt==m:
                test=que.top()
                if beam[i][0]<test:
                    que.pop()
                    que.push(beam[i][0])
                    S+=beam[i][0]-test
            else:
                que.push(beam[i][0])
                S+=beam[i][0]
                cnt+=1
            if cnt==m:
                test=S+data[i+1]
                B=cummin[i+1]
                if test<=0:
                    t=abs(test)
                    if B*ans[0]<ans[1]*t:
                        ans=[t,B]

        start=[-1]*N
        end=[-1]*N
        que=[]
        trash=[]
        cnt=0
        S=0
        data1=[ide_ele]*N
        data2=[ide_ele]*N
        for i in range(N):
            if cnt==m:
                val,id=que[0]
                val=-val
                if val>beam[i][0]:
                    heapq.heappop(que)
                    heapq.heappush(trash,val)
                    end[id]=i-1
                    heapq.heappush(que,(-beam[i][0],i))
                    start[i]=i
                    S+=beam[i][0]-val
                else:
                    heapq.heappush(trash,beam[i][0])
            else:
                heapq.heappush(que,(-beam[i][0],i))
                start[i]=i
                S+=beam[i][0]
                cnt+=1
            if cnt==m:
                if i!=N-1:
                    data1[i]=S+data[i+1]
                    if trash:
                        data2[i]=S+data[i+1]+trash[0]
                    else:
                        data2[i]=S+data[i+1]
                else:
                    data1[i]=S
                    data2[i]=S+trash[0]
        for i in range(N):
            if start[i]!=-1 and end[i]==-1:
                end[i]=N-1

        Seg1=SegTree(data1,segfunc,ide_ele)
        Seg2=SegTree(data2,segfunc,ide_ele)
        for i in range(m):
            if end[i]==m-1:
                temp=Seg1.query(m,N)
                temp+=beam[i][0]-beam[i][1]
                if temp<=0:
                    temp=abs(temp)
                    B=beam[i][1]
                    if B*ans[0]<ans[1]*temp:
                        ans=[temp,B]
            else:
                L,R=m,end[i]
                temp=Seg2.query(L,R+1)-beam[i][0]
                temp+=beam[i][0]-beam[i][1]
                if temp<=0:
                    temp=abs(temp)
                    B=beam[i][1]
                    if B*ans[0]<ans[1]*temp:
                        ans=[temp,B]
                temp=Seg1.query(R+1,N)
                temp+=beam[i][0]-beam[i][1]
                if temp<=0:
                    temp=abs(temp)
                    B=beam[i][1]
                    if B*ans[0]<ans[1]*temp:
                        ans=[temp,B]
        for i in range(m,N):
            if beam[i][0]-beam[i][1]<=0:
                if start[i]==-1:
                    temp=Seg1.query(i,N)
                    temp+=beam[i][0]-beam[i][1]
                    if temp<=0:
                        temp=abs(temp)
                        B=beam[i][1]
                        if B*ans[0]<ans[1]*temp:
                            ans=[temp,B]
                else:
                    L=start[i]
                    R=end[i]
                    temp=Seg2.query(L,R+1)-beam[i][0]
                    temp+=beam[i][0]-beam[i][1]
                    if temp<=0:
                        temp=abs(temp)
                        B=beam[i][1]
                        if B*ans[0]<ans[1]*temp:
                            ans=[t,B]
                    temp=Seg1.query(R+1,N)
                    temp+=beam[i][0]-beam[i][1]
                    if temp<=0:
                        temp=abs(temp)
                        B=beam[i][1]
                        if B*ans[0]<ans[1]*temp:
                            ans=[temp,B]
            else:
                if start[i]==-1:
                    temp=Seg1.query(m-1,N)
                    temp+=beam[i][0]-beam[i][1]
                    if temp<=0:
                        temp=abs(temp)
                        B=beam[i][1]
                        if B*ans[0]<ans[1]*temp:
                            ans=[temp,B]
                else:
                    L=start[i]
                    R=end[i]
                    temp=Seg2.query(L,R+1)-beam[i][0]
                    temp+=beam[i][0]-beam[i][1]
                    if temp<=0:
                        temp=abs(temp)
                        B=beam[i][1]
                        if B*ans[0]<ans[1]*temp:
                            ans=[temp,B]
                    temp=min(Seg1.query(R+1,N),Seg1.query(m-1,L))
                    temp+=beam[i][0]-beam[i][1]
                    if temp<=0:
                        temp=abs(temp)
                        B=beam[i][1]
                        if B*ans[0]<ans[1]*temp:
                            ans=[temp,B]

    else:
        for i in range(N):
            test=data[i]
            B=cummin[i]
            if test<=0:
                t=abs(test)
                if B*ans[0]<ans[1]*t:
                    ans=[t,B]
        M=min(data)
        for i in range(N):
            if beam[i][0]-beam[i][1]>0:
                temp=M+beam[i][0]-beam[i][1]
                if temp<=0:
                    temp=abs(temp)
                    B=beam[i][1]
                    if B*ans[0]<ans[1]*temp:
                        ans=[temp,B]

    p,q=ans
    res=[q*m+p,N*q]
    from math import gcd
    g=gcd(res[0],res[1])
    return (res[0]//g,res[1]//g)

print(*solve2())

Solution (Python):
import sys
input = sys.stdin.readline
def gcd(a, b):
    while b: a, b = b, a % b
    return a

N = int(input())
S = 0
Y = []
for i in range(N):
    a, b = map(int, input().split())
    if b > a:
        S += b-a
        Y.append((b, b))
    else:
        Y.append((a, b))

Y = sorted(Y)
YY = [0] * (N+1)
for i in range(N):
    YY[i+1] = YY[i] + Y[i][0]


def f(i, n):
    return S - Y[i][0] + Y[i][1] - (YY[n] if n <= i else YY[n+1] - Y[i][0])

ma1, ma2 = 0, 1
for i in range(N):
    l = 0
    r = N
    while r - l > 1:
        m = (l+r) // 2
        if f(i, m) >= 0:
            l = m
        else:
            r = m

    a = l * Y[i][1] + min(f(i, l), Y[i][1])
    b = N * Y[i][1]
    if a * ma2 > b * ma1:
        ma1, ma2 = a, b

g = gcd(ma1, ma2)
print(ma1//g, ma2//g)

Solution (Python):
import sys
input = sys.stdin.readline
def gcd(a, b):
    while b: a, b = b, a % b
    return a

N = int(input())
S = 0
Y = []
for i in range(N):
    a, b = map(int, input().split())
    if b > a:
        S += b-a
        Y.append((b, b))
    else:
        Y.append((a, b))

Y = sorted(Y)
YY = [0] * (N+1)
for i in range(N):
    YY[i+1] = YY[i] + Y[i][0]


def f(i, n):
    return S - Y[i][0] + Y[i][1] - (YY[n] if n <= i else YY[n+1] - Y[i][0])

ma1, ma2 = 0, 1
for i in range(N):
    l = 0
    r = N
    while r - l > 1:
        m = (l+r) // 2
        if f(i, m) >= 0:
            l = m
        else:
            r = m

    a = l * Y[i][1] + min(f(i, l), Y[i][1])
    b = N * Y[i][1]
    if a * ma2 > b * ma1:
        ma1, ma2 = a, b

g = gcd(ma1, ma2)
print(ma1//g, ma2//g)

Solution (Python):
import sys
input = sys.stdin.readline
def gcd(a, b):
    while b: a, b = b, a % b
    return a

N = int(input())
S = 0
Y = []
for i in range(N):
    a, b = map(int, input().split())
    if b > a:
        S += b-a
        Y.append((b, b, b-a, 0))
    else:
        Y.append((a, b, 0, a-b))

Y = sorted(Y)
YY = [0] * (N+1)
for i in range(N):
    YY[i+1] = YY[i] + Y[i][0]

def f(i, n):
    if Y[i][2]:
        return S - YY[n] if n <= i else S - (YY[n+1] - Y[i][0])
    return S - Y[i][3] - YY[n] if n <= i else S - Y[i][3] - (YY[n+1] - Y[i][0])

maa, mab = 0, 1
for i in range(N):
    l = 0
    r = N
    while r - l > 1:
        m = (l+r) // 2
        if f(i, m) >= 0:
            l = m
        else:
            r = m

    a = l * Y[i][1] + min(f(i, l), Y[i][1])
    b = N * Y[i][1]
    if a * mab > b * maa:
        maa, mab = a, b

g = gcd(maa, mab)
print(maa//g, mab//g)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from heapq import heappush, heappushpop, heapify
from fractions import gcd

N = int(readline())
m = map(int,read().split())
AB = sorted(zip(m,m),key=lambda x:(x[1],x[0]))
A,B = zip(*AB)

dp2 = [0]*N 
for i in range(N-1,0,-1):
    a = A[i]; b = B[i]
    if a<b:
        dp2[i-1] = dp2[i]+(b-a)
    else:
        dp2[i-1] = dp2[i]

def F(n):
    opt_num = -1
    opt_den = 1
    q = list(-x for x in A[:n])
    heapify(q)
    S = -sum(q)
    for i in range(n,N):
        a = A[i]; b = B[i]
        x = S+a-dp2[i]
        if 0<=x<=b:
            den = b
            num = (n+1)*den-x
            if opt_num*den < opt_den*num:
                opt_num = num; opt_den = den
        x = a
        y = -heappushpop(q,-x)
        S += x-y
    return opt_num, opt_den

opt_num = 0; opt_den = 1
left = -1 
right = N+10 
while left+1 < right:
    mid = (left+right)//2
    num,den = F(mid)
    if num==-1:
        right=mid
    else:
        left=mid
        if opt_num*den < opt_den*num:
            opt_num = num; opt_den = den

opt_den *= N
g = gcd(opt_num,opt_den)
opt_num//=g; opt_den//=g
print(opt_num,opt_den)