#include <bits/stdc++.h>
using namespace std;
int infi = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<long long> arr(n);
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int sum = 0;
  for (long long i = 1; i <= n; i++) {
    if (arr[i] < arr[i - 1]) {
      sum += (arr[i - 1] - arr[i]);
      arr[i] = arr[i - 1];
    }
  }
  cout << sum;
  return 0;
}
