/ * Median distance to all and a minimum number because you can only change a number, so we look for a number, change it and the adjacent median number, the answer smallest first seek a primary answer to each stored number with the number of adjacent down vector, and then find the median of the sort, a reduced amount calculation * / #include <bits / STDC ++ H.> #include <vector> the using namespace STD; #define LL Long Long # DEFINE N 200005 Vector < int > V [N]; int n-, m, A [N]; int main () { CIN >> >> n- m; for ( int I = . 1 ; I <= m; I ++) CIN >> A [I]; Long Long SUM = 0 ; for(int i=1;i<m;i++) sum+=abs(a[i]-a[i+1]); /*if(a[1]!=a[2]){ v[a[1]].push_back(a[2]); v[a[2]].push_back(a[1]); }*/ for(int i=1;i<m;i++)if(a[i]!=a[i+1]){ v[a[i]].push_back(a[i+1]); v[a[i+1]].push_back(a[i]); } for(int i=1;i<=200000;i++)sort(v[i].begin(),v[i].end()); ll ans=sum; for(int i=1;i<=200000;i++)if(v[i].size()){ ll last=0; for(int j=0;j<v[i].size();j++) last+=abs(i-v[i][j]); ll now=0,tmp=v[i].size()/2; for(int j=0;j<v[i].size();j++) now+=abs(v[i][tmp]-v[i][j]); ans=min(ans,sum-last+now); } cout<<ans<<'\n'; }