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