#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repit(it, li) for(auto it=li.begin(); it!=li.end(); it++)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main(){
   int n;
   cin>>n;
   vector<int> a(n);
   rep(i, n) cin>>a[i];

   ll c=0, t=0;
   rep(i, n){
      if(c>a[i]) t+=c-a[i];
      c=max(c, a[i]);
   }

   cout<<t<<endl;
   return 0;
}
