#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, takadai = 0, sa;
  cin >> N;
  vector<int> A(N);
  for (long long i = 0; i < (long long)(N); i++) {
    cin >> A.at(i);
  }
  for (long long i = 0; i < (long long)(N); i++) {
    if (i > 0) {
      if (A.at(i) - A.at(i - 1) < 0) {
        sa = A.at(i - 1) - A.at(i);
        A.at(i) += sa;
        takadai += sa;
      }
    }
  }
  cout << takadai << endl;
}
