#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (auto &it : arr) cin >> it;
  int ans = 0;
  int maxtillnow = arr[0];
  for (int i = 1; i < arr.size(); i++) {
    maxtillnow = max(maxtillnow, arr[i]);
    if (maxtillnow > arr[i]) ans += maxtillnow - arr[i];
  }
  cout << ans;
}
