#include <bits/stdc++.h>
using namespace std;
void solve(long long N, std::vector<long long> A) {
  vector<int> res;
  int result = 0;
  for (long long i = 0; i < (long long)(N - 1); i++) {
    if (i == 0) res.push_back(0);
    if (A[i + 1] - A[i] >= 0) {
      res.push_back(0);
    } else {
      res.push_back(A[i] - A[i + 1]);
      result += A[i] - A[i + 1];
      A[i + 1] += A[i] - A[i + 1];
    }
  }
  cout << result << 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;
}
