#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int temp;
  int prev = 0, i;
  int sum = 0, maxnum = 0;
  for (i = 0; i < n; i++) {
    cin >> temp;
    maxnum = max(temp, maxnum);
    if (temp < prev) {
      sum += prev - temp;
    }
    prev = max(prev, maxnum);
  }
  cout << sum;
}
