Honest Worker
InterviewTime limit1sMemory limit1024 MB
Choose non-overlapping contractor jobs, each optionally quit early, to maximize total pay minus fake cover letter costs.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Binary search
- Solved
- No attempts yet
Problem
Rafael lives in an ideal egalitarian society: all jobs offer identical payment rates, and you only have to work at a job that you find personally fulfilling. Unfortunately, even such a utopia cannot make up for the fact that Rafael is not very skilled at anything.
To compensate, Rafael turns to contractor work, where potential employers do not have the time to notice how unqualified he is for the job. Even so, since he lacks the qualifications for these contractor positions, he must spend money on fake cover letters just to get hired.
Rafael has a choice of contractor jobs. The -th job starts on day , ends at the end of day , and pays exactly gold coins per day. Rafael is a really bad worker and cannot work two jobs at the same time. He can start job only on day , but once hired, he may quit at the end of any day, keeping the money from the days he worked (including the last one). After quitting, he can start another job from the next day on, but not on the same day. A fake cover letter for job costs gold coins. Rafael always has enough savings to pay for any number of cover letters, even before working any of the jobs.
Given the jobs, what is the maximum profit Rafael can make after subtracting the cover letter costs?
Input
The first line contains two integers () and (), the number of jobs and their daily pay. Each of the following lines contains three integers , , and (, ), the start date, end date, and cost of the fake cover letter for job .
Output
Output a single line with one integer: the amount of money Rafael can make after all the jobs are over.