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