#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<int> v(n);
  for (auto &x : v) cin >> x;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (v[i] > v[i - 1]) {
      int d = v[i - 1] - v[i];
      ans += d;
      v[i] += d;
    }
  }
  cout << ans << "\n";
  return 0;
}
