#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, prev, ans = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      cin >> prev;
    } else {
      int buf;
      cin >> buf;
      if (buf < prev) {
        ans += prev - buf;
      }
      prev = buf;
    }
  }
  cout << ans << "\n";
}
