#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const int mod = 1e9 + 7;
int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < (long long)(n); i++) cin >> a[i];
  long long ans = 0;
  for (long long i = 1; i <= (long long)(n); i++) {
    if (a[i - 1] > a[i]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans << endl;
}
