#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  long long a[n];
  long long i, j;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long s = 0;
  for (i = n - 1; i > 0; i--) {
    for (j = i - 1; j >= 0; j--) {
      if (a[j] > a[i]) {
        long long sub = a[j] - a[i];
        s = s + sub;
        a[i] = a[i] + sub;
      }
    }
  }
  cout << s << endl;
  return 0;
}
