#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void setmax(T& a, const T& b) {
  if (b > a) a = b;
}
template <typename T>
inline void setmin(T& a, const T& b) {
  if (b < a) a = b;
}
int main() {
  cout.tie(0)->sync_with_stdio(0);
  int N;
  cin >> N;
  int X = 0;
  vector<int> values(N);
  for (int i = 0; i < N; ++i) {
    cin >> values[i];
  }
  for (int i = 1; i < N; ++i) {
    int cur = abs(values[i] - values[i - 1]);
    if (values[i] < values[i - 1]) {
      X += cur;
      values[i] = values[i - 1];
    }
  }
  cout << X << '\n';
}
