#include <bits/stdc++.h>
using namespace std;
void solve() {
  int i, j;
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  long long int ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    ans += max(0, a[i + 1] - a[i]);
  }
  printf("%lld\n", ans);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int T = 1;
  while (T--) {
    solve();
  }
}
