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