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