#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &it : a) cin >> it;
  int ans = 0;
  int mx = 0, value = 0;
  for (int i = 1; i < n; i++) {
    value = max(value, a[i - 1]);
    if (a[i] - value < 0) {
      mx = max(mx, abs(a[i] - value));
      ans += mx;
    } else {
    }
  }
  cout << ans << "\n";
  return 0;
}
