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