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