Two Pointers (hard version)
Time limit2sMemory limit1024 MB
Alice and Bob must visit n events in order, each taken by one of them, and the task is to find the minimum total distance they drive.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Segment tree
- Solved
- No attempts yet
Problem
Alice and Bob are driving on a very long road that stretches from points to . Alice starts at point and 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 (event 1, then event 2, then event 3, and so on up to event ).
Find the minimum total distance Alice and Bob can drive to visit all events.
Input
The first line contains a single integer (), the number of events.
The second line contains two integers and (), the starting points of Alice and Bob.
The third line contains integers (), the locations of the events that either Alice or Bob must reach.
Output
Print one integer, the minimum total distance Alice and Bob drive.