#include <bits/stdc++.h>
using namespace std;
int readInt() {
  bool minus = false;
  int result = 0;
  char ch;
  ch = getchar();
  while (true) {
    if (ch == '-') break;
    if (ch >= '0' && ch <= '9') break;
    ch = getchar();
  }
  if (ch == '-')
    minus = true;
  else
    result = ch - '0';
  while (true) {
    ch = getchar();
    if (ch < '0' || ch > '9') break;
    result = result * 10 + (ch - '0');
  }
  if (minus)
    return -result;
  else
    return result;
}
template <typename T>
void cloneArray(T *a, T *b, int start, int end) {
  for (int i = start; i <= end; ++i) {
    *(b + i) = *(a + i);
  }
}
template <typename T>
void inputArray(T *a, int start, int end) {
  for (int i = start; i <= end; ++i) cin >> a[i];
}
template <typename T>
void printArray(T *a, int start, int end) {
  for (int i = start; i <= end; ++i) cout << *(a + i) << ' ';
}
void ruffleSort(int *a, int n) {
  unsigned seed = 0;
  shuffle(a, a + n, default_random_engine(seed));
  sort(a, a + n);
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  int a[n];
  inputArray(a, 0, n - 1);
  int ans = 0;
  int max = INT_MIN;
  for (int i = 0; i <= n - 1; ++i) {
    if (a[i] > max)
      max = a[i];
    else {
      ans += (max - a[i]);
    }
  }
  cout << ans;
  return 0;
}
