#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
int n;
int a[5005];
int b[5005];
long long f[2][5005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + 1 + n);
  for (int i = 0; i <= 1; i++) {
    for (int j = 0; j <= n; j++) {
      f[i][j] = INF;
    }
  }
  for (int i = 1; i <= n; i++) {
    f[1][i] = abs(a[1] - b[i]);
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      f[0][j] = f[1][j];
      f[1][j] = INF;
    }
    long long l[5005];
    for (int j = 0; j <= n; j++) {
      l[j] = INF;
    }
    for (int j = 1; j <= n; j++) {
      l[j] = min(l[j - 1], f[0][j]);
    }
    for (int j = 1; j <= n; j++) {
      f[1][j] = l[j] + abs(a[i] - b[j]);
    }
  }
  long long ans = INF;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, f[1][i]);
  }
  cout << ans << "\n";
}
