#P10316. Demon King's Shield Breaker

    ID: 12319 Type: Default 1000ms 256MiB

Demon King's Shield Breaker

Demon King's Shield Breaker

In the Demon King's castle, the protective shield has a health value of xx. Small A wants to destroy the shield within nn days. At the beginning of each day, small B's magic power is restored to mm, but his magic power cannot exceed a maximum value MM. 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 pp, then the damage dealt is pp.

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 hh added to small B's current magic power mm would exceed MM, then his magic power becomes MM. In other words, if you add a crystal with bonus hh, 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 nn days, the total damage (i.e. the sum of the magic power available on each day after purchasing crystals) is at least xx. If it is impossible to reach or exceed xx, output -1.

Input Format: The first line contains four integers xx, nn, mm, and MM. Each of the next nn blocks describes one day. For each day, the first line contains an integer kk representing the number of magic crystals available that day. Then follow kk lines; each line contains two integers hh and pp, where hh is the magic power bonus provided by that crystal and pp is its price.

Note: Without buying any crystals, small B’s damage on each day is mm, so the damage boost that can be achieved on any day is at most MmM-m. 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 mm to at most MM, thus increasing that day’s damage by at most MmM-m.

inputFormat

The input begins with a line containing four integers: xx, nn, mm, and MM, where xx is the required total damage, nn is the number of days, mm is the base magic power per day, and MM is the maximum magic power per day. Then, for each day, there is a block of input beginning with an integer kk (the number of magic crystals available that day). This is followed by kk lines, each containing two integers hh and pp, 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 xx damage over nn 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