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