include <bits/stdc++.h>
using namespace std;

int main() {
  
  int n;
  cin >> n;
  
  vector<int> vec(n);
  
  for (int i = 0; i < n; i++){
    int x;
    cin >> x;
    vec.at(i) = x;
  }
  
  int total = 0;
  
  for (int i = 1; i < n; i++){
    
    int j = i -1;
    
    if(vec.at(j) <= vec.at(i)){
      vec.at(i) = vec.at(i) + 0;
    }
    else if (vec.at(j) > vec.at(i)){
      int num;
      num = vec.at(j) - vec.at(i);
      vec.at(i) = vec.at(i) + num;
      total = total + num;
    }
    
  }
  
  cout << total << endl;
  
}



