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