#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 300005;
int n;
void solv() {
  scanf("%d", &n);
  long long ans = 0;
  priority_queue<int> q;
  while (n--) {
    int x;
    scanf("%d", &x);
    q.push(-x);
    if (!q.empty() && q.top() + x > 0) {
      ans += q.top() + x;
      q.pop();
      q.push(-x);
    }
  }
  printf("%lld\n", ans);
}
int main() {
  solv();
  return 0;
}
