/* Author: shriamrut
 * Language: C++17
 * Made on:22.08.2020
 * */
#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ll long long
#define ss second
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define all(v) (v).begin(),(v).end()
#define maxv(v) *max_element(all(v))
#define minv(v) *min_element(all(v))
using namespace std;
void ra(auto a[],int n) { for(int i=0;i<n;i++) cin>>a[i];}
void pa(auto a[],int n) { for(int i=0;i<n;i++) cout<<a[i]<<" ";}
void rv(vector<auto>&v,int n) {v.resize(n);for(int i=0;i<n;i++)	cin>>v[i]; }
void pv(vector<auto>&v) {for(size_t i=0;i<v.size();i++) cout<<v[i]<<" ";}
ll maxl(ll a,ll b) { if(a>b) return a; return b; }
ll minl(ll a,ll b) { if(a<b) return a; return b; }
/*auto i2=upper_bound(cind[i].rbegin(), cind[i].rend(), range[1],[](int a, int b){return a>=b;});
 * to obtain largest val <=x
 * */
int main() {
	IOS;	
	//freopen("input.txt","r",stdin);
	int n;
	cin>>n;
	vector<int> a(n);
	rv(a,n);
	int ans=0;
	int max_v=a[0];
	for(int i=1;i<n;i++) {
		if(a[i]<max_v)
			ans+=(max_v-a[i]);
		max_v=max(max_v,a[i]);
	}
	cout<<ans;
	return 0;
}
