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