#P7214. Cure All Villagers
Cure All Villagers
Cure All Villagers
In JOI Village there are houses numbered from to , with one villager living in each house (villager lives in house ). Initially, all villagers are infected with the COVILLAGE-19 virus. There are treatment plans proposed. The -th treatment plan is described by four integers , , , and , meaning that on the evening of day , the villagers living in houses in the interval are cured.
However, the virus is extremely potent and spreads every morning: on any morning, if villager is infected, then his neighbors (villager and villager , if they exist) will also become infected. Note that even if a villager was cured the previous evening, they may become infected again if one of their neighbors is infected in the following morning.
A single day may have many treatment plans. In order to permanently cure all villagers, there must exist a day during which the treatments applied cure all houses (i.e. the union of the intervals of treatment plans applied that evening covers the entire range ), so that no one is left infected to re‐infect their neighbors the next morning.
Your task as the Prime Minister is to choose some of the treatment plans (possibly more than one on the same day) so that on at least one day the entire village is cured, and to minimize the total cost spent. Compute the minimum total cost needed. If it is impossible to cure the entire village in one day using the available treatment plans, output .
inputFormat
The input is given in the following format:
...
Where:
(an upper bound), the number of houses.
(an upper bound), the number of treatment plans.
, the day of the treatment plan.
, the interval of houses cured by the plan.
, the cost of the plan.
outputFormat
Output a single integer representing the minimum total cost to permanently cure all villagers, or if it is impossible.
sample
3 3
2 1 1 3
2 2 3 4
1 1 3 10
7