#include <iostream>
#include<bits/stdc++.h>
#define pb(x) push_back(x

using namespace std;


int main() {
    int n,x;
    cin>>n;
    vector<int> A
    int max=0, ans = 0;
    for(int i=0; i<n; i++)
    {
        cin>>x;
        if(x<max)
        {
            ans+=max-ans;
        }
        else
            max = x;
    }
    cout<<ans;
	return 0;
}
