#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  stack<int> s;
  vector<int> p;
  int l = 0;
  for (int i = 0; i < n; i++) {
    if (s.empty() || s.top() <= arr[i]) {
      s.push(arr[i]);
      p.push_back(arr[i]);
    } else {
      int ans = arr[i];
      while (!s.empty() && s.top() > arr[i]) {
        ans = max(ans, s.top());
        s.pop();
      }
      ans = max(ans, arr[i]);
      p.push_back(ans);
      s.push(ans);
    }
  }
  l = 0;
  for (auto i = 0; i < n; i++) {
    l = l + (p[i] - arr[i]);
  }
  cout << l;
  return 0;
}
