#P3743. Maximal Operation Time of Devices
Maximal Operation Time of Devices
Maximal Operation Time of Devices
You are given n devices. The ith device consumes energy continuously at a constant rate of \(a_i\) units per second. Initially, the ith device holds \(b_i\) units of energy. That is, in any duration of \(k\) seconds, the device consumes exactly \(k \times a_i\) units of energy.
You also have a charger (power bank) that can be connected to any one device at a time. The charger supplies energy continuously at a rate of \(p\) units per second to the connected device. Switching the charger between devices can be done instantaneously at any moment.
The devices are used simultaneously, and the system will be stopped as soon as any one device's energy depletes to 0. Determine the maximum time \(T\) for which you can operate all devices together by optimally allocating the charger. If it is possible to run the devices indefinitely, output \(-1\).
Mathematical Formulation:
Let \(t_i\) be the total time the charger is connected to the ith device. Since the charger can be switched instantaneously, we have \(\sum_{i=1}^{n} t_i = T\). For every device, to ensure it never runs out of energy by time \(T\), the following must hold: \[ b_i + p\,t_i \ge a_i\,T. \]
This implies
\[ t_i \ge \max\Big(0, \frac{a_i\,T - b_i}{p}\Big). \]Thus, a feasible \(T\) must satisfy:
\[ \sum_{i=1}^{n} \max\Big(0, \frac{a_i\,T - b_i}{p}\Big) \le T. \]Solve for the maximum \(T\). Note that if \(\sum_{i=1}^{n} a_i \le p\), then \(T\) can be arbitrarily large; in such cases, output \(-1\).
inputFormat
The first line contains two space-separated integers: \(n\) and \(p\) \((1 \le n \le 10^5,\ 1 \le p \le 10^9)\). Each of the next \(n\) lines contains two space-separated integers \(a_i\) and \(b_i\) \((1 \le a_i \le 10^9,\ 1 \le b_i \le 10^9)\), representing the consumption rate and initial energy of the \(ith\) device.
outputFormat
If the devices can run indefinitely, output \(-1\). Otherwise, print the maximum operation time \(T\) (in seconds) with an absolute or relative error of at most \(10^{-6}\).
sample
1 5
4 3
-1