#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int count = 0;
  int prev = 0;
  for (auto i = (0); i < (n); i++) {
    int x;
    cin >> x;
    if (x < prev)
      count += (prev - x);
    else
      prev = x;
  }
  cout << count << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) solve();
  return 0;
}
