#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize "trapv"
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, ans = 0;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long i = 1; i < n; i++) {
    if (a[i] > a[i - 1]) {
      long long p = a[i] - a[i - 1];
      ans += (p);
    }
  }
  cout << ans;
  return 0;
}
