Lanterns
Farmer John took his herd of cows on a hiking trip in the Alps! As night fell, the trip had to end, and some cows got stranded across the mountain range. Now, John needs to rescue them all!
The mountain range can be represented as a series of points in a 2D plane, referred to as "vertices." These vertices are numbered from 1 to . Vertex has coordinates , where is the height of vertex . The heights form a permutation of the numbers from 1 to . This means that for each , there is exactly one such that .
Each pair of consecutive vertices and () is connected by a straight line segment.
Since it is nighttime, John cannot travel without at least one functioning lantern. Fortunately, there are lanterns available for purchase. For each (), the -th lantern can be bought at vertex for francs.
However, each lantern only works when John is at a height within the range . If John's height is strictly less than or strictly greater than , lantern will not function. Lanterns do not break when outside their range; they simply stop working and will function again once John returns to a valid height.
If John is at vertex , he can perform one of the following actions:
- Buy one of the lanterns available at vertex . Once purchased, a lantern can be used indefinitely. - Move to vertex if . - Move to vertex if .
John must always have a functioning lantern to move. He can only travel between two vertices if, at every point along the way, at least one of his purchased lanterns is working. It doesn't have to be the same lantern throughout the journey.
For example, if John is at a vertex with height 4 and wants to move to a neighboring vertex with height 1, he can do so if he has lanterns that work in the height ranges and . However, if his lanterns work in the ranges and , he cannot move between these vertices because no lantern will work at height 1.47.
Your task is to determine the answer for various independent queries.
For each such that , assume John starts his journey from vertex by purchasing lantern . To traverse the entire mountain range, he must visit each of the vertices at least once, using the operations described above. For each , determine the minimum number of francs John needs to spend to traverse the entire mountain range, including the initial cost of purchasing lantern .
Input
The first line contains and (, ) — the number of vertices in the mountain range and the number of available lanterns, respectively.
The second line contains space-separated integers (): the heights of each vertex. It is guaranteed that the values form a permutation of numbers from 1 to .
The next lines each contain four space-separated numbers , , , and (, , ) — the vertex number where lantern can be purchased, its price, and its range, respectively.
Output
For each (), output one line:
- If is outside the range , output . - Otherwise, if John cannot traverse all the vertices starting by purchasing lantern , output . - Otherwise, output the minimum number of francs John needs to spend to visit each vertex of the mountain range starting with lantern .
Examples
Note
For example, if John starts by purchasing lantern 1 at vertex 3, he can:
- Move left twice to vertex 1 - Purchase lantern 2 - Move right to vertex 4 - Purchase lantern 3 - Move right to vertex 7
In this scenario, John visits each vertex at least once and spends a total of 1 + 2 + 4 = 7 francs.
John cannot start by purchasing lanterns 2, 6, or 7, as they do not work at the height where they can be purchased. Therefore, the answer for these lanterns is .
If John starts by purchasing lantern 3 or 4, he can visit all vertices without buying additional lanterns.
If John starts by purchasing lantern 5, he must also purchase lantern 4 later.
If John starts by purchasing lantern 8, he will get stuck at vertex 7. Even if he buys lantern 7, he still cannot move from vertex 7 to vertex 6.
Scoring
Block 1 (9 points): and .
Block 2 (12 points): and .
Block 3 (23 points): , and for all .
Block 4 (16 points): , .
Block 5 (40 points): no additional constraints.