#include <bits/stdc++.h>
int main() {
  long long n;
  std::cin >> n;
  std::vector<long long> a(n);
  for (long long i = 0; i < n; ++i) {
    std::cin >> a[i];
  }
  long long sum = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (a[i] > a[j]) {
        sum += a[i] - a[j];
        a[j] += a[i] - a[j];
      }
    }
  }
  std::cout << sum << std::endl;
  return 0;
}
