#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const int INF = 1 << 30;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  long long int maxi = 0, c = 0, v;
  for (int i = 0; i < n; i++) {
    cin >> v;
    maxi += max(0ll, c - v);
    c = v;
  }
  cout << maxi;
  return 0;
}
