#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  int now;
  cin >> now;
  int ans = 0;
  for (int i = 1; i < n; ++i) {
    int tmp;
    cin >> tmp;
    if (tmp <= now)
      ans += now - tmp;
    else
      now = tmp;
  }
  cout << ans << endl;
  return 0;
}
