#include <bits/stdc++.h>
using namespace std;
const int sz = 1e6 + 10;
void solve() {
  long long int n, i, sum = 0;
  cin >> n;
  long long int ar[n];
  for (i = 0; i < n; i++) cin >> ar[i];
  for (i = 1; i < n; i++) {
    if (ar[i] > ar[i - 1]) sum += ar[i] - ar[i - 1];
  }
  cout << sum << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
