#include <bits/stdc++.h>
const int MAXN = 2 * 1e5;
int main() {
  int n;
  std::scanf("%d", &n);
  static int arr[MAXN + 5];
  for (int i = 1; i <= n; i++) {
    std::scanf("%d", &arr[i]);
  }
  int cnt = 0;
  for (int i = 2; i <= n; i++) {
    if (arr[i] < arr[i - 1]) {
      cnt += arr[i - 1] - arr[i];
      arr[i] = arr[i - 1];
    }
  }
  std::printf("%d\n", cnt);
  return 0;
}
