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