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