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