#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
  int n;cin>>n;
  int ans=0,pre;cin>>pre;
  for(int i=1;i<n;i++){
    int a;cin>>a;
    if(pre>a)ans+=pre-a;
    else pre=a;
  }
  cout<<ans<<endl;
}
