#include <bits/stdc++.h>
using namespace std;
using namespace __detail;
template <typename A, typename B>
inline void chmin(A &a, B b) {
  if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A &a, B b) {
  if (a < b) a = b;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  int prev, ans = 0;
  cin >> prev;
  for (int i = 1; i < n; i++) {
    int c;
    cin >> c;
    if (c < prev) ans += abs(c - prev);
    prev = max(c, prev);
  }
  cout << ans;
}
