#include<bits/stdc++.h>
#define ll long long
#define nl "\n"
#define pi acos(-1)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define pcase printf("Case %d:\n",ca)
int ca=1;
using namespace std;
int main(){
   int a;
   cin>>a;
   int arr[a];
   for(int i = 0;i<a;i++){
    cin>>arr[i];
   }
   ll ma = arr[0];
   ll ans = 0;
   for(int i = 1;i<a;i++){
        ma = max(arr[i],ma);
    if(arr[i]<ma){
        ans+=ma-arr[i];
    }
   }
   cout<<ans<<nl;
   }


