Lanterns#10661

Lanterns

3 seconds
1,024 megabytes
Easy

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 to . Vertex has coordinates (i, h_i), where h_i is the height of vertex . The heights h_1, h_2, \ldots, h_n form a permutation of the numbers from to . This means that for each j = 1, ..., n, there is exactly one such that h_i = j.

Each pair of consecutive vertices and i+1 (1 \le i < n) 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 (1 \le j \le k), the -th lantern can be bought at vertex p_j for c_j francs.

However, each lantern only works when John is at a height within the range [a_j, b_j]. If John's height is strictly less than a_j or strictly greater than b_j, 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:

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 and wants to move to a neighboring vertex with height , 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 .

Your task is to determine the answer for various independent queries.

For each 1 \le j \le k such that a_j \le h_{p_j} \le b_j, assume John starts his journey from vertex p_j 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 (1 \leq n \leq 2000, 1 \leq k \leq 2000) — the number of vertices in the mountain range and the number of available lanterns, respectively.

The second line contains space-separated integers h_1, h_2, \ldots, h_n (1 \leq h_i \leq n): the heights of each vertex. It is guaranteed that the values h_i form a permutation of numbers from to .

The next lines each contain four space-separated numbers p_j, c_j, a_j, and b_j (1 \leq p_j \leq n, 1 \leq c_j \leq 10^6, 1 \leq a_j \leq b_j \leq n) — the vertex number where lantern can be purchased, its price, and its range, respectively.

Output

For each (1 \le j \le k), output one line:

  • If h_{p_j} is outside the range [a_j, b_j], 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

Example 1

Loading
Loading

Note

If John starts by purchasing lantern at vertex , he can:

  • Move left twice to vertex

  • Purchase lantern

  • Move right to vertex

  • Purchase lantern

  • Move right to vertex

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 , , or , 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 or , he can visit all vertices without buying additional lanterns.

If John starts by purchasing lantern , he must also purchase lantern later.

If John starts by purchasing lantern , he will get stuck at vertex . Even if he buys lantern , he still cannot move from vertex to vertex .

Scoring

Block 1 (9 points): n \le 20 and k \le 6.

Block 2 (12 points): n \le 70 and k \le 70.

Block 3 (23 points): n \le 300, k \le 300 and h_i = i for all 1 \le i \le n.

Block 4 (16 points): n \le 300, k \le 300.

Block 5 (40 points): no additional constraints.