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