#include<bits/stdc++.h>
#define ll long long 
using namepsace std;
int main(){
  ll n;
  cin >> n;
  ll a[n];
  for(int i = 0; i < n; i++){
    cin >> a[i];
  }
  ll nax = a[0], ans = 0;
  for(int i = 1; i < n; i++){
    if(a[i] >= nax){
      nax = a[i];
    }
    else{
      ans += nax - a[i];
    }
  }
  cout << ans;
}