#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long int> ip(n);
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> ip[i];
    if (i) {
      if (ip[i] < ip[i - 1]) {
        ans += ip[i - 1] - ip[i];
      }
    }
  }
  cout << ans << "\n";
}
