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

int main()
{
	int N;
	scanf("%d", &N);
	
	int maximum;
	scanf("%d", &maximum);
	
	long long ans = 0;
	for(int i = 1; i < N; i++)
	{
		int person;
		scanf("%d", &person);
		
		if(person >= maximum)
			maximum = person;
			
		else
			ans += maximum - person;
	}
	
	printf("%lld", ans);
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    ll x;
    cin>>x;
    ll a[x];
    ll ans = 0;
    ll maxT = 0;
    for(int i=0;i<x;i++) {
        cin>>a[i];
        if(i == 0) {
            maxT = a[0];
        }
        if(i > 0) {
            if(a[i] < a[i-1]) {
                ans += (a[i-1]-a[i]);
                a[i] = a[i-1];
            }
            maxT = a[i];
        }
    }
    // 2 1 5 4 3
    cout<<ans<<endl;

}
