#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long int ahead = 0;
  long int my = 0;
  int to = 0;
  for (int i = 0; i < n; i++) {
    cin >> my;
    if (ahead > my) {
      to = to + (ahead - my);
      my = ahead;
    }
    ahead = my;
  }
  cout << to << endl;
}
