#include<bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(),v.end()
#define puts(i) cout << i << endl
#define INF INT_MAX
#define INFL LLONG_MAX
typedef long long ll;
using namespace std;
int main(){a
  ll n; cin >> n;

  ll ans = 0;
  ll max = 0;

  rep(i,n){
    ll a; cin >> a;
    if(a>=max){
      max = a;
    }else{
      ans+=max-a;
    }
  }

  cout << ans <<endl;

}
