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