#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, result = 0;
  cin >> n;
  int list[n];
  for (int i = 0; i < n; i++) {
    cin >> list[i];
    if (i == 0) {
      continue;
    }
    if (list[i] < list[i - 1]) {
      result = result + (list[i - 1] - list[i]);
      list[i] = list[i - 1];
    }
  }
  cout << result << endl;
  return 0;
}
