#include<bits/stdc++.h>
#include <iostream>
#include<map>
#include <algorithm>
using namespace std;
typedef long long      ll;
#define mp             make_pair
#define pb             push_back
#define fi             first
#define se             second
#define sz(x)          (int)((x).size())
#define cena           ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MOD  1000000007
#define maxn 500005
class myData
{
    public:
    int t;
    int x;
    int y;
    int d;
    myData(int t,int x,int y,int d)
    : t(t),x(x),y(y),d(d)
    {
    }
    bool operator<(const myData& a) const
    {
        return (this->t < a.t);
    }
};
ll exponentMod(ll A, ll B,ll C)
{
    if (A == 0)
        return 0;
    if (B == 0)
        return 1;
    ll y;
    if (B % 2 == 0) {
        y = exponentMod(A, B / 2, C);
        y = (y * y) % C;
    }
    else {
        y = A % C;
        y = (y * exponentMod(A, B - 1, C) % C) % C;
    }
    return (ll)((y + C) % C);
}
pair<int,int>  factorize(ll n) {
    int ans=1;
    int c=0;
    for (ll i = 2; i * i <= n; ++i) {
        while (n % i == 0) {
            if(i!=2)
                ans*=i,c++;
            n/=i;
        }
    }
    if (n != 1) {
            if(n!=2)
        ans*=n,c++;
    }
    return {ans,c};
}
////////////    FENWICK TREE    //////////////////
map<ll,ll> bit;
int N1= 2e5;
void update(ll i){
    for(;i<=N1;i+=i&(-i))
        bit[i]++;
}
ll get(ll i){
    ll ans = 0;
    for(;i>0;i-=i&(-i))
        ans+=bit[i];
    return ans;
}
int now;
bool sortByVal(const myData &a,
               const myData &b)
{
    return (a.t<b.t);
}/*char s[1005][1005];
int vis[1005][1005];
//int a[15],b[15],c[15];
int n,m,p;
int check(int x,int y){
	if(x<1||x>n||y<1||y>m||s[x][y]=='#'||vis[x][y])return 0;
	return 1;
}struct node{
	int x,y;
};
int dx[10]={0,0,1,-1};
int dy[10]={1,-1,0,0};
queue<node>q[15];
void bfs(int k){
	node no,ne;
	int h=q[k].size();
	while(h--){
		no=q[k].front();
		q[k].pop();
		for(int i=0;i<4;i++){
			ne.x=no.x+dx[i];
			ne.y=no.y+dy[i];
			if(check(ne.x,ne.y)){
				vis[ne.x][ne.y]=1;
				q[k].push(ne);
				b[k]++;
			}
		}
	}
}*/

ll ans1=0;
/*ap<int,int>m;
int dfs(int i,int n) {
    m[i]=1;
    int j=2;
    while(abs(i*j)<=n){
            ans1+=j;
            if(m[i*j]==0)
            dfs(i*j,n);
            ans1+=j;
            if(m[-i*j]==0)
            dfs(-i*j,n);
            j++;
    }
}*/
#define MAXN   1000003
int spf[MAXN];
ll  calc[MAXN+1];
ll nCk(ll n,ll k,ll mod) {
    return ((((calc[n]%mod)*exponentMod(calc[k],mod-2,mod))%mod)*exponentMod(calc[n-k],mod-2,mod))%mod;
}
void sieve()
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++)
        spf[i] = i;
    for (int i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i] == i)
        {
            for (int j=i*i; j<MAXN; j+=i)
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}
set<int>getFactorization(int x)
{
    set<int> ret;
    while (x != 1)
    {
        ret.insert(spf[x]);
        x = x / spf[x];
    }
    return ret;
}
bool prime[MAXN+1];
void sieve2()
{
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=MAXN; p++)
    {
        if (prime[p]==true)
        {
            for (int i=p*p; i<=MAXN; i += p)
                prime[i] = false;
        }
    }
}
int MEX(int a[],int n) {
    set<int>s;
    for(int i=0;i<n;i++) {
        s.insert(a[i]);
    }
    int c=0;
    for(auto i=s.begin();i!=s.end();i++) {
       if(*i==c) {
        c++;
       }
       else
        break;
    }
    return c;
}
int smallestDivisor(int n)
{
    if (n % 2 == 0)
        return 2;
    for (int i = 3; i * i <= n; i += 2) {
        if (n % i == 0)
            return i;
    }
    return n;
}
int main() {
     int n;
     cin>>n;

     int a[n];
     for(int i=0;i<n;i++) cin>>a[i];
     int m=a[0];
     ll kp=0;
     for(int i=1;i<n;i++ ){
        if(a[i]<m) {
           kp+=(m-a[i]);
        } else {
        m=a[i];}
     }
     cout<<kp<<endl;	return 0;
}









Measured memory_bytes: 4388