#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char str[200010];
int main() {
  scanf("%d", &n);
  m = 0;
  k = 0;
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    if (k <= x)
      k = x;
    else
      m += k - x;
  }
  printf("%d\n", m);
  return 0;
}
