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

int main(){
int n;
long long a,now = 0,sum = 0;
cin >> n;

for(int i =0;i < n; i++){
cin >> a;


if(now < a){
  sum += 0;
  now = a;
}else{
  sum += now - a;
}

}
cout << sum << endl;
  return 0;
}