You have n devices that you want to use simultaneously.

The i-th device uses a_{i} units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·a_{i} units of power. The i-th device currently has b_{i} units of power stored. All devices can store an arbitrary amount of power.

You have a single charger that can plug to any single device. The charger will add p units of power per second to a device. This charging is continuous. That is, if you plug in a device for λ seconds, it will gain λ·p units of power. You can switch which device is charging at any arbitrary unit of time (including real numbers), and the time it takes to switch is negligible.

You are wondering, what is the maximum amount of time you can use the devices until one of them hits 0 units of power.

If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.


-----Input-----

The first line contains two integers, n and p (1 ≤ n ≤ 100 000, 1 ≤ p ≤ 10^9) — the number of devices and the power of the charger.

This is followed by n lines which contain two integers each. Line i contains the integers a_{i} and b_{i} (1 ≤ a_{i}, b_{i} ≤ 100 000) — the power of the device and the amount of power stored in the device in the beginning.


-----Output-----

If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.

Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 4}.

Namely, let's assume that your answer is a and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-4}$.


-----Examples-----
Input
2 1
2 2
2 1000

Output
2.0000000000
Input
1 100
1 1

Output
-1

Input
3 5
4 3
5 2
6 1

Output
0.5000000000


-----Note-----

In sample test 1, you can charge the first device for the entire time until it hits zero power. The second device has enough power to last this time without being charged.

In sample test 2, you can use the device indefinitely.

In sample test 3, we can charge the third device for 2 / 5 of a second, then switch to charge the second device for a 1 / 10 of a second.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Sorting

Mathematics


Solutions:
def f(N, P, A, B):
	sa = sum(A) - P
	sb = sum(B)
	if sa <= 0:
		return -1
	AB = sorted([(A[i], B[i]) for i in range(N)], key=lambda x: -(x[1] / x[0]))
	sa = sum(A) - P
	sb = sum(B)
	for (a, b) in AB:
		if sa - a <= 0.0:
			continue
		if (sb - b) / (sa - a) <= b / a:
			sb -= b
			sa -= a
	return sb / sa
(N, P) = input().split(' ')
N = int(N)
P = float(P)
A = []
B = []
for _ in range(N):
	(a, b) = [float(x) for x in input().split(' ')]
	A.append(a)
	B.append(b)
print(f(N, P, A, B))


import math
(n, p) = map(int, input().split())
items = []
for i in range(n):
	(ai, bi) = map(int, input().split())
	items += [(ai, bi)]
items.sort(key=lambda item: item[1] / item[0])
surv_time = math.inf
cur_saved = 0
cur_power = 0
for (i, item) in enumerate(items):
	(ai, bi) = item
	current_death_time = bi / ai
	if surv_time < current_death_time:
		break
	cur_saved += bi
	cur_power += ai
	if cur_power <= p:
		surv_time = math.inf
	else:
		surv_time = cur_saved / (cur_power - p)
if surv_time == math.inf:
	print(-1)
else:
	print(surv_time)


def check_infinite(N, P, A, B):
	if sum(A) <= P:
		return True
	return False

def f(N, P, A, B):
	if check_infinite(N, P, A, B) == True:
		return -1
	AB = [(i, A[i], B[i]) for i in range(N)]
	AB.sort(key=lambda x: -(x[2] / x[1]))
	Ord = [x[0] for x in AB]
	sa = sum(A) - P
	sb = sum(B)
	for i in Ord:
		if sa - A[i] <= 0.0:
			continue
		if (sb - B[i]) / (sa - A[i]) <= B[i] / A[i]:
			sb -= B[i]
			sa -= A[i]
	return sb / sa
(N, P) = input().split(' ')
N = int(N)
P = float(P)
A = []
B = []
for _ in range(N):
	(a, b) = [float(x) for x in input().split(' ')]
	A.append(a)
	B.append(b)
print(f(N, P, A, B))


def check_infinite(N, P, A, B):
	if sum(A) <= P:
		return True
	return False

