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