cswork / OAOpen practice workspace

UberOA #32

Defuse the Bombs

Estimated MediumJudge-ready

Topics (AI-labelled)Greedy

You can defuse only one bomb at a time and cannot interrupt it. Bomb i takes X[i] time and must be finished at or before time Y[i]. Time starts at 0. Find the minimum total time to defuse all bombs; if it is impossible, output −1.

This site has made the standard input and output explicit.

Input

The first line contains n, followed by n lines of duration deadline. 1≤n≤200000; the source gives no time bounds, so on this site 1≤duration≤10⁹ and 1≤deadline≤10¹⁵.

Output

Output one integer, the answer.

Example 1

Input:

5
2 4
1 9
1 8
4 9
3 12

Output:

11

Example 2

Input:

2
2 2
1 2

Output:

-1

Example 3

Input:

1
3 3

Output:

3

Example notes

Example 1: scheduling by deadline, the cumulative completion times 2, 3, 4, 8, 11 are all on time, so output 11. Example 2: the total time is 3, but both must be finished before 2, so output −1. Example 3: the work finishes exactly at deadline 3, so output 3.

Editorial

Loading solution…