#include <bits/stdc++.h>
using namespace std;
vector<int> dy = {1, 0, -1, 0};
vector<int> dx = {0, 1, 0, -1};
int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  int ans = 0;
  cin >> vec.at(0);
  int comp = vec.at(0);
  for (int i = 1; i < n; i++) {
    cin >> vec.at(i);
    if (comp > vec.at(i)) {
      ans += (comp - vec.at(i));
    } else {
      comp = vec.at(i);
    }
  }
  cout << ans << endl;
  return 0;
}
