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