#include<bits/stdc++.h>
using namespace std;
int a[200005];
int main()
{
    int n;
    long long ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ans+=max(0,a[i-1]-a[i]),a[i]=max(a[i],a[i-1]);
    printf("%lld",ans);
    return 0;
}