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