#include <bits/stdc++.h>
using namespace std;
long long a[200010];
signed main() {
  long long n, i, ans = 0;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 2; i <= n; i++) ans += max(0ll, a[i] - a[i - 1]);
  cout << ans;
  return 0;
}
