## COCI '07 Contest 6 #6 Cestarine

View as PDF

Points: 20
Time limit: 0.6s
Memory limit: 32M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

In a single day, of Luka's trucks travel a specific highway. The highway has a number of exits and entrances. An exit with a particular number is in the same location as the entrance with that number. Upon entering the highway, a truck driver receives a ticket which indicates the entrance he used. When exiting, the driver pays a toll equal to the absolute difference of the entrance and exit numbers. For example, if a ticket says he used entrance , then exiting at exit will cost him .

Luka has figured out a way to save toll money that his company daily spends. Any two drivers can meet on the highway and exchange tickets, even if their routes don't overlap. Tickets can be exchanged an arbitrary number of times.

However, a driver cannot use an exit if his ticket says he used the same entrance, since that would be suspicious.

Write a program that calculates the least total amount of tolls that the drivers can achieve by exchanging tickets.

#### Input Specification

The first line contains the integer , the number of trucks.

Each of the following lines contains two distinct integers between and . These are in order the entrance and exit numbers of one truck.

No two trucks will use the same highway entrance or the same exit.

#### Output Specification

Output the least total amount of tolls Luka's company must pay.

Note: use 64-bit integer types (long long in C/C++, int64 in Pascal).

#### Sample Input 1

3
3 65
45 10
60 25

#### Sample Output 1

32

#### Sample Input 2

3
5 5
6 7
8 8

#### Sample Output 2

5

In the first example, the first and third drivers will exchange tickets. After this, the second and third drivers exchange tickets. After this, the drivers will have the tickets respectively. The total amount in tolls is .

## Comments

There are no comments at the moment.