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