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