#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a[1000000005], n, temp, tm = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i = 0) temp = a[i];
    if (i > 0) {
      if (temp > a[i]) tm += temp - a[i];
    }
    temp = a[i];
  }
  cout << tm << endl;
  return 0;
}
