#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, count = 0;
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] > a[i + 1]) {
      count += a[i] - a[i + 1];
      a[i + 1] += a[i] - a[i + 1];
    }
  }
  cout << count << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cout << fixed;
  cout << setprecision(10);
  { solve(); }
}
