参考:
http://www.cnblogs.com/kuangbin/archive/2013/04/21/3034081.html
插入,找前驱,找后继
#pragma comment(linker, "/STACK:102400000000,102400000000")
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
typedef long long LL;
typedef vector <int> VI;
const int INF = 0x3f3f3f3f;
const double eps = 1e-10;
const int maxn = 2111111;
#define ll ch[x][0]
#define rr ch[x][1]
#define KT (ch[ch[rt][1]][0])
int a[maxn];
struct SplayTree{
int ch[maxn][2];
int pre[maxn], val[maxn];
int rt, tot;
void Rotate(int x, int f)
{
int y = pre[x];
ch[y][!f] = ch[x][f];
pre[ch[x][f]] = y;
pre[x] = pre[y];
if (pre[x]) ch[pre[y]][ch[pre[y]][1] == y] = x;
ch[x][f] = y;
pre[y] = x;
}
void Splay(int x, int goal)
{
while (pre[x] != goal)
{
if (pre[pre[x]] == goal) Rotate(x, ch[pre[x]][0] == x);
else
{
int y = pre[x], z = pre[y];
int f = ( ch[z][0] == y );
if (ch[y][f] == x) Rotate(x, !f), Rotate(x, f);
else Rotate(y, f), Rotate(x, f);
}
}
if (goal == 0) rt = x;
}
void Newnode(int &x, int v, int f)
{
x = ++tot;
ll = rr = 0;
pre[x] = f;
val[x] = v;
}
void Init()
{
ch[0][0] = ch[0][1] = pre[0] = 0;
val[0] = 0;
rt = tot = 0;///
Newnode(rt, -INF, 0);
Newnode(ch[rt][1], INF, rt);
}
void Insert(int &x, int v, int f)
{
if (!x)
{
Newnode(x, v, f);
Splay(x, 0);///
}
else Insert(ch[x][val[x] < v], v, x);
}
void Insert(int v) { Insert(rt, v, 0); }
// void Insert(int v)///!!!
// {
// if (rt == 0)
// {
// Newnode(rt, v, 0);
// }
// int x = rt;
// while (ch[x][ val[x] < v ])
// x = ch[x][ val[x] < v ];
// Newnode(ch[x][val[x] < v], v, x);
// Splay(ch[x][val[x] < v], 0);///0
// }
int getmin()
{
int x = ch[rt][0];
while (rr) x = rr;
return val[x];
}
int getmax()
{
int x = ch[rt][1];
while (ll) x = ll;
return val[x];
}
int getans()
{
int tmp = INF;
tmp = min(tmp, val[rt] - getmin());
tmp = min(tmp, getmax() - val[rt]);
return tmp;
}
// void findpre(int x, int v, int &ans)
// {
// if (!x) return ;
// if (val[x] <= v)
// {
// ans = val[x];
// findpre(rr, v, ans);
// }
// else findpre(ll, v, ans);
// }
// void findnext(int x, int v, int &ans)
// {
// if (!x) return ;
// if (val[x] >= v)
// {
// ans = val[x];
// findnext(ll, v, ans);
// }
// else findnext(rr, v, ans);
// }
// int getans(int v)
// {
// int tmp = INF;
// int x, y;
// findpre(rt, v, x); tmp = min(tmp, v - x);
// findnext(rt, v, y); tmp = min(tmp, y - v);
// return tmp;
// }
}sp;
int main()
{
int n, m;
int x;
int ans = 0;
while (~scanf("%d", &n))
{
sp.Init();
REP(i, n)
{
if (scanf("%d", &x)==EOF) x = 0;
sp.Insert(x);
if (!i) ans += x;
else ans += sp.getans();
}
printf("%d\n", ans);
}
return 0;
}