Under construction

Still being built. Things may change

Lanterns

Time limit3sMemory limit1024 MB

Summary
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 nn points on a vertical 2D plane. We call these points peaks. The peaks are numbered from 11 to nn in order. Peak ii has coordinates (i,hi)(i, h_i), where hih_i is the altitude of peak ii. The values h1,h2,…,hnh_1, h_2, \dots, h_n form a permutation of 11 through nn. That is, for each j=1,…,nj = 1, \dots, n, there is exactly one i∈{1,…,n}i \in \{1, \dots, n\} with hi=jh_i = j.

For each 1≤i<n1 \le i < n, peaks ii and i+1i+1 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 kk lanterns for sale. Lantern jj can be bought at peak pjp_j for cjc_j francs. It works only when John's altitude is within [aj,bj][a_j, b_j]. If his altitude is strictly below aja_j or strictly above bjb_j, lantern jj 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 pp, he can do one of three things:

  • Buy one of the lanterns available at peak pp. Once bought, a lantern can be used forever.
  • If p>1p > 1, walk to peak p−1p-1.
  • If p<np < n, walk to peak p+1p+1.

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 44 and wants to walk to an adjacent peak with altitude 11. If he has lanterns working on [1,3][1, 3] and [3,4][3, 4], he can make the walk. If he has lanterns working only on [1,1][1, 1] and [2,5][2, 5], he cannot. No lantern works at altitude 1.471.47, for instance.

For each jj with aj≤hpj≤bja_j \le h_{p_j} \le b_j, suppose John starts by buying lantern jj at peak pjp_j. He must then visit all nn peaks at least once using the three actions above. For each such jj, find the minimum total number of francs he must spend to search the whole range. The cost includes the price of lantern jj.

Input

The first line contains nn and kk (1≤n≤20001 \le n \le 2000, 1≤k≤20001 \le k \le 2000). The second line contains h1,h2,…,hnh_1, h_2, \dots, h_n (1≤hi≤n1 \le h_i \le n), which is a permutation of 11 through nn.

Each of the next kk lines contains four integers pjp_j, cjc_j, aja_j, and bjb_j (1≤pj≤n1 \le p_j \le n, 1≤cj≤1061 \le c_j \le 10^6, 1≤aj≤bj≤n1 \le a_j \le b_j \le n). These are the peak where lantern jj can be bought, its cost, and the lower and upper bounds of its working altitude range.

Output

For each jj, print one line.

  • If hpjh_{p_j} is outside [aj,bj][a_j, b_j], print −1-1.
  • Otherwise, if John cannot search the whole range after buying lantern jj first, print −1-1.
  • Otherwise, print the minimum total cost for searching the whole range when John starts by buying lantern jj.

Hint

If John starts by buying lantern 11 at peak 33, he can do the following:

  • Walk left twice to peak 11.
  • Buy lantern 22.
  • Walk right to peak 44.
  • Buy lantern 33.
  • Walk right to peak 77.

By then he has visited every peak and spent 1+2+4=71 + 2 + 4 = 7 francs.

Lanterns 22, 66, and 77 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 −1-1.

If John starts with lantern 33 or 44, he can visit all peaks without buying anything else.

If John starts with lantern 55, he must also buy lantern 44 later.

If John starts with lantern 88, he gets stuck at peak 77. Even if he also buys lantern 77, he still cannot walk from peak 77 to peak 66.

Examples1

  1. Example 1

    Input
    7 8
    4 2 3 1 5 6 7
    3 1 2 4
    1 2 1 3
    4 4 1 7
    6 10 1 7
    6 20 6 6
    6 30 5 5
    7 40 1 6
    7 50 7 7
    
    Expected output
    7
    -1
    4
    10
    30
    -1
    -1
    -1