#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define repr(i,x,n) for(int i=x; i<(n); i++)
#define ALL(x) (x).begin(),(x).end()
#define INF 1e9
typedef long long ll;
using VI = vector<int>;
using VB = vector<bool>;
using VS = vector<string>;
using graph = vector<vector<int>>;

int main()
{
ll n,ans=0;
cin>>n;
vector<ll> a(n+1);
a.at(0)=0;
for (int i = 1; i <= n; i++)
{
    cin>>a.at(i);
    if (a.at(i)<a.at(i-1))
    {
        ans=ans+a.at(i-1)-a.at(i);
        a.at(i)=a.at(i-1);
    }
    
}
cout<<ans<<endl;
    return 0;
}