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