#K76882. Maximum Completed Tasks

    ID: 34741 Type: Default 1000ms 256MiB

Maximum Completed Tasks

Maximum Completed Tasks

You are given a set of tasks. Each task i requires (T_i) time units and must be completed by its deadline (D_i). Tasks are processed sequentially and once a task is started it must be completed. The task can only be completed if the sum of the durations of previously completed tasks and the current task does not exceed its deadline, i.e.,

[ current_time + T_i \leq D_i ]

Your objective is to determine the maximum number of tasks that can be completed. Use a greedy strategy by sorting the tasks based on their deadlines (and duration if necessary) to achieve the optimal solution.

inputFormat

The input is read from standard input (stdin). The first line contains an integer (n) representing the number of tasks. Each of the following (n) lines contains two space-separated integers (T) and (D), where (T) is the time required to complete the task, and (D) is the deadline by which the task must be finished.

outputFormat

Output a single integer to standard output (stdout), indicating the maximum number of tasks that can be successfully completed.## sample

3
3 5
8 12
4 9
2