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