#include<bits/stdc++.h>
#include<map>
#include<bitset>
#define MOD 1000000007
#define md 998244353
#define INF LLONG_MAX
#define F first
#define S second
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define sz(x) (long long)x.size()
#define vi vector<int>
#define vll vector<long long>
#define vpl vector<pair<long long,long long> >
#define vpi vector<pair<int,int> >
#define pb push_back
#define debug(x) cerr<<#x<<"= "<<x<<endl
#define all(v) v.begin(),v.end()
#define TT ll test;cin>>test; while(test--)
#define rep(i,a,n) for(ll i=a;i<(long long)n;++i)
#define repr(i,n,a) for(ll i=n;i>(long long)a;--i)
#define endl '\n'
#define SORTA(v) sort(v.begin(),v.end())
#define SORTD(v) sort(v.begin(),v.end(),greater<int>())
#define REV(v) reverse(v.begin(),v.end())
#define MAXN 200005
#define     mem(a)      memset(a , 0 ,sizeof a)
#define     memn(a)     memset(a , -1 ,sizeof a)
typedef long long ll;
typedef long double ld;
using namespace std;

ll BigMod(ll base,ll pow,ll mod)
{
    if(pow==0||base==1)return 1;
    else if(pow==1)return base;
    else if(pow%2)return BigMod(base,pow-1,mod)*base%mod;
    else {ll x=(BigMod(base,pow/2,mod)%mod);x=(x*x)%mod;return x;}
}

ll isprime(ll prime)
{
	ll cnt=0;
	for(int i=2;i*i<=prime;i++)
	{
		if(prime%i==0)return false;
		
	}
	return true;
}

   void DBG() { cerr << "]" << '\n'; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h;
    if (sizeof...(t)) cerr << ", ";
    DBG(t...);
}
 
#ifdef LOCAL // compile with -DLOCAL
    #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
    #define dbg(...) 0
#endif 

//***************** GRAPH VISITED *****************************************************
const int dx[]={+1,-1,+0,+0};
const int dy[]={+0,+0,+1,-1};
const int f1x[]={+0,+0,+1,-1,-1,+1,-1,+1};   /// Kings Move
const int f1y[]={-1,+1,+0,+0,+1,+1,-1,-1};  /// Kings Move
const int f2x[]={-2, -2, -1, -1,  1,  1,  2,  2};  /// Knights Move
const int f2y[]={-1,  1, -2,  2, -2,  2, -1,  1}; /// Knights Move

const int maX=1e3+10;
ll vis[maX][maX];
ll n,m;
void DFS(ll x, ll y)
{
	cout<<x<<' '<<y<<endl;
	vis[x][y]=1;
	
	for(ll i=0;i<4;i++)
	{
		ll d=x+dx[i];
		ll dd=y+dy[i];
		
		if(d>=1 && d<=n && dd>=1 && dd<=m && vis[d][dd] !=1) DFS(d,dd);
	}
   	
	for(ll i=x;i<=n;i++)if(vis[i][y] ==0) DFS(i,y);
	for(ll i=x-1;i>=1;i--)if(vis[i][y]==0)DFS(i,y);
	for(ll i=y;i<=m;i++)if(vis[x][i]==0)DFS(x,i);
	for(ll i=y-1;i>=1;i--)if(vis[x][i]==0)DFS(x,i);
	 
 
	
}

void solve()
{
    ll n,i,j,cc=0,l,r;
    cin>>l>>r;
    if(l==r){
        cout<<0<<endl;
        return;
    }
    j=l^r;
    while(j){
        cc++;
        j >>=1;
    }
    ll ans=0;
    while(cc){
        ans<<=1;
        ans++;
        cc--;
    }
    cout<<ans<<endl;
}

 
//**********************************MAIN_FUNCTION ************************************************************
int main( ) {
 
      ll n,m,i,j,k,len=0,e,p,q,a,b,c,d;
    string str;
    int t;
     ll cnt=0;
        cin>>n;
        vector<ll>v(n);
        for(auto &el:v)
        cin>>el;
        
       for(i=0;i<n-1;i++)
       { 
       	  if(v[i]>v[i+1])
			 {
			 	cnt+=(v[i]-v[i+1]);
			 	v[i+1]=v[i];
			 }
	   }  
	  cout<<cnt<<endl;  
      
	return 0;
}
 

Measured memory_bytes: 4836