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