#include <bits/stdc++.h>
using namespace std;
long long n, i, a, b, rez;
int main() {
  cin >> n >> a;
  for (i = 1; i < n; ++i) {
    cin >> b;
    if (a - b > 0) {
      rez += a - b;
    }
    a = b;
  }
  cout << rez;
  return 0;
}
