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