#include <bits/stdc++.h>
using namespace std;
int main() {
  int n = 5;
  cin >> n;
  int stol_height = 0;
  int input = 0;
  int prev_height = 0;
  while (n--) {
    cin >> input;
    if (prev_height <= input) {
      prev_height = max(prev_height, input);
      continue;
    }
    stol_height += (prev_height - input);
  }
  cout << stol_height << std::endl;
  return 0;
}
