#include <bits/stdc++.h>
using namespace std;
const int lt = 100005;
long long int mod = 1000000007;
void printv(vector<long long int> a) {
  for (int i = 0; i < a.size(); i++) cout << a[i] << " ";
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n;
  cin >> n;
  vector<long long int> a;
  for (int i = 0; i < n; i++) {
    long long int k;
    cin >> k;
    a.push_back(k);
  }
  int mx = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < mx)
      ans += mx - a[i];
    else
      mx = a[i];
  }
  cout << ans << endl;
  return 0;
}
