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