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