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