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