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