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