This page is still under construction.

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

Distance

Interview

Time limit3sMemory limit1024 MB

Summary
Given N points on a grid, compute the sum of Manhattan distances over all distinct pairs.
Level

Medium5 of 10

Topics
Math, Sorting, Prefix sum, Array
Solved
No attempts yet

Problem

The City of Manhattan is organized as a grid of streets and avenues, with streets running in the North-South direction and avenues running in the East-West direction. Streets are numbered from East to West starting from 1, and avenues are numbered from North to South starting from 1. Each intersection is labelled by the street and avenue number (s,a)(s, a). The distance between two intersections (s1,a1)(s_1, a_1) and (s2,a2)(s_2, a_2) is ∣s1−s2∣+∣a1−a2∣|s_1-s_2| + |a_1-a_2|.

Your company operates several food trucks at different intersections in Manhattan, and you want to spread them out so they do not compete with each other. To estimate how spread out they are, you have decided to compute the total distance between every distinct pair of your food trucks. A small total distance would mean that on average, a pair of food trucks is too close together.

What is the total distance between every distinct pair of food trucks?

Input

The first line of input contains an integer NN (2≤N≤200 0002 \leq N \leq 200\,000), which is the number of food trucks.

The next nn lines describe the food trucks' locations. Each of these lines contains two integers ss (1≤s≤1 000 0001 \leq s \leq 1\,000\,000), which is the street number of this food truck, and aa (1≤a≤1 000 0001 \leq a \leq 1\,000\,000), which is the avenue number of this food truck.

Output

Display the total distance between every distinct pair of food trucks.

Examples1

  1. Example 1

    Input
    3
    1 1
    4 5
    2 3
    
    Expected output
    14