#include<bits/stdc++.h>
 
using namespace std;
 
int main(){
  int n;
  cin >> n;
  unit64_t a[n-1];
    for (int i=0; i<n; i++){
  cin >> a[i];
    }
  unit64_t l;
  l=0;
  for (int i=1; i<n; i++){
     if (a[i] < a[i-1])
     { l += a[i-1]-a[i];
       a[i]=a[i-1];
     }
  }
 cout << l <<endl;
  return 0;
}
