#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
int main() {

  ll n;
  cin>>n;
  vector<ll> a;
  rep(i,n){
    cin>>a[i];
  }
  ll ans=0;
  rep(i,n){
  if(x[i]>x[i+1]){
    ans+=x[i]-x[i+1];
    x[i]=x[i+1];
  }
    else{
      continue;
    }
  }
    
  cout<<ans<<endl;
}

  