#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, sum = 0;
  vector<int> a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tall;
    cin >> tall;
    a.push_back(tall);
  }
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i]) {
      sum += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << sum << "\n";
}
