#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  int sum = 0;
  int h;
  cin >> n;
  int x;
  cin >> x;
  h = x;
  for (int i = 1; i < n; i++) {
    cin >> x;
    if (h > x) {
      sum += h - x;
    } else {
      h = x;
    }
  }
  cout << sum;
  return 0;
}
