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