#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int now, last = 0;
  int res = 0;
  while (N--) {
    cin >> now;
    if (now < last) {
      res += last - now;
      now += last - now;
    }
    last = now;
  }
  cout << res;
}
