#include
using namespace std;
int water_capacity(int a[], int N)
{
int l[100000] = {0}, r[100000] = {0}, water = 0;
l[0] = a[0];
r[N-1] = a[N-1];
for(int i = 1; i < N; i++)
l[i] = max(l[i-1], a[i]);
for(int j = N-2; j >= 0; j--)
r[j] = max(r[j+1], a[j]);
for(int k = 0; k < N; k++)
water += min(l[k], r[k]) - a[k];
return water;
}
int main() {
int a[100000] = {0}, N;
cin>>N;
for(int i = 0; i < N; i++)
cin>>a[i];
cout<<water_capacity(a, N);
return 0;
}