#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  long long int arr[n];
  for (int k = 0; k < n; k++) {
    cin >> arr[k];
  }
  long long int count = 0;
  for (int i = 0; i < n; i++) {
    long long int curr = arr[i];
    for (int j = i + 1; j < n; j++) {
      if (curr > arr[j]) {
        count += curr - arr[j];
        arr[j] = curr;
      }
    }
  }
  cout << count << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  solve();
  return 0;
}
