#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<long long> a(n);
  for (auto& i : a) cin >> i;
  long long ans = 0;
  for (int i = 1; i < n; ++i) ans += max(0LL, a[i] - a[i - 1]);
  cout << ans;
}
