Under construction

Still being built. Things may change

Two Pointers (hard version)

Time limit2sMemory limit1024 MB

Summary
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 −109-10^9 to 10910^9. Alice starts at point AA and Bob starts at point BB. There are nn events to visit, where event ii is at position tit_i. 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 nn).

Find the minimum total distance Alice and Bob can drive to visit all events.

Input

The first line contains a single integer nn (1≤n≤3⋅1051\le n\le3\cdot10^5), the number of events.

The second line contains two integers AA and BB (−109≤A,B≤109-10^9\le A,B\le10^9), the starting points of Alice and Bob.

The third line contains nn integers t1,t2,…,tnt_1, t_2, \dots, t_n (−109≤ti≤109-10^9\le t_i\le10^9), the locations of the events that either Alice or Bob must reach.

Output

Print one integer, the minimum total distance Alice and Bob drive.

Examples3

  1. Example 1

    Input
    5
    2 3
    5 1 4 4 7
    
    Expected output
    7
    
  2. Example 2

    Input
    6
    540 152
    450 600 532 496 325 336
    
    Expected output
    526
    
  3. Example 3

    Input
    8
    35 315
    -406 -543 114 205 -840 161 540 -731
    
    Expected output
    1699