Two Pointers (hard version)
면접 대비시간 제한2초메모리 제한1024 MB
두 운전자가 A와 B에서 출발해 n개의 이벤트를 순서대로 방문할 때 총 이동 거리의 최솟값을 구한다.
문제
Alice and Bob are driving on a very long road that stretches from points to . Alice starts at point while Bob starts at point . There are events to visit, where event is at position . Either Alice or Bob must visit each event, but they must be visited in order (they must visit event , then event , then event , \dots then event ).
Find the minimum total distance Alice and Bob can drive to visit all events.
입력
The first line contains a single integer () --- the number of events.
The second line contains two integers and () --- Alice and Bob's starting points.
The third line contains integers () --- the locations of events either Alice or Bob must get to.
출력
Output an integer --- the minimum total distance Alice and Bob drive.
힌트
In the first example:
- Bob moves from position to position to attend event , driving units.
- Alice moves from position to position to attend event , driving unit.
- Bob moves from position to position for event , driving unit.
- Bob stays at position , attending event , driving units.
- Bob moves from position to position for event , driving units.
The total distance travelled is .
In the second example, Alice visits all events.