#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> z(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> z[i];
  }
  int count = 0;
  for (int i = 1; i < n; i++) {
    if (z[i] < z[i - 1]) {
      int diff = z[i - 1] - z[i];
      count += diff;
      z[i] += diff;
    }
  }
  cout << count << endl;
  return 0;
}
