#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  vector<long long> v;
  long long x, n, ans = 0;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  for (long long i = 0; i < n - 1; i++) {
    if (v[i] < v[i + 1]) ans += v[i + 1] - v[i];
  }
  if (n == 1)
    cout << 0 << endl;
  else
    cout << ans << endl;
  return 0;
}
