#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n], answer = 0, mx = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < mx) {
      answer += mx - a[i];
    } else {
      mx = a[i];
    }
  }
  cout << answer;
  return 0;
}
