#include <bits/stdc++.h>
using namespace std;
int n;
int h[100001];
int main() {
  cin >> n;
  int m = 0;
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  long long ret = 0;
  for (int i = 1; i < n; ++i) {
    if (h[i] < h[i - 1]) {
      ret += h[i - 1] - h[i];
    }
  }
  cout << ret << endl;
  return 0;
}
