#include<bits/stdc++.h>
typedef long long ll
using namespace std;

int main()
{
    ll N;
    cin >> N;
    ll maxHeight=0;
    ll totalHeight=0;

    while(N--)
    {
        ll height;
        cin >> height;
        if(height >= maxHeight)
        {
            maxHeight = height;
        }
        else if(height < maxHeight)
        {
            totalHeight += maxHeight - height;
        }
    }
    cout << totalHeight;
    return 0;
}