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