#include <iostream>
#include <vector>
using namespace std;

int main() {

  int n;
  cin >> n;

  vector<ll> arr;
  for(int i=0; i<n; i++) {
    ll t;
    cin >> t;
    arr.push_back(t);
  }

  ll cnt =0;
  ll j=0;
  ll max_so_far = -1;
  for(int i=1; i<arr.size(); i++) {
    max_so_far = max(max_so_far, arr[i-1]);
    if(arr[i] < max_so_far) {
      cnt += (max_so_far - arr[i]);
    }
  }

  cout << cnt << endl;

}
