Embassy
InterviewTime limit1sMemory limit128 MB
Order N people in a queue to minimize the total fee paid for those who finish after their train departure time.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Dynamic programming
- Solved
- No attempts yet
Problem
There are participants standing in a single line in front of an embassy. An embassy official talks to exactly one participant for exactly one unit of time, so the participant standing in the -th position of the queue finishes at time .
Participants are numbered from to in the order they enter. Participant has a train departure time (in time units) and a ticket change fee . If a participant finishes at or before time , they catch their train. However, if they finish after time , they miss their train and must change their ticket, which costs a fee of .
A fund covers the sum of all such ticket change fees and wants to make that sum as small as possible. You may arrange the participants in the queue in any order. Find the minimum total fee the fund must pay.
Input
The first line contains the number of participants .
Each of the next lines describes one participant: the -th line contains and of participant , separated by a single space.
All numbers are positive integers not greater than .
Output
Print, on a single line, the minimum total fee the fund must pay.