#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long a[100005];
  vector<long long> sol;
  long long ans = 0, k = 0, s = 0;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      ans += a[i] - a[i + 1];
    }
  }
  cout << ans;
}