def f(N, P, A, B):
	if check_infinite(N, P, A, B) == True:
		return -1
	sa = sum(A)
	sb = sum(B)
	remain = N
	deleted = set()
	while True:
		found = False
		i = 0
		while i < N:
			if i in deleted:
				i += 1
				continue
			if remain <= 1:
				break
			if sa - P - A[i] <= 0.0:
				i += 1
				continue
			if (sb - B[i]) / (sa - A[i] - P) <= B[i] / A[i]:
				remain -= 1
				sb -= B[i]
				sa -= A[i]
				deleted.add(i)
				found = True
			i += 1
		if not found:
			break
	return sb / (sa - P)
(N, P) = input().split(' ')
N = int(N)
P = float(P)
A = []
B = []
for _ in range(N):
	(a, b) = [float(x) for x in input().split(' ')]
	A.append(a)
	B.append(b)
print(f(N, P, A, B))


import math
from bisect import bisect_right
from itertools import accumulate
R = lambda : map(int, input().split())
(n, p) = R()
devs = []
for i in range(n):
	devs.append(tuple(R()))
devs = sorted(devs, key=lambda x: x[1] / x[0])
(sp, sr, t) = (0, 0, math.inf)
for d in devs:
	sp += d[0]
	sr += d[1]
	if sp > p:
		t = min(t, sr / (sp - p))
print(t if t < math.inf else -1)


from math import fsum
(n, power) = map(int, input().split())
devices = [tuple(map(int, input().split())) for i in range(n)]

def can(time):
	need = fsum((max(0, use * time - have) for (use, have) in devices))
	return need <= power * time
use = sum((use for (use, _) in devices))
if use <= power:
	print(-1)
else:
	left = 0
	right = 1
	while can(right):
		left = right
		right *= 10
	for i in range(20):
		mid = (left + right) / 2
		if can(mid):
			left = mid
		else:
			right = mid
	print(left)


import math as mt
import sys, string, bisect
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
d = defaultdict(int)
(n, p) = M()
la = []
s = 0

def check(mid):
	w = 0
	for i in range(n):
		q = max(0, mid * la[i][0] - la[i][1])
		w += q
	if w <= p * mid:
		return True
	return False
for _ in range(n):
	(a, b) = M()
	la.append((a, b))
	s += a
if p >= s:
	print(-1)
else:
	l = 0
	h = 1
	while check(h):
		l = h
		h *= 10
	while h - l > 1:
		mid = (l + h) // 2
		if check(mid):
			l = mid
		else:
			h = mid
	for j in range(100):
		mid = (l + h) / 2
		if check(mid):
			l = mid
			key = mid
		else:
			h = mid
	print(key)


class SmartReader:

	def __init__(self):
		self.buf = iter([])

	def read(self, *factories):
		for factory in factories:
			yield self._next(factory)

	def _next(self, factory=str):
		res = next(self.buf, None)
		if res is None:
			self.buf = iter(input().split())
			return self._next(factory)
		return factory(res)
reader = SmartReader()
(n, power) = reader.read(int, int)
devices = [tuple(reader.read(int, int)) for i in range(n)]

def can(time):
	need = sum((max(0, use * time - have) for (use, have) in devices))
	return need <= power * time
use = sum((use for (use, _) in devices))
if use <= power:
	print(-1)
else:
	left = 0
	right = 1
	while can(right):
		left = right
		right *= 10
	while right - left > 1:
		mid = (left + right) // 2
		if can(mid):
			left = mid
		else:
			right = mid
	for i in range(20):
		mid = (left + right) / 2
		if can(mid):
			left = mid
		else:
			right = mid
	print(left)


import sys
input = sys.stdin.readline
(n, p) = map(int, input().split())
a = [0] * n
b = [0] * n
for i in range(n):
	(a[i], b[i]) = map(int, input().split())
s = sum(a)
if s <= p:
	print(-1)
	exit()
t = [b[i] / a[i] for i in range(n)]
INF = 10 ** 10 + 1
l = 0
r = INF
while r - l > 10 ** (-5):
	x = l + (r - l) / 2
	use = 0
	for i in range(n):
		use += max(x * a[i] - b[i], 0)
	if use / x <= p:
		l = x
	else:
		r = x
print(l)


