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