#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n, a[200001], count = 0;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  if (n == 1) {
    cout << 0 << endl;
  } else {
    for (int i = 0; i < n - 1; i++) {
      if (a[i + 1] < a[i]) {
        count += (a[i] - a[i + 1]);
        a[i + 1] = a[i];
      }
    }
    cout << count << endl;
  }
}
