#include <bits/stdc++.h>
using namespace std;
int main() {
  int size;
  int prev;
  int now;
  int totalStools = 0;
  cin >> size;
  cin >> prev;
  for (int i = 1; i < size; i++) {
    cin >> now;
    if (now < prev) {
      totalStools += (prev - now);
    } else if (now > prev) {
      prev = now;
    }
  }
  cout << totalStools << endl;
}
