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