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

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