#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> height(N);
  for (int i = 0; i < N; i++) {
    cin >> height.at(i);
  }
  int sum = 0;
  for (int i = 0; i < N; i++) {
    if (height.at(i) > height.at(i + 1)) {
      sum += height.at(i) - height.at(i + 1);
      height.at(i + 1) = height.at(i);
    }
  }
  cout << sum;
}
