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