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