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