#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n+1]={0};
int l[n+1]={0};
int r[n+1]={0};
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
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 i = n - 2; i >= 0; i--)
{
r[i] = max(r[i + 1], a[i]);
}
int water = 0;
for (int i = 0; i < n; i++)
{
water += (min(l[i], r[i]) - a[i]);
}
cout << water;
}