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