#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int ans = 0;
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  int curr_height = arr[0];
  for (int i = 1; i < n; ++i) {
    if (arr[i] >= curr_height) {
      curr_height = arr[i];
      continue;
    } else {
      ans += (curr_height - arr[i]);
    }
  }
  cout << ans << endl;
  return 0;
}
