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