#include <bits/stdc++.h>
using namespace std;
const int CN = 2e5 + 5;
int read() {
  int s = 0, ne = 1;
  char c = getchar();
  while (c < '0' || c > '9') ne = c == '-' ? -1 : 1, c = getchar();
  while (c >= '0' && c <= '9') s = (s << 1) + (s << 3) + c - '0', c = getchar();
  return s * ne;
}
int n, a[CN], sum = 0, mx = 0;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  mx = a[1];
  for (int i = 2; i <= n; i++)
    sum += mx - a[i] > 0 ? mx - a[i] : 0, mx = max(mx, a[i]);
  printf("%d", sum);
}
