#include <bits/stdc++.h>
using namespace std;
void startup() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  startup();
  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; ++i) cin >> ar[i];
  int ans = 0;
  for (int i = 1; i < n; ++i) {
    if (ar[i] < ar[i - 1]) {
      ans += ar[i - 1] - ar[i];
      ar[i] += ar[i - 1] - ar[i];
    }
  }
  cout << ans;
  return 0;
}
