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