#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int main() {
int N;
  ll count,countmae;
 cin >> N;
  count=0;
  vector<int> vec(N);
  for (int i = 0; i < N; ++i) cin >> vec[i];
  for (int j = 0; j < N-1; ++j){
  if(vec.at(j)>vec.at(j+1)){
    countmae =count;
    count=(vec.at(j)-vec.at(j+1))+countmae;
    vec.at(j+1)=vec.at(j);
    
  }
  }
  cout << count << endl;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int main() {
int N;
  ll count,countmae;
 cin >> N;
  count=0;
  vector<int> vec(N);
  for (int i = 0; i < N; ++i) cin >> vec[i];
  for (int j = 0; j < N-1; ++j){
  if(vec.at(j)>vec.at(j+1)){
    countmae =count;
    count=(vec.at(j)-vec.at(j+1))+countmae;
    vec.at(j+1)=vec.at(j);
    
  }
  }
  cout << count << endl;
}
