#include <bits/stdc++.h>
using namespace std;
void test_case() {
  int n;
  cin >> n;
  int stool = 0, tallest;
  cin >> tallest;
  for (int i = 1; i < n; i++) {
    int a;
    cin >> a;
    if (tallest > a) {
      stool += tallest - a;
    }
    tallest = max(tallest, a);
  }
  cout << stool << endl;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    test_case();
  }
  return 0;
}
