4320: 【基础】美食街(1769)

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:0 解决:0

题目描述

小 lns="http://www.w3.org/1998/Math/MathML">� 是一个美食爱好者。市里新开了一家美食街,这当然是小 lns="http://www.w3.org/1998/Math/MathML">� 不能错过的盛宴啦。

美食街是一条笔直的直线,在街道的不同的点上,有着不同种类的美食,第 lns="http://www.w3.org/1998/Math/MathML">� 个美食店的位置为 lns="http://www.w3.org/1998/Math/MathML">��,美食品种编号为 lns="http://www.w3.org/1998/Math/MathML">��。

这么多种美食让小 lns="http://www.w3.org/1998/Math/MathML">� 眼花缭乱,小 lns="http://www.w3.org/1998/Math/MathML">� 想要品尝所有品种的美食,又想走最少的路。

请编程帮助小 lns="http://www.w3.org/1998/Math/MathML">� 计算,他品尝所有品种的美食,要走的最短路程有多长?

输入

第 lns="http://www.w3.org/1998/Math/MathML">1 行有一个整数 lns="http://www.w3.org/1998/Math/MathML">� ,表示街道上美食店的总数量;

接下来 lns="http://www.w3.org/1998/Math/MathML">� 行,每行有 lns="http://www.w3.org/1998/Math/MathML">2 个整数 lns="http://www.w3.org/1998/Math/MathML">�� 和 lns="http://www.w3.org/1998/Math/MathML">��,分别代表了不同美食店的位置,以及这个美食店的美食品种。

测试数据保证同一个位置 lns="http://www.w3.org/1998/Math/MathML">�� ,只会开一家美食店。

输出

输出一个整数,代表小 lns="http://www.w3.org/1998/Math/MathML">� 要走的最短路程;

路程的计算方式为:如果从 lns="http://www.w3.org/1998/Math/MathML">�� 点到 lns="http://www.w3.org/1998/Math/MathML">�� (lns="http://www.w3.org/1998/Math/MathML">��≤��)包含了所有品种的美食,那么路程长度 lns="http://www.w3.org/1998/Math/MathML">=��−��。

样例输入 复制

7
2 2 
1 3
5 2
4 1
6 3
10 2
8 1

样例输出 复制

2

提示

【样例解释】

样例中 lns="http://www.w3.org/1998/Math/MathML">�� 可选取区间 lns="http://www.w3.org/1998/Math/MathML">[4,6],可以包含所有的美食品种。

【数据范围】

对于 lns="http://www.w3.org/1998/Math/MathML">20% 的数据,lns="http://www.w3.org/1998/Math/MathML">10≤�≤20;

对于另外 lns="http://www.w3.org/1998/Math/MathML">40% 的数据,lns="http://www.w3.org/1998/Math/MathML">10≤�≤1000;

对于 lns="http://www.w3.org/1998/Math/MathML">100% 的数据,lns="http://www.w3.org/1998/Math/MathML">1≤�≤50000,lns="http://www.w3.org/1998/Math/MathML">1≤��,��≤109;