#P8465. Minimizing Fatigue with Sword Amulets
Minimizing Fatigue with Sword Amulets
Minimizing Fatigue with Sword Amulets
Senio-illus Sword is composed of amulets with weights . William will perform exactly 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 , which means that during the –th adjustment he must choose an amulet among the first amulets (i.e. with index ) or during the –th adjustment he must choose an amulet among the last amulets (i.e. with index ); otherwise the sacred sword will collapse.
Note that each amulet can be adjusted more than once. Determine the minimum total fatigue incurred after all adjustments.
inputFormat
The first line contains three integers , , and , where is the number of amulets, is the number of adjustments, and is the number of restrictions.
The second line contains integers , representing the weight of each amulet.
Each of the next lines contains four integers , , , and , describing a restriction as explained above.
outputFormat
Output a single integer --- the minimum total fatigue value after performing all 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