#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[2000001];
  long long b = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    b += max(0, (a[i + 1] - a[i]));
  }
  cout << b;
}
