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