#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, num, prev = 0, total = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> num;
    if (num - prev < 0) total += abs(num - prev);
    prev = num;
  }
  cout << total;
  return 0;
}
