#include <iostream>
using namespace std;
int a[10010];
int main()
{
int n, mx = 0, cnt = 0;
cin >> n;
for (int i = 1; i <= n - 1; i++)
{
cin >> a[i];
}
for (int i = 2; i < n; i++)
{
bool f = true;
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[j - 1])
{
cout << cnt;
return 0;
}
}
if (a[i] > mx) mx = a[i];
cnt += mx - a[i];
}
cout << cnt;
return 0;
}