#P8465. Minimizing Fatigue with Sword Amulets

    ID: 21640 Type: Default 1000ms 256MiB

Minimizing Fatigue with Sword Amulets

Minimizing Fatigue with Sword Amulets

Senio-illus Sword is composed of nn amulets with weights aia_i. William will perform exactly kk adjustments; in each adjustment he picks one amulet and incurs fatigue equal to its weight. However, due to a strange link between the amulets, there are restrictions on the adjustments. Each restriction is given as a 4‐tuple (i,j,x,y)(i, j, x, y), which means that during the ii–th adjustment he must choose an amulet among the first xx amulets (i.e. with index x\le x) or during the jj–th adjustment he must choose an amulet among the last yy amulets (i.e. with index ny+1\ge n-y+1); otherwise the sacred sword will collapse.

Note that each amulet can be adjusted more than once. Determine the minimum total fatigue incurred after all kk adjustments.

inputFormat

The first line contains three integers nn, kk, and qq, where nn is the number of amulets, kk is the number of adjustments, and qq is the number of restrictions.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n, representing the weight of each amulet.

Each of the next qq lines contains four integers ii, jj, xx, and yy, describing a restriction as explained above.

outputFormat

Output a single integer --- the minimum total fatigue value after performing all kk adjustments while satisfying all restrictions. If no valid sequence of adjustments exists, output -1.

sample

3 3 1
1 2 3
1 3 2 1
3