#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, a, aa, dai;
  dai = 0;
  cin >> N;
  cin >> a;
  for (int i = 1; i < N; i++) {
    cin >> aa;
    if (a > aa) {
      dai += a - aa;
    }
    a = aa;
  }
  cout << dai;
}
