#icpc2013autumng. [icpc2013autumn_g]Floating Islands

[icpc2013autumn_g]Floating Islands

MathJax.Hub.Config({ tex2jax: { inlineMath: [["","",""], ["\\(","\\)"]], processEscapes: true }}); blockquote { font-family: Menlo, Monaco, "Courier New", monospace; color: #333333; display: block; padding: 8.5px; margin: 0 0 9px; font-size: 12px; line-height: 18px; background-color: #f5f5f5; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.15); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; white-space: pre; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; }

Problem Statement

You have just arrived in a small country. Unfortunately a huge hurricane swept across the country a few days ago.

The country is made up of nn islands, numbered 11 through nn. Many bridges connected the islands, but all the bridges were washed away by a flood. People in the islands need new bridges to travel among the islands again.

The problem is cost. The country is not very wealthy. The government has to keep spending down. They asked you, a great programmer, to calculate the minimum cost to rebuild bridges. Write a program to calculate it!

Each bridge connects two islands bidirectionally. Each island ii has two parameters d_id\_i and p_ip\_i. An island ii can have at most d_id\_i bridges connected. The cost to build a bridge between an island ii and another island jj is calculated by p_ip_j|p\_i - p\_j|. Note that it may be impossible to rebuild new bridges within given limitations although people need to travel between any pair of islands over (a sequence of) bridges.


Input

The input is a sequence of datasets. The number of datasets is less than or equal to 6060. Each dataset is formatted as follows.

nn
p_1p\_1 d_1d\_1
p_2p\_2 d_2d\_2
:
:
p_np\_n d_nd\_n

Everything in the input is an integer. nn (2leqnleq4,0002 \\leq n \\leq 4{,}000) on the first line indicates the number of islands. Then nn lines follow, which contain the parameters of the islands. p_ip\_i (1leqp_ileq1091 \\leq p\_i \\leq 10^9) and d_id\_i (1leqd_ileqn1 \\leq d\_i \\leq n) denote the parameters of the island ii.

The end of the input is indicated by a line with a single zero.

Output

For each dataset, output the minimum cost in a line if it is possible to rebuild bridges within given limitations in the dataset. Otherwise, output 1-1 in a line.


Sample Input

4
1 1
8 2
9 1
14 2
4
181 4
815 4
634 4
370 4
4
52 1
40 1
81 2
73 1
10
330 1
665 3
260 1
287 2
196 3
243 1
815 1
287 3
330 1
473 4
0```

### Output for the Sample Input

```plain
18
634
-1
916```

* * *

### Source Name

[JAG Practice Contest for ACM-ICPC Asia Regional 2013](http://acm-icpc.aitea.net/index.php?2013%2FPractice%2F%CC%CF%B5%BC%C3%CF%B6%E8%CD%BD%C1%AA)

* * *