#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < (int)(n); i++) cin >> a[i];
  ll ans = 0;
  for (int i = 0; i < (int)(n - 1); i++) {
    if (a[i + 1] - a[i] > 0) ans += (a[i + 1] - a[i]);
  }
  cout << ans << endl;
}
