#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ldb = long double;
int main() {
  int N;
  cin >> N;
  vector<ll> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  ll tot = 0;
  for (int i = 1; i < N; i++) {
    tot += max((ll)0, a[i] - a[i - 1]);
  }
  cout << tot << endl;
}
