This page is still under construction.

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

Sails

Time limit1sMemory limit128 MB

Summary
Place K sails on each mast (height H) to minimize the total count of same-height sails behind each sail.
Level

Medium7 of 10

Topics
Greedy, Sorting, Prefix sum
Solved
No attempts yet

Problem

A new pirate sailing ship is being built. The ship has NN masts (vertical poles), each divided into unit-sized segments; the height of a mast equals its number of segments. Each mast carries a number of sails, and every sail fits exactly into one segment. The sails on a single mast may be placed arbitrarily among its segments, but each segment can hold at most one sail.

Different arrangements of sails generate different amounts of thrust in the wind. A sail placed in front of other sails at the same height catches less wind and contributes less thrust. For each sail we define its inefficiency as the number of sails that are at the same height and behind it. Here "in front of" and "behind" refer to the orientation of the ship: in the figure below, "in front of" means to the left and "behind" means to the right.

The total inefficiency of an arrangement is the sum of the inefficiencies of all individual sails.

Sails arranged on six masts

The ship in the figure has 6 masts, whose heights from front (left) to back (right) are 3, 5, 4, 2, 4, and 3. The arrangement shown has a total inefficiency of 10; the number written inside each sail is that sail's inefficiency.

Given the height and the number of sails on each of the NN masts, write a program that determines the smallest possible total inefficiency.

Input

The first line contains an integer NN (2≤N≤100 0002 \le N \le 100\,000), the number of masts.

Each of the next NN lines contains two integers HH and KK (1≤H≤100 0001 \le H \le 100\,000, 1≤K≤H1 \le K \le H): the height of a mast and the number of sails on it. The masts are listed in order from the front of the ship to the back.

Output

Print a single integer: the smallest possible total inefficiency.

The result can exceed the range of a 32-bit integer, so use a 64-bit integer type (for example, long long in C/C++) to compute and print it.

Examples1

  1. Example 1

    Input
    6
    3 2
    5 3
    4 1
    2 1
    4 3
    3 2
    
    Expected output
    10