#P15292. [MCO 2023] Two Pointers (hard version)

[MCO 2023] Two Pointers (hard version)

Description

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 while 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\textbf{in order} (they must visit event 11, then event 22, then event 33, \dots then event nn).

Find the minimum total\textbf{total} distance Alice and Bob can drive to visit all events.

Input Format

The first line contains a single integer nn (1n31051\le n\le3\cdot10^5) --- the number of events.

The second line contains two integers AA and BB (109A,B109-10^9\le A,B\le10^9) --- Alice and Bob's starting points.

The third line contains nn integers t1,t2,,tnt_1,t_2,\dots,t_n (109ti109-10^9\le t_i\le10^9) --- the locations of events either Alice or Bob must get to.

Output Format

Output an integer --- the minimum total distance Alice and Bob drive.

5
2 3
5 1 4 4 7
7
6
540 152
450 600 532 496 325 336
526
8
35 315
-406 -543 114 205 -840 161 540 -731
1699

Hint

Note

In the first example:

  • Bob moves from position 33 to position 55 to attend event 11, driving 22 units.
  • Alice moves from position 22 to position 11 to attend event 22, driving 11 unit.
  • Bob moves from position 55 to position 44 for event 33, driving 11 unit.
  • Bob stays at position 44, attending event 44, driving 00 units.
  • Bob moves from position 44 to position 77 for event 55, driving 33 units.

The total distance travelled is 2+1+1+0+3=72+1+1+0+3=7.

In the second example, Alice visits all events.

Scoring

Subtask 1 (55 points) ti,A1000,B=109|t_i|,|A|\le1000,B=10^9

Subtask 2 (88 points) n20n\le20

Subtask 3 (1919 points) n3000n\le3000

Subtask 4 (1212 points) n105,ti,A,B100n\le10^5,|t_i|,|A|,|B|\le100

Subtask 5 (4343 points) ti,A,B2105|t_i|,|A|,|B|\le2\cdot10^5

Subtask 6 (1313 points) No additional constraints