IOI '07 - Zagreb, Croatia
A new pirate sailing ship is being built. The ship has
Different configurations of sails generate different amounts of thrust when exposed to the wind. Sails in front of other sails at the same height get less wind and contribute less thrust. For each sail we define its inefficiency as the total number of sails that are behind this sail and at the same height. Note that "in front of" and "behind" relate 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 a configuration is the sum of the inefficiencies of all individual sails.

This ship has 6 masts, of heights 3, 5, 4, 2, 4 and 3 from front (left side of image) to back.
This distribution of sails gives a total inefficiency of 10. The individual inefficiency of each sail is written inside the sail.
Write a program that, given the height and the number of sails on each
of the
Input Specification
The first line of input contains an integer
Output Specification
Output should consist of a single integer, the smallest possible total inefficiency.
Note: use a 64-bit integer type to calculate and output the result
(long long
in C/C++, int64
in Pascal).
Sample Input
6
3 2
5 3
4 1
2 1
4 3
3 2
Sample Output
10
Note: In test cases worth a total of 25% of the points, the number
of ways to arrange the sails will not exceed
Comments