#include<bits/stdc++.h>
using namespace std;
int main(){
  long N, base=0, ans=0;
  cin >> N;
  long A[N];
  for(long i=0;i<N;i++){
    cin >> A[i];
    if(base < A[i])
      base = A[i];
    ans += base - A[i];
  }
  cout << ans;
}
