#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long a[maxn], n, b[maxn], f[maxn];
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), b[i] = a[i];
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (j == 1)
        f[j] += abs(b[j] - a[i]);
      else
        f[j] = min(f[j - 1], f[j] + abs(b[j] - a[i]));
  printf("%lld\n", f[n]);
  return 0;
}
