#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
int main() {
  long long n;
  cin >> n;
  long long a[n];
  long long sum = 0, b;
  for (long long i = (0); i < (n); i++) cin >> a[i];
  for (long long i = (1); i < (n + 1); i++) {
    if (a[i] < a[i - 1]) {
      b = abs(a[i - 1] - a[i]);
      sum += b;
      a[i] += b;
    }
  }
  cout << sum << "\n";
}
