#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int result = 0;
  int height = 0;
  for (int i = 0; i < N; i++) {
    if (height > A[i]) {
      result += (height - A[i]);
    } else {
      height = A[i];
    }
  }
  cout << result;
  return 0;
}
