#include<bits/stdc++.h>
using namespace std;
int main(){int n;long long s=0;long h=0;cin>>n;vector<long>a(n);
           for(int i=0;i<n;i++){cin>>a.at(i);if(h>a.at(i))s+=(h-a.at(i));else h=a.at(i);}
cout<<s<<endl;}