#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  int a;
  int count = 0;
  cin >> n >> a;
  for (int i = 0; i < (int)n - 1; i++) {
    int b;
    cin >> b;
    if (b < a) {
      count += (a - b);
      b = a;
    }
    a = b;
  }
  cout << count << endl;
}
