#include <bits/stdc++.h>
using namespace std;
unsigned int takasa(unsigned int a, unsigned int b) {
  unsigned int h = 0;
  if (a > b) {
    h = a - b;
  }
  return h;
}
unsigned int humidai(vector<unsigned int> &humidai, int N) {
  unsigned int H = 0;
  for (int i = 1; i < N; i++) {
    unsigned int high = takasa(humidai.at(i - 1), humidai.at(i));
    humidai.at(i) = humidai.at(i) + high;
    H += high;
  }
  return H;
}
int main() {
  int N;
  cin >> N;
  vector<unsigned int> tall(N);
  for (int i = 0; i < N; i++) {
    cin >> tall.at(i);
  }
  cout << humidai(tall, N) << endl;
}
