全部WA掉了……
#include<bits/stdc++.h>
using namespace std;
int n,x,t;
long long ans;
set<int>s;
set<int>::iterator it,tmp;
inline int read(){
int s=0,f=1;
char ch=getchar();
while (!isdigit(ch)){
if (ch=='-') f=-1;
ch=getchar();
}
while (isdigit(ch)){
s=s*10+(ch-'0');
ch=getchar();
}
return s*f;
}
int main(){
n=read();
s.insert(0);
for (int i=1;i<=n;i++){
x=read();
it=s.lower_bound(x);
if (it!=s.end()){
if ((*it)==x) continue;
tmp=it;
tmp--;
ans+=min(x-(*tmp),(*it)-x);
}
else{
ans+=x-(*it);
s.insert(x);
}
}
printf("%d\n",ans);
return 0;
}
谢谢!