#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[200100];
ll b=-1,n,ans;
int main()
{
  cin>>n;
  for(int i=1;i<=n;i++)cin>>a[i];
  for(int i=1;i<=n;i++){
     b=max(b,a[i]);
     ans+=b-a[i];
  }
  cout<<ans;
  return 0;
}