#include <bits/stdc++.h>
using namespace std;
long long max(long long b, long long c) {
  if (b < c) {
    c = b;
  }
  return c;
}
int main() {
  long long n, s = 0;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] > a[i + 1]) {
      s = s + max(0LL, a[i - 1] - a[i]);
    } else {
      continue;
    }
  }
  cout << -s;
}
