Page not loading? Try clicking here.
Placeholder

#3234

Arrow (HS) 1s 128MB

Problems

N points are given on a line, and each point has one of N colors. 

For convenience, colors are represented by numbers from 1 to N, and the coordinates of the points are all distinct.

For each point p, we want to connect it to another point q using a straight arrow starting from p

Here, point q must be the point closest to p among the points of the same color as p.

If there are two or more closest points, choose any one of them.

For each point p, draw one arrow  \vec{l_p} going to q that satisfies the above condition. 

Specifically, if there is no other point of the same color on the line for point p, then |\vec{l_p}|=0. Here,  |\vec{l_p}| represents the length of the arrow \vec{l_p}.

For example, when points are represented as ordered pairs (coordinate, color), let p1 = (0, 1), p2 = (1, 2), p3 = (3, 1), p4= (4, 1)​

The arrow  \vec{l_p} of point p1​ connects p1​→ p3.

The arrows \vec{l_{p_3}} and \vec{l_{p_4}} of points p3​ and p4​ connect p3​→p4​​ and p4→p3​, respectively. 

In the case of point p2, no other point of the same color exists. 

Therefore, the sum of the lengths of all arrows is |\vec{l_{p1}}|+|\vec{l_{p2}}|+|\vec{l_{p3}}|+|\vec{l_{p4}}|=3+0+1+1=5 .

Given the coordinates and colors of the points, write a program that outputs the sum of the lengths of the arrows starting from all points, in other words, \sum_{p}|\vec{l_p}|.


Input

The following information is given as standard input.

The first line contains an integer N representing the number of points. 

Each of the next N lines contains two integers x and y representing the coordinate and color of a point.


Output

Print the sum of the lengths of the arrows starting from all points to standard output.

[Constraints]

 In all subtasks, the coordinates x and colors y of the points satisfy 0 ≤ x ≤10^9 and 1≤ y ≤ N, respectively. 


Subtask

# Score Condition
#121

The colors of the points are all the same, and the number of points satisfies 1 ≤ N ≤ 10

#212

There are exactly two colors of points, and the number of points satisfies 1 ≤ N ≤ 2,000

#329

The number of points satisfies 1 ≤ N ≤ 10​,000

#438

The number of points satisfies 1 ≤ N ≤ 100​,000​.


Example #1

4

0 1
1 2
3 1
4 1
5

Example #2

4

1 1
0 2
4 3
3 4
0

Example #3

9

10 2
11 3
20 2
22 1
25 1
0 1
4 2
5 2
7 2
45


Source

KOI 전국 2018 고1

You must sign in to write code.