#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
  ll n,i,ans=0;
  cin>>n;
  ll arr[n]={0};
  for(i=0;i<n;i++){
    cin>>arr[i];  
  }
  ll maxi=arr[0];
  for(i=0;i<n;i++){
    if(arr[i]>maxi)maxi=arr[i];
    ans+=abs(maxi-arr[i]);
  }
  cour<<ans<<"\n";
  
}