Fashionista
InterviewTime limit1sMemory limit128 MB
For each day pick any clothing whose temperature range covers that day's high, maximizing the sum of absolute flashiness differences between consecutive days.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Array, Implementation, Math
- Solved
- No attempts yet
Problem
Sang-geun is planning what to wear on each of the next days (day through day ). Because clothing style is closely tied to the day's high temperature, he plans based on the weather forecast. The high temperature on day is .
Sang-geun owns pieces of clothing, numbered from to . Clothing () can only be worn on a day whose high temperature is between and inclusive, and its flashiness is .
He may wear the same clothing on several days, and some clothing may never be worn.
Wearing similar clothing on consecutive days is unappealing, so he wants to maximize the total difference in flashiness between the clothing worn on adjacent days. That is, if he wears clothing on day , he wants to maximize .
Write a program that computes the maximum value of this sum.
Input
The first line contains and . ()
Each of the next lines contains the high temperature of one day; the -th line contains . ()
Each of the following lines describes one piece of clothing with , , . (, )
On every day there is at least one piece of clothing that can be worn.
Output
Print the maximum total difference in flashiness on a single line.
Hint
In the first example, wearing clothing on day , clothing on day , and clothing on day gives , which is the maximum.