#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    int stool = 0, prev = 0;
    for (int i = 0; i < n; ++i) {
      int aux;
      cin >> aux;
      if (aux <= prev) {
        stool += (prev - aux);
      } else {
        prev = aux;
      }
    }
    cout << stool << endl;
  }
  return 0;
}
