#include <bits/stdc++.h>
using namespace std;
int a[200005];
int main() {
  int n;
  cin >> n;
  memset(a, 0, sizeof(a));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int i = 0;
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      int k = a[i];
      while (k > a[i + 1] && i < n - 1) {
        ans += k - a[i + 1];
        i++;
      }
    }
  }
  cout << ans;
  return 0;
}
