AcWing 265. 营业额统计
原题链接
简单
作者:
皓首不倦
,
2020-09-01 21:32:05
,
所有人可见
,
阅读 365
#include <stdio.h>
#include <set>
#include <algorithm>
using namespace std;
int main(void) {
//freopen("/Users/grh/Programming/CLionProjects/Test/data.txt", "r", stdin);
long long n, val;
long long ans = 0, sub;
scanf("%lld", &n);
set<long long> vals;
for (int i = 0; i < n; i++) {
scanf("%lld", &val);
if (i == 0) {
ans += val;
} else {
auto itr = vals.lower_bound(val);
if (itr != vals.end()) {
sub = *itr - val;
} else {
sub = 0x7fffffff;
}
if (itr != vals.begin()) {
itr--;
sub = min(sub, val - *itr);
}
ans += sub;
}
vals.insert(val);
}
printf("%lld", ans);
return 0;
}