Language: C++

#include <bits/stdc++.h>
#define N 256101
#define Q 3414680
using namespace std;

typedef long long ll;
const ll INF = 0x7f7f7f7f7f7f7f7fll;

struct tuple{
	int w, id, pos;
	tuple (int w0 = 0, int id0 = 0, int pos0 = 0): w(w0), id(id0), pos(pos0) {}
};

int n, A, B, i, inf, sup;
int x[N], fst[N];
ll f[2][2][N], *F[2], *G[2], ans;

tuple que[Q];

template <typename T>
inline void up(T &x, const T y){x < y ? x = y : 0;}
template <typename T>
inline void down(T &x, const T y){x > y ? x = y : 0;}
inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}

void bfs(){
	int h, t = 1, W = -1, i, j, k; ll f0, f1;
	tuple z, z0, z1;
	que[0] = tuple(0, 0, B);
	for(h = 0; h < t; h++){
		if(que[h].w > W){
			W = que[h].w;
			fst[W] = h;
			swap(F[0], G[0]); swap(F[1], G[1]);
			if(W >= 1)
				for(i = fst[W - 1]; i < h; i++)
					f[que[i].w & 1][que[i].id][que[i].pos] = INF;
			sort(que + h, que + t, cmp);
			for(j = h, i = h + 1; i < t; i++){
				z0 = que[j]; z1 = que[i];
				if(z0.id == z1.id)
					if(f[z0.w & 1][z0.id][z0.pos] + abs(z0.pos - z1.pos) <= f[z1.w & 1][z1.id][z1.pos]){
						f[z1.w & 1][z1.id][z1.pos] = INF;
						continue;
					}
				que[++j] = que[i];
			}
			t = ++j;
		}
		z = que[h];
		if(W == n){down(ans, F[z.id][z.pos]); continue;}
		if(z.id){ // right
			z0 = tuple(W + 1, 0, x[W]); // left
			f0 = F[1][z.pos] + abs(z.pos - x[z0.w]);
			z1 = tuple(W + 1, 1, z.pos); // right
			f1 = F[1][z.pos] + abs(x[W] - x[z1.w]);
		}else{ // left
			z0 = tuple(W + 1, 0, z.pos); // left
			f0 = F[0][z.pos] + abs(x[W] - x[z0.w]);
			z1 = tuple(W + 1, 1, x[W]); // right
			f1 = F[0][z.pos] + abs(z.pos - x[z1.w]);
		}
		if(x[z0.w] <= z0.pos && f0 < G[0][z0.pos]){
			if(G[0][z0.pos] >= INF)
				que[t++] = z0;
			G[0][z0.pos] = f0;
		}
		if(z1.pos <= x[z1.w] && f1 < G[1][z1.pos]){
			if(G[1][z1.pos] >= INF)
				que[t++] = z1;
			G[1][z1.pos] = f1;
		}
	}
}

int main(){
	scanf("%d%d%d%d", &inf, &n, &A, &B);
	if(A > B) swap(A, B);
	for(sup = i = 1; i <= n; i++){
		scanf("%d", x + i);
		down(inf, x[i]); up(sup, x[i]);
	}
	memset(f, 127, sizeof f);
	G[0] = f[0][0]; G[1] = f[0][1];
	F[0] = f[1][0]; F[1] = f[1][1];
	x[0] = A; G[0][B] = 0;
	ans = LLONG_MAX;
	bfs();
	printf("%lld\n", ans);
	return 0;
}

Compiler output:
a.cc:18:1: error: reference to 'tuple' is ambiguous
   18 | tuple que[Q];
      | ^~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_pair.h:92:11: note: candidates are: 'template<class ...> class std::tuple'
   92 |     class tuple;
      |           ^~~~~
