#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long input[n];
  for (long long i = 0; i < n; ++i) {
    long long x;
    cin >> x;
    input[i] = x;
  }
  size_t ans = 0;
  for (long long i = 1; i < n; ++i) {
    if (input[i] < input[i - 1]) ans += input[i - 1] - input[i];
  }
  cout << ans;
  return 0;
}
