#include <iostream>
using namespace std;
int n, x, y;
long long s;

int main()
{
   cin>>n;
   cin>>x;
   for(int i=2; i<=n; i++)
   {
       cin>>y;
       if(y<x) s+=(x-y), y=y+(x-y);
       x=y;

   }
   cout<<s;
    return 0;
}
