Lanterns
Time limit3sMemory limit1024 MB
For each lantern that works at its own peak, find the minimum cost to buy more lanterns so John can visit all peaks while always having a working one.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Greedy
- Solved
- No attempts yet
Statement
Farmer John took his herd of cows on a hiking trip in the Alps. Night fell and the trip ended. But some cows are still stranded along the mountain range, and John must rescue all of them.
The mountain range is a series of points on a vertical 2D plane. We call these points peaks. The peaks are numbered from to in order. Peak has coordinates , where is the altitude of peak . The values form a permutation of through . That is, for each , there is exactly one with .
For each , peaks and are joined by a straight line segment.
Problem
Because it is night, John cannot walk anywhere unless he has at least one working lantern. There are lanterns for sale. Lantern can be bought at peak for francs. It works only when John's altitude is within . If his altitude is strictly below or strictly above , lantern does not work. Lanterns do not break when they leave their range. If John returns to an altitude within the range, the lantern works again.
If John is at peak , he can do one of three things:
- Buy one of the lanterns available at peak . Once bought, a lantern can be used forever.
- If , walk to peak .
- If , walk to peak .
John must never move without a working lantern. He can walk between two adjacent peaks only if, at every moment of the walk, at least one lantern he already owns works. The lantern does not have to be the same one throughout the walk.
For example, suppose John is at a peak with altitude and wants to walk to an adjacent peak with altitude . If he has lanterns working on and , he can make the walk. If he has lanterns working only on and , he cannot. No lantern works at altitude , for instance.
For each with , suppose John starts by buying lantern at peak . He must then visit all peaks at least once using the three actions above. For each such , find the minimum total number of francs he must spend to search the whole range. The cost includes the price of lantern .
Input
The first line contains and (, ). The second line contains (), which is a permutation of through .
Each of the next lines contains four integers , , , and (, , ). These are the peak where lantern can be bought, its cost, and the lower and upper bounds of its working altitude range.
Output
For each , print one line.
- If is outside , print .
- Otherwise, if John cannot search the whole range after buying lantern first, print .
- Otherwise, print the minimum total cost for searching the whole range when John starts by buying lantern .
Hint
If John starts by buying lantern at peak , he can do the following:
- Walk left twice to peak .
- Buy lantern .
- Walk right to peak .
- Buy lantern .
- Walk right to peak .
By then he has visited every peak and spent francs.
Lanterns , , and cannot be used as a starting purchase, because they do not work at the altitude of the peak where they are sold. Their answers are all .
If John starts with lantern or , he can visit all peaks without buying anything else.
If John starts with lantern , he must also buy lantern later.
If John starts with lantern , he gets stuck at peak . Even if he also buys lantern , he still cannot walk from peak to peak .