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