#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define MOD 1000000007
#define MAX 100010
#define aout(a) REP(i,sizeof(a)) cout << a[i] << " "; cout << endl;
#define vout(v) REP(i,v,size()) cout << v[i] << " "; cout << endl;
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Graph = vector<vector<int>>;

int main(){
  int N; cin>>N;
  vector<int> A(N);
  ll ans = 0;
  int mae = 0;
  REP(i,N) cin>>A[i];
  REP(i,N){
    if(mae <= A[i]) mae = A[i];
    else{
      ans += mae-A[i];
    }
  }a
  cout << ans;
}