Cookie-Loving Chunbae
InterviewTime limit1sMemory limit1024 MB
Each cookie has a position, a cost, and a free-range radius, and the task asks for the cheapest set of purchases whose ranges together cover all K cookies.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Segment tree, Intervals
- Solved
- No attempts yet
Problem

Chunbae ate every cookie at home again today. When Siwon tells him about an event at a cookie shop, he decides to go.
The shop has cookies on a display shelf of cells. Cookie is in cell and costs won. The cookies are all in different positions.
Because the shop is running an event, if Chunbae buys cookie , the shop gives him, for free, any cookies he wants among those whose distance from cookie is at most . The distance between cookies and is .
Chunbae is broke after buying a MacBook, and he wants to eat all cookies at the lowest cost. Find the minimum amount he must pay, by buying cookies or receiving them for free, to get all cookies.
Input
The first line contains the shelf length and the number of cookies , separated by a space.
From the second line on, the next lines each contain , , and , separated by spaces.
Output
Print the minimum amount that must be paid to buy all cookies.