#include <bits/stdc++.h>
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
using namespace std;
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
  return a.first < b.first;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    long long arr[n + 1];
    vector<long long> sol;
    sol.emplace_back(0);
    for (long long i = 0; i < n; ++i) cin >> arr[i];
    long long high = arr[0];
    for (long long i = 0; i < n; ++i) {
      if (high > arr[i])
        sol.emplace_back(high - arr[i]);
      else
        high = arr[i];
    }
    long long sum;
    for (const auto &i : sol) sum += i;
    cout << sum << "\n";
  }
  return 0;
}
