#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
struct HASH {
  size_t operator()(const pair<int, int>& x) const {
    return hash<long long>()(((long long)x.first) ^
                             (((long long)x.second) << 32));
  }
};
string inttostr(long long int n) {
  std::stringstream ss;
  ss << n;
  string out = ss.str();
  return out;
}
long long int stringtoint(string s) {
  istringstream iss(s);
  long long int n;
  iss >> n;
  return n;
}
template <typename T>
T gcd(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / gcd(a, b));
}
int main() {
  int n;
  cin >> n;
  int arr[n + 10];
  for (long long int i = 0; i < n; i++) cin >> arr[i];
  ;
  int sum = 0;
  for (int i = 1; i < n; ++i) {
    if (arr[i] >= arr[i - 1]) continue;
    sum += (arr[i - 1] - arr[i]);
    arr[i] = arr[i - 1];
  }
  cout << sum << '\n';
}
