#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<unsigned int> h;
  unsigned int n;
  unsigned int cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    unsigned int temp;
    cin >> temp;
    h.emplace_back(temp);
  }
  for (int i = 0; i < n - 1; i++) {
    if (h[i] > h[i + 1]) {
      cnt += h[i] - h[i + 1];
      h[i + 1] += h[i] - h[i + 1];
    }
  }
  cout << cnt << endl;
  return 0;
}
