#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, A[200001];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int before = A[0];
  int sum = 0;
  for (int i = 1; i < N; i++) {
    if (before > A[i]) {
      sum += before - A[i];
      continue;
    }
    before = A[i];
  }
  std::cout << sum << std::endl;
  return 0;
}
