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