#P10334. Fruit Juice Satisfaction Problem

    ID: 12338 Type: Default 1000ms 256MiB

Fruit Juice Satisfaction Problem

Fruit Juice Satisfaction Problem

There is a juice machine that produces one cup of juice per minute with an arbitrary volume. There are \(n\) persons in a queue. The \(i\)th person will arrive at minute \(t_i\) and will take the cup with the maximum volume among the juice produced so far. The person is satisfied if the volume of the taken cup is at least \(a_i\). If no cup is available or the chosen cup has a volume less than \(a_i\), the person will be unsatisfied.

Your task is to determine whether it is possible to satisfy all persons. If it is possible, output the minimum total volume of juice required to achieve that. Otherwise, output -1.

Note that the juice machine produces exactly one cup per minute and you are allowed to set the volume of each cup arbitrarily (non‐negative values). The persons pick cups in the order they appear in the queue. A simple strategy is to produce a cup exactly at the minute when a person arrives with a volume equal to their requirement \(a_i\), and produce cups with 0 volume at all other times. However, if the arrival time \(t_i\) of the \(i\)th person is less than \(i\) (i.e. there are not enough cups available by the time they arrive), it becomes impossible to satisfy everyone.

inputFormat

The first line contains an integer \(n\) representing the number of persons in the queue.

Each of the next \(n\) lines contains two integers \(t_i\) and \(a_i\), where \(t_i\) is the minute at which the \(i\)th person arrives at the juice machine and \(a_i\) is the minimum required volume of juice for that person to be satisfied.

It is guaranteed that persons are given in the order of the queue.

outputFormat

If it is possible to satisfy all persons, output a single integer representing the minimum total volume of juice required. Otherwise, output -1.

sample

1
1 5
5

</p>