#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ;
  int n;
  cin >> n;
  vector<int> v(n);
  int ans = 0;
  for (int i = 0; i < n; i++) cin >> v[i];
  for (int i = 0; i < n - 1; i++) {
    if (v[i] <= v[i + 1])
      continue;
    else {
      int k = v[i] - v[i + 1];
      ans += k;
      v[i + 1] += k;
    }
  }
  cout << ans << "\n";
}
