#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  int N;
  vector<long long> arr;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    long long tmp;
    cin >> tmp;
    arr.push_back(tmp);
  }
  int sum = 0;
  int max = arr[0];
  for (int i = 1; i < N; ++i) {
    if (max > arr[i]) {
      sum += (max - arr[i]);
    } else {
      max = arr[i];
    }
  }
  cout << sum << endl;
  return 0;
}
