#include using namespace std; typedef long long int ll; typedef unsigned long long ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } struct SlopeTrick{ ll res; priority_queue pq; SlopeTrick():res(0),pq(){} void add(const ll x){ if(pq.size() and pq.top()>x){ res+=pq.top()-x; pq.pop(); pq.push(x); } pq.push(x); } }; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; SlopeTrick slope; for(int i=0;i> a; slope.add(a); } cout << slope.res << endl; }