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