Mountain Hike
InterviewTime limit1sMemory limit1024 MB
Pick peaks so each new one is at least as high as all visited and no farther from the top peak; maximize total charm.
- Level
Medium5 of 10
- Topics
- Sorting, Dynamic programming
- Solved
- No attempts yet
Problem
Ignas planned to celebrate his birthday with friends. The pandemic and quarantine ruined those plans, so he decided not to organize a party.
Luckily, the epidemiological situation improved a lot right before his birthday. As the country began to open its borders, flights to Bitkalnija resumed first, because nobody in Bitkalnija was infected. Ignas decided to fly there for his birthday and climb the highest peak in Bitkalnija. For the climb, he hired a guide.
Since Ignas was the guide's first client after a long break, the guide offered to take him to other peaks of his choosing for free before going to the highest peak. Peak can be visited if it satisfies both of these conditions:
- The height of is not less than the height of any peak already visited.
- is not farther from the highest peak in Bitkalnija than the peaks already visited.
Ignas was very happy to hear the news. After studying the map, he decided how interesting each peak is to him and rated it with an integer.
What is the maximum total interest (charm) of the peaks that Ignas can reach with the guide?
Input
The first line contains the number of mountains .
Each of the next lines contains four integers separated by spaces: , , , and . This means that a peak with height is located at coordinates , and Ignas rated its interest as .
Output
Print one integer: the maximum possible total interest of the peaks Ignas can climb with the guide.
Constraints
- All mountains are at different locations, and there is exactly one highest mountain.