#include <bits/stdc++.h>
using namespace std;
int a[2 * 10 ^ 5 + 5];
int main() {
  int n, min_sum = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n - 1; ++i) {
    if (a[i] > a[i + 1]) {
      min_sum += a[i] - a[i + 1];
      a[i + 1] += a[i] - a[i + 1];
    }
  }
  cout << min_sum << endl;
}
