#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 sum = 0;
  int temp = a[0];
  for (int i = 0; i < n; i++) {
    if (temp > a[i])
      sum = sum + max((temp - a[i]), 0);
    else
      temp = a[i];
  }
  cout << sum;
}
