#include <bits/stdc++.h>
using namespace std;
long long int a[10000000];
int main() {
  long long int n, i, count = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (i = 2; i <= n; i++) {
    if (a[i - 1] > a[i]) {
      count = count + a[i - 1] - a[i];
    }
  }
  cout << count;
}
