#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int a, sum, pre, aager, n;
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n;
    if (i == 0) {
      sum = 0;
      a = n;
      aager = 0;
    } else {
      if (n <= a) {
        pre = (a - n) + aager;
        aager = pre;
        sum += pre;
        a = n;
      } else {
        aager = 0;
        a = n;
      }
    }
  }
  cout << sum << endl;
  return 0;
}
