#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n;
  cin >> n;
  vector<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 = 1; i < n; i++) {
    if (arr[i] > arr[i - 1])
      continue;
    else {
      long long int t = i - 1;
      while (arr[i] < arr[i - 1] and i != n) i++;
      long long int maxi = *max_element(arr.begin() + t, arr.begin() + i);
      for (long long int j = t; j < i; j++) sum += maxi - arr[j];
    }
  }
  cout << sum;
  return 0;
}
