# include <bits/stdc++.h>
# define ll long long 
using namespace std;
int main(){
  ll n,max=0,a,s=0,i;
  cin>>n;
  for(i=0;i<n;i++){
    cin>>a;
    if(a>=max)
      	max=a;
   	else
      s+=max-a; 
  }
  cout<<s;
}
