Voltage Keepsake
Contest: VK Cup 2017 - Round 2
Difficulty rating: 1800
Tags: binary search, math

You have n devices that you want to use simultaneously.

The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi 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 format:
The first line contains two integers, n and p (1 ≤ n ≤ 100 000, 1 ≤ p ≤ 109) — 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 ai and bi (1 ≤ ai, bi ≤ 100 000) — the power of the device and the amount of power stored in the device in the beginning.

Output format:
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|}{\max(1,b)} \leq 10^{-4}$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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.

Example input:
2 1
2 2
2 1000

Example output:
2.0000000000

Example input:
1 100
1 1

Example output:
-1

Editorial:
First, let's deal with the infinite case. If the supply of power is at least as big as the sum of demands, we can keep all devices alive indefinitely.
Otherwise, let's binary search for the result. We can do binary search since if we can keep all devices alive for E seconds, we can keep it alive for any time less than E seconds.
Since all usage/charging is continuous, we can think about it as "splitting" up the charge amount.
For each device and a fixed time T, we can compute the rate that we need to charge it so that the device always has positive power. Well, it uses ai power per second, so it uses a total of T * ai power. It currently has bi power, so we need Xi = max(0, T * ai - bi) units of power. This means we need Xi / T units of power per second to this device.
So, we just need to check that $$\sum_{i} X_i / T \leq P$$. If so, then it is possible to keep the devices alive for T seconds, and we can use this to continue our binary search.
Since we are binary searching on doubles, it is useful to just do it for a fixed number of steps. We can approximate the max answer is somewhere around 1014 and we need a precision of 10 - 4, so we need approximately $$\log_{2}(10^{18}) \approx 60$$ iterations.