#include<bits/stdc++.h>
using namespace std;
int small(int a,int b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
//Taking input.
int a[1000000],n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
//Approach for calculation
int sum_water=0;
for(int i=0;i<n;i++)
{
int lmax=*max_element(a,a+i);//This is maximum element to the left side of the element a[i];
int rmax=*max_element(&a[i],a+n);//This is maximum element to the right side of the element a[i];
int small_height=small(lmax,rmax);//This is small of the two maximum element
if((small_height-a[i])>=0)
sum_water+=(small_height-a[i]);// This is the total sum summing up.
}
cout<<sum_water<<endl;
return 0;
}