#P7194. [COCI 2007/2008 #6] CESTARINE

    ID: 7528 远端评测题 1000ms 32MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP2007O2优化COCI(克罗地亚)

[COCI 2007/2008 #6] CESTARINE

Problem Description

Luka’s nn trucks are driving on a highway. There are many entrances and exits on the highway. We consider entrances/exits with the same number to be at the same location.

After entering the highway, a driver receives a slip of paper with their entrance number written on it. When leaving, the toll the driver pays is equal to the absolute difference between the entrance number and the exit number.

Luka is someone who likes to take advantage of small savings. He发现 that even if their routes do not overlap, drivers can exchange their slips on the highway.

However, entering and exiting cannot happen at entrances/exits that are at the same location.

Please write a program to find the minimum total toll.

Input Format

The first line contains a positive integer nn, the number of trucks.

The next nn lines each contain two positive integers xi,yix_i, y_i, representing the entrance number and the exit number.

Output Format

The first line contains a positive integer ansans, the minimum total toll.

3
3 65
45 10
60 25 

32
3
5 5
6 7
8 8 

5

Hint

Sample #1 Explanation

The minimum total toll is 6560+103+2545=32 |65−60| + |10−3| + |25−45| = 32.

Constraints

For 100%100\% of the testdata, 1n1051 \le n \le 10^5, 1x,y1061 \le x, y \le 10^6, xyx \not= y.

Hint

Please use a 6464-bit integer type (in C / C++ it is long long, in Pascal it is int64), otherwise the answer may be wrong (i.e., Wrong Answer).

Notes

  • This problem is worth 8080 points in total.
  • This problem enables the O2 optimization switch by default.
  • Translated from COCI2007-2008 CONTEST #6 T6 CESTARINE, translator
    https://www.luogu.com.cn/user/219791

Translated by ChatGPT 5