#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
  int n,i;
  ll a,b=0,res=0;
  cin >> n;
  for(i=0;i<n;i++){
    cin >> a;
    res+=max(b-a,(ll)0);
    b=max(a,b);
  }
  cout << res << endl;
  return 0;
}