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