#include<iostream>
using namespace std;

int main(){
  int n; cin >> n;
  int a[n];
  for(int i = 0; i < n; i++){
  	cin >> a[n];
  }
  
  int running_max = a[0];
  for(int i = 1; i < n; i++){
    running_max = max(a[i],running_max);
    sum += (running_max - a[i]);
  }
  cout << sum << endl;
  return 0;
}