#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> ar(n);
  for (int &x : ar) cin >> x;
  int res = 0;
  for (int i = 1; i < n; ++i) {
    res += -min(0, ar[i] - ar[i - 1]);
    ar[i] = max(ar[i], ar[i - 1]);
  }
  cout << res << '\n';
  return 0;
}
