#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long test = 1;
  while (test--) {
    long long n;
    cin >> n;
    long long a[n], store[n], tot = 0;
    for (long long i = 0; i < n; i++) {
      cin >> a[i], tot += a[i];
    }
    long long ans = 0;
    for (long long i = 1; i < n; i++) {
      long long k = max(a[i] - a[i - 1], 0LL);
      ans += k;
      a[i] = k;
    }
    cout << ans << '\n';
  }
  return 0;
}
