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