#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int prev;
  cin >> prev;
  int count = 0;
  for (int k = 0; k < n - 1; k++) {
    int in;
    cin >> in;
    if (in - prev < 0) {
      count += prev - in;
    } else {
      prev = in;
    }
  }
  cout << count << "\n";
}
