#include<bits/stdc++.h>

typedef long long ll;
using namespace std;
int main()
{
ll n,c=0; cin>>n;
ll a[n];
ll mx=INT_MIN;
for(ll i=0;i<n;i++)
{
    cin>>a[i];
    mx=max(mx,a[i]);
    //cout<<mx<<endl;
    c+=mx-a[i];
}
cout<<c<<endl;
}
