All problems

Problem 464

Weighted Job Scheduling

Hard · 30 pts · Array, Dynamic Programming, Binary Search, Sorting

Problem

Each job has a start time, an end time and a profit. Choose a set of jobs that do not overlap in time and maximise the total profit. A job that ends at time t is compatible with a job that starts at time t. Sort the jobs by end time; for each job, the best total either skips it or takes it together with the best total of the jobs that end no later than its start, which binary search finds.

Input

First line: N. Next N lines: start, end and profit of a job.

Output

Print the maximum total profit.

Constraints

1 <= N <= 50000; 1 <= start < end <= 1000000000; 1 <= profit <= 10000

Example input

4
1 3 50
2 4 10
3 5 40
3 6 70

Example output

120

Explanation

With jobs (1-3, 50), (2-4, 10), (3-5, 40) and (3-6, 70), taking the first job and the last one gives 50 + 70 = 120. They do not overlap because the first ends at 3 when the last starts.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Optimal Strategy for a Coin Game Next: Frog Jump with K Steps