#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) cin >> ar[i];
  int cnt = 0;
  for (int i = 1; i < n; i++) {
    if (ar[i - 1] >= ar[i]) {
      cnt += abs(ar[i - 1] - ar[i]);
      ar[i] = ar[i - 1];
    }
  }
  cout << cnt;
}
int main() {
  int t = 1;
  while (t--) solve();
}
