#include <bits/stdc++.h>

int main()
{
    unsigned long longN;
    std::cin >> N;

    unsigned long longA[N];
    for(unsigned long longi=0; i<N; i++)
        std::cin >> A[i];
    
    unsigned long longBbfr = 0;
    unsigned long longBaft;
    unsigned long longsum = 0;

    for(unsigned long longj=0; j<N-1; j++) // uekizan
    {
        if(A[j]+Bbfr > A[j+1])
        {
            Baft=A[j]+Bbfr-A[j+1];
        }
        else
        {
            Baft = 0;
        }
        

        sum+=Baft;

        Bbfr=Baft;
    }
    std::cout << sum << std::endl;

    
}