#include <bits/stdc++.h>
using namespace std;
void dbg_out() { cerr << "\b\b]\n"; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << H << ", ";
  dbg_out(T...);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  long long ans = 0;
  vector<int> A(n);
  for (int &i : A) cin >> i;
  for (int i = 1; i < n; ++i) {
    ans += max(A[i] - A[i - 1], 0);
  }
  cout << ans;
  return 0;
}
