#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  int ans = 0;
  {
    int h, f;
    f = 0;
    for (int i = 0; i < (n - 1); ++i) {
      h = a[i] + f;
      if (h > a[i + 1]) {
        f = h - a[i + 1];
      } else {
        f = 0;
      }
      ans += f;
    }
  }
  cout << ans << endl;
  return 0;
}
