#include <bits/stdc++.h>
using namespace std;
int arr[1000000];
int main() {
  int n, mx, sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (i == 0) {
      mx = arr[i];
      continue;
    }
    if (arr[i] < mx) {
      sum += mx - arr[i];
    } else {
      mx = arr[i];
    }
  }
  cout << sum << endl;
}
