#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int sum = 0;
  int before = 0;
  cin >> before;
  for (int i = 0; i < n - 1; i++) {
    int a;
    cin >> a;
    if (before > a)
      sum += before - a;
    else {
      before = a;
    }
  }
  cout << sum << endl;
  return 0;
}
