| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 2 초 | 2048 MB | 5 | 1 | 1 | 33.333% |
You and your pet red panda live in a one-dimensional world. Your red panda really loves eating apples. There are $N$ boxes (numbered from $1$ to $N$), each containing an apple. Box $i$ is located at point $A_i$. Unfortunately, all the boxes are locked. Luckily, you know the location of all keys; key $i$ that can unlock box $i$ is located at point $B_i$.
Currently, both you and your red panda are at point $S$. You want to gather all the apples and bring them back to point $S$ for your red panda. At any time, you can carry any number of keys and apples.
The distance between two points $p$ and $q$ is $|p - q|$. Determine the minimum total distance you need to cover to bring all the $N$ apples to point $S$.
The first line consists of two integers $N$ $S$ ($1 ≤ N ≤ 100\, 000$; $-10^9 ≤ S ≤ 10^9$).
Each of the next $N$ lines consists of two integers $A_i$ $B_i$ ($-10^9 ≤ A_i , B_i ≤ 10^9$).
Output a single integer representing the minimum total distance you need to cover to bring back all the $N$ apples to point $S$.
4 2 7 9 -1 4 7 -7 1 3
36
You can bring back all the apples in $36$ seconds by doing the following:
1 1 1 1
0