#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++){
    cin >> A[i];
  }
  long long int ans = 0;
  for (int i = 1; i < n; i++){
    if (a.at(i-1) > a.at(i)){
      ans = ans + (a.at(i-1)-a.at(i));
      a.at(i) = a.at(i-1);
    }
  }
  cout << ans << endl;
  return 0;
}