#include <iostream>
#include <string>
using namespace std;
int main(void)
{
   long long n,a,x,ans=0;
   cin>>n>>a;
   while(--n)
   {
       cin>>x;
       if(x<a)ans+=a-x;
       else a=x;
   }
   cout<<ans<<endl;
   return 0;
}
