#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int ara[n], count = 0;
  for (int i = 0; i < n; i++) {
    cin >> ara[i];
  }
  for (int i = 0; i < n; i++) {
    if (ara[i] > ara[i + 1]) {
      count += ara[i] - ara[i + 1];
      ara[i + 1] = ara[i];
    }
  }
  cout << count << endl;
}
