#P10316. Demon King's Shield Breaker
Demon King's Shield Breaker
Demon King's Shield Breaker
In the Demon King's castle, the protective shield has a health value of . Small A wants to destroy the shield within days. At the beginning of each day, small B's magic power is restored to , but his magic power cannot exceed a maximum value . When small B uses his burst magic (which can be used only once per day), every unit of his current magic power deals one point of damage to the shield. That is, if his magic power at that moment is , then the damage dealt is .
To ensure that enough damage is done, small A is allowed to purchase magic crystals from the shop on each day, which can boost small B's magic power. However, the shop sells a different number of magic crystals every day and each crystal has its own magic power bonus and price. Note: Magic crystals bought on a day must be used on that day; they expire at the end of the day. Also, if a crystal’s magic power bonus added to small B's current magic power would exceed , then his magic power becomes . In other words, if you add a crystal with bonus , his updated magic power is given by
[
m' = \min(M, m+h).
]
Determine the minimum amount of money small A needs to spend so that over the course of days, the total damage (i.e. the sum of the magic power available on each day after purchasing crystals) is at least . If it is impossible to reach or exceed , output -1.
Input Format: The first line contains four integers , , , and . Each of the next blocks describes one day. For each day, the first line contains an integer representing the number of magic crystals available that day. Then follow lines; each line contains two integers and , where is the magic power bonus provided by that crystal and is its price.
Note: Without buying any crystals, small B’s damage on each day is , so the damage boost that can be achieved on any day is at most . You may purchase a subset of the available crystals each day (each crystal can be used at most once) to raise his magic power from to at most , thus increasing that day’s damage by at most .
inputFormat
The input begins with a line containing four integers: , , , and , where is the required total damage, is the number of days, is the base magic power per day, and is the maximum magic power per day. Then, for each day, there is a block of input beginning with an integer (the number of magic crystals available that day). This is followed by lines, each containing two integers and , which denote the magic power bonus and the price of that crystal, respectively.
outputFormat
Output a single integer representing the minimum total cost required to achieve at least damage over days. If it is impossible, output -1.
sample
20 3 5 10
2
3 4
5 10
1
2 3
2
4 7
1 2
10