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