Fish 3
Time limit2sMemory limit1024 MB
For each range of fish, find the fewest type A foods needed to match the target intelligence values, or -1 if the targets cannot be reached.
Problem
JOI-kun is breeding fish in a large tank. Each fish is numbered from to .
JOI-kun has two types of fish food, A and B, and both are available in sufficient quantities. Each time one piece of food is added to the tank, exactly one fish eats it, and any fish can eat it. The intelligence of each fish changes depending on the food type and the fish that ate it:
- When fish () eats one piece of type A food, the intelligence of fish increases by exactly .
- When fish () eats one piece of type B food, the intelligence of every fish numbered and above increases by exactly .
At the start, every fish has intelligence . JOI-kun wants the intelligence of fish () to equal its ideal intelligence , but this is not always possible.
JOI-kun has questions. The -th question asks the following. Starting from intelligence for all fish, and repeating the action of adding food zero or more times, can fish all reach exactly their ideal intelligence values? If so, what is the minimum number of pieces of type A food that must be added to the tank?
Write a program that answers these questions, given the information about the fish and the questions.
Input
The first line contains and .
The second line contains .
The third line contains .
Each of the next lines contains and .
Output
Print lines. On line , print the minimum number of pieces of type A food needed to make fish have exactly their ideal intelligence values. If it is impossible, print -1.
Constraints
- .
- .
- .
- ().
- ().
- All given values are integers.