#include <bits/stdc++.h>
#include<iostream>
using namespace std;
 
int main() {
    int a,b,c,d,tmp,max;
    string s;
 	long long 
    a = b = c = d = ans = tmp = 0;
    

    cin >> a;
    vector<int>x(a);
  	for(int i =0;i < a;i++){
      cin >> x[i];
    }
  
   for(int i = 1;i < a;i++){
     if(x[i] < x[i-1]){
       ans += x[i-1] - x[i];
     }
   }
   cout << ans;
}
