#include <bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   cin>>n;
   int i,a[n],j=-1;
   int64_t sum=0;
   for(i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]>j)
            j=a[i];
         sum+=j-a[i];
    }
   cout<<sum<<endl;
}
