#include<bits/stdc++.h>
const int N = 1e6 + 10;
int a[N];
signed main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n; std::cin >> n;
for (int i = 1; i <= n; i++) std::cin >> a[i];
std::priority_queue<int, std::vector<int>, std::greater<int>> q;
int64_t res = 0;
for (int i = 1; i <= n; i++) {
if (q.size() && a[i] > q.top()) {
res += a[i] - q.top(); q.pop();
q.push(a[i]); q.push(a[i]);
}
else q.push(a[i]);
}
std::cout << res << '\n';
return 0;
}
自用