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