#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tt = 1;
  while (tt--) {
    long long n;
    cin >> n;
    long long A[n];
    for (long long i = 0; i < n; i++) cin >> A[i];
    long long sum = 0;
    for (long long i = 1; i < n; i++) {
      sum += max(A[i] - A[i - 1], 0ll);
    }
    cout << sum;
  }
  return 0;
}
