#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int sum = 0;
  int c;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i; j++) {
      if (A[i] < A[j]) {
        c = A[j] - A[i];
        A[i] += c;
        sum += c;
      }
    }
  }
  cout << sum << endl;
}
