#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int prev;
  cin >> prev;
  int ma = prev;
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    int curr;
    cin >> curr;
    ma = max(prev, ma);
    if (curr < prev) {
      if (prev > ma)
        ans += prev - curr;
      else
        ans += ma - curr;
    }
    prev = curr;
  }
  cout << ans;
  return 0;
}
