#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t{}, T{};
  t = 1;
  while (t--) {
    long long n{}, m{}, ans{}, k{}, a{}, b{}, c{}, f{1};
    cin >> n;
    std::vector<long long> v(n);
    for (long long i = 0; i < n; ++i) cin >> v[i];
    for (long long i = n - 2; i >= 0; --i) {
      if (v[i] < v[i + 1]) {
        ans += v[i + 1] - v[i];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
