#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x, prev = -1, ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    if (x < prev) ans += (prev - x);
    prev = max(prev, x);
  }
  printf("%d\n", ans);
  return 0;
}
