#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int total_height = 0;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (int i = 1; i < n; i++) {
    if (v[i] < v[i - 1]) {
      total_height += (v[i - 1] - v[i]);
      v[i] = v[i - 1];
    }
  }
  cout << total_height << '\n';
  return 0;
}
