#include
#include
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,a[1000];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int sum = 0;
int diff= 0;
int premax = INT_MIN;
int postmax = INT_MIN;
for(int i=1;i<n-1;i++)
{
for(int j=0 ; j<i ; j++)
{
premax = max(a[j],premax);
}
for(int k=i+1 ; k<n ; k++)
{
postmax = max(a[k],postmax);
}
if(premax>=a[i] && postmax>=a[i])
{
if(premax>postmax)
{
diff = postmax-a[i];
sum = sum + diff;
}
else
{
diff = premax-a[i];
sum = sum + diff;
}}
}
cout<< sum;
return 0;
}