#include <bits/stdc++.h>
using namespace std;
int mod(int x) {
  if (x > 0)
    return x;
  else
    return -1 * x;
}
int main() {
  int n;
  cin >> n;
  int steps = 0;
  int h[n];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 1; i < n; i++) {
    if (h[i] < h[i - 1]) {
      steps = steps + h[i - 1] - h[i];
      h[i] = h[i] + (h[i - 1] - h[i]);
    }
  }
  cout << steps << "\n";
}
