#include<iostream>
#include<vector>
int main(){
  int n;
  std::cin>>n;
  std::vector<int> a(n);
  for(auto& ai:a)std::cin>>ai;
  int64_t answer = 0;
  for(int i=1;i<n;++i){
    if(a[i-1]>a[i]){
      answer += a[i-1]-a[i];
      a[i] = a[i-1];
    }
  }
  std::cout<<answer<<std::endl;