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