#include
#include
using namespace std;
int main() {
int lmax, rmax, N;
long int count=0;
cin >> N;
if(N == 1)
{
cout << “0”;
return 0;
}
int arr[N];
for(int i=0; i<N; i++)
cin >> arr[i];
for(int i=1; i<N-1; i++)
{
lmax = *max_element(arr, arr+i);
rmax = *max_element(arr+i, arr+N);
if((min(lmax, rmax) - arr[i]) > 0)
count = count + min(lmax, rmax) - arr[i];
}
cout << count;
return 0;
}