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