#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, ans = 0;
  cin >> N;
  int prev, frn;
  cin >> prev;
  for (int i = 1; i < N; i++) {
    cin >> frn;
    int diff = prev - frn;
    if (diff > 0) {
      frn += diff;
      ans += diff;
    }
    prev = frn;
  }
  cout << ans << endl;
  return 0;
}
