#include <bits/stdc++.h>
#include <numeric>//int sum = accumulate(v.begin(), v.end(), 0)
#include <math.h>//pow(x,y)=x^y
#include <algorithm>//sort(a.begin(), a.end());reverse(a.begin(), a.end());
#define rep(init,i,n) for (int i=init; i <(n);i++)
#define ll long long
using namespace std;


int main() {
int n;
cin>>n;

vector<ll> a(n);
vector<ll> b(n);

rep(0,i,n){
    cin >> a.at(i);
}
ll temp_max = 0;
ll index_oldmax = 0;
rep(0,i,n){
    if(a.at(i)>temp_max){
        rep(index_oldmax,j,i){
           b.at(j) = (temp_max-a.at(j));
           a.at(j) = temp_max;
           //cout<<"i="<<i<<endl;//
           //cout<<"j="<<j<<endl;//
           //cout<<"aj"<<a.at(j)<<endl;//
           //cout<<"bj"<<b.at(j)<<endl;//
        }
        temp_max = a.at(i);
        index_oldmax = i;
    }
    if(i==n-1){
            rep(index_oldmax,j,i+1){
           b.at(j) = (temp_max-a.at(j));
           a.at(j) = temp_max;
           //cout<<"i="<<i<<endl;//
           //cout<<"j="<<j<<endl;//
           //cout<<"bj"<<b.at(j)<<endl;//
        }
    }
}
/*rep(0,i,n){
cout<<" a.at(i)="<<a.at(i);//
//
}*/
int sum = 0;
rep(0,i,n){
    sum += b.at(i);
}
cout<<sum;

}
