#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> A(n, 0);
  for (int i = 0; i < n; i++) cin >> A[i];
  long long a = 0, b = 0;
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    if (A[i] >= A[i - 1]) {
      a = 0;
      continue;
    } else {
      a = A[i - 1] - A[i];
      ans += a;
    }
  }
  cout << ans;
  return 0;
}
