Stop the overtime!!!
Time limit1sMemory limit1024 MB
Given tasks with deadlines and work units, move up to K deadlines later by one day each to minimize the overtime hours needed, with five regular days per week.
Problem
Junmin could not escape overtime before his discharge, so right before he left the unit he handed the remaining tasks to his junior soldier Minwook. Minwook received tasks in total. Task requires units of work and has deadline . No two tasks have the same deadline.
Minwook starts working on Monday. Regular duty runs only from Monday to Friday, and overtime duty can be done on any day of the week. Each day allows at most one regular shift and at most one overtime shift. In each shift, Minwook picks one task he wants and completes 1 unit of work on it.
Junmin wants to change the deadlines without the officers noticing. Each change moves the deadline of one chosen task later by 1 day. He will make at most changes so that he is not caught. To hide the changes, Minwook finishes all tasks in the order of their original deadlines, from earliest to latest.
After making at most deadline changes, find the minimum number of overtime hours Minwook must work.
Input
The first line contains the number of tasks and the number of deadline changes , separated by a space. Each of the next lines contains the deadline and the work amount of the -th task, separated by a space.
Output
Print the minimum overtime hours Minwook must work after changing deadlines at most times. If it is impossible to finish all tasks on time no matter what, print -1.
Constraints
, , , , and for . All input values are integers.