(n, p) = [int(x) for x in input().split()]
a = []
pl = 0
pn = 0
t = 0
for i in range(n):
	(x, y) = [int(x) for x in input().split()]
	a += [(x, y / x)]
a.sort(key=lambda x: x[1])
(pn, t) = (0, a[0][1])
pl = t * p
for i in range(1, n):
	(ec, et) = (a[i - 1][0], a[i][1])
	pn += ec
	ts = et - t
	if pn * ts <= pl + p * ts:
		pl += p * ts - pn * ts
		t = et
	else:
		print(t + pl / (pn - p))
		break
else:
	pn += a[-1][0]
	if pn <= p:
		print(-1)
	else:
		print(t + pl / (pn - p))


n__power = input().split()
n = int(n__power[0])
power = int(n__power[1])
items = []
for _ in range(n):
	p__a = input().split()
	p = int(p__a[0])
	a = int(p__a[1])
	time = a / p
	items.append((time, p, a))
items.sort()
sys_power = -power
sys_amount = 0
sys_time = None
while len(items) > 0:
	(t, p, a) = items.pop(0)
	if sys_time is not None and sys_time < t:
		break
	sys_power += p
	sys_amount += a
	if sys_power > 0:
		sys_time = sys_amount / sys_power
if sys_time is None:
	print('-1')
else:
	print(sys_time)


f = lambda : map(int, input().split())
(n, p) = f()
s = [0] * n
(x, y) = (-p, 0)
for i in range(n):
	(a, b) = f()
	x += a
	y += b
	s[i] = (a, b)
s.sort(key=lambda q: q[0] / q[1])
for (a, b) in s:
	if b * x > a * y:
		x -= a
		y -= b
	else:
		break
print(y / x if x > 0 else -1)


if 1:
	(N, P) = [int(i) for i in input().split()]
	p = []
	b = []
	for _ in range(N):
		(pp, bb) = [int(i) for i in input().split()]
		p.append(pp)
		b.append(bb)
else:
	(N, P) = (10000, 999999999)
	p = [100000] * N
	b = [100000] * N
sp = sum(p)
sb = sum(b)
if P >= sp:
	print(-1)
else:
	ep = sp - P
	hi = sb / ep
	lo = 0
	while lo + 1e-05 < hi:
		t = (lo + hi) / 2
		tp = 0
		for i in range(N):
			mp = t * p[i]
			tp += min(mp, b[i])
		if tp < t * ep:
			hi = t
		else:
			lo = t
	print(hi)


def check(t):
	global a, b, p, n
	x = 0
	for i in range(n):
		x += max(0, t * a[i] - b[i])
	return x / t <= p
(n, p) = map(int, input().split())
p = float(p)
(a, b) = ([0 for i in range(n)], [0 for i in range(n)])
s = 0
for i in range(n):
	(a[i], b[i]) = map(float, input().split())
	s += a[i]
if s <= p:
	print(-1)
else:
	l = 0
	r = 10 ** 10 + 1
	j = 0
	while r - l > 10 ** (-4):
		m = (l + r) / 2
		if check(m):
			l = m
		else:
			r = m
		j += 1
	print(l)


(n, p) = map(int, input().split())
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	l.append((a, b, b / a))
l.sort(key=lambda x: x[2])
(sum_a, sum_b, sumt) = (0, 0, 0)
for i in range(n):
	(a, b, _) = l[i]
	c = 1e+18 if i == n - 1 else l[i + 1][2]
	sum_a += a
	sum_b += b
	dp = sum_a - p
	if dp > 0 and sum_b / dp < c:
		print(sum_b / dp)
		exit()
print(-1)


from math import inf
(n, p) = [int(x) for x in input().split()]
l = []
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	l.append((a, b, b / a))
l.sort(key=lambda x: x[2])
asum = 0
bsum = 0
sumt = 0
for i in range(n):
	(a0, b0, _) = l[i]
	c1 = inf if i == n - 1 else l[i + 1][2]
	asum += a0
	bsum += b0
	dp = asum - p
	if dp > 0:
		t = bsum / dp
		if t < c1:
			print(t)
			exit()
print(-1)


