#include <bits/stdc++.h>
using namespace std;
int main() {
  int x;
  cin >> x;
  vector<int> v(x);
  for (int i = 0; i < x; i++) cin >> v[i];
  int sum = 0;
  for (int i = 1; i < x; i++) {
    if (v[i - 1] > v[i]) {
      sum += (v[i - 1] - v[i]);
      v[i] = v[i - 1];
    }
  }
  cout << sum << endl;
}
