#include <bits/stdc++.h>
using namespace std;
int n, x, t, i;
void solve() {
  cin >> n;
  cin >> x;
  int tallest = x;
  int stool = 0;
  for (i = 0; i < n - 1; i++) {
    cin >> x;
    if (x >= tallest) {
      tallest = x;
    } else {
      stool += (x - tallest);
    }
  }
  cout << stool;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
