AtCoder Regular Contest 073
F
Many Moves
AtCoder
⌀ 2867
原题
← E→
翻译中…

Problem Statement

翻译中…

There are $N$ squares in a row. The squares are numbered $1, 2, ..., N$ from left to right.

翻译中…

You have two pieces, initially placed on square $A$ and $B$, respectively. You will be asked to process $Q$ queries of the following kind, in the order received:

翻译中…
  • Given an integer $x_i$, move one of the two pieces of your choice to square $x_i$.
翻译中…

Here, it takes you one second to move a piece one square. That is, the time it takes to move a piece from square $X$ to $Y$ is $|X-Y|$ seconds.

翻译中…

Your objective is to process all the queries in the shortest possible time.

翻译中…

You may only move the pieces in response to queries, and you may not move both pieces at the same time. Also, it is not allowed to rearrange the order in which queries are given. It is, however, allowed to have both pieces in the same square at the same time.

翻译中…

Constraints

翻译中…
  • $1 ≤ N, Q ≤ 200,000$
  • $1 ≤ A, B ≤ N$
  • $1 ≤ x_i ≤ N$
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N Q A B
x_1 x_2 ... x_Q

翻译中…

Output

翻译中…

Let the shortest possible time to process all the queries be $X$ seconds. Print $X$.

翻译中…

样例

Input #1
8 3 1 8
3 5 1
Output #1
7
All the queries can be processed in seven seconds, by: - moving the piece at square $1$ to $3$ - moving the piece at square $8$ to $5$ - moving the piece at square $3$ to $1$
Input #2
9 2 1 9
5 1
Output #2
4
The piece at square $9$ should be moved first.
Input #3
9 2 1 9
5 9
Output #3
4
The piece at square $1$ should be moved first.
Input #4
11 16 8 1
1 1 5 1 11 4 5 2 5 3 3 3 5 5 6 7
Output #4
21
题目信息
题号
arc073_d
时间限制
2 s
内存限制
256 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。