#include
using namespace std;
int main() {
int n,water=0,maxl,maxr,i,j;
cin>>n;
int *arr = new int[n];
int *arr1 = new int[n];
int *arr3 = new int[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
maxl = arr[0];
for( i=1,j=0;i<n&&j<n;i++,j++)
{
if(arr[i]>maxl)
{
maxl=arr[i];
arr1[j] = maxl;
}
}
maxr=arr[n-1];
for( i=n-2,j=n-1;i>=0&&j>=0;i--,j--)
{
if(arr[i]>maxr)
{
maxr=arr[i];
arr3[j] = maxr;
}
}
for( j=0;j<n;j++)
{
if(arr1[j]<arr3[j])
water =water +( arr1[j] - arr[i] );
else
water = water +(arr3[j]-arr[i]);
}
cout<<water;
return 0;
}