Woburn Challenge 2015-16 Round 4 - Senior Division
MI6 has received intelligence that members of the evil Spectre organization may be holding a gathering somewhere along a certain street in London. Unfortunately, they're not sure exactly in which building this gathering will take place. As such, they'll need to recruit some agents to stake out the street and look for suspicious activity.
The street can be represented by a number line. There are suspicious buildings which must be monitored, with the -th one at position . There are also agents stationed along the street, with the -th one at position . All of these positions are distinct. Agent has a sight range of , meaning that they're able to keep watch over all buildings at positions in the inclusive range of positions . Times are tough at MI6, with the agents not even willing to take this assignment for free – M will have to pay agent a fee of dollars to stay at their post, otherwise they'll get bored of the stakeout and go out for some steak instead.
In light of this monetary issue, M will need to decide on a tradeoff
between cost and thoroughness. A hired agent can successfully keep an
eye on all buildings in his range simultaneously, but it might not be
sufficient to have each building only be watched by a single agent, in
case they're taken out. As such, M would like to ask himself
questions, with the -th one asking "What's the minimum
cost to hire agents such that each suspicious building is in sight of at
least of them, if this is possible at
all?". Can you help M answer each of these questions, for the sake of
the MI6 fundraising department? If it's impossible to have each building
covered by at least agents, you should output -1
instead.
Otherwise, the total cost (in dollars) may be quite large, so you only
need to output it modulo .
Subtasks
In test cases worth of the points, and .
In test cases worth another of the points, and .
In test cases worth another of the points, and .
Input Specification
The first line of input consists of three space-separated integers ,
, and .
The next lines each consist of a single integer , for .
The next lines each consist of two space-separated integers
and , for .
The next lines each consist of a single integer , for .
Output Specification
Output lines, one integer per line. The -th line (for )
should consist of the answer to the -th query (modulo
) or -1
if it's impossible.
Sample Input
2 4 3
10
20
14 5
22 11
0 1
15 5
1
2
3
Sample Output
6
22
-1
Explanation
If M hires agents and (for a cost of dollars), each building will be in sight of a single agent, which is just enough to satisfy the first question. Hiring agents , , and will allow each building to be in sight of at least two agents. It's impossible to have each building be in sight of three or more agents.
Comments