Logo
(追記) (追記ここまで)

20905번 - Exhausting Errands 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 (추가 시간 없음) 512 MB197753.846%

문제

Dolly the delivery drone is out for a busy working day. It has to complete $n$ errands in a street where $\ell$ houses are lined up in a row, numbered in ascending order as 1,ドル \dots, \ell$. The distance between adjacent houses is 1ドル$. Each errand consists of picking up a package at some house $a$ and delivering it to another house $b$. Dolly can start with any errand, complete the errands in any order, and is able to carry an unlimited number of packages at the same time. Your job is to find the minimal total distance Dolly has to cover to complete all errands. The delivery route can start and finish at arbitrary locations along the street.

Figure E.1: Illustration of Sample Input 1. The shortest route is 2ドル \rightarrow 1 \rightarrow 9 \rightarrow 4$ with length 14ドル$.

입력

The input consists of:

  • One line with two integers $\ell$ and $n$ (1ドル \le \ell \le 10^9,ドル 1ドル \le n \le 10^5$), where $\ell$ is the number of houses in the street, and $n$ is the number of errands.
  • $n$ lines, each with two integers $a$ and $b$ (1ドル \le a, b \le \ell$ with $a \not= b$) describing an errand where a package must be picked up at house $a$ and be delivered to house $b$.

출력

Output a single line with the minimal distance Dolly has to cover from picking up the first package until delivering the last package.

제한

예제 입력 1

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

예제 출력 1

14

예제 입력 2

100 3
11 50
50 49
36 35

예제 출력 2

42

힌트

출처

ICPC > Regionals > Europe > Northwestern European Regional Contest > German Collegiate Programming Contest > GCPC 2020 E번

  • 문제를 만든 사람: Sandro Esquivel
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /