#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int current;
  int step = 0;
  int last = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> current;
    if (last > current) {
      step += last - current;
    } else {
      last = current;
    }
  }
  cout << step << endl;
}
