#include <bits/stdc++.h>
using namespace std;
map<pair<long long int, long long int>, long long int> cnt;
int main(void) {
  long long int n;
  cin >> n;
  long long int res = 0;
  long long int prev = 0;
  for (long long int i = 0; i < ((long long int)(n)); i++) {
    long long int a;
    cin >> a;
    res += min(0ll, prev - a);
    prev = a;
  }
  cout << res << endl;
  return 0;
}
