#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  int count = 0;
  for (int j = 1; j < a.size(); j++) {
    if (a[j] < a[j - 1]) {
      count = count + abs(a[j] - a[j - 1]);
      a[j] = a[j - 1];
    }
  }
  cout << count << endl;
}
