P2234](https://www.luogu.org/problem/P2234)
其实就是问每一天的前驱和后继 取个min即可 所以我们上平衡树
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <ctime>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
const int MAX_N = 40025;
struct fhqtp
{
int ch[2],rd,size,val;
}t[MAX_N];
int arr[MAX_N],root,r1,r2,r3,r4,cnt = 0;
namespace fhq
{
void up(int rt) {t[rt].size = t[t[rt].ch[0]].size+t[t[rt].ch[1]].size+1;}
void split(int x,int val,int &a,int &b)
{
if(!x) {a = b = 0;return;}
if(t[x].val>val) b = x,split(t[x].ch[0],val,a,t[x].ch[0]);
else a = x,split(t[x].ch[1],val,t[x].ch[1],b);
up(x);
}
int Merge(int x,int y)
{
if(x==0||y==0) return x + y;
if(t[x].rd<t[y].rd)
{
t[x].ch[1] = Merge(t[x].ch[1],y);
up(x);return x;
}
else
{
t[y].ch[0] = Merge(x,t[y].ch[0]);
up(y);return y;
}
}
int newnode(int val)
{
t[++cnt].val = val;t[cnt].rd = rand();t[cnt].size = 1;
return cnt;
}
void Insert(int val)
{
split(root,val,r1,r2);
root = Merge(r1,Merge(newnode(val),r2));
}
int Kth(int rt,int k)
{
int x = rt;
while(1)
{
int ls = t[x].ch[0],rs = t[x].ch[1];
if(k<=t[ls].size) x = ls;
else if(k>t[ls].size+1)
k-=t[ls].size+1,x = rs;
else return x;
}
}
int pre(int val)
{
r1 = r2 = 0;split(root,val-1,r1,r2);
int ans = t[Kth(r1,t[r1].size)].val;
root = Merge(r1,r2);
return ans;
}
int nex(int val)
{
r1 = r2 = 0;split(root,val,r1,r2);
int ans = t[Kth(r2,1)].val;
root = Merge(r1,r2);
return ans;
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("turnover.in","r",stdin);
//freopen("turnover.out","w",stdout);
int n,ans = 0;srand(19260817);
scanf("%d",&n);
fhq::Insert(inf);fhq::Insert(_inf);
for(int i = 1;i<=n;++i)
{
scanf("%d",&arr[i]);
if(i==1) ans+=arr[i];
else
{
ans+=min(abs(fhq::pre(arr[i]+1)-arr[i]),abs(fhq::nex(arr[i]-1)-arr[i]));
}
fhq::Insert(arr[i]);
}
printf("%d\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}