This page is still under construction.

Parts of this page are still being built. What you see may change.

Embassy

Interview

Time limit1sMemory limit128 MB

Summary
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 NN 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 kk-th position of the queue finishes at time kk.

Participants are numbered from 11 to NN in the order they enter. Participant ii has a train departure time did_i (in time units) and a ticket change fee wiw_i. If a participant finishes at or before time did_i, they catch their train. However, if they finish after time did_i, they miss their train and must change their ticket, which costs a fee of wiw_i.

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 NN.

Each of the next NN lines describes one participant: the (i+1)(i+1)-th line contains did_i and wiw_i of participant ii, separated by a single space.

All numbers are positive integers not greater than 3000030000.

Output

Print, on a single line, the minimum total fee the fund must pay.

Examples1

  1. Example 1

    Input
    7
    3 40
    2 60
    6 10
    1 30
    4 70
    4 50
    4 20
    
    Expected output
    50