QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 256 MB Total points: 100 Hackable ✓

#17287. Two Pointers (hard version)

Statistics

Alice and Bob are driving on a very long road that stretches from points $-10^9$ to $10^9$. Alice starts at point $A$ while Bob starts at point $B$. There are $n$ events to visit, where event $i$ is at position $t_i$. Either Alice or Bob must visit each event, but they must be visited in order (they must visit event $1$, then event $2$, then event $3$, \dots then event $n$).

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

Input

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

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

The third line contains $n$ integers $t_1,t_2,\dots,t_n$ ($-10^9\le t_i\le10^9$) --- the locations of events either Alice or Bob must get to.

Output

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

Scoring

Subtask 1 ($5$ points) $|t_i|,|A|\le1000,B=10^9$

Subtask 2 ($8$ points) $n\le20$

Subtask 3 ($19$ points) $n\le3000$

Subtask 4 ($12$ points) $n\le10^5,|t_i|,|A|,|B|\le100$

Subtask 5 ($43$ points) $|t_i|,|A|,|B|\le2\cdot10^5$

Subtask 6 ($13$ points) No additional constraints

Examples

Input 1

5
2 3
5 1 4 4 7

Output 1

7

Input 2

6
540 152
450 600 532 496 325 336

Output 2

526

Input 3

8
35 315
-406 -543 114 205 -840 161 540 -731

Output 3

1699

Notes

In the first example:

  • Bob moves from position $3$ to position $5$ to attend event $1$, driving $2$ units.
  • Alice moves from position $2$ to position $1$ to attend event $2$, driving $1$ unit.
  • Bob moves from position $5$ to position $4$ for event $3$, driving $1$ unit.
  • Bob stays at position $4$, attending event $4$, driving $0$ units.
  • Bob moves from position $4$ to position $7$ for event $5$, driving $3$ units.

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

In the second example, Alice visits all events.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.