Back to Solutions

Tasks And Deadlines

Explanation

Here, we are given n tasks, with their durations and deadlines. The reward for completing each task is deadline - finish_time and we want to maximize the total reward.

This is simple. Just do the tasks that take the least time first. This way we can complete the most tasks and get the most reward.

Code

input = __import__("sys").stdin.readline # To avoid TLE just because of slow input
tasks = sorted([tuple(map(int, input().split())) for _ in range(int(input()))])

reward = 0
time = 0

for duration, deadline in tasks:
    time += duration
    reward += deadline - time

print(reward)