#include <bits/stdc++.h>
using namespace std;
void solve(long long N, std::vector<long long> A) {
  int ans = 0;
  for (int i = 0; i < (A.size()); ++i) {
    if (i == 0) continue;
    if (A[i - 1] > A[i]) {
      ans += A[i - 1] - A[i];
      A[i] = A[i - 1];
    }
  }
  cout << ans << endl;
}
int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}
