#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  long long int ans = 0;
  long long int a[n];
  for (auto &it : a) cin >> it;
  for (long long int i = 0; i < n - 1; i++) {
    if (a[i + 1] > a[i]) ans += a[i + 1] - a[i];
  }
  cout << ans;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int T = 1;
  for (long long int i = 1; i <= T; i++) {
    solve();
  }
  return 0;
}
