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