#include <bits/stdc++.h>
using namespace std;
int main() {
  int sum = 0, n, pre, cur;
  cin >> n;
  cin >> pre;
  for (int i = 2; i <= n; i++) {
    cin >> cur;
    if (cur < pre) {
      sum += pre - cur;
    } else {
      pre = cur;
    }
  }
  cout << sum;
}
