#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  vector<long long> vec(n);
  for (long long &i : vec) cin >> i;
  long long last = vec[0];
  long long ans = 0;
  for (long long i = 1; i < n; i++) {
    if (last < vec[i]) ans += vec[i] - last;
    last = vec[i];
  }
  cout << ans << '\n';
  return 0;
}
