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