#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int maximum, current;
  cin >> maximum;
  int ans = 0;
  while (--n) {
    cin >> current;
    int diff = maximum - current;
    ans += diff > 0 ? diff : 0;
    maximum = max(maximum, current);
  }
  cout << ans << '\n';
  return 0;
}
