#P3891. Balanced Resource Collection

    ID: 17139 Type: Default 1000ms 256MiB

Balanced Resource Collection

Balanced Resource Collection

In the classic game Warcraft 3, strategic resources are collected by units such as peasants, grunt, faerie and monk. In the development of Warcraft 4, the designers thought that this harvesting model was too monotonous and decided to add more types of workers (all referred to as "grunt") with different work efficiencies and different construction costs. In order to test the balance of this new mode, they designed a method: if different races have the same starting resources, then the time required to accumulate a given amount of resource (by optimally constructing additional workers) should be identical if the design is balanced.

Your task is to simulate the resource gathering process under an optimal strategy and, given the data for each race, determine whether the design is balanced, i.e. whether all races can finish collecting the target resource in exactly the same amount of time (up to a tolerance of 10-6).

The process is modeled as follows:

  • You start with an initial resource S and a base production rate of 1 unit per time unit.
  • There are several types of worker available. Each worker type is characterized by a cost \(C_i\) and an efficiency increase \(E_i\). When you buy a worker, you must have enough resource to pay its cost. The purchase is instantaneous, and its efficiency (added to the production rate) takes effect immediately. After a purchase, the resource amount decreases by \(C_i\) (or if you already had extra resource beyond the cost, the leftover remains).
  • You may wait to accumulate enough resource to buy a worker. At any moment you may choose to wait to eventually finish the game without making any further purchases. Finishing the game means accumulating a total resource amount of at least \(T\).

Formally, at any moment with current resource R and production rate \(r\), if you do nothing further, the finishing time is
\(\displaystyle \frac{T - R}{r}\). Alternatively, for each worker type \(i\) the waiting time to be able to afford it is \(\max(0, \frac{C_i - R}{r})\); after buying it, your new state becomes: time increased by the waiting time, resource set to \(R' = \max(R - C_i, 0)\), and production rate becomes \(r' = r + E_i\). You then continue optimizing from the new state. The overall minimal finishing time is the optimum over all possible purchase strategies.

The input provides the initial resource S and the target resource T, followed by data for several races. Each race description starts with an integer n (the number of worker types available for that race) followed by n lines each containing two numbers: the cost \(C_i\) and the efficiency \(E_i\) of a worker type. Compute the minimal total time needed to reach at least \(T\) resources for each race under optimal strategy. If all races yield times that are equal (difference less than 10-6), then the design is considered balanced and you must output Yes; otherwise, output No.

inputFormat

The input is given as follows:

  • The first line contains two numbers S and T (the starting and target resource amounts).
  • The second line contains an integer R representing the number of races.
  • For each race, the first number is an integer n (the number of worker types available), followed by n lines. Each of these lines contains two numbers: the cost \(C_i\) and efficiency \(E_i\) of that worker type.

All numbers are separated by whitespace.

outputFormat

Output a single line: Yes if the minimal times computed for all races are equal (balanced), or No otherwise.

sample

0 100
2
1
10 10
1
20 20
No