#include<bits/stdc++.h> 
#define ll long long 
#define pb push_back
#define vec vector<int>
#define fo(i,n) for (int i = 0; i < n; i++)
#define so(v) sort(v.begin(), v.end())
#define fi first
#define se second
using namespace std;  




int main()
{

ll n;
cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++)
{
    cin>>v[i];
}

       
       ll maxx = 0 , count = 0;
     for(int i = 0; i < n;i++){
     	maxx = max(maxx , v[i]);
     	count += (maxx - v[i]);
     }
     cout<<count<<endl;
}