#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0;i < (int)(n);i++)

int main() {
  long int N, sum = 0, max;
  cin >> N;
  vector<long int> a(N);
  rep(i,N) cin >> a.at(i);
  max = a.at(0);
  rep(i,N-1) {
    if(x <= a.at(i+1)) x = a.at(i+1);
    else sum += (x - a.at(i+1));
  }
  cout << sum << endl;
}
