#include <bits/stdc++.h>
using namespace std;
int n, a[200005], ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int book = a[1];
  for (int i = 2; i <= n; i++) {
    if (a[i] > book)
      book = a[i];
    else
      ans += book - a[i];
  }
  printf("%d", ans);
  return 0;
}
