#include<bits/stdc++.h>
using namespace std;
#define freop freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define st string
typedef long long ll;
 
void solve(){
     int n, ans;
     cin >> n;
     int arr[n];
     for(int i = 0; i < n; i++){
         cin >> arr[i];
     }
  	 int aux = arr[0];
  	 int ans = 0;
     for(int i = 1; i < n; i++){
         if(aux > arr[i]) {
             ans += aux - arr[i];
             
         }
         aux = max(aux, arr[i]);
     }
     cout << ans;
}
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    return 0;
}