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