#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);
  int ans = 0;
  for (int i = 0; i < n; i++) cin >> v[i];
  stack<int> s;
  for (int i = 0; i < n; i++) {
    if (s.empty())
      s.push(v[i]);
    else if (s.top() <= v[i])
      s.push(v[i]);
    else
      ans += (s.top() - v[i]);
  }
  cout << ans << "\n";
}
