#include <bits/stdc++.h>
using namespace std;
long long int a[100005] = {0};
long long int dp[100005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int n;
  cin >> n;
  for (long long int i = 0; i < n; i++) cin >> a[i];
  ;
  long long int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a[i + 1] < a[i]) {
      sum += abs(a[i] - a[i + 1]);
      a[i + 1] = a[i];
    }
  }
  cout << sum << "\n";
}
