#include <bits/stdc++.h>
using namespace std;
//型に気を付けよう
//a=0x61=97 '0'=0x30=48
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long  ll;
typedef long double  ld;
typedef string str;
typedef pair<int,int> pairii;
typedef vector<int> veci;
const double pi=3.1415926535;
const ll  mod=1000000007;
//桁数指定　cout << fixed << setprecision(2)
int main() {
  ll n;
  cin>>n;
  veci v(n);
  {rep(i,n)cin>>v[i];}
  ll ans=0;
  ll  ma=0;
  rep(i,n){
    ma=max(ma,v[i]);
    ans+=ma-v[i];
  }
  cout<<ans<<endl;
}