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