Consultations before resignation
InterviewTime limit2sMemory limit512 MB
Given up to 1.5 million days, each with a job of length T_i and pay P_i, pick jobs that fit before day N+1 to maximize total pay.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Array, Greedy, Implementation
- Solved
- No attempts yet
Problem
Minjun works as a consultant and is preparing to resign.
He leaves on day N+1 counting from today, so he wants to handle as many consultations as he can during the N days that remain.
Minjun asked his secretary to book as many consultations as possible, and she booked one consultation per day, each with a different client.
Every consultation has a duration , the number of days it takes to finish, and a payment , the amount he receives once it is done.
Look at the schedule for N = 7.
The consultation booked on day 1 takes 3 days and pays 10. The consultation booked on day 5 takes 2 days and pays 15.
A single consultation can take longer than one day, so he cannot handle all of them. For example, if he starts the consultation on day 1, he cannot handle the ones on days 2 and 3. If he starts the one on day 2, he cannot handle the ones booked on days 3, 4, 5 and 6.
He is also not at the company on day N+1, so he cannot handle the consultations on days 6 and 7.
The largest profit before he resigns comes from handling the consultations on days 1, 4 and 5, and that profit is 10+20+15=45.
Write a program that computes the largest profit Minjun can earn by choosing the consultations well.
Input
The first line contains N ().
Each of the next N lines contains and separated by a space, given in order from day 1 to day N. (, )
Output
Print the largest profit Minjun can earn on the first line.