#include<bits/stdc++.h>
#define ll long long int
using namepsace std;
int main()
{
  ll n;
  cin>>n;
  ll a[n];
  ll s=0;
  for(ll i=0;i<n;i++)
  {
    cin>>a[i];
  }
  for(ll i=1;i<n;i++)
  {
    if(a[i-1]>a[i])
      s+=(a[i-1]-a[i])
  }
  cout<<s<<endl;
  return 0;
}