SP6988 STJEPAN - Beer Machines
Description
Little Stjepan lives in a village which can be represented as X-axis. In village there are N beer machines, machine i has x coordinate P $ _{i} $ and needs T $ _{i} $ seconds to produce 1 liter of beer.
This year M tourists (one by one because there is only one guide, Stjepan) will visit his village, tourist i will arrive at point A $ _{i} $ , want to drink L $ _{i} $ liters of beer but will have energy to walk at most D $ _{i} $ seconds (that is D $ _{i} $ units of length), he will walk to beer machine Stjepan suggests him and as soon as machine produces L $ _{i} $ liters of beer tourist will be able to enjoy it.
As Stjepan wants all tourists to come next year too he will choose machine for each tourist so that tourist can get a beer as soon as possible. Help Stjepan to do that and write program which will output minimal sum of passed times from arrival of tourist to getting a beer. If a tourist can't get a beer then his time is 0.
Note that tourists are independent and machines can be used multiple times.
Input Format
On first line of standard input you are given two integers (2
Next 5 lines contain 4 integers (X $ _{0} $ , A, B, C) each and they describe arrays P, T, A, L and D, respectively.
With these four numbers i-th element of array is defined as X $ _{i} $ = 1 + ((X $ _{i-1} $ \*A + B) mod C), where indices are 1-based and X $ _{0} $ is given in input.
1
**Note:** Author's solution doesn't depend on properties of pseudo-random generator.
### Output
Output total time from task statement. Answer will fit in 64-bit signed integer.
Output Format
N/A