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