from sys import stdin, stdout
(n, p) = map(int, stdin.readline().rstrip().split())
powerData = []
powerList = []
for i in range(n):
	(a, b) = map(int, stdin.readline().rstrip().split())
	deathTime = b / a
	powerData.append((a, b, deathTime))
	powerList.append(a)
powerData.sort(key=lambda x: x[2])
if sum(powerList) <= p:
	print(-1)
else:
	i = 0
	combinedPower = 0
	startingPower = 0
	while combinedPower <= p:
		combinedPower += powerData[i][0]
		startingPower += powerData[i][1]
		i += 1
	finished = False
	deathTime = startingPower / (combinedPower - p)
	while i < n and deathTime > powerData[i][2]:
		combinedPower += powerData[i][0]
		startingPower += powerData[i][1]
		deathTime = startingPower / (combinedPower - p)
		i += 1
	print(deathTime)


def solve():
	(n, p) = map(int, input().split())
	persec = []
	cur = []
	for i in range(n):
		(a, b) = map(int, input().split())
		persec.append(a)
		cur.append(b)
	if sum(persec) <= p:
		print(-1)
		return
	full_time = 0
	curtime = [cur[i] / persec[i] for i in range(len(cur))]
	ids = list(range(len(cur)))
	ids.sort(key=lambda i: curtime[i])
	cur_next = 0
	powered_minus = 0
	additional_energy = 0
	while True:
		if cur_next < len(ids):
			curmin = curtime[ids[cur_next]] - full_time
			next_time = curmin * (p - powered_minus)
			if additional_energy > -next_time:
				additional_energy += curmin * (p - powered_minus)
				full_time += curmin
				powered_minus += persec[ids[cur_next]]
				cur_next += 1
				continue
		full_time += additional_energy / (powered_minus - p)
		break
	print('%.9f' % full_time)
solve()


(n, p) = map(int, input().split())
charge = [tuple(map(int, input().split())) for i in range(n)]
charge.sort(key=lambda x: x[1] / x[0], reverse=True)

def lifetime(x):
	if x[0] <= p:
		return float('inf')
	return x[1] / (x[0] - p)
while len(charge) > 1:
	a = charge[-1]
	b = charge[-2]
	if lifetime(a) < b[1] / b[0]:
		break
	charge.pop()
	charge.pop()
	charge.append((a[0] + b[0], a[1] + b[1]))
if len(charge) == 1 and lifetime(charge[0]) == float('inf'):
	print(-1)
else:
	print(lifetime(charge[-1]))


from sys import stdin, stdout
from decimal import *

def ri():
	return map(int, input().split())
getcontext().prec = 100
(n, p) = ri()
p = Decimal(p)
a = [Decimal(0) for i in range(n)]
b = [Decimal(0) for i in range(n)]
ab = [[Decimal(0), Decimal(0)] for i in range(n)]
A = [Decimal(0) for i in range(n)]
B = [Decimal(0) for i in range(n)]
for i in range(n):
	(aa, bb) = ri()
	ab[i][0] = Decimal(aa)
	ab[i][1] = Decimal(bb)
ab.sort(key=lambda e: e[1] / e[0])
for i in range(n):
	a[i] = Decimal(ab[i][0])
	b[i] = Decimal(ab[i][1])
if p >= sum(a):
	print(-1)
	exit()
A[0] = Decimal(0)
B[0] = Decimal(1)
for i in range(1, n):
	A[i] = a[i] - b[i] * a[i - 1] / b[i - 1] + b[i] * A[i - 1] / b[i - 1]
	B[i] = b[i] * B[i - 1] / b[i - 1]
sigmaA = Decimal(0)
sigmaB = Decimal(0)
for i in range(0, n):
	sigmaA += A[i]
	sigmaB += B[i]
	p0 = Decimal(p - sigmaA) / Decimal(sigmaB)
	if Decimal(a[0] - p0) <= 0:
		continue
	t = Decimal(b[0]) / Decimal(a[0] - p0)
	if i != n - 1 and t <= Decimal(b[i + 1] / a[i + 1]) and (t > 0):
		print(t)
		exit()
print(t)