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

18339번 - Greedy Termite 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB170443623.077%

문제

There are n wooden rods vertically placed over a horizontal line. The rods are numbered 1 through n from left to right. Each rod i (1 ⩽ i ⩽ n) is placed at position xi and has a height hi.

A termite wants to eat all the rods one by one. It starts eating from an arbitrary rod s (1 ⩽ s ⩽ n). Then, after eating a rod i, the termite selects the next rod to eat based on the following method. Among the remaining rods j, the one with maximum hj −|xi −xj| is selected. If there are ties, the one with minimum |xi −xj| is selected. If there are still ties, the left-most rod is selected.

Your task is to calculate the total (horizontal) distance traveled by the termite to eat all the rods.

입력

The first line of the input contains two space-separated integers n, the number of rods, and s, the starting rod number (1 ⩽ s ⩽ n ⩽ 100 000). The rods are described in the next n lines. On the line 1 + i (1 ⩽ i ⩽ n), the i-th rod is specified with two space-separated integers xi (|xi| ⩽ 109) and hi (1 ⩽ hi ⩽ 109). Additionally, for each i (1 ⩽ i ⩽ n − 1), xi < xi+1.

출력

You should print a single integer denoting the total distance traveled by the termite.

제한

예제 입력 1

5 3
1 3
4 8
8 2
10 4
11 1

예제 출력 1

17

힌트

출처

ICPC > Regionals > Asia West Continent > Iran > Tehran Site 2019 J번

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

출처

대학교 대회

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

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