#include
#include
using namespace std;
int calculateCapacity(int arr[],int n){
int left[1000000],right[1000000],totalCapacity=0;
left[0]=arr[0];
right[n-1]=arr[n-1];
for(int i=1;i<n;i++){
left[i]=max(left[i-1],arr[i]);
}
for(int i=n-2;i>=0;i–){
right[i]=max(right[i+1],arr[i]);
}
for(int i=1;i<n-1;i++){
totalCapacity+=min(left[i],right[i])-arr[i];
}
return totalCapacity;
}
int main(){
int n,arr[1000000],i;
cin >> n;
for(i=0;i<n;i++)
cin >> arr[i];
int total_capacity=calculateCapacity(arr,n);
cout << total_capacity;
}