a.cc:9:8: note:                 'struct tuple'
    9 | struct tuple{
      |        ^~~~~
a.cc:24:23: error: reference to 'tuple' is ambiguous
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                       ^~~~~
/usr/include/c++/14/bits/stl_pair.h:92:11: note: candidates are: 'template<class ...> class std::tuple'
   92 |     class tuple;
      |           ^~~~~
a.cc:9:8: note:                 'struct tuple'
    9 | struct tuple{
      |        ^~~~~
a.cc:24:39: error: reference to 'tuple' is ambiguous
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                       ^~~~~
/usr/include/c++/14/bits/stl_pair.h:92:11: note: candidates are: 'template<class ...> class std::tuple'
   92 |     class tuple;
      |           ^~~~~
a.cc:9:8: note:                 'struct tuple'
    9 | struct tuple{
      |        ^~~~~
a.cc: In function 'bool cmp(const int&, const int&)':
a.cc:24:58: error: request for member 'id' in 'A', which is of non-class type 'const int'
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                                          ^~
a.cc:24:65: error: request for member 'id' in 'B', which is of non-class type 'const int'
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                                                 ^~
a.cc:24:73: error: request for member 'id' in 'A', which is of non-class type 'const int'
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                                                         ^~
a.cc:24:81: error: request for member 'id' in 'B', which is of non-class type 'const int'
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                                                                 ^~
a.cc:24:91: error: request for member 'w' in 'A', which is of non-class type 'const int'
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                                                                           ^
a.cc:24:100: error: request for member 'id' in 'A', which is of non-class type 'const int'
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                                                                                    ^~
a.cc:24:106: error: request for member 'pos' in 'A', which is of non-class type 'const int'
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                                                                                          ^~~
a.cc:24:117: error: request for member 'w' in 'B', which is of non-class type 'const int'
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                                                                                                     ^
a.cc:24:126: error: request for member 'id' in 'B', which is of non-class type 'const int'
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                                                                                                              ^~
a.cc:24:132: error: request for member 'pos' in 'B', which is of non-class type 'const int'
   24 | inline bool cmp(const tuple &A, const tuple &B){return A.id < B.id || A.id == B.id && f[A.w & 1][A.id][A.pos] < f[B.w & 1][B.id][B.pos];}
      |                                                                                                                                    ^~~
a.cc: In function 'void bfs()':
a.cc:28:9: error: reference to 'tuple' is ambiguous
   28 |         tuple z, z0, z1;
      |         ^~~~~
/usr/include/c++/14/bits/stl_pair.h:92:11: note: candidates are: 'template<class ...> class std::tuple'
   92 |     class tuple;
      |           ^~~~~
a.cc:9:8: note:                 'struct tuple'
    9 | struct tuple{
      |        ^~~~~
a.cc:29:9: error: 'que' was not declared in this scope
   29 |         que[0] = tuple(0, 0, B);
      |         ^~~
a.cc:29:18: error: reference to 'tuple' is ambiguous
   29 |         que[0] = tuple(0, 0, B);
      |                  ^~~~~
/usr/include/c++/14/bits/stl_pair.h:92:11: note: candidates are: 'template<class ...> class std::tuple'
   92 |     class tuple;
      |           ^~~~~
a.cc:9:8: note:                 'struct tuple'
    9 | struct tuple{
      |        ^~~~~
a.cc:40:33: error: 'z0' was not declared in this scope; did you mean 'f0'?
   40 |                                 z0 = que[j]; z1 = que[i];
      |                                 ^~
      |                                 f0
a.cc:40:46: error: 'z1' was not declared in this scope; did you mean 'f1'?
   40 |                                 z0 = que[j]; z1 = que[i];
      |                                              ^~
      |                                              f1
a.cc:50:17: error: 'z' was not declared in this scope
   50 |                 z = que[h];
      |                 ^
a.cc:53:25: error: 'z0' was not declared in this scope; did you mean 'f0'?
   53 |                         z0 = tuple(W + 1, 0, x[W]); // left
      |                         ^~
      |                         f0
a.cc:53:30: error: reference to 'tuple' is ambiguous
   53 |                         z0 = tuple(W + 1, 0, x[W]); // left
      |                              ^~~~~
/usr/include/c++/14/bits/stl_pair.h:92:11: note: candidates are: 'template<class ...> class std::tuple'
   92 |     class tuple;
      |           ^~~~~
a.cc:9:8: note:                 'struct tuple'
    9 | struct tuple{
      |        ^~~~~
a.cc:55:25: error: 'z1' was not declared in this scope; did you mean 'f1'?
   55 |                         z1 = tuple(W + 1, 1, z.pos); // right
      |                         ^~
      |                         f1
a.cc:55:30: error: reference to 'tuple' is ambiguous
   55 |                         z1 = tuple(W + 1, 1, z.pos); // right
      |                              ^~~~~
/usr/include/c++/14/bits/stl_pair.h:92:11: note: candidates are: 'template<class ...> class std::tuple'
   92 |     class tuple;
      |           ^~~~~
a.cc:9:8: note:                 'struct tuple'
    9 | struct tuple{
      |        ^~~~~
a.cc:58:25: error: 'z0' was not declared in this scope; did you mean 'f0'?
   58 |                         z0 = tuple(W + 1, 0, z.pos); // left
      |                         ^~
      |                         f0
a.cc:58:30: error: reference to 'tuple' is ambiguous
   58 |                         z0 = tuple(W + 1, 0, z.pos); // left
      |                              ^~~~~
/usr/include/c++/14/bits/stl_pair.h:92:11: note: candidates are: 'template<class ...> class std::tuple'
   92 |     class tuple;
      |           ^~~~~
a.cc:9:8: note:                 'struct tuple'
    9 | struct tuple{
      |        ^~~~~
a.cc:60:25: error: 'z1' was not declared in this scope; did you mean 'f1'?
   60 |                         z1 = tuple(W + 1, 1, x[W]); // right
      |                         ^~
      |                         f1
a.cc:60:30: error: reference to 'tuple' is ambiguous
   60 |                         z1 = tuple(W + 1, 1, x[W]); // right
      |                              ^~~~~
/usr/include/c++/14/bits/stl_pair.h:92:11: note: candidates are: 'template<class ...> class std::tuple'
   92 |     class tuple;
      |           ^~~~~
a.cc:9:8: note:                 'struct tuple'
    9 | struct tuple{
      |        ^~~~~
a.cc:63:22: error: 'z0' was not declared in this scope; did you mean 'f0'?
   63 |                 if(x[z0.w] <= z0.pos && f0 < G[0][z0.pos]){
      |                      ^~
      |                      f0
a.cc:68:20: error: 'z1' was not declared in this scope; did you mean 'f1'?
   68 |                 if(z1.pos <= x[z1.w] && f1 < G[1][z1.pos]){
      |                    ^~
      |                    f1