#include <bits/stdc++.h>
const int mod = ((1E9) + 7);
const int intmax = ((1E9) + 7);
using namespace std;
long long aa[100005];
int main() {
  int test, a, b, c, n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) cin >> aa[i];
  long long prev = aa[0];
  long long steps = 0;
  for (int i = 1; i < n; i++) {
    if (aa[i] < prev) steps += prev - aa[i];
    prev = aa[i];
  }
  cout << steps;
  return 0;
}
